Skip to content

Commit

Permalink
azurerm_cosmosdb_table doesn't call get throughput api when cosmos …
Browse files Browse the repository at this point in the history
…account is serverless (#9749)

This is a fix for #9417

This is based on #8673 #9311 #9187
  • Loading branch information
AxelLiu authored Dec 27, 2020
1 parent 9951bc6 commit 29cc51f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
37 changes: 29 additions & 8 deletions azurerm/internal/services/cosmos/cosmosdb_table_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions azurerm/internal/services/cosmos/cosmosdb_table_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 29cc51f

Please sign in to comment.