From f27e14cd62742ed6356aedc351876e0779542f8c Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Mon, 22 Mar 2021 15:20:27 -0400 Subject: [PATCH 1/2] Add getBaseURI --- modules/azure/client_factory.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/modules/azure/client_factory.go b/modules/azure/client_factory.go index d871de180..2dc5dead7 100644 --- a/modules/azure/client_factory.go +++ b/modules/azure/client_factory.go @@ -45,7 +45,7 @@ type ClientType int // the Azure environment that is currently setup (or "Public", if none is setup). func CreateSubscriptionsClientE() (subscriptions.Client, error) { // Lookup environment URI - baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName) + baseURI, err := getBaseURI() if err != nil { return subscriptions.Client{}, err } @@ -66,7 +66,7 @@ func CreateVirtualMachinesClientE(subscriptionID string) (compute.VirtualMachine } // Lookup environment URI - baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName) + baseURI, err := getBaseURI() if err != nil { return compute.VirtualMachinesClient{}, err } @@ -87,7 +87,7 @@ func CreateManagedClustersClientE(subscriptionID string) (containerservice.Manag } // Lookup environment URI - baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName) + baseURI, err := getBaseURI() if err != nil { return containerservice.ManagedClustersClient{}, err } @@ -106,7 +106,7 @@ func CreateCosmosDBAccountClientE(subscriptionID string) (*documentdb.DatabaseAc } // Lookup environment URI - baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName) + baseURI, err := getBaseURI() if err != nil { return nil, err } @@ -127,7 +127,7 @@ func CreateCosmosDBSQLClientE(subscriptionID string) (*documentdb.SQLResourcesCl } // Lookup environment URI - baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName) + baseURI, err := getBaseURI() if err != nil { return nil, err } @@ -148,7 +148,7 @@ func CreateKeyVaultManagementClientE(subscriptionID string) (*kvmng.VaultsClient } // Lookup environment URI - baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName) + baseURI, err := getBaseURI() if err != nil { return nil, err } @@ -197,3 +197,13 @@ func getFieldValue(env *autorestAzure.Environment, field string) string { fieldVal := reflect.Indirect(structValue).FieldByName(field) return fieldVal.String() } + +// getBaseURI gets the base URI endpoint. +func getBaseURI() (string, error) { + // Lookup environment URI + baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName) + if err != nil { + return "", err + } + return baseURI, nil +} From a46b17f2ff000b39f18fa5aaba91584021cad141 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Mon, 22 Mar 2021 15:30:18 -0400 Subject: [PATCH 2/2] Add CreateAvailabilitySetClientE --- modules/azure/availabilityset.go | 7 ++++--- modules/azure/availabilityset_test.go | 4 ++-- modules/azure/client_factory.go | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/modules/azure/availabilityset.go b/modules/azure/availabilityset.go index 8dc5cf2db..c764a400e 100644 --- a/modules/azure/availabilityset.go +++ b/modules/azure/availabilityset.go @@ -39,7 +39,7 @@ func CheckAvailabilitySetContainsVM(t testing.TestingT, vmName string, avsName s // CheckAvailabilitySetContainsVME checks if the Virtual Machine is contained in the Availability Set VMs func CheckAvailabilitySetContainsVME(t testing.TestingT, vmName string, avsName string, resGroupName string, subscriptionID string) (bool, error) { - client, err := GetAvailabilitySetClientE(subscriptionID) + client, err := CreateAvailabilitySetClientE(subscriptionID) if err != nil { return false, err } @@ -71,7 +71,7 @@ func GetAvailabilitySetVMNamesInCaps(t testing.TestingT, avsName string, resGrou // GetAvailabilitySetVMNamesInCapsE gets a list of VM names in the specified Azure Availability Set func GetAvailabilitySetVMNamesInCapsE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) ([]string, error) { - client, err := GetAvailabilitySetClientE(subscriptionID) + client, err := CreateAvailabilitySetClientE(subscriptionID) if err != nil { return nil, err } @@ -120,7 +120,7 @@ func GetAvailabilitySetE(t testing.TestingT, avsName string, resGroupName string } // Get the client reference - client, err := GetAvailabilitySetClientE(subscriptionID) + client, err := CreateAvailabilitySetClientE(subscriptionID) if err != nil { return nil, err } @@ -135,6 +135,7 @@ func GetAvailabilitySetE(t testing.TestingT, avsName string, resGroupName string } // GetAvailabilitySetClientE gets a new Availability Set client in the specified Azure Subscription +// TODO: remove in next version func GetAvailabilitySetClientE(subscriptionID string) (*compute.AvailabilitySetsClient, error) { // Validate Azure subscription ID subscriptionID, err := getTargetAzureSubscription(subscriptionID) diff --git a/modules/azure/availabilityset_test.go b/modules/azure/availabilityset_test.go index 314225419..8490ee5c2 100644 --- a/modules/azure/availabilityset_test.go +++ b/modules/azure/availabilityset_test.go @@ -17,12 +17,12 @@ The below tests are currently stubbed out, with the expectation that they will t If/when methods to create and delete network resources are added, these tests can be extended. */ -func TestGetAvailabilitySetClientE(t *testing.T) { +func TestCreateAvailabilitySetClientE(t *testing.T) { t.Parallel() subscriptionID := "" - client, err := GetAvailabilitySetClientE(subscriptionID) + client, err := CreateAvailabilitySetClientE(subscriptionID) require.NoError(t, err) assert.NotEmpty(t, *client) diff --git a/modules/azure/client_factory.go b/modules/azure/client_factory.go index 2dc5dead7..7709a45bd 100644 --- a/modules/azure/client_factory.go +++ b/modules/azure/client_factory.go @@ -159,6 +159,33 @@ func CreateKeyVaultManagementClientE(subscriptionID string) (*kvmng.VaultsClient return &vaultClient, nil } +// CreateAvailabilitySetClientE creates a new Availability Set client in the specified Azure Subscription +func CreateAvailabilitySetClientE(subscriptionID string) (*compute.AvailabilitySetsClient, error) { + // Validate Azure subscription ID + subscriptionID, err := getTargetAzureSubscription(subscriptionID) + if err != nil { + return nil, err + } + + // Lookup environment URI + baseURI, err := getBaseURI() + if err != nil { + return nil, err + } + + // Get the Availability Set client + client := compute.NewAvailabilitySetsClientWithBaseURI(baseURI, subscriptionID) + + // Create an authorizer + authorizer, err := NewAuthorizer() + if err != nil { + return nil, err + } + client.Authorizer = *authorizer + + return &client, nil +} + // GetKeyVaultURISuffixE returns the proper KeyVault URI suffix for the configured Azure environment. // This function would fail the test if there is an error. func GetKeyVaultURISuffixE() (string, error) {