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 098c174cd818229a23baa11787c078a3578b2575 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Wed, 24 Mar 2021 12:15:01 -0400 Subject: [PATCH 2/2] Refactor CreateVirtualMachinesClientE --- modules/azure/client_factory.go | 18 ++++++++++++++---- modules/azure/compute.go | 10 ++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/modules/azure/client_factory.go b/modules/azure/client_factory.go index 2dc5dead7..116d862ce 100644 --- a/modules/azure/client_factory.go +++ b/modules/azure/client_factory.go @@ -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 := 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 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 }