From 94176d399bfbfba6a2f3570fcbae9672fc41fe5a Mon Sep 17 00:00:00 2001 From: axel831102 Date: Tue, 8 Dec 2020 17:55:49 +0800 Subject: [PATCH] Do not read throughput for serverless cosmos table --- .../cosmos/cosmosdb_table_resource.go | 37 +++++++++++++++---- .../cosmos/cosmosdb_table_resource_test.go | 31 ++++++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_table_resource.go b/azurerm/internal/services/cosmos/cosmosdb_table_resource.go index ce154163dafc..d3247555f74b 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_table_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_table_resource.go @@ -192,6 +192,7 @@ func resourceArmCosmosDbTableUpdate(d *schema.ResourceData, meta interface{}) er func resourceArmCosmosDbTableRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Cosmos.TableClient + accountClient := meta.(*clients.Client).Cosmos.DatabaseClient ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() @@ -219,16 +220,36 @@ func resourceArmCosmosDbTableRead(d *schema.ResourceData, meta interface{}) erro } } - throughputResp, err := client.GetTableThroughput(ctx, id.ResourceGroup, id.DatabaseAccountName, id.Name) + accResp, err := accountClient.Get(ctx, id.ResourceGroup, id.DatabaseAccountName) if err != nil { - if !utils.ResponseWasNotFound(throughputResp.Response) { - return fmt.Errorf("Error reading Throughput on Cosmos Table %q (Account: %q) ID: %v", id.Name, id.DatabaseAccountName, err) - } else { - d.Set("throughput", nil) - d.Set("autoscale_settings", nil) + return fmt.Errorf("reading CosmosDB Account %q (Resource Group %q): %+v", id.DatabaseAccountName, id.ResourceGroup, err) + } + + if accResp.ID == nil || *accResp.ID == "" { + return fmt.Errorf("cosmosDB Account %q (Resource Group %q) ID is empty or nil", id.DatabaseAccountName, 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.GetTableThroughput(ctx, id.ResourceGroup, id.DatabaseAccountName, id.Name) + if err != nil { + if !utils.ResponseWasNotFound(throughputResp.Response) { + return fmt.Errorf("Error reading Throughput on Cosmos Table %q (Account: %q) ID: %v", id.Name, id.DatabaseAccountName, 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_table_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_table_resource_test.go index 1faa7f3bf1de..d0f294ecb9cf 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_table_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_table_resource_test.go @@ -98,6 +98,25 @@ func TestAccAzureRMCosmosDbTable_autoscale(t *testing.T) { }) } +func TestAccAzureRMCosmosDbTable_serverless(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_table", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMCosmosDbTableDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMCosmosDbTable_serverless(data), + Check: resource.ComposeAggregateTestCheckFunc( + testCheckAzureRMCosmosDbTableExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + func testCheckAzureRMCosmosDbTableDestroy(s *terraform.State) error { client := acceptance.AzureProvider.Meta().(*clients.Client).Cosmos.TableClient ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext @@ -193,3 +212,15 @@ resource "azurerm_cosmosdb_table" "test" { } `, testAccAzureRMCosmosDBAccount_capabilities(data, documentdb.GlobalDocumentDB, []string{"EnableTable"}), data.RandomInteger, maxThroughput) } + +func testAccAzureRMCosmosDbTable_serverless(data acceptance.TestData) string { + return fmt.Sprintf(` +%[1]s + +resource "azurerm_cosmosdb_table" "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", "EnableTable"}), data.RandomInteger) +}