github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/azure/sql.go (about) 1 package azure 2 3 import ( 4 "context" 5 6 "github.com/Azure/azure-sdk-for-go/profiles/preview/sql/mgmt/sql" 7 "github.com/gruntwork-io/terratest/modules/testing" 8 "github.com/stretchr/testify/require" 9 ) 10 11 // GetSQLServerClient is a helper function that will setup a sql server client 12 // TODO: remove in next version 13 func GetSQLServerClient(subscriptionID string) (*sql.ServersClient, error) { 14 // Validate Azure subscription ID 15 subscriptionID, err := getTargetAzureSubscription(subscriptionID) 16 if err != nil { 17 return nil, err 18 } 19 20 // Create a sql server client 21 sqlClient := sql.NewServersClient(subscriptionID) 22 23 // Create an authorizer 24 authorizer, err := NewAuthorizer() 25 if err != nil { 26 return nil, err 27 } 28 29 // Attach authorizer to the client 30 sqlClient.Authorizer = *authorizer 31 32 return &sqlClient, nil 33 } 34 35 // GetSQLServer is a helper function that gets the sql server object. 36 // This function would fail the test if there is an error. 37 func GetSQLServer(t testing.TestingT, resGroupName string, serverName string, subscriptionID string) *sql.Server { 38 sqlServer, err := GetSQLServerE(t, subscriptionID, resGroupName, serverName) 39 require.NoError(t, err) 40 41 return sqlServer 42 } 43 44 // GetSQLServerE is a helper function that gets the sql server object. 45 func GetSQLServerE(t testing.TestingT, subscriptionID string, resGroupName string, serverName string) (*sql.Server, error) { 46 // Create a SQl Server client 47 sqlClient, err := CreateSQLServerClient(subscriptionID) 48 if err != nil { 49 return nil, err 50 } 51 52 //Get the corresponding server client 53 sqlServer, err := sqlClient.Get(context.Background(), resGroupName, serverName) 54 if err != nil { 55 return nil, err 56 } 57 58 //Return sql server 59 return &sqlServer, nil 60 } 61 62 // GetDatabaseClient is a helper function that will setup a sql DB client 63 // TODO: remove in next version 64 func GetDatabaseClient(subscriptionID string) (*sql.DatabasesClient, error) { 65 // Validate Azure subscription ID 66 subscriptionID, err := getTargetAzureSubscription(subscriptionID) 67 if err != nil { 68 return nil, err 69 } 70 71 // Create a sql DB client 72 sqlDBClient := sql.NewDatabasesClient(subscriptionID) 73 74 // Create an authorizer 75 authorizer, err := NewAuthorizer() 76 if err != nil { 77 return nil, err 78 } 79 80 // Attach authorizer to the client 81 sqlDBClient.Authorizer = *authorizer 82 83 return &sqlDBClient, nil 84 } 85 86 //ListSQLServerDatabases is a helper function that gets a list of databases on a sql server 87 func ListSQLServerDatabases(t testing.TestingT, resGroupName string, serverName string, subscriptionID string) *[]sql.Database { 88 dbList, err := ListSQLServerDatabasesE(t, resGroupName, serverName, subscriptionID) 89 require.NoError(t, err) 90 91 return dbList 92 } 93 94 //ListSQLServerDatabasesE is a helper function that gets a list of databases on a sql server 95 func ListSQLServerDatabasesE(t testing.TestingT, resGroupName string, serverName string, subscriptionID string) (*[]sql.Database, error) { 96 // Create a SQl db client 97 sqlClient, err := CreateDatabaseClient(subscriptionID) 98 if err != nil { 99 return nil, err 100 } 101 102 //Get the corresponding DB client 103 sqlDbs, err := sqlClient.ListByServer(context.Background(), resGroupName, serverName, "", "") 104 if err != nil { 105 return nil, err 106 } 107 108 // Return DB ID 109 return sqlDbs.Value, nil 110 } 111 112 // GetSQLDatabase is a helper function that gets the sql db. 113 // This function would fail the test if there is an error. 114 func GetSQLDatabase(t testing.TestingT, resGroupName string, serverName string, dbName string, subscriptionID string) *sql.Database { 115 database, err := GetSQLDatabaseE(t, subscriptionID, resGroupName, serverName, dbName) 116 require.NoError(t, err) 117 118 return database 119 } 120 121 // GetSQLDatabaseE is a helper function that gets the sql db. 122 func GetSQLDatabaseE(t testing.TestingT, subscriptionID string, resGroupName string, serverName string, dbName string) (*sql.Database, error) { 123 // Create a SQl db client 124 sqlClient, err := CreateDatabaseClient(subscriptionID) 125 if err != nil { 126 return nil, err 127 } 128 129 //Get the corresponding DB client 130 sqlDb, err := sqlClient.Get(context.Background(), resGroupName, serverName, dbName, "") 131 if err != nil { 132 return nil, err 133 } 134 135 // Return DB 136 return &sqlDb, nil 137 }