Skip to content

Commit

Permalink
Fix hashicorp#106 composeAzureResourceID doesn't generate consistent …
Browse files Browse the repository at this point in the history
…results
  • Loading branch information
whiskeyjay committed Jun 14, 2017
1 parent 95d12b7 commit 031fbea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
20 changes: 18 additions & 2 deletions azurerm/resourceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ package azurerm
import (
"fmt"
"net/url"
"sort"
"strings"
)

// ResourceID represents a parsed long-form Azure Resource Manager ID
// with the Subscription ID, Resource Group and the Provider as top-
// level fields, and other key-value pairs available via a map in the
// Path field.
// Path field. When a ResourceID instance is used to compose a resource
// ID string, the PathOrder must specify the order of the items in Path
// since map itself can only be iterated in random order.
type ResourceID struct {
SubscriptionID string
ResourceGroup string
Provider string
Path map[string]string
PathOrder map[int]string
}

// parseAzureResourceID converts a long-form Azure Resource Manager ID
Expand Down Expand Up @@ -113,9 +117,21 @@ func composeAzureResourceID(idObj *ResourceID) (id string, err error) {
return "", fmt.Errorf("ResourceID.Path should have at least one item when ResourceID.Provider is specified")
}

if len(idObj.PathOrder) != len(idObj.Path) {
return "", fmt.Errorf("ResourceID.PathOrder must have the same number of elements as ResourceID.Path")
}

id += fmt.Sprintf("/providers/%s", idObj.Provider)

for k, v := range idObj.Path {
var keys []int
for k := range idObj.PathOrder {
keys = append(keys, k)
}
sort.Ints(keys)

for _, i := range keys {
k := idObj.PathOrder[i]
v := idObj.Path[k]
if k == "" || v == "" {
return "", fmt.Errorf("ResourceID.Path cannot contain empty strings")
}
Expand Down
32 changes: 32 additions & 0 deletions azurerm/resourceid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ func TestComposeAzureResourceID(t *testing.T) {
"k2": "v2",
"k3": "v3",
},
PathOrder: map[int]string{
0: "k1",
1: "k2",
2: "k3",
},
},
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup1/providers/foo.bar/k1/v1/k2/v2/k3/v3",
false,
Expand Down Expand Up @@ -192,6 +197,10 @@ func TestComposeAzureResourceID(t *testing.T) {
"k2": "v2",
"": "v1",
},
PathOrder: map[int]string{
0: "k2",
1: "",
},
},
"",
true,
Expand All @@ -206,6 +215,29 @@ func TestComposeAzureResourceID(t *testing.T) {
"k1": "v1",
"k2": "",
},
PathOrder: map[int]string{
0: "k1",
1: "k2",
},
},
"",
true,
},
{
// Number of items in Path doesn't match PathOrder
&ResourceID{
SubscriptionID: "00000000-0000-0000-0000-000000000000",
ResourceGroup: "testGroup1",
Provider: "foo.bar",
Path: map[string]string{
"k1": "v1",
"k2": "",
},
PathOrder: map[int]string{
0: "k1",
1: "k2",
2: "k3",
},
},
"",
true,
Expand Down

0 comments on commit 031fbea

Please sign in to comment.