From 71b4d9aacebdbdbf497beab2ebf312be600b4687 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 21 Jun 2017 12:04:25 -0400 Subject: [PATCH] Sort ResourceID.Path keys for consistent output While the API may not care what order the key-value path parts are in, sort the keys so that we can maintain a consistent order for tests and debugging. Add more key-values to the test so that it fails more frequently than not. --- azurerm/resourceid.go | 11 ++++++++++- azurerm/resourceid_test.go | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/azurerm/resourceid.go b/azurerm/resourceid.go index bcf5eb45bcdd..5523b43f23bf 100644 --- a/azurerm/resourceid.go +++ b/azurerm/resourceid.go @@ -3,6 +3,7 @@ package azurerm import ( "fmt" "net/url" + "sort" "strings" ) @@ -115,7 +116,15 @@ func composeAzureResourceID(idObj *ResourceID) (id string, err error) { id += fmt.Sprintf("/providers/%s", idObj.Provider) - for k, v := range idObj.Path { + // sort the path keys so our output is deterministic + var pathKeys []string + for k := range idObj.Path { + pathKeys = append(pathKeys, k) + } + sort.Strings(pathKeys) + + for _, k := range pathKeys { + v := idObj.Path[k] if k == "" || v == "" { return "", fmt.Errorf("ResourceID.Path cannot contain empty strings") } diff --git a/azurerm/resourceid_test.go b/azurerm/resourceid_test.go index 69d64be932a8..6857f181811d 100644 --- a/azurerm/resourceid_test.go +++ b/azurerm/resourceid_test.go @@ -159,9 +159,14 @@ func TestComposeAzureResourceID(t *testing.T) { "k1": "v1", "k2": "v2", "k3": "v3", + "k4": "v4", + "k5": "v5", + "k6": "v6", + "k7": "v7", + "k8": "v8", }, }, - "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup1/providers/foo.bar/k1/v1/k2/v2/k3/v3", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup1/providers/foo.bar/k1/v1/k2/v2/k3/v3/k4/v4/k5/v5/k6/v6/k7/v7/k8/v8", false, }, {