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] Add sovereign cloud support for Storage #868

Merged
merged 4 commits into from
Apr 16, 2021
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ name: ci-workflow
# target_pr: pr number on the source repo (e.g. 14, 25, etc.)

on:
push:
branches: master
workflow_dispatch:
inputs:
repo:
Expand Down
69 changes: 63 additions & 6 deletions modules/azure/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"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/resources/mgmt/2019-06-01/subscriptions"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
autorestAzure "github.com/Azure/go-autorest/autorest/azure"
)

Expand All @@ -45,7 +46,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 @@ -66,7 +67,7 @@ func CreateVirtualMachinesClientE(subscriptionID string) (compute.VirtualMachine
}

// Lookup environment URI
baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName)
baseURI, err := getBaseURI()
if err != nil {
return compute.VirtualMachinesClient{}, err
}
Expand All @@ -87,7 +88,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 +107,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 +128,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 +149,7 @@ func CreateKeyVaultManagementClientE(subscriptionID string) (*kvmng.VaultsClient
}

// Lookup environment URI
baseURI, err := getEnvironmentEndpointE(ResourceManagerEndpointName)
baseURI, err := getBaseURI()
if err != nil {
return nil, err
}
Expand All @@ -159,6 +160,52 @@ func CreateKeyVaultManagementClientE(subscriptionID string) (*kvmng.VaultsClient
return &vaultClient, nil
}

// CreateStorageAccountClientE creates a storage account client.
func CreateStorageAccountClientE(subscriptionID string) (*storage.AccountsClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
return nil, err
}

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

storageAccountClient := storage.NewAccountsClientWithBaseURI(baseURI, subscriptionID)
authorizer, err := NewAuthorizer()
if err != nil {
return nil, err
}
storageAccountClient.Authorizer = *authorizer
return &storageAccountClient, nil
}

// CreateStorageBlobContainerClientE creates a storage container client.
func CreateStorageBlobContainerClientE(subscriptionID string) (*storage.BlobContainersClient, error) {
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
return nil, err
}

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

blobContainerClient := storage.NewBlobContainersClientWithBaseURI(baseURI, subscriptionID)
authorizer, err := NewAuthorizer()

if err != nil {
return nil, err
}
blobContainerClient.Authorizer = *authorizer
return &blobContainerClient, 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 Expand Up @@ -197,3 +244,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
}
6 changes: 4 additions & 2 deletions modules/azure/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func GetStorageBlobContainerE(containerName, storageAccountName, resourceGroupNa
if err2 != nil {
return nil, err2
}
client, err := GetStorageBlobContainerClientE(subscriptionID)
client, err := CreateStorageBlobContainerClientE(subscriptionID)
if err != nil {
return nil, err
}
Expand All @@ -162,7 +162,7 @@ func GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscript
if err2 != nil {
return nil, err2
}
client, err := GetStorageAccountClientE(subscriptionID)
client, err := CreateStorageAccountClientE(subscriptionID)
if err != nil {
return nil, err
}
Expand All @@ -174,6 +174,7 @@ func GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscript
}

// GetStorageAccountClientE creates a storage account client.
// TODO: remove in next version

This comment was marked as resolved.

func GetStorageAccountClientE(subscriptionID string) (*storage.AccountsClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
Expand All @@ -191,6 +192,7 @@ func GetStorageAccountClientE(subscriptionID string) (*storage.AccountsClient, e
}

// GetStorageBlobContainerClientE creates a storage container client.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the method GetStorageBlobContainerClientE as it is no longer used

Copy link
Contributor

@yorinasub17 yorinasub17 Apr 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned this somewhere, but I can't find where I said this, so reiterating it here:

Since this is a public method, removing it means we will have to cut a backward incompatible release. It would be a bit of a pain for us to release a backward incompatible release for each of these changes. I think it would be better to keep these in so that we can merge all of these changes as backward compatible patch releases, and then at the very end once all the files are updated, we can have one big PR to remove all the Get.*Client functions and cut one backward incompatible release.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good @yorinasub17 , I approved it and it is ready for review @gruntwork-terratest-terragrunt

// TODO: remove in next version
func GetStorageBlobContainerClientE(subscriptionID string) (*storage.BlobContainersClient, error) {
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
Expand Down