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 PublicIP address module to use sovereign client factory #852

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
21 changes: 21 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 @@ -138,6 +139,26 @@ func CreateCosmosDBSQLClientE(subscriptionID string) (*documentdb.SQLResourcesCl
return &cosmosClient, nil
}

// CreatePublicIPAddressesClientE returns a public IP address client instance configured with the correct BaseURI depending on
// the Azure environment that is currently setup (or "Public", if none is setup).
func CreatePublicIPAddressesClientE(subscriptionID string) (*network.PublicIPAddressesClient, 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
client := network.NewPublicIPAddressesClientWithBaseURI(baseURI, subscriptionID)
return &client, err
}

// CreateKeyVaultManagementClientE is a helper function that will setup a key vault management client with the correct BaseURI depending on
// the Azure environment that is currently setup (or "Public", if none is setup).
func CreateKeyVaultManagementClientE(subscriptionID string) (*kvmng.VaultsClient, error) {
Expand Down
34 changes: 34 additions & 0 deletions modules/azure/client_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,37 @@ func TestCosmosDBSQLClientBaseURISetCorrectly(t *testing.T) {
})
}
}

func TestPublicIPAddressesClientBaseURISetCorrectly(t *testing.T) {
var cases = []struct {
CaseName string
EnvironmentName string
ExpectedBaseURI string
}{
{"GovCloud/CosmosDBAccountClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint},
{"PublicCloud/CosmosDBAccountClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint},
{"ChinaCloud/CosmosDBAccountClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint},
{"GermanCloud/CosmosDBAccountClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint},
}

// save any current env value and restore on exit
currentEnv := os.Getenv(AzureEnvironmentEnvName)
defer os.Setenv(AzureEnvironmentEnvName, currentEnv)

for _, tt := range cases {
// The following is necessary to make sure testCase's values don't
// get updated due to concurrency within the scope of t.Run(..) below
tt := tt
t.Run(tt.CaseName, func(t *testing.T) {
// Override env setting
os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName)

// Get a VM client
client, err := CreatePublicIPAddressesClientE("")
require.NoError(t, err)

// Check for correct ARM URI
assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI)
})
}
}
9 changes: 3 additions & 6 deletions modules/azure/publicaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,18 @@ func GetPublicIPAddressE(publicIPAddressName string, resGroupName string, subscr

// GetPublicIPAddressClientE creates a Public IP Addresses client in the specified Azure Subscription.
func GetPublicIPAddressClientE(subscriptionID string) (*network.PublicIPAddressesClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
// Get the Public IP Address client from clientfactory
client, err := CreatePublicIPAddressesClientE(subscriptionID)
if err != nil {
return nil, err
}

// Get the Public IP Address client
client := network.NewPublicIPAddressesClient(subscriptionID)

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

return &client, nil
return client, nil
}