Skip to content

Commit

Permalink
azurerm_cosmosdb_mongo_database doesn't call get throughput api whe…
Browse files Browse the repository at this point in the history
…n cosmos account is serverless (#8673)
  • Loading branch information
yupwei68 authored Nov 5, 2020
1 parent efd03d2 commit 7d60c91
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func resourceArmCosmosDbMongoDatabaseUpdate(d *schema.ResourceData, meta interfa

func resourceArmCosmosDbMongoDatabaseRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Cosmos.MongoDbClient
accountClient := meta.(*clients.Client).Cosmos.DatabaseClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down Expand Up @@ -224,16 +225,37 @@ func resourceArmCosmosDbMongoDatabaseRead(d *schema.ResourceData, meta interface
}
}

throughputResp, err := client.GetMongoDBDatabaseThroughput(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 Mongo Database %q (Account: %q): %+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 the cosmos Account is serverless, it could not call the get throughput api
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.GetMongoDBDatabaseThroughput(ctx, id.ResourceGroup, id.Account, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(throughputResp.Response) {
return fmt.Errorf("Error reading Throughput on Cosmos Mongo Database %q (Account: %q): %+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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ func TestAccAzureRMCosmosDbMongoDatabase_autoscale(t *testing.T) {
})
}

func TestAccAzureRMCosmosDbMongoDatabase_serverless(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_mongo_database", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMCosmosDbMongoDatabaseDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCosmosDbMongoDatabase_serverless(data),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbMongoDatabaseExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMCosmosDbMongoDatabaseDestroy(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Cosmos.MongoDbClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
Expand Down Expand Up @@ -182,3 +201,15 @@ resource "azurerm_cosmosdb_mongo_database" "test" {
}
`, testAccAzureRMCosmosDBAccount_basic(data, documentdb.MongoDB, documentdb.Strong), data.RandomInteger, maxThroughput)
}

func testAccAzureRMCosmosDbMongoDatabase_serverless(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
resource "azurerm_cosmosdb_mongo_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.MongoDB, []string{"EnableServerless", "mongoEnableDocLevelTTL", "EnableMongo"}), data.RandomInteger)
}

0 comments on commit 7d60c91

Please sign in to comment.