diff --git a/modules/azure/client_factory.go b/modules/azure/client_factory.go index d871de180..116d862ce 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 } @@ -58,21 +58,31 @@ func CreateSubscriptionsClientE() (subscriptions.Client, error) { // CreateVirtualMachinesClientE returns a virtual machines client instance configured with the correct BaseURI depending on // the Azure environment that is currently setup (or "Public", if none is setup). -func CreateVirtualMachinesClientE(subscriptionID string) (compute.VirtualMachinesClient, error) { +func CreateVirtualMachinesClientE(subscriptionID string) (*compute.VirtualMachinesClient, error) { // Validate Azure subscription ID subscriptionID, err := getTargetAzureSubscription(subscriptionID) if err != nil { - return compute.VirtualMachinesClient{}, err + return nil, err } // Lookup environment URI - baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName) + baseURI, err := getBaseURI() if err != nil { - return compute.VirtualMachinesClient{}, err + return nil, err } // Create correct client based on type passed - return compute.NewVirtualMachinesClientWithBaseURI(baseURI, subscriptionID), nil + vmClient := compute.NewVirtualMachinesClientWithBaseURI(baseURI, subscriptionID) + + // Create an authorizer + authorizer, err := NewAuthorizer() + if err != nil { + return nil, err + } + + // Attach authorizer to the client + vmClient.Authorizer = *authorizer + return &vmClient, nil } // snippet-tag-end::client_factory_example.CreateClient @@ -87,7 +97,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 +116,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 +137,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 +158,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 +207,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 +} diff --git a/modules/azure/compute.go b/modules/azure/compute.go index 8fc666698..7e4075f78 100644 --- a/modules/azure/compute.go +++ b/modules/azure/compute.go @@ -9,6 +9,7 @@ import ( ) // GetVirtualMachineClient is a helper function that will setup an Azure Virtual Machine client on your behalf. +// TODO: remove in next version func GetVirtualMachineClient(t testing.TestingT, subscriptionID string) *compute.VirtualMachinesClient { vmClient, err := GetVirtualMachineClientE(subscriptionID) require.NoError(t, err) @@ -16,6 +17,7 @@ func GetVirtualMachineClient(t testing.TestingT, subscriptionID string) *compute } // GetVirtualMachineClientE is a helper function that will setup an Azure Virtual Machine client on your behalf. +// TODO: remove in next version func GetVirtualMachineClientE(subscriptionID string) (*compute.VirtualMachinesClient, error) { // snippet-tag-start::client_factory_example.helper @@ -34,7 +36,7 @@ func GetVirtualMachineClientE(subscriptionID string) (*compute.VirtualMachinesCl // Attach authorizer to the client vmClient.Authorizer = *authorizer - return &vmClient, nil + return vmClient, nil } // VirtualMachineExists indicates whether the specifcied Azure Virtual Machine exists. @@ -272,7 +274,7 @@ func ListVirtualMachinesForResourceGroup(t testing.TestingT, resGroupName string func ListVirtualMachinesForResourceGroupE(resourceGroupName string, subscriptionID string) ([]string, error) { var vmDetails []string - vmClient, err := GetVirtualMachineClientE(subscriptionID) + vmClient, err := CreateVirtualMachinesClientE(subscriptionID) if err != nil { return nil, err } @@ -301,7 +303,7 @@ func GetVirtualMachinesForResourceGroup(t testing.TestingT, resGroupName string, // VM Object represents the entire set of VM compute properties accessible by using the VM name as the map key. func GetVirtualMachinesForResourceGroupE(resourceGroupName string, subscriptionID string) (map[string]compute.VirtualMachineProperties, error) { // Create VM Client - vmClient, err := GetVirtualMachineClientE(subscriptionID) + vmClient, err := CreateVirtualMachinesClientE(subscriptionID) if err != nil { return nil, err } @@ -356,7 +358,7 @@ func GetVirtualMachineE(vmName string, resGroupName string, subscriptionID strin } // Get the client reference - client, err := GetVirtualMachineClientE(subscriptionID) + client, err := CreateVirtualMachinesClientE(subscriptionID) if err != nil { return nil, err }