From f7ec17d62576b689bfc665ead655066c00c9d363 Mon Sep 17 00:00:00 2001 From: Haitao Chen Date: Mon, 9 Nov 2020 14:22:39 -0800 Subject: [PATCH] implement stringify on go-autorest/autorest/azure.Resource (#579) --- autorest/azure/azure.go | 5 +++++ autorest/azure/azure_test.go | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/autorest/azure/azure.go b/autorest/azure/azure.go index 26be936b7..a0b969dff 100644 --- a/autorest/azure/azure.go +++ b/autorest/azure/azure.go @@ -171,6 +171,11 @@ type Resource struct { ResourceName string } +// String function returns a string in form of azureResourceID +func (r Resource) String() string { + return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s/%s/%s", r.SubscriptionID, r.ResourceGroup, r.Provider, r.ResourceType, r.ResourceName) +} + // ParseResourceID parses a resource ID into a ResourceDetails struct. // See https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#return-value-4. func ParseResourceID(resourceID string) (Resource, error) { diff --git a/autorest/azure/azure_test.go b/autorest/azure/azure_test.go index 5438653c0..8e942a2a0 100644 --- a/autorest/azure/azure_test.go +++ b/autorest/azure/azure_test.go @@ -21,6 +21,7 @@ import ( "net/http" "reflect" "strconv" + "strings" "testing" "time" @@ -544,6 +545,12 @@ func TestParseResourceID_WithValidBasicResourceID(t *testing.T) { t.Logf("got: %+v\nwant: %+v", got, want) t.Fail() } + + reGenResourceID := got.String() + if !strings.EqualFold(basicResourceID, reGenResourceID) { + t.Logf("got: %+v\nwant: %+v", reGenResourceID, basicResourceID) + t.Fail() + } } func TestParseResourceID_WithValidSubResourceID(t *testing.T) {