From 9dac352c9f24023b3cfb990ded46fc0bbc9bf294 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Wed, 5 May 2021 17:00:01 -0400 Subject: [PATCH] [azure] Add sovereign cloud support for remaining modules (#884) * Update ci.yml * Adding action to set up Terraform 14.9 * Update CI to pin to terraform 15.1 * Add CreateResourceGroupClient * Add SQL sovereign cloud support * Add MySQL sovereign cloud support * Add disk sovereign cloud support * Add sovereign cloud support for action group * Bring missing clients from master * Fix imports Co-authored-by: Engin Polat Co-authored-by: Richard Guthrie Co-authored-by: Hadwa Abdelhalem Co-authored-by: Hattan Shobokshi --- modules/azure/actiongroup.go | 5 +- modules/azure/client_factory.go | 165 ++++++++++++++++++++++++++++++++ modules/azure/disk.go | 3 +- modules/azure/mysql.go | 5 +- modules/azure/resourcegroup.go | 5 +- modules/azure/sql.go | 8 +- 6 files changed, 181 insertions(+), 10 deletions(-) diff --git a/modules/azure/actiongroup.go b/modules/azure/actiongroup.go index 92d84b4a6..a2ff552d0 100644 --- a/modules/azure/actiongroup.go +++ b/modules/azure/actiongroup.go @@ -4,7 +4,7 @@ import ( "context" "testing" - "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/insights" + "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/monitor/mgmt/insights" "github.com/stretchr/testify/require" ) @@ -29,7 +29,7 @@ func GetActionGroupResourceE(ruleName string, resGroupName string, subscriptionI return nil, err } - client, err := getActionGroupClient(subscriptionID) + client, err := CreateActionGroupClient(subscriptionID) if err != nil { return nil, err } @@ -42,6 +42,7 @@ func GetActionGroupResourceE(ruleName string, resGroupName string, subscriptionI return &actionGroup, nil } +// TODO: remove in next version func getActionGroupClient(subscriptionID string) (*insights.ActionGroupsClient, error) { subID, err := getTargetAzureSubscription(subscriptionID) if err != nil { diff --git a/modules/azure/client_factory.go b/modules/azure/client_factory.go index a9b29b942..722868abc 100644 --- a/modules/azure/client_factory.go +++ b/modules/azure/client_factory.go @@ -14,6 +14,9 @@ import ( "os" "reflect" + "github.com/Azure/azure-sdk-for-go/profiles/latest/mysql/mgmt/mysql" + "github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/resources" + "github.com/Azure/azure-sdk-for-go/profiles/latest/sql/mgmt/sql" "github.com/Azure/azure-sdk-for-go/profiles/preview/cosmos-db/mgmt/documentdb" "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" @@ -234,6 +237,168 @@ func CreateAvailabilitySetClientE(subscriptionID string) (*compute.AvailabilityS return &client, nil } +// CreateResourceGroupClientE gets a resource group client in a subscription +func CreateResourceGroupClientE(subscriptionID string) (*resources.GroupsClient, error) { + subscriptionID, err := getTargetAzureSubscription(subscriptionID) + if err != nil { + return nil, err + } + + // Lookup environment URI + baseURI, err := getBaseURI() + if err != nil { + return nil, err + } + + resourceGroupClient := resources.NewGroupsClientWithBaseURI(baseURI, subscriptionID) + + authorizer, err := NewAuthorizer() + if err != nil { + return nil, err + } + resourceGroupClient.Authorizer = *authorizer + return &resourceGroupClient, nil +} + +// CreateSQLServerClient is a helper function that will create and setup a sql server client +func CreateSQLServerClient(subscriptionID string) (*sql.ServersClient, 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 + } + + // Create a sql server client + sqlClient := sql.NewServersClientWithBaseURI(baseURI, subscriptionID) + + // Create an authorizer + authorizer, err := NewAuthorizer() + if err != nil { + return nil, err + } + + // Attach authorizer to the client + sqlClient.Authorizer = *authorizer + + return &sqlClient, nil +} + +// CreateDatabaseClient is a helper function that will create and setup a SQL DB client +func CreateDatabaseClient(subscriptionID string) (*sql.DatabasesClient, 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 + } + + // Create a sql DB client + sqlDBClient := sql.NewDatabasesClientWithBaseURI(baseURI, subscriptionID) + + // Create an authorizer + authorizer, err := NewAuthorizer() + if err != nil { + return nil, err + } + + // Attach authorizer to the client + sqlDBClient.Authorizer = *authorizer + + return &sqlDBClient, nil +} + +// CreateMySQLServerClientE is a helper function that will setup a mysql server client. +func CreateMySQLServerClientE(subscriptionID string) (*mysql.ServersClient, 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 + } + + // Create a mysql server client + mysqlClient := mysql.NewServersClientWithBaseURI(baseURI, subscriptionID) + + // Create an authorizer + authorizer, err := NewAuthorizer() + if err != nil { + return nil, err + } + + // Attach authorizer to the client + mysqlClient.Authorizer = *authorizer + + return &mysqlClient, nil +} + +// CreateDisksClientE returns a new Disks client in the specified Azure Subscription +func CreateDisksClientE(subscriptionID string) (*compute.DisksClient, 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 + } + + // Get the Disks client + client := compute.NewDisksClientWithBaseURI(baseURI, subscriptionID) + + // Create an authorizer + authorizer, err := NewAuthorizer() + if err != nil { + return nil, err + } + + client.Authorizer = *authorizer + + return &client, nil +} + +func CreateActionGroupClient(subscriptionID string) (*insights.ActionGroupsClient, error) { + subID, err := getTargetAzureSubscription(subscriptionID) + if err != nil { + return nil, err + } + + // Lookup environment URI + baseURI, err := getBaseURI() + if err != nil { + return nil, err + } + + metricAlertsClient := insights.NewActionGroupsClientWithBaseURI(baseURI, subID) + + authorizer, err := NewAuthorizer() + if err != nil { + return nil, err + } + + metricAlertsClient.Authorizer = *authorizer + + return &metricAlertsClient, nil +} + // CreateVMInsightsClientE gets a VM Insights client func CreateVMInsightsClientE(subscriptionID string) (*insights.VMInsightsClient, error) { // Validate Azure subscription ID diff --git a/modules/azure/disk.go b/modules/azure/disk.go index 3e667478c..3b9871283 100644 --- a/modules/azure/disk.go +++ b/modules/azure/disk.go @@ -46,7 +46,7 @@ func GetDiskE(diskName string, resGroupName string, subscriptionID string) (*com } // Get the client reference - client, err := GetDiskClientE(subscriptionID) + client, err := CreateDisksClientE(subscriptionID) if err != nil { return nil, err } @@ -61,6 +61,7 @@ func GetDiskE(diskName string, resGroupName string, subscriptionID string) (*com } // GetDiskClientE returns a new Disk client in the specified Azure Subscription +// TODO: remove in next major/minor version func GetDiskClientE(subscriptionID string) (*compute.DisksClient, error) { // Validate Azure subscription ID subscriptionID, err := getTargetAzureSubscription(subscriptionID) diff --git a/modules/azure/mysql.go b/modules/azure/mysql.go index af7c74736..b73e2a299 100644 --- a/modules/azure/mysql.go +++ b/modules/azure/mysql.go @@ -3,12 +3,13 @@ package azure import ( "context" - "github.com/Azure/azure-sdk-for-go/services/mysql/mgmt/2017-12-01/mysql" + "github.com/Azure/azure-sdk-for-go/profiles/latest/mysql/mgmt/mysql" "github.com/gruntwork-io/terratest/modules/testing" "github.com/stretchr/testify/require" ) // GetMYSQLServerClientE is a helper function that will setup a mysql server client. +// TODO: remove in next version func GetMYSQLServerClientE(subscriptionID string) (*mysql.ServersClient, error) { // Validate Azure subscription ID subscriptionID, err := getTargetAzureSubscription(subscriptionID) @@ -43,7 +44,7 @@ func GetMYSQLServer(t testing.TestingT, resGroupName string, serverName string, // GetMYSQLServerE is a helper function that gets the server. func GetMYSQLServerE(t testing.TestingT, subscriptionID string, resGroupName string, serverName string) (*mysql.Server, error) { // Create a mySQl Server client - mysqlClient, err := GetMYSQLServerClientE(subscriptionID) + mysqlClient, err := CreateMySQLServerClientE(subscriptionID) if err != nil { return nil, err } diff --git a/modules/azure/resourcegroup.go b/modules/azure/resourcegroup.go index 4b885f55c..994a2855f 100644 --- a/modules/azure/resourcegroup.go +++ b/modules/azure/resourcegroup.go @@ -35,7 +35,7 @@ func GetResourceGroupE(resourceGroupName, subscriptionID string) (bool, error) { if err != nil { return false, err } - client, err := GetResourceGroupClientE(subscriptionID) + client, err := CreateResourceGroupClientE(subscriptionID) if err != nil { return false, err } @@ -46,7 +46,8 @@ func GetResourceGroupE(resourceGroupName, subscriptionID string) (bool, error) { return (resourceGroupName == *rg.Name), nil } -//GetResourceGroupClientE gets a resource group client in a subscription +// GetResourceGroupClientE gets a resource group client in a subscription +// TODO: remove in next version func GetResourceGroupClientE(subscriptionID string) (*resources.GroupsClient, error) { subscriptionID, err := getTargetAzureSubscription(subscriptionID) if err != nil { diff --git a/modules/azure/sql.go b/modules/azure/sql.go index bbcb0919d..b1e5992be 100644 --- a/modules/azure/sql.go +++ b/modules/azure/sql.go @@ -9,6 +9,7 @@ import ( ) // GetSQLServerClient is a helper function that will setup a sql server client +// TODO: remove in next version func GetSQLServerClient(subscriptionID string) (*sql.ServersClient, error) { // Validate Azure subscription ID subscriptionID, err := getTargetAzureSubscription(subscriptionID) @@ -43,7 +44,7 @@ func GetSQLServer(t testing.TestingT, resGroupName string, serverName string, su // GetSQLServerE is a helper function that gets the sql server object. func GetSQLServerE(t testing.TestingT, subscriptionID string, resGroupName string, serverName string) (*sql.Server, error) { // Create a SQl Server client - sqlClient, err := GetSQLServerClient(subscriptionID) + sqlClient, err := CreateSQLServerClient(subscriptionID) if err != nil { return nil, err } @@ -59,6 +60,7 @@ func GetSQLServerE(t testing.TestingT, subscriptionID string, resGroupName strin } // GetDatabaseClient is a helper function that will setup a sql DB client +// TODO: remove in next version func GetDatabaseClient(subscriptionID string) (*sql.DatabasesClient, error) { // Validate Azure subscription ID subscriptionID, err := getTargetAzureSubscription(subscriptionID) @@ -92,7 +94,7 @@ func ListSQLServerDatabases(t testing.TestingT, resGroupName string, serverName //ListSQLServerDatabasesE is a helper function that gets a list of databases on a sql server func ListSQLServerDatabasesE(t testing.TestingT, resGroupName string, serverName string, subscriptionID string) (*[]sql.Database, error) { // Create a SQl db client - sqlClient, err := GetDatabaseClient(subscriptionID) + sqlClient, err := CreateDatabaseClient(subscriptionID) if err != nil { return nil, err } @@ -119,7 +121,7 @@ func GetSQLDatabase(t testing.TestingT, resGroupName string, serverName string, // GetSQLDatabaseE is a helper function that gets the sql db. func GetSQLDatabaseE(t testing.TestingT, subscriptionID string, resGroupName string, serverName string, dbName string) (*sql.Database, error) { // Create a SQl db client - sqlClient, err := GetDatabaseClient(subscriptionID) + sqlClient, err := CreateDatabaseClient(subscriptionID) if err != nil { return nil, err }