mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-07 22:53:08 -06:00
c3ce8b81ef
When parsing resource IDs (probably incorrectly since they are URIs and should therefore be opaque), we need to look for "resourcegroups" in addition to "resourceGroups" because the Azure CDN resources return that in their URIs. Unfortunately the casing semantics of the rest of the string are not clear, so downcasing the entire URI is probably best avoided. This is a fix for a single case.
120 lines
3.3 KiB
Go
120 lines
3.3 KiB
Go
package azurerm
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseAzureResourceID(t *testing.T) {
|
|
testCases := []struct {
|
|
id string
|
|
expectedResourceID *ResourceID
|
|
expectError bool
|
|
}{
|
|
{
|
|
"random",
|
|
nil,
|
|
true,
|
|
},
|
|
{
|
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
|
nil,
|
|
true,
|
|
},
|
|
{
|
|
"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
|
nil,
|
|
true,
|
|
},
|
|
{
|
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1",
|
|
&ResourceID{
|
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
|
ResourceGroup: "testGroup1",
|
|
Provider: "",
|
|
Path: map[string]string{},
|
|
},
|
|
false,
|
|
},
|
|
{
|
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network",
|
|
&ResourceID{
|
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
|
ResourceGroup: "testGroup1",
|
|
Provider: "Microsoft.Network",
|
|
Path: map[string]string{},
|
|
},
|
|
false,
|
|
},
|
|
{
|
|
// Missing leading /
|
|
"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/",
|
|
nil,
|
|
true,
|
|
},
|
|
{
|
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1",
|
|
&ResourceID{
|
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
|
ResourceGroup: "testGroup1",
|
|
Provider: "Microsoft.Network",
|
|
Path: map[string]string{
|
|
"virtualNetworks": "virtualNetwork1",
|
|
},
|
|
},
|
|
false,
|
|
},
|
|
{
|
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1?api-version=2006-01-02-preview",
|
|
&ResourceID{
|
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
|
ResourceGroup: "testGroup1",
|
|
Provider: "Microsoft.Network",
|
|
Path: map[string]string{
|
|
"virtualNetworks": "virtualNetwork1",
|
|
},
|
|
},
|
|
false,
|
|
},
|
|
{
|
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/subnets/publicInstances1?api-version=2006-01-02-preview",
|
|
&ResourceID{
|
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
|
ResourceGroup: "testGroup1",
|
|
Provider: "Microsoft.Network",
|
|
Path: map[string]string{
|
|
"virtualNetworks": "virtualNetwork1",
|
|
"subnets": "publicInstances1",
|
|
},
|
|
},
|
|
false,
|
|
},
|
|
{
|
|
"/subscriptions/34ca515c-4629-458e-bf7c-738d77e0d0ea/resourcegroups/acceptanceTestResourceGroup1/providers/Microsoft.Cdn/profiles/acceptanceTestCdnProfile1",
|
|
&ResourceID{
|
|
SubscriptionID: "34ca515c-4629-458e-bf7c-738d77e0d0ea",
|
|
ResourceGroup: "acceptanceTestResourceGroup1",
|
|
Provider: "Microsoft.Cdn",
|
|
Path: map[string]string{
|
|
"profiles": "acceptanceTestCdnProfile1",
|
|
},
|
|
},
|
|
false,
|
|
},
|
|
}
|
|
|
|
for _, test := range testCases {
|
|
parsed, err := parseAzureResourceID(test.id)
|
|
if test.expectError && err != nil {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %s", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(test.expectedResourceID, parsed) {
|
|
t.Fatalf("Unexpected resource ID:\nExpected: %+v\nGot: %+v\n", test.expectedResourceID, parsed)
|
|
}
|
|
}
|
|
}
|