-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request juju#18434 from Aflynn50/resource-id-to-uuid
juju#18434 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. <!-- The PR title should match: <type>(optional <scope>): <description>. Please also ensure all commits in this PR comply with our conventional commits specification: https://github.com/juju/juju/blob/main/doc/conventional-commits.md --> <!-- 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 - [ ] [Integration tests](https://github.com/juju/juju/tree/main/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 Juju compiles and unit tests pass
- Loading branch information
Showing
14 changed files
with
126 additions
and
126 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.