Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_cosmosdb_table doesn't call get throughput api when cosmos account is serverless #9749

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}