Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Azure] Resolve correct Base URI for compute module #843

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions modules/azure/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
10 changes: 6 additions & 4 deletions modules/azure/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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)
return vmClient
}

// 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
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down