diff --git a/core/cloud/cloud.go b/core/cloud/cloud.go index 771838a0a97..caba0943582 100644 --- a/core/cloud/cloud.go +++ b/core/cloud/cloud.go @@ -11,33 +11,33 @@ import ( "github.com/juju/juju/internal/uuid" ) -// ID represents a unique id within the Juju controller for a cloud. -type ID string +// UUID represents a unique id within the Juju controller for a cloud. +type UUID string -// NewID generates a new cloud [ID] -func NewID() (ID, error) { +// NewUUID generates a new cloud [UUID] +func NewUUID() (UUID, error) { uuid, err := uuid.NewUUID() if err != nil { - return ID(""), fmt.Errorf("creating new cloud id: %w", err) + return UUID(""), fmt.Errorf("creating new cloud id: %w", err) } - return ID(uuid.String()), nil + return UUID(uuid.String()), nil } // String implements the stringer interface returning a string representation of -// the cloud ID. -func (i ID) String() string { - return string(i) +// the cloud UUID. +func (u UUID) String() string { + return string(u) } -// Validate ensures the consistency of the id. If the [ID] is invalid an error +// Validate ensures the consistency of the uuid. If the [UUID] is invalid an error // satisfying [errors.NotValid] will be returned. -func (i ID) Validate() error { - if i == "" { - return fmt.Errorf("cloud id cannot be empty%w", errors.Hide(errors.NotValid)) +func (u UUID) Validate() error { + if u == "" { + return fmt.Errorf("cloud uuid cannot be empty%w", errors.Hide(errors.NotValid)) } - if !uuid.IsValidUUIDString(string(i)) { - return fmt.Errorf("cloud id %q %w", i, errors.NotValid) + if !uuid.IsValidUUIDString(string(u)) { + return fmt.Errorf("cloud uuid %q %w", u, errors.NotValid) } return nil } diff --git a/core/cloud/testing/testing.go b/core/cloud/testing/testing.go index f6e4b292105..729d0065018 100644 --- a/core/cloud/testing/testing.go +++ b/core/cloud/testing/testing.go @@ -10,10 +10,10 @@ import ( corecloud "github.com/juju/juju/core/cloud" ) -// GenModelUUID can be used in testing for generating a cloud id that is +// GenCloudUUID can be used in testing for generating a cloud uuid that is // checked for subsequent errors using the test suits go check instance. -func GenCloudID(c *gc.C) corecloud.ID { - uuid, err := corecloud.NewID() +func GenCloudUUID(c *gc.C) corecloud.UUID { + uuid, err := corecloud.NewUUID() c.Assert(err, jc.ErrorIsNil) return uuid } diff --git a/domain/cloud/state/state.go b/domain/cloud/state/state.go index b8e64c700a5..658345b48f1 100644 --- a/domain/cloud/state/state.go +++ b/domain/cloud/state/state.go @@ -141,9 +141,10 @@ func (st *State) Cloud(ctx context.Context, name string) (*cloud.Cloud, error) { return result, errors.Trace(err) } -// GetCloudForID returns the cloud associated with the provided id. If no cloud is -// found for the given id then a [clouderrors.NotFound] error is returned. -func (st *State) GetCloudForID(ctx context.Context, id corecloud.ID) (cloud.Cloud, error) { +// GetCloudForUUID returns the cloud associated with the provided uuid. If no +// cloud is found for the given uuid then a [clouderrors.NotFound] error is +// returned. +func (st *State) GetCloudForUUID(ctx context.Context, id corecloud.UUID) (cloud.Cloud, error) { db, err := st.DB() if err != nil { return cloud.Cloud{}, errors.Trace(err) @@ -151,21 +152,22 @@ func (st *State) GetCloudForID(ctx context.Context, id corecloud.ID) (cloud.Clou var rval cloud.Cloud return rval, db.Txn(ctx, func(ctx context.Context, tx *sqlair.TX) error { - rval, err = GetCloudForID(ctx, st, tx, id) + rval, err = GetCloudForUUID(ctx, st, tx, id) return err }) } -// GetCloudForID returns the cloud associated with the provided id. If no cloud is -// found for the given id then a [clouderrors.NotFound] error is returned. -func GetCloudForID( +// GetCloudForUUID returns the cloud associated with the provided uuid. If no +// cloud is found for the given id then a [clouderrors.NotFound] error is +// returned. +func GetCloudForUUID( ctx context.Context, st domain.Preparer, tx *sqlair.TX, - id corecloud.ID, + uuid corecloud.UUID, ) (cloud.Cloud, error) { cloudID := cloudID{ - UUID: id.String(), + UUID: uuid.String(), } q := ` @@ -182,9 +184,9 @@ func GetCloudForID( var records []cloudWithAuthType err = tx.Query(ctx, stmt, cloudID).GetAll(&records) if errors.Is(err, sqlair.ErrNoRows) { - return cloud.Cloud{}, fmt.Errorf("%w for uuid %q", clouderrors.NotFound, id) + return cloud.Cloud{}, fmt.Errorf("%w for uuid %q", clouderrors.NotFound, uuid) } else if err != nil { - return cloud.Cloud{}, fmt.Errorf("getting cloud %q: %w", id, err) + return cloud.Cloud{}, fmt.Errorf("getting cloud %q: %w", uuid, err) } cld := cloud.Cloud{ @@ -203,17 +205,17 @@ func GetCloudForID( cld.AuthTypes = append(cld.AuthTypes, cloud.AuthType(record.AuthType)) } - caCerts, err := loadCACerts(ctx, tx, []string{id.String()}) + caCerts, err := loadCACerts(ctx, tx, []string{uuid.String()}) if err != nil { - return cloud.Cloud{}, fmt.Errorf("loading cloud %q ca certificates: %w", id, err) + return cloud.Cloud{}, fmt.Errorf("loading cloud %q ca certificates: %w", uuid, err) } - cld.CACertificates = caCerts[id.String()] + cld.CACertificates = caCerts[uuid.String()] - regions, err := loadRegions(ctx, tx, []string{id.String()}) + regions, err := loadRegions(ctx, tx, []string{uuid.String()}) if err != nil { - return cloud.Cloud{}, fmt.Errorf("loading cloud %q regions: %w", id, err) + return cloud.Cloud{}, fmt.Errorf("loading cloud %q regions: %w", uuid, err) } - cld.Regions = regions[id.String()] + cld.Regions = regions[uuid.String()] return cld, nil } diff --git a/domain/cloud/state/state_test.go b/domain/cloud/state/state_test.go index 71fcdfb18d9..e53fb95366e 100644 --- a/domain/cloud/state/state_test.go +++ b/domain/cloud/state/state_test.go @@ -976,23 +976,23 @@ FROM v_permission } func (s *stateSuite) TestGetCloudForNonExistentID(c *gc.C) { - fakeID := cloudtesting.GenCloudID(c) + fakeID := cloudtesting.GenCloudUUID(c) st := NewState(s.TxnRunnerFactory()) - _, err := st.GetCloudForID(context.Background(), fakeID) + _, err := st.GetCloudForUUID(context.Background(), fakeID) c.Check(err, jc.ErrorIs, clouderrors.NotFound) } -func (s *stateSuite) TestGetCloudForID(c *gc.C) { +func (s *stateSuite) TestGetCloudForUUID(c *gc.C) { st := NewState(s.TxnRunnerFactory()) s.assertInsertCloud(c, st, testCloud) db := s.DB() - var id corecloud.ID - err := db.QueryRow("SELECT uuid FROM v_cloud where name = ?", testCloud.Name).Scan(&id) + var uuid corecloud.UUID + err := db.QueryRow("SELECT uuid FROM v_cloud where name = ?", testCloud.Name).Scan(&uuid) c.Assert(err, jc.ErrorIsNil) c.Assert(err, jc.ErrorIsNil) - cloud, err := st.GetCloudForID(context.Background(), id) + cloud, err := st.GetCloudForUUID(context.Background(), uuid) c.Check(err, jc.ErrorIsNil) c.Check(cloud, jc.DeepEquals, testCloud) }