From 7cd0e152fd9749f7f183e492fe08e621a531534a Mon Sep 17 00:00:00 2001 From: Massimiliano Donini Date: Fri, 6 Nov 2020 09:14:31 +0100 Subject: [PATCH] Do not read throughput for serverless cosmos sql --- .../cosmos/cosmosdb_sql_database_resource.go | 37 +++++++++++++++---- .../cosmosdb_sql_database_resource_test.go | 30 +++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_sql_database_resource.go b/azurerm/internal/services/cosmos/cosmosdb_sql_database_resource.go index c2b0c3ad8624..5ef091d67ad5 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_database_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_database_resource.go @@ -192,6 +192,7 @@ func resourceArmCosmosDbSQLDatabaseUpdate(d *schema.ResourceData, meta interface func resourceArmCosmosDbSQLDatabaseRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Cosmos.SqlClient + accountClient := meta.(*clients.Client).Cosmos.DatabaseClient ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() @@ -219,16 +220,36 @@ func resourceArmCosmosDbSQLDatabaseRead(d *schema.ResourceData, meta interface{} } } - throughputResp, err := client.GetSQLDatabaseThroughput(ctx, id.ResourceGroup, id.Account, id.Name) + accResp, err := accountClient.Get(ctx, id.ResourceGroup, id.Account) if err != nil { - if !utils.ResponseWasNotFound(throughputResp.Response) { - return fmt.Errorf("Error reading Throughput on Cosmos SQL Database %q (Account: %q) ID: %v", id.Name, id.Account, err) - } else { - d.Set("throughput", nil) - d.Set("autoscale_settings", nil) + return fmt.Errorf("reading CosmosDB Account %q (Resource Group %q): %+v", id.Account, id.ResourceGroup, err) + } + + if accResp.ID == nil || *accResp.ID == "" { + return fmt.Errorf("cosmosDB Account %q (Resource Group %q) ID is empty or nil", id.Account, id.ResourceGroup) + } + + if props := accResp.DatabaseAccountGetProperties; props != nil && props.Capabilities != nil { + serverless := false + for _, v := range *props.Capabilities { + if *v.Name == "EnableServerless" { + serverless = true + } + } + + if !serverless { + throughputResp, err := client.GetSQLDatabaseThroughput(ctx, id.ResourceGroup, id.Account, id.Name) + if err != nil { + if !utils.ResponseWasNotFound(throughputResp.Response) { + return fmt.Errorf("Error reading Throughput on Cosmos SQL Database %q (Account: %q) ID: %v", id.Name, id.Account, err) + } else { + d.Set("throughput", nil) + d.Set("autoscale_settings", nil) + } + } else { + common.SetResourceDataThroughputFromResponse(throughputResp, d) + } } - } else { - common.SetResourceDataThroughputFromResponse(throughputResp, d) } return nil diff --git a/azurerm/internal/services/cosmos/cosmosdb_sql_database_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_sql_database_resource_test.go index 6676f36b68b8..774cc6674546 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_database_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_database_resource_test.go @@ -100,6 +100,25 @@ func TestAccAzureRMCosmosDbSqlDatabase_autoscale(t *testing.T) { }) } +func TestAccAzureRMCosmosDbSqlDatabase_serverless(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_sql_database", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMCosmosDbSqlDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMCosmosDbSqlDatabase_serverless(data), + Check: resource.ComposeAggregateTestCheckFunc( + testCheckAzureRMCosmosDbSqlDatabaseExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + func testCheckAzureRMCosmosDbSqlDatabaseDestroy(s *terraform.State) error { client := acceptance.AzureProvider.Meta().(*clients.Client).Cosmos.SqlClient ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext @@ -195,3 +214,14 @@ resource "azurerm_cosmosdb_sql_database" "test" { } `, testAccAzureRMCosmosDBAccount_basic(data, documentdb.GlobalDocumentDB, documentdb.Strong), data.RandomInteger, maxThroughput) } + +func testAccAzureRMCosmosDbSqlDatabase_serverless(data acceptance.TestData) string { + return fmt.Sprintf(` +%[1]s +resource "azurerm_cosmosdb_sql_database" "test" { + name = "acctest-%[2]d" + resource_group_name = azurerm_cosmosdb_account.test.resource_group_name + account_name = azurerm_cosmosdb_account.test.name +} +`, testAccAzureRMCosmosDBAccount_capabilities(data, documentdb.GlobalDocumentDB, []string{"EnableServerless"}), data.RandomInteger) +}