Skip to content

Commit

Permalink
[Azure] Add sovereign cloud support for Storage (#868)
Browse files Browse the repository at this point in the history
* Add  getBaseURI

* Update ci.yml

* Add CreateStorageAccountClientE and CreateStorageBlobContainerClientE

Co-authored-by: Engin Polat <[email protected]>
  • Loading branch information
davidkpiano and polatengin authored Apr 16, 2021
1 parent a3dee0b commit 0637ac7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
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
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.
// TODO: remove in next version
func GetStorageBlobContainerClientE(subscriptionID string) (*storage.BlobContainersClient, error) {
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
Expand Down

0 comments on commit 0637ac7

Please sign in to comment.