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] Updated VNet/Subnet module to use sovereign client factory (aztfmod) #848

Closed
wants to merge 1 commit into from
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
41 changes: 41 additions & 0 deletions modules/azure/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-11-01/containerservice"
kvmng "github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2016-10-01/keyvault"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-06-01/subscriptions"
autorestAzure "github.com/Azure/go-autorest/autorest/azure"
)
Expand Down Expand Up @@ -159,6 +160,46 @@ func CreateKeyVaultManagementClientE(subscriptionID string) (*kvmng.VaultsClient
return &vaultClient, nil
}

// CreateNewSubnetClientE returns a Subnet client instance configured with the
// correct BaseURI depending on the Azure environment that is currently setup (or "Public", if none is setup).
func CreateNewSubnetClientE(subscriptionID string) (*network.SubnetsClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
return nil, err
}

// Lookup environment URI
baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName)
if err != nil {
return nil, err
}

// create client
subnetClient := network.NewSubnetsClientWithBaseURI(baseURI, subscriptionID)
return &subnetClient, nil
}

// CreateNewVirtualNetworkClientE returns a Virtual Network client instance configured with the
// correct BaseURI depending on the Azure environment that is currently setup (or "Public", if none is setup).
func CreateNewVirtualNetworkClientE(subscriptionID string) (*network.VirtualNetworksClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
return nil, err
}

// Lookup environment URI
baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName)
if err != nil {
return nil, err
}

// create client
vnetClient := network.NewVirtualNetworksClientWithBaseURI(baseURI, subscriptionID)
return &vnetClient, 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) {
Expand Down
18 changes: 6 additions & 12 deletions modules/azure/virtualnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,20 @@ func GetSubnetE(subnetName string, vnetName string, resGroupName string, subscri

// GetSubnetClientE creates a subnet client.
func GetSubnetClientE(subscriptionID string) (*network.SubnetsClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
// Create a new Subnet client from client factory
client, err := CreateNewSubnetClientE(subscriptionID)
if err != nil {
return nil, err
}

// Get the Subnet client
client := network.NewSubnetsClient(subscriptionID)

// Create an authorizer
authorizer, err := NewAuthorizer()
if err != nil {
return nil, err
}
client.Authorizer = *authorizer

return &client, nil
return client, nil
}

// GetVirtualNetworkE gets Virtual Network in the specified Azure Resource Group.
Expand All @@ -205,21 +202,18 @@ func GetVirtualNetworkE(vnetName string, resGroupName string, subscriptionID str

// GetVirtualNetworksClientE creates a virtual network client in the specified Azure Subscription.
func GetVirtualNetworksClientE(subscriptionID string) (*network.VirtualNetworksClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
// Create a new Virtual Network client from client factory
client, err := CreateNewVirtualNetworkClientE(subscriptionID)
if err != nil {
return nil, err
}

// Get the Virtual Network client
client := network.NewVirtualNetworksClient(subscriptionID)

// Create an authorizer
authorizer, err := NewAuthorizer()
if err != nil {
return nil, err
}
client.Authorizer = *authorizer

return &client, nil
return client, nil
}