From 6625898ce6bedde2d55a652ac858a616722f392c Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Tue, 29 Sep 2020 16:41:20 +0800 Subject: [PATCH 1/5] update --- .../cosmosdb_mongo_database_resource.go | 36 +++++++++++++++---- .../cosmosdb_mongo_database_resource_test.go | 31 ++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go index 9972ef2a68e5..e3366421f07f 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go @@ -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() @@ -224,16 +225,28 @@ 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) + 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 && !cosmosdbAccountPropsIsServerless(props) { + 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 { - d.Set("throughput", nil) - d.Set("autoscale_settings", nil) + common.SetResourceDataThroughputFromResponse(throughputResp, d) } - } else { - common.SetResourceDataThroughputFromResponse(throughputResp, d) } return nil @@ -263,3 +276,12 @@ func resourceArmCosmosDbMongoDatabaseDelete(d *schema.ResourceData, meta interfa return nil } + +func cosmosdbAccountPropsIsServerless(props *documentdb.DatabaseAccountGetProperties) bool { + for _, v := range *props.Capabilities { + if *v.Name == "EnableServerless" { + return true + } + } + return false +} diff --git a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go index 6e93843d3670..fc3ffdbc05d8 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go @@ -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 @@ -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", "MongoDBv3.4"}), data.RandomInteger) +} From 3a3a225848d5b41f06be6c20965f0630332e7c60 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Wed, 30 Sep 2020 09:36:23 +0800 Subject: [PATCH 2/5] fmt --- .../services/cosmos/cosmosdb_mongo_database_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go index fc3ffdbc05d8..8bf2f2898562 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go @@ -96,7 +96,7 @@ func TestAccAzureRMCosmosDbMongoDatabase_serverless(t *testing.T) { CheckDestroy: testCheckAzureRMCosmosDbMongoDatabaseDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMCosmosDbMongoDatabase_serverless(data), + Config: testAccAzureRMCosmosDbMongoDatabase_serverless(data), Check: resource.ComposeAggregateTestCheckFunc( testCheckAzureRMCosmosDbMongoDatabaseExists(data.ResourceName), ), From 4ef512d9b32e2d5e2212089b55fc9eb0035aaf39 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Fri, 9 Oct 2020 10:59:01 +0800 Subject: [PATCH 3/5] update --- .../cosmosdb_mongo_database_resource.go | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go index e3366421f07f..82d1b5c85ad7 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go @@ -235,17 +235,25 @@ func resourceArmCosmosDbMongoDatabaseRead(d *schema.ResourceData, meta interface } // if the cosmos Account is serverless, it could not call the get throughput api - if props := accResp.DatabaseAccountGetProperties; props != nil && props.Capabilities != nil && !cosmosdbAccountPropsIsServerless(props) { - 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) + 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 { - d.Set("throughput", nil) - d.Set("autoscale_settings", nil) + common.SetResourceDataThroughputFromResponse(throughputResp, d) } - } else { - common.SetResourceDataThroughputFromResponse(throughputResp, d) } } @@ -275,13 +283,4 @@ func resourceArmCosmosDbMongoDatabaseDelete(d *schema.ResourceData, meta interfa } return nil -} - -func cosmosdbAccountPropsIsServerless(props *documentdb.DatabaseAccountGetProperties) bool { - for _, v := range *props.Capabilities { - if *v.Name == "EnableServerless" { - return true - } - } - return false -} +} \ No newline at end of file From ad53bb9235c83bacd5be16afc35ab81fa559d13e Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Fri, 9 Oct 2020 11:01:05 +0800 Subject: [PATCH 4/5] update --- .../services/cosmos/cosmosdb_mongo_database_resource.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go index 82d1b5c85ad7..7b30c1d109a3 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource.go @@ -242,6 +242,7 @@ func resourceArmCosmosDbMongoDatabaseRead(d *schema.ResourceData, meta interface serverless = true } } + if !serverless { throughputResp, err := client.GetMongoDBDatabaseThroughput(ctx, id.ResourceGroup, id.Account, id.Name) if err != nil { @@ -283,4 +284,4 @@ func resourceArmCosmosDbMongoDatabaseDelete(d *schema.ResourceData, meta interfa } return nil -} \ No newline at end of file +} From 073e70b7a1e495aa141eef5b457929905b13262e Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Wed, 4 Nov 2020 15:39:17 +0800 Subject: [PATCH 5/5] update --- .../services/cosmos/cosmosdb_mongo_database_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go index 8bf2f2898562..8cd62a0a0d15 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_mongo_database_resource_test.go @@ -211,5 +211,5 @@ resource "azurerm_cosmosdb_mongo_database" "test" { 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", "MongoDBv3.4"}), data.RandomInteger) +`, testAccAzureRMCosmosDBAccount_capabilities(data, documentdb.MongoDB, []string{"EnableServerless", "mongoEnableDocLevelTTL", "EnableMongo"}), data.RandomInteger) }