Skip to content

Commit

Permalink
[azure] Add sovereign cloud support for remaining modules (#884)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Richard Guthrie <[email protected]>
Co-authored-by: Hadwa Abdelhalem <[email protected]>
Co-authored-by: Hattan Shobokshi <[email protected]>
  • Loading branch information
5 people authored May 5, 2021
1 parent 9d82b97 commit 9dac352
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 10 deletions.
5 changes: 3 additions & 2 deletions modules/azure/actiongroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
165 changes: 165 additions & 0 deletions modules/azure/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/azure/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions modules/azure/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions modules/azure/resourcegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions modules/azure/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 9dac352

Please sign in to comment.