github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resourceid_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestParseAzureResourceID(t *testing.T) {
     9  	testCases := []struct {
    10  		id                 string
    11  		expectedResourceID *ResourceID
    12  		expectError        bool
    13  	}{
    14  		{
    15  			// Missing "resourceGroups".
    16  			"/subscriptions/00000000-0000-0000-0000-000000000000//myResourceGroup/",
    17  			nil,
    18  			true,
    19  		},
    20  		{
    21  			// Empty resource group ID.
    22  			"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//",
    23  			nil,
    24  			true,
    25  		},
    26  		{
    27  			"random",
    28  			nil,
    29  			true,
    30  		},
    31  		{
    32  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    33  			nil,
    34  			true,
    35  		},
    36  		{
    37  			"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    38  			nil,
    39  			true,
    40  		},
    41  		{
    42  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1",
    43  			&ResourceID{
    44  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    45  				ResourceGroup:  "testGroup1",
    46  				Provider:       "",
    47  				Path:           map[string]string{},
    48  			},
    49  			false,
    50  		},
    51  		{
    52  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network",
    53  			&ResourceID{
    54  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    55  				ResourceGroup:  "testGroup1",
    56  				Provider:       "Microsoft.Network",
    57  				Path:           map[string]string{},
    58  			},
    59  			false,
    60  		},
    61  		{
    62  			// Missing leading /
    63  			"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/",
    64  			nil,
    65  			true,
    66  		},
    67  		{
    68  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1",
    69  			&ResourceID{
    70  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    71  				ResourceGroup:  "testGroup1",
    72  				Provider:       "Microsoft.Network",
    73  				Path: map[string]string{
    74  					"virtualNetworks": "virtualNetwork1",
    75  				},
    76  			},
    77  			false,
    78  		},
    79  		{
    80  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1?api-version=2006-01-02-preview",
    81  			&ResourceID{
    82  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    83  				ResourceGroup:  "testGroup1",
    84  				Provider:       "Microsoft.Network",
    85  				Path: map[string]string{
    86  					"virtualNetworks": "virtualNetwork1",
    87  				},
    88  			},
    89  			false,
    90  		},
    91  		{
    92  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/subnets/publicInstances1?api-version=2006-01-02-preview",
    93  			&ResourceID{
    94  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    95  				ResourceGroup:  "testGroup1",
    96  				Provider:       "Microsoft.Network",
    97  				Path: map[string]string{
    98  					"virtualNetworks": "virtualNetwork1",
    99  					"subnets":         "publicInstances1",
   100  				},
   101  			},
   102  			false,
   103  		},
   104  		{
   105  			"/subscriptions/34ca515c-4629-458e-bf7c-738d77e0d0ea/resourcegroups/acceptanceTestResourceGroup1/providers/Microsoft.Cdn/profiles/acceptanceTestCdnProfile1",
   106  			&ResourceID{
   107  				SubscriptionID: "34ca515c-4629-458e-bf7c-738d77e0d0ea",
   108  				ResourceGroup:  "acceptanceTestResourceGroup1",
   109  				Provider:       "Microsoft.Cdn",
   110  				Path: map[string]string{
   111  					"profiles": "acceptanceTestCdnProfile1",
   112  				},
   113  			},
   114  			false,
   115  		},
   116  		{
   117  			"/subscriptions/34ca515c-4629-458e-bf7c-738d77e0d0ea/resourceGroups/testGroup1/providers/Microsoft.ServiceBus/namespaces/testNamespace1/topics/testTopic1/subscriptions/testSubscription1",
   118  			&ResourceID{
   119  				SubscriptionID: "34ca515c-4629-458e-bf7c-738d77e0d0ea",
   120  				ResourceGroup:  "testGroup1",
   121  				Provider:       "Microsoft.ServiceBus",
   122  				Path: map[string]string{
   123  					"namespaces":    "testNamespace1",
   124  					"topics":        "testTopic1",
   125  					"subscriptions": "testSubscription1",
   126  				},
   127  			},
   128  			false,
   129  		},
   130  	}
   131  
   132  	for _, test := range testCases {
   133  		parsed, err := parseAzureResourceID(test.id)
   134  		if test.expectError && err != nil {
   135  			continue
   136  		}
   137  		if err != nil {
   138  			t.Fatalf("Unexpected error: %s", err)
   139  		}
   140  
   141  		if !reflect.DeepEqual(test.expectedResourceID, parsed) {
   142  			t.Fatalf("Unexpected resource ID:\nExpected: %+v\nGot:      %+v\n", test.expectedResourceID, parsed)
   143  		}
   144  	}
   145  }