Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic resource id recasing #221

Merged
merged 8 commits into from
Mar 27, 2024
Merged

Add support for dynamic resource id recasing #221

merged 8 commits into from
Mar 27, 2024

Conversation

catriona-m
Copy link
Member

partially addresses #189

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments inline but otherwise 👍

&DevCenterId{},
&DiskEncryptionSetId{},
&ExpressRouteCircuitPeeringId{},
&FleetId{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build failure here

"github.com/hashicorp/go-azure-helpers/resourcemanager/recaser"
)

func TestRecaserWithIncorrectCasing(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add a couple of the awkward test cases:

  1. There's a handful of APIs that return the full URI with a port in some cases e.g. https://management.azure.com:80/subscriptions/12345
  2. Could we add a Data Plane URI here to ensure this is ignored too: https://example.blob.storage.azure.com/container1 should be fine

As such could we add a couple of test cases covering those?

Comment on lines 17 to 24
resourceIdsWriteLock.Lock()
for _, id := range commonids.CommonIds() {
key := strings.ToLower(id.ID())
if _, ok := knownResourceIds[key]; !ok {
knownResourceIds[key] = id
}
}
resourceIdsWriteLock.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps pedantic, but is there a reason not to call RegisterResourceId here instead?

Suggested change
resourceIdsWriteLock.Lock()
for _, id := range commonids.CommonIds() {
key := strings.ToLower(id.ID())
if _, ok := knownResourceIds[key]; !ok {
knownResourceIds[key] = id
}
}
resourceIdsWriteLock.Unlock()
for _, id := range commonids.CommonIds() {
RegisterResourceId(id)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense 👍 fixed now!

Comment on lines 16 to 17
// ReCaseWithIds tries to determine the type of Resource ID defined in `input` to be able to re-case it from based on an input list of Resource IDs
func ReCaseWithIds(input string, ids map[string]resourceids.ResourceId) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this function private/unexported? This should only be used for testing purposes, else we might end up using this unintentionally (which would be a bug, since it'd mean we're not registering all of the Resource IDs)

Suggested change
// ReCaseWithIds tries to determine the type of Resource ID defined in `input` to be able to re-case it from based on an input list of Resource IDs
func ReCaseWithIds(input string, ids map[string]resourceids.ResourceId) string {
// reCaseWithIds tries to determine the type of Resource ID defined in `input` to be able to re-case it from based on an input list of Resource IDs
func reCaseWithIds(input string, ids map[string]resourceids.ResourceId) string {

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor points but this otherwise LGTM 👍


// buildInputKey takes an input id string and removes user-specified values from it
// so it can be used as a key to extract the correct id from knownResourceIds
func buildInputKey(input string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we should update this to also return a boolean for whether this is usable or not?

Suggested change
func buildInputKey(input string) string {
func buildInputKey(input string) (*string, bool) {

That'd allow this to become:

key, ok := buildInputKey(input)
if ok {
  // assuming that `key` had a value so *key should be safe
}

output = fmt.Sprintf("%s/%s/", output, key)
if strings.EqualFold(key, "providers") && len(segments) >= i+2 {
value := segments[i+1]
output = fmt.Sprintf("%s%s", output, value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps it's worth being consistent here?

Suggested change
output = fmt.Sprintf("%s%s", output, value)
output = fmt.Sprintf("%s/%s", output, value)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case we don't need to add / since there is no user input between providers and the next segment. I've added some comments to make the expected inputs and outputs clearer here

Comment on lines 92 to 93
output = fmt.Sprintf("%s/%s/", output, key)
if strings.EqualFold(key, "providers") && len(segments) >= i+2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth a comment explaining the why here?

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var KnownResourceIds = make(map[string]resourceids.ResourceId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, what's the reason for having this public/exported?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the compiler was giving an error, but you're right it looks like it doesn't really need to be public - fixed

Copy link
Member

@stephybun stephybun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @catriona-m. One minor suggestion on the function comment but otherwise LGTM 👍

resourcemanager/recaser/recase.go Outdated Show resolved Hide resolved
Co-authored-by: stephybun <steph@hashicorp.com>
Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@tombuildsstuff tombuildsstuff merged commit 9ac8d61 into main Mar 27, 2024
2 checks passed
@tombuildsstuff tombuildsstuff deleted the recaser branch March 27, 2024 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants