Skip to content

Commit

Permalink
Merge pull request juju#18097 from Aflynn50/cloud-id-to-uuid
Browse files Browse the repository at this point in the history
juju#18097

UUID is being used consistantly to refer to the unique identifier of an entity. This change makes cloud consistant.

<!-- 
The PR title should match: <type>(optional <scope>): <description>.

Please also ensure all commits in this PR comply with our conventional commits specification:
https://docs.google.com/document/d/1SYUo9G7qZ_jdoVXpUVamS5VCgHmtZ0QA-wZxKoMS-C0 
-->

<!-- Why this change is needed and what it does. -->

## Checklist

<!-- If an item is not applicable, use `~strikethrough~`. -->

- [x] Code style: imports ordered, good names, simple structure, etc
- [x] Comments saying why design decisions were made
- [x] Go unit tests, with comments saying what you're testing
- [x] [doc.go](https://discourse.charmhub.io/t/readme-in-packages/451) added or updated in changed packages

## QA steps
Check it compiles and unit tests pass
<!-- Describe steps to verify that the change works. -->
  • Loading branch information
jujubot authored Sep 16, 2024
2 parents 75fc15f + 21d372f commit f64d7f7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
30 changes: 15 additions & 15 deletions core/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions core/cloud/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
36 changes: 19 additions & 17 deletions domain/cloud/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,33 @@ 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)
}

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 := `
Expand All @@ -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{
Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions domain/cloud/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit f64d7f7

Please sign in to comment.