github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/azure/cosmosdb.go (about)

     1  package azure
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/Azure/azure-sdk-for-go/profiles/preview/cosmos-db/mgmt/documentdb"
     7  	"github.com/gruntwork-io/terratest/modules/testing"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // GetCosmosDBAccountClientE is a helper function that will setup a CosmosDB account client.
    12  func GetCosmosDBAccountClientE(subscriptionID string) (*documentdb.DatabaseAccountsClient, error) {
    13  
    14  	// Create a CosmosDB client
    15  	cosmosClient, err := CreateCosmosDBAccountClientE(subscriptionID)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	// Create an authorizer
    21  	authorizer, err := NewAuthorizer()
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	// Attach authorizer to the client
    27  	cosmosClient.Authorizer = *authorizer
    28  
    29  	return cosmosClient, nil
    30  }
    31  
    32  // GetCosmosDBAccountClient is a helper function that will setup a CosmosDB account client. This function would fail the test if there is an error.
    33  func GetCosmosDBAccountClient(t testing.TestingT, subscriptionID string) *documentdb.DatabaseAccountsClient {
    34  	cosmosDBAccount, err := GetCosmosDBAccountClientE(subscriptionID)
    35  	require.NoError(t, err)
    36  
    37  	return cosmosDBAccount
    38  }
    39  
    40  // GetCosmosDBAccount is a helper function that gets the database account. This function would fail the test if there is an error.
    41  func GetCosmosDBAccount(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string) *documentdb.DatabaseAccountGetResults {
    42  	cosmosDBAccount, err := GetCosmosDBAccountE(t, subscriptionID, resourceGroupName, accountName)
    43  	require.NoError(t, err)
    44  
    45  	return cosmosDBAccount
    46  }
    47  
    48  // GetCosmosDBAccountE is a helper function that gets the database account.
    49  func GetCosmosDBAccountE(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string) (*documentdb.DatabaseAccountGetResults, error) {
    50  	// Create a CosmosDB client
    51  	cosmosClient, err := GetCosmosDBAccountClientE(subscriptionID)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	// Get the corresponding database account
    57  	cosmosDBAccount, err := cosmosClient.Get(context.Background(), resourceGroupName, accountName)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	//Return DB
    63  	return &cosmosDBAccount, nil
    64  }
    65  
    66  // GetCosmosDBSQLClientE is a helper function that will setup a CosmosDB SQL client.
    67  func GetCosmosDBSQLClientE(subscriptionID string) (*documentdb.SQLResourcesClient, error) {
    68  
    69  	// Create a CosmosDB client
    70  	cosmosClient, err := CreateCosmosDBSQLClientE(subscriptionID)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	// Create an authorizer
    76  	authorizer, err := NewAuthorizer()
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	// Attach authorizer to the client
    82  	cosmosClient.Authorizer = *authorizer
    83  
    84  	return cosmosClient, nil
    85  }
    86  
    87  // GetCosmosDBSQLClient is a helper function that will setup a CosmosDB SQL client. This function would fail the test if there is an error.
    88  func GetCosmosDBSQLClient(t testing.TestingT, subscriptionID string) *documentdb.SQLResourcesClient {
    89  	cosmosClient, err := GetCosmosDBSQLClientE(subscriptionID)
    90  	require.NoError(t, err)
    91  
    92  	return cosmosClient
    93  }
    94  
    95  // GetCosmosDBSQLDatabase is a helper function that gets a SQL database. This function would fail the test if there is an error.
    96  func GetCosmosDBSQLDatabase(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string, databaseName string) *documentdb.SQLDatabaseGetResults {
    97  	cosmosSQLDB, err := GetCosmosDBSQLDatabaseE(t, subscriptionID, resourceGroupName, accountName, databaseName)
    98  	require.NoError(t, err)
    99  
   100  	return cosmosSQLDB
   101  }
   102  
   103  // GetCosmosDBSQLDatabaseE is a helper function that gets a SQL database.
   104  func GetCosmosDBSQLDatabaseE(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string, databaseName string) (*documentdb.SQLDatabaseGetResults, error) {
   105  	// Create a CosmosDB client
   106  	cosmosClient, err := GetCosmosDBSQLClientE(subscriptionID)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	// Get the corresponding database
   112  	cosmosSQLDB, err := cosmosClient.GetSQLDatabase(context.Background(), resourceGroupName, accountName, databaseName)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	//Return DB
   118  	return &cosmosSQLDB, nil
   119  }
   120  
   121  // GetCosmosDBSQLContainer is a helper function that gets a SQL container. This function would fail the test if there is an error.
   122  func GetCosmosDBSQLContainer(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string, databaseName string, containerName string) *documentdb.SQLContainerGetResults {
   123  	cosmosSQLContainer, err := GetCosmosDBSQLContainerE(t, subscriptionID, resourceGroupName, accountName, databaseName, containerName)
   124  	require.NoError(t, err)
   125  
   126  	return cosmosSQLContainer
   127  }
   128  
   129  // GetCosmosDBSQLContainerE is a helper function that gets a SQL container.
   130  func GetCosmosDBSQLContainerE(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string, databaseName string, containerName string) (*documentdb.SQLContainerGetResults, error) {
   131  	// Create a CosmosDB client
   132  	cosmosClient, err := GetCosmosDBSQLClientE(subscriptionID)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	// Get the corresponding SQL container
   138  	cosmosSQLContainer, err := cosmosClient.GetSQLContainer(context.Background(), resourceGroupName, accountName, databaseName, containerName)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	//Return container
   144  	return &cosmosSQLContainer, nil
   145  }
   146  
   147  // GetCosmosDBSQLDatabaseThroughput is a helper function that gets a SQL database throughput configuration. This function would fail the test if there is an error.
   148  func GetCosmosDBSQLDatabaseThroughput(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string, databaseName string) *documentdb.ThroughputSettingsGetResults {
   149  	cosmosSQLDBThroughput, err := GetCosmosDBSQLDatabaseThroughputE(t, subscriptionID, resourceGroupName, accountName, databaseName)
   150  	require.NoError(t, err)
   151  
   152  	return cosmosSQLDBThroughput
   153  }
   154  
   155  // GetCosmosDBSQLDatabaseThroughputE is a helper function that gets a SQL database throughput configuration.
   156  func GetCosmosDBSQLDatabaseThroughputE(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string, databaseName string) (*documentdb.ThroughputSettingsGetResults, error) {
   157  	// Create a CosmosDB client
   158  	cosmosClient, err := GetCosmosDBSQLClientE(subscriptionID)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	// Get the corresponding database throughput config
   164  	cosmosSQLDBThroughput, err := cosmosClient.GetSQLDatabaseThroughput(context.Background(), resourceGroupName, accountName, databaseName)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	//Return throughput config
   170  	return &cosmosSQLDBThroughput, nil
   171  }
   172  
   173  // GetCosmosDBSQLContainerThroughput is a helper function that gets a SQL container throughput configuration. This function would fail the test if there is an error.
   174  func GetCosmosDBSQLContainerThroughput(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string, databaseName string, containerName string) *documentdb.ThroughputSettingsGetResults {
   175  	cosmosSQLCtrThroughput, err := GetCosmosDBSQLContainerThroughputE(t, subscriptionID, resourceGroupName, accountName, databaseName, containerName)
   176  	require.NoError(t, err)
   177  
   178  	return cosmosSQLCtrThroughput
   179  }
   180  
   181  // GetCosmosDBSQLContainerThroughputE is a helper function that gets a SQL container throughput configuration.
   182  func GetCosmosDBSQLContainerThroughputE(t testing.TestingT, subscriptionID string, resourceGroupName string, accountName string, databaseName string, containerName string) (*documentdb.ThroughputSettingsGetResults, error) {
   183  	// Create a CosmosDB client
   184  	cosmosClient, err := GetCosmosDBSQLClientE(subscriptionID)
   185  	if err != nil {
   186  		return nil, err
   187  	}
   188  
   189  	// Get the corresponding container throughput config
   190  	cosmosSQLCtrThroughput, err := cosmosClient.GetSQLContainerThroughput(context.Background(), resourceGroupName, accountName, databaseName, containerName)
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  
   195  	//Return throughput config
   196  	return &cosmosSQLCtrThroughput, nil
   197  }