Merge pull request #9163 from BedeGaming/azurerm-resource-id-fix

provider/azurerm: fix resource ID parsing for subscriptions resources
This commit is contained in:
Paul Stack 2016-10-03 17:07:36 +01:00 committed by GitHub
commit 122a985767
2 changed files with 25 additions and 4 deletions

View File

@ -45,22 +45,29 @@ func parseAzureResourceID(id string) (*ResourceID, error) {
return nil, fmt.Errorf("The number of path segments is not divisible by 2 in %q", path)
}
var subscriptionID string
// Put the constituent key-value pairs into a map
componentMap := make(map[string]string, len(components)/2)
for current := 0; current < len(components); current += 2 {
key := components[current]
value := components[current+1]
componentMap[key] = value
// Catch the subscriptionID before it can be overwritten by another "subscriptions"
// value in the ID which is the case for the Service Bus subscription resource
if key == "subscriptions" && subscriptionID == "" {
subscriptionID = value
} else {
componentMap[key] = value
}
}
// Build up a ResourceID from the map
idObj := &ResourceID{}
idObj.Path = componentMap
if subscription, ok := componentMap["subscriptions"]; ok {
idObj.SubscriptionID = subscription
delete(componentMap, "subscriptions")
if subscriptionID != "" {
idObj.SubscriptionID = subscriptionID
} else {
return nil, fmt.Errorf("No subscription ID found in: %q", path)
}

View File

@ -101,6 +101,20 @@ func TestParseAzureResourceID(t *testing.T) {
},
false,
},
{
"/subscriptions/34ca515c-4629-458e-bf7c-738d77e0d0ea/resourceGroups/testGroup1/providers/Microsoft.ServiceBus/namespaces/testNamespace1/topics/testTopic1/subscriptions/testSubscription1",
&ResourceID{
SubscriptionID: "34ca515c-4629-458e-bf7c-738d77e0d0ea",
ResourceGroup: "testGroup1",
Provider: "Microsoft.ServiceBus",
Path: map[string]string{
"namespaces": "testNamespace1",
"topics": "testTopic1",
"subscriptions": "testSubscription1",
},
},
false,
},
}
for _, test := range testCases {