From 8b95fe3729c8a7e370e828d6bff07e00d160e419 Mon Sep 17 00:00:00 2001 From: Alastair Flynn Date: Tue, 26 Nov 2024 14:02:04 +0000 Subject: [PATCH] refactor(resource): change ID to UUID Use UUID instead of ID for resources. The value is a UUID so should be called as such to match with other UUID of other entites. --- core/resource/id.go | 50 ------------------- core/resource/resource.go | 4 +- core/resource/resource_test.go | 4 +- core/resource/testing/resource.go | 6 +-- core/resource/uuid.go | 50 +++++++++++++++++++ core/resource/{id_test.go => uuid_test.go} | 8 +-- .../application/resource/fileresourcestore.go | 6 +-- .../resource/fileresourcestore_test.go | 20 ++++---- domain/application/resource/resourcestore.go | 6 +-- domain/application/resource/types.go | 8 +-- .../application/service/package_mock_test.go | 28 +++++------ domain/application/service/resource.go | 36 ++++++------- domain/application/service/resource_test.go | 16 +++--- domain/application/state/resource.go | 10 ++-- 14 files changed, 126 insertions(+), 126 deletions(-) delete mode 100644 core/resource/id.go create mode 100644 core/resource/uuid.go rename core/resource/{id_test.go => uuid_test.go} (87%) diff --git a/core/resource/id.go b/core/resource/id.go deleted file mode 100644 index 08ee5fc7839..00000000000 --- a/core/resource/id.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2024 Canonical Ltd. -// Licensed under the AGPLv3, see LICENCE file for details. - -package resource - -import ( - "fmt" - - "github.com/juju/errors" - - "github.com/juju/juju/internal/uuid" -) - -// ID represents a resource unique identifier. -type ID string - -// NewID is a convince function for generating a new resource uuid. -func NewID() (ID, error) { - id, err := uuid.NewUUID() - if err != nil { - return ID(""), err - } - return ID(id.String()), nil -} - -// ParseID returns a new ID from the given string. If the string is not a valid -// uuid an error satisfying [errors.NotValid] will be returned. -func ParseID(value string) (ID, error) { - if !uuid.IsValidUUIDString(value) { - return "", fmt.Errorf("id %q %w", value, errors.NotValid) - } - return ID(value), nil -} - -// String implements the stringer interface for ID. -func (u ID) String() string { - return string(u) -} - -// Validate ensures the consistency of the ID. If the uuid is invalid an error -// satisfying [errors.NotValid] will be returned. -func (u ID) Validate() error { - if u == "" { - return fmt.Errorf("%wid cannot be empty", errors.Hide(errors.NotValid)) - } - if !uuid.IsValidUUIDString(string(u)) { - return fmt.Errorf("id %q %w", u, errors.NotValid) - } - return nil -} diff --git a/core/resource/resource.go b/core/resource/resource.go index 8930ee25051..2b88bb7a3df 100644 --- a/core/resource/resource.go +++ b/core/resource/resource.go @@ -35,7 +35,7 @@ import ( // A resource may also be added to the model as "pending", meaning it // is queued up to be used as a resource for the application. Until it is // "activated", a pending resources is virtually invisible. There may -// be more that one pending resource for a given resource ID. +// be more that one pending resource for a given resource UUID. type Resource struct { resource.Resource @@ -78,7 +78,7 @@ func (res Resource) Validate() error { } if res.ApplicationID == "" { - return errors.NewNotValid(nil, "missing application ID") + return errors.NewNotValid(nil, "missing application UUID") } // TODO(ericsnow) Require that Username be set if timestamp is? diff --git a/core/resource/resource_test.go b/core/resource/resource_test.go index 2a18c52e20f..3437021396a 100644 --- a/core/resource/resource_test.go +++ b/core/resource/resource_test.go @@ -51,7 +51,7 @@ func (ResourceSuite) TestValidateUploadPending(c *gc.C) { res := resource.Resource{ Resource: newFullCharmResource(c, "spam"), ID: "a-application/spam", - PendingID: "some-unique-ID", + PendingID: "some-unique-UUID", ApplicationID: "a-application", Username: "a-user", Timestamp: time.Now(), @@ -109,7 +109,7 @@ func (ResourceSuite) TestValidateMissingApplicationID(c *gc.C) { err := res.Validate() c.Check(err, jc.ErrorIs, errors.NotValid) - c.Check(err, gc.ErrorMatches, `.*missing application ID.*`) + c.Check(err, gc.ErrorMatches, `.*missing application UUID.*`) } func (ResourceSuite) TestValidateMissingUsername(c *gc.C) { diff --git a/core/resource/testing/resource.go b/core/resource/testing/resource.go index 0ba7fd5e32d..e2fbe3d6f6d 100644 --- a/core/resource/testing/resource.go +++ b/core/resource/testing/resource.go @@ -126,10 +126,10 @@ func newStubReadCloser(stub *testing.Stub, content string) io.ReadCloser { } } -// GenResourceID can be used in testing for generating a charm ID that is +// GenResourceUUID can be used in testing for generating a resource UUID that is // checked for subsequent errors using the test suit's go check instance. -func GenResourceID(c *gc.C) resource.ID { - id, err := resource.NewID() +func GenResourceUUID(c *gc.C) resource.UUID { + id, err := resource.NewUUID() c.Assert(err, jc.ErrorIsNil) return id } diff --git a/core/resource/uuid.go b/core/resource/uuid.go new file mode 100644 index 00000000000..d3c4fa82180 --- /dev/null +++ b/core/resource/uuid.go @@ -0,0 +1,50 @@ +// Copyright 2024 Canonical Ltd. +// Licensed under the AGPLv3, see LICENCE file for details. + +package resource + +import ( + "fmt" + + "github.com/juju/errors" + + "github.com/juju/juju/internal/uuid" +) + +// UUID represents a resource unique identifier. +type UUID string + +// NewUUID is a convince function for generating a new resource uuid. +func NewUUID() (UUID, error) { + id, err := uuid.NewUUID() + if err != nil { + return UUID(""), err + } + return UUID(id.String()), nil +} + +// ParseUUID returns a new UUID from the given string. If the string is not a +// valid uuid an error satisfying [errors.NotValid] will be returned. +func ParseUUID(value string) (UUID, error) { + if !uuid.IsValidUUIDString(value) { + return "", fmt.Errorf("id %q %w", value, errors.NotValid) + } + return UUID(value), nil +} + +// String implements the stringer interface for UUID. +func (u UUID) String() string { + return string(u) +} + +// Validate ensures the consistency of the UUID. If the uuid is invalid an error +// satisfying [errors.NotValid] will be returned. +func (u UUID) Validate() error { + if u == "" { + return fmt.Errorf("%wuuid cannot be empty", errors.Hide(errors.NotValid)) + } + if !uuid.IsValidUUIDString(string(u)) { + return fmt.Errorf("uuid %q %w", u, errors.NotValid) + } + return nil +} diff --git a/core/resource/id_test.go b/core/resource/uuid_test.go similarity index 87% rename from core/resource/id_test.go rename to core/resource/uuid_test.go index 7c9a149b2e2..b3f860f7dbc 100644 --- a/core/resource/id_test.go +++ b/core/resource/uuid_test.go @@ -18,7 +18,7 @@ type resourcesSuite struct { var _ = gc.Suite(&resourcesSuite{}) -func (*resourcesSuite) TestIDValidate(c *gc.C) { +func (*resourcesSuite) TestUUIDValidate(c *gc.C) { tests := []struct { uuid string err error @@ -38,7 +38,7 @@ func (*resourcesSuite) TestIDValidate(c *gc.C) { for i, test := range tests { c.Logf("test %d: %q", i, test.uuid) - err := ID(test.uuid).Validate() + err := UUID(test.uuid).Validate() if test.err == nil { c.Check(err, gc.IsNil) @@ -49,7 +49,7 @@ func (*resourcesSuite) TestIDValidate(c *gc.C) { } } -func (*resourcesSuite) TestParseID(c *gc.C) { +func (*resourcesSuite) TestParseUUID(c *gc.C) { tests := []struct { uuid string err error @@ -69,7 +69,7 @@ func (*resourcesSuite) TestParseID(c *gc.C) { for i, test := range tests { c.Logf("test %d: %q", i, test.uuid) - id, err := ParseID(test.uuid) + id, err := ParseUUID(test.uuid) if test.err == nil { if c.Check(err, gc.IsNil) { diff --git a/domain/application/resource/fileresourcestore.go b/domain/application/resource/fileresourcestore.go index 307735334a7..37aa373dd8b 100644 --- a/domain/application/resource/fileresourcestore.go +++ b/domain/application/resource/fileresourcestore.go @@ -21,7 +21,7 @@ type fileResourceStore struct { // Get the specified resource from the object store. func (f fileResourceStore) Get( ctx context.Context, - resourceUUID coreresource.ID, + resourceUUID coreresource.UUID, ) (io.ReadCloser, int64, error) { if err := resourceUUID.Validate(); err != nil { return nil, 0, errors.Errorf("validating resource UUID: %w", err) @@ -33,7 +33,7 @@ func (f fileResourceStore) Get( // storage path. It returns the UUID of the object store metadata. func (f fileResourceStore) Put( ctx context.Context, - resourceUUID coreresource.ID, + resourceUUID coreresource.UUID, r io.Reader, size int64, fingerprint resource.Fingerprint, @@ -56,7 +56,7 @@ func (f fileResourceStore) Put( // Remove the specified resource from the object store. func (f fileResourceStore) Remove( ctx context.Context, - resourceUUID coreresource.ID, + resourceUUID coreresource.UUID, ) error { if err := resourceUUID.Validate(); err != nil { return errors.Errorf("validating resource UUID: %w", err) diff --git a/domain/application/resource/fileresourcestore_test.go b/domain/application/resource/fileresourcestore_test.go index 1e65a05ccec..02c8ac2de1b 100644 --- a/domain/application/resource/fileresourcestore_test.go +++ b/domain/application/resource/fileresourcestore_test.go @@ -38,7 +38,7 @@ func (s *fileResourceStoreSuite) SetUpTest(c *gc.C) { fingerprint, err := charmresource.ParseFingerprint(fp) c.Assert(err, jc.ErrorIsNil) s.resource = Resource{ - ID: resourcestesting.GenResourceID(c), + UUID: resourcestesting.GenResourceUUID(c), Resource: charmresource.Resource{ Meta: charmresource.Meta{ Name: "spam-resource", @@ -67,7 +67,7 @@ func (s *fileResourceStoreSuite) TestFileResourceStorePut(c *gc.C) { expectedStorageUUID := objectstoretesting.GenObjectStoreUUID(c) s.objectStore.EXPECT().PutAndCheckHash( context.Background(), - s.resource.ID.String(), + s.resource.UUID.String(), s.file, s.resource.Size, s.resource.Fingerprint.String(), @@ -75,7 +75,7 @@ func (s *fileResourceStoreSuite) TestFileResourceStorePut(c *gc.C) { storageUUID, err := store.Put( context.Background(), - s.resource.ID, + s.resource.UUID, s.file, s.resource.Size, s.resource.Fingerprint, @@ -102,7 +102,7 @@ func (s *fileResourceStoreSuite) TestFileResourceStorePutNilReader(c *gc.C) { store := fileResourceStore{s.objectStore} _, err := store.Put( context.Background(), - s.resource.ID, + s.resource.UUID, nil, s.resource.Size, s.resource.Fingerprint, @@ -115,7 +115,7 @@ func (s *fileResourceStoreSuite) TestFileResourceStorePutBadFingerprint(c *gc.C) store := fileResourceStore{s.objectStore} _, err := store.Put( context.Background(), - s.resource.ID, + s.resource.UUID, s.file, s.resource.Size, charmresource.Fingerprint{}, @@ -128,7 +128,7 @@ func (s *fileResourceStoreSuite) TestFileResourceStorePutZeroSize(c *gc.C) { store := fileResourceStore{s.objectStore} _, err := store.Put( context.Background(), - s.resource.ID, + s.resource.UUID, s.file, 0, charmresource.Fingerprint{}, @@ -140,9 +140,9 @@ func (s *fileResourceStoreSuite) TestFileResourceStoreGet(c *gc.C) { defer s.setupMocks(c).Finish() store := fileResourceStore{s.objectStore} - s.objectStore.EXPECT().Get(context.Background(), s.resource.ID.String()).Return(s.file, s.resource.Size, nil) + s.objectStore.EXPECT().Get(context.Background(), s.resource.UUID.String()).Return(s.file, s.resource.Size, nil) - reader, size, err := store.Get(context.Background(), s.resource.ID) + reader, size, err := store.Get(context.Background(), s.resource.UUID) c.Assert(err, jc.ErrorIsNil) c.Assert(reader, gc.Equals, s.file) c.Assert(size, gc.Equals, s.resource.Size) @@ -162,9 +162,9 @@ func (s *fileResourceStoreSuite) TestFileResourceStoreRemove(c *gc.C) { defer s.setupMocks(c).Finish() store := fileResourceStore{s.objectStore} - s.objectStore.EXPECT().Remove(context.Background(), s.resource.ID.String()).Return(nil) + s.objectStore.EXPECT().Remove(context.Background(), s.resource.UUID.String()).Return(nil) - err := store.Remove(context.Background(), s.resource.ID) + err := store.Remove(context.Background(), s.resource.UUID) c.Assert(err, jc.ErrorIsNil) } diff --git a/domain/application/resource/resourcestore.go b/domain/application/resource/resourcestore.go index a315cfeec5e..b810c4c2bbd 100644 --- a/domain/application/resource/resourcestore.go +++ b/domain/application/resource/resourcestore.go @@ -21,14 +21,14 @@ type ResourceStore interface { // Get returns an io.ReadCloser for a resource in the resource store. Get( ctx context.Context, - resourceUUID coreresource.ID, + resourceUUID coreresource.UUID, ) (r io.ReadCloser, size int64, err error) // Put stores data from io.Reader in the resource store at the // using the resourceUUID as the key. Put( ctx context.Context, - resourceUUID coreresource.ID, + resourceUUID coreresource.UUID, r io.Reader, size int64, fingerprint resource.Fingerprint, @@ -37,7 +37,7 @@ type ResourceStore interface { // Remove removes a resource from storage. Remove( ctx context.Context, - resourceUUID coreresource.ID, + resourceUUID coreresource.UUID, ) error } diff --git a/domain/application/resource/types.go b/domain/application/resource/types.go index 146d78193ac..53947218762 100644 --- a/domain/application/resource/types.go +++ b/domain/application/resource/types.go @@ -72,8 +72,8 @@ type ApplicationResources struct { type Resource struct { resource.Resource - // ID uniquely identifies a resource within the model. - ID coreresource.ID + // UUID uniquely identifies a resource within the model. + UUID coreresource.UUID // ApplicationID identifies the application for the resource. ApplicationID application.ID @@ -146,8 +146,8 @@ type SetUnitResourceArgs struct { // SetUnitResourceResult is the result data from setting a unit's resource. type SetUnitResourceResult struct { - // ID uniquely identifies the unit resource within the model. - ID coreresource.ID + // UUID uniquely identifies the unit resource within the model. + UUID coreresource.UUID // Timestamp indicates when the unit started using resource. Timestamp time.Time } diff --git a/domain/application/service/package_mock_test.go b/domain/application/service/package_mock_test.go index b3c3f25d608..a169aadb1f7 100644 --- a/domain/application/service/package_mock_test.go +++ b/domain/application/service/package_mock_test.go @@ -374,10 +374,10 @@ func (c *MockStateGetApplicationLifeCall) DoAndReturn(f func(domain.AtomicContex } // GetApplicationResourceID mocks base method. -func (m *MockState) GetApplicationResourceID(arg0 context.Context, arg1 resource0.GetApplicationResourceIDArgs) (resource.ID, error) { +func (m *MockState) GetApplicationResourceID(arg0 context.Context, arg1 resource0.GetApplicationResourceIDArgs) (resource.UUID, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetApplicationResourceID", arg0, arg1) - ret0, _ := ret[0].(resource.ID) + ret0, _ := ret[0].(resource.UUID) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -395,19 +395,19 @@ type MockStateGetApplicationResourceIDCall struct { } // Return rewrite *gomock.Call.Return -func (c *MockStateGetApplicationResourceIDCall) Return(arg0 resource.ID, arg1 error) *MockStateGetApplicationResourceIDCall { +func (c *MockStateGetApplicationResourceIDCall) Return(arg0 resource.UUID, arg1 error) *MockStateGetApplicationResourceIDCall { c.Call = c.Call.Return(arg0, arg1) return c } // Do rewrite *gomock.Call.Do -func (c *MockStateGetApplicationResourceIDCall) Do(f func(context.Context, resource0.GetApplicationResourceIDArgs) (resource.ID, error)) *MockStateGetApplicationResourceIDCall { +func (c *MockStateGetApplicationResourceIDCall) Do(f func(context.Context, resource0.GetApplicationResourceIDArgs) (resource.UUID, error)) *MockStateGetApplicationResourceIDCall { c.Call = c.Call.Do(f) return c } // DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *MockStateGetApplicationResourceIDCall) DoAndReturn(f func(context.Context, resource0.GetApplicationResourceIDArgs) (resource.ID, error)) *MockStateGetApplicationResourceIDCall { +func (c *MockStateGetApplicationResourceIDCall) DoAndReturn(f func(context.Context, resource0.GetApplicationResourceIDArgs) (resource.UUID, error)) *MockStateGetApplicationResourceIDCall { c.Call = c.Call.DoAndReturn(f) return c } @@ -1046,7 +1046,7 @@ func (c *MockStateGetModelTypeCall) DoAndReturn(f func(context.Context) (model.M } // GetResource mocks base method. -func (m *MockState) GetResource(arg0 context.Context, arg1 resource.ID) (resource0.Resource, error) { +func (m *MockState) GetResource(arg0 context.Context, arg1 resource.UUID) (resource0.Resource, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetResource", arg0, arg1) ret0, _ := ret[0].(resource0.Resource) @@ -1073,13 +1073,13 @@ func (c *MockStateGetResourceCall) Return(arg0 resource0.Resource, arg1 error) * } // Do rewrite *gomock.Call.Do -func (c *MockStateGetResourceCall) Do(f func(context.Context, resource.ID) (resource0.Resource, error)) *MockStateGetResourceCall { +func (c *MockStateGetResourceCall) Do(f func(context.Context, resource.UUID) (resource0.Resource, error)) *MockStateGetResourceCall { c.Call = c.Call.Do(f) return c } // DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *MockStateGetResourceCall) DoAndReturn(f func(context.Context, resource.ID) (resource0.Resource, error)) *MockStateGetResourceCall { +func (c *MockStateGetResourceCall) DoAndReturn(f func(context.Context, resource.UUID) (resource0.Resource, error)) *MockStateGetResourceCall { c.Call = c.Call.DoAndReturn(f) return c } @@ -1669,7 +1669,7 @@ func (c *MockStateListResourcesCall) DoAndReturn(f func(context.Context, applica } // OpenApplicationResource mocks base method. -func (m *MockState) OpenApplicationResource(arg0 context.Context, arg1 resource.ID) (resource0.Resource, error) { +func (m *MockState) OpenApplicationResource(arg0 context.Context, arg1 resource.UUID) (resource0.Resource, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "OpenApplicationResource", arg0, arg1) ret0, _ := ret[0].(resource0.Resource) @@ -1696,19 +1696,19 @@ func (c *MockStateOpenApplicationResourceCall) Return(arg0 resource0.Resource, a } // Do rewrite *gomock.Call.Do -func (c *MockStateOpenApplicationResourceCall) Do(f func(context.Context, resource.ID) (resource0.Resource, error)) *MockStateOpenApplicationResourceCall { +func (c *MockStateOpenApplicationResourceCall) Do(f func(context.Context, resource.UUID) (resource0.Resource, error)) *MockStateOpenApplicationResourceCall { c.Call = c.Call.Do(f) return c } // DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *MockStateOpenApplicationResourceCall) DoAndReturn(f func(context.Context, resource.ID) (resource0.Resource, error)) *MockStateOpenApplicationResourceCall { +func (c *MockStateOpenApplicationResourceCall) DoAndReturn(f func(context.Context, resource.UUID) (resource0.Resource, error)) *MockStateOpenApplicationResourceCall { c.Call = c.Call.DoAndReturn(f) return c } // OpenUnitResource mocks base method. -func (m *MockState) OpenUnitResource(arg0 context.Context, arg1 resource.ID, arg2 unit.UUID) (resource0.Resource, error) { +func (m *MockState) OpenUnitResource(arg0 context.Context, arg1 resource.UUID, arg2 unit.UUID) (resource0.Resource, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "OpenUnitResource", arg0, arg1, arg2) ret0, _ := ret[0].(resource0.Resource) @@ -1735,13 +1735,13 @@ func (c *MockStateOpenUnitResourceCall) Return(arg0 resource0.Resource, arg1 err } // Do rewrite *gomock.Call.Do -func (c *MockStateOpenUnitResourceCall) Do(f func(context.Context, resource.ID, unit.UUID) (resource0.Resource, error)) *MockStateOpenUnitResourceCall { +func (c *MockStateOpenUnitResourceCall) Do(f func(context.Context, resource.UUID, unit.UUID) (resource0.Resource, error)) *MockStateOpenUnitResourceCall { c.Call = c.Call.Do(f) return c } // DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *MockStateOpenUnitResourceCall) DoAndReturn(f func(context.Context, resource.ID, unit.UUID) (resource0.Resource, error)) *MockStateOpenUnitResourceCall { +func (c *MockStateOpenUnitResourceCall) DoAndReturn(f func(context.Context, resource.UUID, unit.UUID) (resource0.Resource, error)) *MockStateOpenUnitResourceCall { c.Call = c.Call.DoAndReturn(f) return c } diff --git a/domain/application/service/resource.go b/domain/application/service/resource.go index 322a1014ef1..04ccd6e9db4 100644 --- a/domain/application/service/resource.go +++ b/domain/application/service/resource.go @@ -18,17 +18,17 @@ import ( charmresource "github.com/juju/juju/internal/charm/resource" ) -// ResourceState describes retrieval and persistence methods for resources. +// ResourceState describes retrieval and persistence methods for resource. type ResourceState interface { // GetApplicationResourceID returns the ID of the application resource // specified by natural key of application and resource name. - GetApplicationResourceID(ctx context.Context, args resource.GetApplicationResourceIDArgs) (coreresource.ID, error) + GetApplicationResourceID(ctx context.Context, args resource.GetApplicationResourceIDArgs) (coreresource.UUID, error) // ListResources returns the list of resources for the given application. ListResources(ctx context.Context, applicationID application.ID) (resource.ApplicationResources, error) // GetResource returns the identified resource. - GetResource(ctx context.Context, resourceID coreresource.ID) (resource.Resource, error) + GetResource(ctx context.Context, resourceUUID coreresource.UUID) (resource.Resource, error) // SetResource adds the resource to blob storage and updates the metadata. SetResource(ctx context.Context, config resource.SetResourceArgs) (resource.Resource, error) @@ -37,13 +37,13 @@ type ResourceState interface { SetUnitResource(ctx context.Context, config resource.SetUnitResourceArgs) (resource.SetUnitResourceResult, error) // OpenApplicationResource returns the metadata for an application's resource. - OpenApplicationResource(ctx context.Context, resourceID coreresource.ID) (resource.Resource, error) + OpenApplicationResource(ctx context.Context, resourceUUID coreresource.UUID) (resource.Resource, error) // OpenUnitResource returns the metadata for a resource a. A unit resource is // created to track the given unit and which resource its using. - OpenUnitResource(ctx context.Context, resourceID coreresource.ID, unitID coreunit.UUID) (resource.Resource, error) + OpenUnitResource(ctx context.Context, resourceUUID coreresource.UUID, unitID coreunit.UUID) (resource.Resource, error) - // SetRepositoryResources sets the "polled" resources for the + // SetRepositoryResources sets the "polled" resource for the // application to the provided values. The current data for this // application/resource combination will be overwritten. SetRepositoryResources(ctx context.Context, config resource.SetRepositoryResourcesArgs) error @@ -70,7 +70,7 @@ type ResourceStoreGetter interface { func (s *Service) GetApplicationResourceID( ctx context.Context, args resource.GetApplicationResourceIDArgs, -) (coreresource.ID, error) { +) (coreresource.UUID, error) { if err := args.ApplicationID.Validate(); err != nil { return "", fmt.Errorf("application id: %w", err) } @@ -110,12 +110,12 @@ func (s *Service) ListResources( // not exist. func (s *Service) GetResource( ctx context.Context, - resourceID coreresource.ID, + resourceUUID coreresource.UUID, ) (resource.Resource, error) { - if err := resourceID.Validate(); err != nil { + if err := resourceUUID.Validate(); err != nil { return resource.Resource{}, fmt.Errorf("application id: %w", err) } - return s.st.GetResource(ctx, resourceID) + return s.st.GetResource(ctx, resourceUUID) } // SetResource adds the application resource to blob storage and updates the metadata. @@ -173,17 +173,17 @@ func (s *Service) SetUnitResource( // OpenApplicationResource returns the details of and a reader for the resource. // // The following error types can be expected to be returned: -// - errors.NotValid is returned if the coreresource.ID is not valid. +// - errors.NotValid is returned if the resource.UUID is not valid. // - application.ResourceNotFound if the specified resource does // not exist. func (s *Service) OpenApplicationResource( ctx context.Context, - resourceID coreresource.ID, + resourceUUID coreresource.UUID, ) (resource.Resource, io.ReadCloser, error) { - if err := resourceID.Validate(); err != nil { + if err := resourceUUID.Validate(); err != nil { return resource.Resource{}, nil, fmt.Errorf("resource id: %w", err) } - res, err := s.st.OpenApplicationResource(ctx, resourceID) + res, err := s.st.OpenApplicationResource(ctx, resourceUUID) return res, &noopReadCloser{}, err } @@ -193,7 +193,7 @@ func (s *Service) OpenApplicationResource( // exhausted. Typically used for File resources. // // The following error types can be returned: -// - errors.NotValid is returned if the coreresource.ID is not valid. +// - errors.NotValid is returned if the resource.UUID is not valid. // - errors.NotValid is returned if the unit UUID is not valid. // - application.ResourceNotFound if the specified resource does // not exist. @@ -201,16 +201,16 @@ func (s *Service) OpenApplicationResource( // not exist. func (s *Service) OpenUnitResource( ctx context.Context, - resourceID coreresource.ID, + resourceUUID coreresource.UUID, unitID coreunit.UUID, ) (resource.Resource, io.ReadCloser, error) { if err := unitID.Validate(); err != nil { return resource.Resource{}, nil, fmt.Errorf("unit id: %w", err) } - if err := resourceID.Validate(); err != nil { + if err := resourceUUID.Validate(); err != nil { return resource.Resource{}, nil, fmt.Errorf("resource id: %w", err) } - res, err := s.st.OpenUnitResource(ctx, resourceID, unitID) + res, err := s.st.OpenUnitResource(ctx, resourceUUID, unitID) return res, &noopReadCloser{}, err } diff --git a/domain/application/service/resource_test.go b/domain/application/service/resource_test.go index c229d4dbb31..5dccc2de1fb 100644 --- a/domain/application/service/resource_test.go +++ b/domain/application/service/resource_test.go @@ -29,7 +29,7 @@ var _ = gc.Suite(&resourceServiceSuite{}) func (s *resourceServiceSuite) TestGetApplicationResourceID(c *gc.C) { defer s.setupMocks(c).Finish() - retID := resourcestesting.GenResourceID(c) + retID := resourcestesting.GenResourceUUID(c) args := resource.GetApplicationResourceIDArgs{ ApplicationID: applicationtesting.GenApplicationUUID(c), Name: "test-resource", @@ -89,7 +89,7 @@ func (s *resourceServiceSuite) TestListResourcesBadID(c *gc.C) { func (s *resourceServiceSuite) TestGetResource(c *gc.C) { defer s.setupMocks(c).Finish() - id := resourcestesting.GenResourceID(c) + id := resourcestesting.GenResourceUUID(c) expectedRes := resource.Resource{ SuppliedBy: "admin", SuppliedByType: resource.Application, @@ -205,7 +205,7 @@ func (s *resourceServiceSuite) TestSetUnitResource(c *gc.C) { }, } expectedRet := resource.SetUnitResourceResult{ - ID: resourcestesting.GenResourceID(c), + UUID: resourcestesting.GenResourceUUID(c), } s.state.EXPECT().SetUnitResource(gomock.Any(), args).Return(expectedRet, nil) @@ -216,9 +216,9 @@ func (s *resourceServiceSuite) TestSetUnitResource(c *gc.C) { func (s *resourceServiceSuite) TestOpenApplicationResource(c *gc.C) { defer s.setupMocks(c).Finish() - id := resourcestesting.GenResourceID(c) + id := resourcestesting.GenResourceUUID(c) expectedRes := resource.Resource{ - ID: id, + UUID: id, ApplicationID: applicationtesting.GenApplicationUUID(c), } s.state.EXPECT().OpenApplicationResource(gomock.Any(), id).Return(expectedRes, nil) @@ -237,10 +237,10 @@ func (s *resourceServiceSuite) TestOpenApplicationResourceBadID(c *gc.C) { func (s *resourceServiceSuite) TestOpenUnitResource(c *gc.C) { defer s.setupMocks(c).Finish() - resourceID := resourcestesting.GenResourceID(c) + resourceID := resourcestesting.GenResourceUUID(c) unitID := unittesting.GenUnitUUID(c) expectedRes := resource.Resource{ - ID: resourceID, + UUID: resourceID, ApplicationID: applicationtesting.GenApplicationUUID(c), } s.state.EXPECT().OpenUnitResource(gomock.Any(), resourceID, unitID).Return(expectedRes, nil) @@ -253,7 +253,7 @@ func (s *resourceServiceSuite) TestOpenUnitResource(c *gc.C) { func (s *resourceServiceSuite) TestOpenUnitResourceBadUnitID(c *gc.C) { defer s.setupMocks(c).Finish() - resourceID := resourcestesting.GenResourceID(c) + resourceID := resourcestesting.GenResourceUUID(c) _, _, err := s.service.OpenUnitResource(context.Background(), resourceID, "") c.Assert(err, jc.ErrorIs, errors.NotValid) diff --git a/domain/application/state/resource.go b/domain/application/state/resource.go index b9e7bbbb7bf..1e297d4dad4 100644 --- a/domain/application/state/resource.go +++ b/domain/application/state/resource.go @@ -17,11 +17,11 @@ import ( func (st *State) GetApplicationResourceID( ctx context.Context, args resource.GetApplicationResourceIDArgs, -) (coreresource.ID, error) { +) (coreresource.UUID, error) { return "", nil } -// ListResources returns the list of resources for the given application. +// ListResources returns the list of resource for the given application. func (st *State) ListResources( ctx context.Context, applicationID application.ID, @@ -30,7 +30,7 @@ func (st *State) ListResources( } // GetResource returns the identified resource. -func (st *State) GetResource(ctx context.Context, resourceID coreresource.ID) (resource.Resource, error) { +func (st *State) GetResource(ctx context.Context, resourceUUID coreresource.UUID) (resource.Resource, error) { return resource.Resource{}, nil } @@ -53,7 +53,7 @@ func (st *State) SetUnitResource( // OpenApplicationResource returns the metadata for a resource. func (st *State) OpenApplicationResource( ctx context.Context, - resourceID coreresource.ID, + resourceUUID coreresource.UUID, ) (resource.Resource, error) { return resource.Resource{}, nil } @@ -63,7 +63,7 @@ func (st *State) OpenApplicationResource( // its using. func (st *State) OpenUnitResource( ctx context.Context, - resourceID coreresource.ID, + resourceUUID coreresource.UUID, unitID coreunit.UUID, ) (resource.Resource, error) { return resource.Resource{}, nil