From e4f7ae5ca6e408d6a50475bb995ee5a50c2adea4 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 1 Apr 2020 18:55:51 +0800 Subject: [PATCH 01/14] Improve code for cosmosdb --- .../resource_arm_cosmosdb_mongo_collection.go | 75 ++++++++++++++----- ...urce_arm_cosmosdb_mongo_collection_test.go | 46 ++++++++++++ 2 files changed, 101 insertions(+), 20 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index 5b6cb44645ec..70e4014dacb7 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -82,6 +82,26 @@ func resourceArmCosmosDbMongoCollection() *schema.Resource { Computed: true, ValidateFunc: validate.CosmosThroughput, }, + + "index": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "keys": { + Type: schema.TypeSet, + Required: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + + "unique": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + }, + }, + }, }, } } @@ -121,7 +141,7 @@ func resourceArmCosmosDbMongoCollectionCreate(d *schema.ResourceData, meta inter MongoDBCollectionCreateUpdateProperties: &documentdb.MongoDBCollectionCreateUpdateProperties{ Resource: &documentdb.MongoDBCollectionResource{ ID: &name, - Indexes: expandCosmosMongoCollectionIndexes(ttl), + Indexes: expandCosmosMongoCollectionIndex(d.Get("index").(*schema.Set).List(), ttl), }, Options: map[string]*string{}, }, @@ -181,7 +201,7 @@ func resourceArmCosmosDbMongoCollectionUpdate(d *schema.ResourceData, meta inter MongoDBCollectionCreateUpdateProperties: &documentdb.MongoDBCollectionCreateUpdateProperties{ Resource: &documentdb.MongoDBCollectionResource{ ID: &id.Collection, - Indexes: expandCosmosMongoCollectionIndexes(ttl), + Indexes: expandCosmosMongoCollectionIndex(d.Get("index").(*schema.Set).List(), ttl), }, Options: map[string]*string{}, }, @@ -264,7 +284,12 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa } if props.Indexes != nil { - d.Set("default_ttl_seconds", flattenCosmosMongoCollectionIndexes(props.Indexes)) + ttl := flattenCosmosMongoCollectionIndex(props.Indexes) + if err := d.Set("default_ttl_seconds", ttl); err != nil { + return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err) + } + + d.Set("index", d.Get("index").(*schema.Set).List()) } } @@ -307,11 +332,26 @@ func resourceArmCosmosDbMongoCollectionDelete(d *schema.ResourceData, meta inter return nil } -func expandCosmosMongoCollectionIndexes(defaultTtl *int) *[]documentdb.MongoIndex { - outputs := make([]documentdb.MongoIndex, 0) +func expandCosmosMongoCollectionIndex(indexes []interface{}, defaultTtl *int) *[]documentdb.MongoIndex { + results := make([]documentdb.MongoIndex, 0) + + if len(indexes) != 0 { + for _, v := range indexes { + index := v.(map[string]interface{}) + + results = append(results, documentdb.MongoIndex{ + Key: &documentdb.MongoIndexKeys{ + Keys: utils.ExpandStringSlice(index["keys"].(*schema.Set).List()), + }, + Options: &documentdb.MongoIndexOptions{ + Unique: utils.Bool(index["unique"].(bool)), + }, + }) + } + } if defaultTtl != nil { - outputs = append(outputs, documentdb.MongoIndex{ + results = append(results, documentdb.MongoIndex{ Key: &documentdb.MongoIndexKeys{ Keys: &[]string{"_ts"}, }, @@ -321,24 +361,19 @@ func expandCosmosMongoCollectionIndexes(defaultTtl *int) *[]documentdb.MongoInde }) } - return &outputs + return &results } -func flattenCosmosMongoCollectionIndexes(indexes *[]documentdb.MongoIndex) *int { - var ttl int - for _, i := range *indexes { - if key := i.Key; key != nil { - var ttlInner int32 - - if keys := key.Keys; keys != nil && len(*keys) > 0 { - k := (*keys)[0] - - if k == "_ts" { - ttl = int(ttlInner) - } +func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) *int32 { + var ttl *int32 + for _, v := range *input { + if v.Key != nil && v.Key.Keys != nil { + key := (*v.Key.Keys)[0] + if key == "_ts" && v.Options != nil && v.Options.ExpireAfterSeconds != nil { + ttl = v.Options.ExpireAfterSeconds } } } - return &ttl + return ttl } diff --git a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go index 0a84cb94d9e6..8aed43f70e2d 100644 --- a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go +++ b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go @@ -121,6 +121,27 @@ func TestAccAzureRMCosmosDbMongoCollection_throughput(t *testing.T) { }) } +func TestAccAzureRMCosmosDbMongoCollection_withIndex(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_mongo_collection", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMCosmosDbMongoCollectionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMCosmosDbMongoCollection_withIndex(data), + Check: resource.ComposeAggregateTestCheckFunc( + testCheckAzureRMCosmosDbMongoCollectionExists(data.ResourceName), + resource.TestCheckResourceAttr(data.ResourceName, "default_ttl_seconds", "707"), + resource.TestCheckResourceAttr(data.ResourceName, "index.#", "2"), + ), + }, + data.ImportStep(), + }, + }) +} + func testCheckAzureRMCosmosDbMongoCollectionDestroy(s *terraform.State) error { client := acceptance.AzureProvider.Meta().(*clients.Client).Cosmos.DatabaseClient ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext @@ -238,3 +259,28 @@ resource "azurerm_cosmosdb_mongo_collection" "test" { } `, testAccAzureRMCosmosDbMongoDatabase_basic(data), data.RandomInteger, throughput) } + +func testAccAzureRMCosmosDbMongoCollection_withIndex(data acceptance.TestData) string { + return fmt.Sprintf(` +%[1]s + +resource "azurerm_cosmosdb_mongo_collection" "test" { + name = "acctest-%[2]d" + resource_group_name = azurerm_cosmosdb_mongo_database.test.resource_group_name + account_name = azurerm_cosmosdb_mongo_database.test.account_name + database_name = azurerm_cosmosdb_mongo_database.test.name + default_ttl_seconds = 707 + throughput = 400 + + index { + keys = ["seven", "six"] + unique = true + } + + index { + keys = ["day"] + unique = false + } +} +`, testAccAzureRMCosmosDbMongoDatabase_basic(data), data.RandomInteger) +} From 191818119a0ad6b9013e2c714970268dd6f1c11a Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 2 Apr 2020 12:48:31 +0800 Subject: [PATCH 02/14] Update code --- .../cosmos/resource_arm_cosmosdb_mongo_collection.go | 2 -- .../tests/resource_arm_cosmosdb_mongo_collection_test.go | 8 ++++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index 70e4014dacb7..820a50e23841 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -288,8 +288,6 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa if err := d.Set("default_ttl_seconds", ttl); err != nil { return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err) } - - d.Set("index", d.Get("index").(*schema.Set).List()) } } diff --git a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go index 8aed43f70e2d..bba009775864 100644 --- a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go +++ b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go @@ -134,10 +134,10 @@ func TestAccAzureRMCosmosDbMongoCollection_withIndex(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testCheckAzureRMCosmosDbMongoCollectionExists(data.ResourceName), resource.TestCheckResourceAttr(data.ResourceName, "default_ttl_seconds", "707"), - resource.TestCheckResourceAttr(data.ResourceName, "index.#", "2"), + resource.TestCheckResourceAttr(data.ResourceName, "index.#", "3"), ), }, - data.ImportStep(), + data.ImportStep("index"), }, }) } @@ -281,6 +281,10 @@ resource "azurerm_cosmosdb_mongo_collection" "test" { keys = ["day"] unique = false } + + index { + keys = ["month"] + } } `, testAccAzureRMCosmosDbMongoDatabase_basic(data), data.RandomInteger) } From e6b747dbfe3f14305b4e5cb87e2339604a8f1f50 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 2 Apr 2020 15:32:03 +0800 Subject: [PATCH 03/14] Update code --- website/docs/r/cosmosdb_mongo_collection.html.markdown | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/website/docs/r/cosmosdb_mongo_collection.html.markdown b/website/docs/r/cosmosdb_mongo_collection.html.markdown index a24f69d3c3a6..a850828b6831 100644 --- a/website/docs/r/cosmosdb_mongo_collection.html.markdown +++ b/website/docs/r/cosmosdb_mongo_collection.html.markdown @@ -45,8 +45,17 @@ The following arguments are supported: * `database_name` - (Required) The name of the Cosmos DB Mongo Database in which the Cosmos DB Mongo Collection is created. Changing this forces a new resource to be created. * `default_ttl_seconds` - (Required) The default Time To Live in seconds. If the value is `0` items are not automatically expired. * `shard_key` - (Required) The name of the key to partition on for sharding. There must not be any other unique index keys. +* `index` - (Optional) One `index` block as defined below. * `throughput` - (Optional) The throughput of the MongoDB collection (RU/s). Must be set in increments of `100`. The minimum value is `400`. This must be set upon database creation otherwise it cannot be updated without a manual terraform destroy-apply. +--- + +The `index` block exports the following: + +* `keys` - (Required) Specifies the set of keys for each Cosmos DB Mongo Collection. + +* `unique` - (Optional) Is the index unique or not? Defaults to `true`. + ## Attributes Reference The following attributes are exported: From 06efb64e8e7f9ae8b727daf4f95ae159ab831126 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 9 Apr 2020 08:58:19 +0800 Subject: [PATCH 04/14] Update code --- website/docs/r/cosmosdb_mongo_collection.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/cosmosdb_mongo_collection.html.markdown b/website/docs/r/cosmosdb_mongo_collection.html.markdown index a850828b6831..d7ece42a2317 100644 --- a/website/docs/r/cosmosdb_mongo_collection.html.markdown +++ b/website/docs/r/cosmosdb_mongo_collection.html.markdown @@ -45,12 +45,12 @@ The following arguments are supported: * `database_name` - (Required) The name of the Cosmos DB Mongo Database in which the Cosmos DB Mongo Collection is created. Changing this forces a new resource to be created. * `default_ttl_seconds` - (Required) The default Time To Live in seconds. If the value is `0` items are not automatically expired. * `shard_key` - (Required) The name of the key to partition on for sharding. There must not be any other unique index keys. -* `index` - (Optional) One `index` block as defined below. +* `index` - (Optional) One or more `index` blocks as defined below. * `throughput` - (Optional) The throughput of the MongoDB collection (RU/s). Must be set in increments of `100`. The minimum value is `400`. This must be set upon database creation otherwise it cannot be updated without a manual terraform destroy-apply. --- -The `index` block exports the following: +The `index` block supports the following: * `keys` - (Required) Specifies the set of keys for each Cosmos DB Mongo Collection. From c201aee02e36e27c9dc1a401944238e445493cfe Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 9 Apr 2020 17:13:29 +0800 Subject: [PATCH 05/14] Update code --- .../services/cosmos/resource_arm_cosmosdb_mongo_collection.go | 3 +-- .../tests/resource_arm_cosmosdb_mongo_collection_test.go | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index 820a50e23841..f94e4267f04e 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -284,8 +284,7 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa } if props.Indexes != nil { - ttl := flattenCosmosMongoCollectionIndex(props.Indexes) - if err := d.Set("default_ttl_seconds", ttl); err != nil { + if err := d.Set("default_ttl_seconds", flattenCosmosMongoCollectionIndex(props.Indexes)); err != nil { return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err) } } diff --git a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go index bba009775864..1be6474d643f 100644 --- a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go +++ b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go @@ -137,6 +137,7 @@ func TestAccAzureRMCosmosDbMongoCollection_withIndex(t *testing.T) { resource.TestCheckResourceAttr(data.ResourceName, "index.#", "3"), ), }, + // Service API would return additional system indexes aside from user specified indexes. So index needs to be ignored. Otherwise, it would occur diff. data.ImportStep("index"), }, }) From 9dffb47af3b0e57b19f87023f76b7436cc330094 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Mon, 13 Apr 2020 12:01:52 +0800 Subject: [PATCH 06/14] Update code --- .../resource_arm_cosmosdb_mongo_collection.go | 40 ++++++++++++++++--- ...urce_arm_cosmosdb_mongo_collection_test.go | 3 +- .../r/cosmosdb_mongo_collection.html.markdown | 2 +- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index f94e4267f04e..52daa7b38697 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -97,7 +97,7 @@ func resourceArmCosmosDbMongoCollection() *schema.Resource { "unique": { Type: schema.TypeBool, Optional: true, - Default: true, + Default: false, }, }, }, @@ -284,9 +284,17 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa } if props.Indexes != nil { - if err := d.Set("default_ttl_seconds", flattenCosmosMongoCollectionIndex(props.Indexes)); err != nil { + + } + + if props.Indexes != nil { + indexes, ttl := flattenCosmosMongoCollectionIndex(props.Indexes) + if err := d.Set("default_ttl_seconds", ttl); err != nil { return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err) } + if err := d.Set("index", indexes); err != nil { + return fmt.Errorf("failed to set `index`: %+v", err) + } } } @@ -361,16 +369,36 @@ func expandCosmosMongoCollectionIndex(indexes []interface{}, defaultTtl *int) *[ return &results } -func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) *int32 { +func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[string]interface{}, *int32) { + indexes := make([]map[string]interface{}, 0) var ttl *int32 + for _, v := range *input { + index := map[string]interface{}{} + if v.Key != nil && v.Key.Keys != nil { key := (*v.Key.Keys)[0] - if key == "_ts" && v.Options != nil && v.Options.ExpireAfterSeconds != nil { - ttl = v.Options.ExpireAfterSeconds + + // Updating `DocumentDBDefaultIndex` system index is not a supported scenario. + // `_id` system index is always unique. + // As `DocumentDBDefaultIndex` and `_id` cannot be updated, so they should be ignored. + if key != "_id" && key != "DocumentDBDefaultIndex" { + if key == "_ts" && v.Options.ExpireAfterSeconds != nil { + ttl = v.Options.ExpireAfterSeconds + } else { + index["keys"] = utils.FlattenStringSlice(v.Key.Keys) + + if v.Options != nil { + if v.Options.Unique != nil { + index["unique"] = *v.Options.Unique + } + } + + indexes = append(indexes, index) + } } } } - return ttl + return &indexes, ttl } diff --git a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go index 1be6474d643f..7e153da8bdd2 100644 --- a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go +++ b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go @@ -137,8 +137,7 @@ func TestAccAzureRMCosmosDbMongoCollection_withIndex(t *testing.T) { resource.TestCheckResourceAttr(data.ResourceName, "index.#", "3"), ), }, - // Service API would return additional system indexes aside from user specified indexes. So index needs to be ignored. Otherwise, it would occur diff. - data.ImportStep("index"), + data.ImportStep(), }, }) } diff --git a/website/docs/r/cosmosdb_mongo_collection.html.markdown b/website/docs/r/cosmosdb_mongo_collection.html.markdown index d7ece42a2317..032bfb091a44 100644 --- a/website/docs/r/cosmosdb_mongo_collection.html.markdown +++ b/website/docs/r/cosmosdb_mongo_collection.html.markdown @@ -54,7 +54,7 @@ The `index` block supports the following: * `keys` - (Required) Specifies the set of keys for each Cosmos DB Mongo Collection. -* `unique` - (Optional) Is the index unique or not? Defaults to `true`. +* `unique` - (Optional) Is the index unique or not? Defaults to `false`. ## Attributes Reference From d77aa845a2eb2d84931cbf81159cf003084ea7a6 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Mon, 13 Apr 2020 12:46:30 +0800 Subject: [PATCH 07/14] Update code --- .../services/cosmos/resource_arm_cosmosdb_mongo_collection.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index 52daa7b38697..410cb693443a 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -283,10 +283,6 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa d.Set("shard_key", k) } - if props.Indexes != nil { - - } - if props.Indexes != nil { indexes, ttl := flattenCosmosMongoCollectionIndex(props.Indexes) if err := d.Set("default_ttl_seconds", ttl); err != nil { From a334dd0c24f470b4c8cfd77e8ef548bc31602f48 Mon Sep 17 00:00:00 2001 From: v-cheye Date: Tue, 14 Apr 2020 11:19:43 +0800 Subject: [PATCH 08/14] Update code --- .../resource_arm_cosmosdb_mongo_collection.go | 49 +++++++++++++++---- ...urce_arm_cosmosdb_mongo_collection_test.go | 1 + .../r/cosmosdb_mongo_collection.html.markdown | 11 ++++- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index 410cb693443a..d1c5dae28026 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -102,6 +102,25 @@ func resourceArmCosmosDbMongoCollection() *schema.Resource { }, }, }, + + "system_index": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "keys": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + + "unique": { + Type: schema.TypeBool, + Computed: true, + }, + }, + }, + }, }, } } @@ -284,13 +303,16 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa } if props.Indexes != nil { - indexes, ttl := flattenCosmosMongoCollectionIndex(props.Indexes) + indexes, systemIndexes, ttl := flattenCosmosMongoCollectionIndex(props.Indexes) if err := d.Set("default_ttl_seconds", ttl); err != nil { return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err) } if err := d.Set("index", indexes); err != nil { return fmt.Errorf("failed to set `index`: %+v", err) } + if err := d.Set("system_index", systemIndexes); err != nil { + return fmt.Errorf("failed to set `system_index`: %+v", err) + } } } @@ -365,29 +387,38 @@ func expandCosmosMongoCollectionIndex(indexes []interface{}, defaultTtl *int) *[ return &results } -func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[string]interface{}, *int32) { +func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[string]interface{}, *[]map[string]interface{}, *int32) { indexes := make([]map[string]interface{}, 0) + systemIndexes := make([]map[string]interface{}, 0) var ttl *int32 for _, v := range *input { index := map[string]interface{}{} + systemIndex := map[string]interface{}{} if v.Key != nil && v.Key.Keys != nil { key := (*v.Key.Keys)[0] // Updating `DocumentDBDefaultIndex` system index is not a supported scenario. // `_id` system index is always unique. - // As `DocumentDBDefaultIndex` and `_id` cannot be updated, so they should be ignored. - if key != "_id" && key != "DocumentDBDefaultIndex" { + // As `DocumentDBDefaultIndex` and `_id` cannot be updated, so they would be moved into `system_index`. + if key == "_id" || key == "DocumentDBDefaultIndex" { + systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys) + + if v.Options != nil && v.Options.Unique != nil { + systemIndex["unique"] = *v.Options.Unique + } + + systemIndexes = append(systemIndexes, systemIndex) + } else { + // As `ExpireAfterSeconds` only can be applied to system index `_ts`, so it would be set in `default_ttl_seconds`. if key == "_ts" && v.Options.ExpireAfterSeconds != nil { ttl = v.Options.ExpireAfterSeconds } else { index["keys"] = utils.FlattenStringSlice(v.Key.Keys) - if v.Options != nil { - if v.Options.Unique != nil { - index["unique"] = *v.Options.Unique - } + if v.Options != nil && v.Options.Unique != nil { + index["unique"] = *v.Options.Unique } indexes = append(indexes, index) @@ -396,5 +427,5 @@ func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[s } } - return &indexes, ttl + return &indexes, &systemIndexes, ttl } diff --git a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go index 7e153da8bdd2..39edf0097054 100644 --- a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go +++ b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go @@ -135,6 +135,7 @@ func TestAccAzureRMCosmosDbMongoCollection_withIndex(t *testing.T) { testCheckAzureRMCosmosDbMongoCollectionExists(data.ResourceName), resource.TestCheckResourceAttr(data.ResourceName, "default_ttl_seconds", "707"), resource.TestCheckResourceAttr(data.ResourceName, "index.#", "3"), + resource.TestCheckResourceAttr(data.ResourceName, "system_index.#", "2"), ), }, data.ImportStep(), diff --git a/website/docs/r/cosmosdb_mongo_collection.html.markdown b/website/docs/r/cosmosdb_mongo_collection.html.markdown index 032bfb091a44..1779aa518292 100644 --- a/website/docs/r/cosmosdb_mongo_collection.html.markdown +++ b/website/docs/r/cosmosdb_mongo_collection.html.markdown @@ -47,15 +47,24 @@ The following arguments are supported: * `shard_key` - (Required) The name of the key to partition on for sharding. There must not be any other unique index keys. * `index` - (Optional) One or more `index` blocks as defined below. * `throughput` - (Optional) The throughput of the MongoDB collection (RU/s). Must be set in increments of `100`. The minimum value is `400`. This must be set upon database creation otherwise it cannot be updated without a manual terraform destroy-apply. +* `system_index` - (Computed) One or more `system_index` blocks as defined below. --- The `index` block supports the following: -* `keys` - (Required) Specifies the set of keys for each Cosmos DB Mongo Collection. +* `keys` - (Required) Specifies the list of user settable keys for each Cosmos DB Mongo Collection. * `unique` - (Optional) Is the index unique or not? Defaults to `false`. +--- + +The `system_index` block supports the following: + +* `keys` - (Computed) The list of system keys which are not settable for each Cosmos DB Mongo Collection. + +* `unique` - (Computed) The unique status of index. + ## Attributes Reference The following attributes are exported: From 4b828a09be326392a2c82a3333b57ad403fd623d Mon Sep 17 00:00:00 2001 From: v-cheye Date: Tue, 14 Apr 2020 11:24:59 +0800 Subject: [PATCH 09/14] Update code --- .../r/cosmosdb_mongo_collection.html.markdown | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/website/docs/r/cosmosdb_mongo_collection.html.markdown b/website/docs/r/cosmosdb_mongo_collection.html.markdown index 1779aa518292..219ed12b4350 100644 --- a/website/docs/r/cosmosdb_mongo_collection.html.markdown +++ b/website/docs/r/cosmosdb_mongo_collection.html.markdown @@ -47,7 +47,6 @@ The following arguments are supported: * `shard_key` - (Required) The name of the key to partition on for sharding. There must not be any other unique index keys. * `index` - (Optional) One or more `index` blocks as defined below. * `throughput` - (Optional) The throughput of the MongoDB collection (RU/s). Must be set in increments of `100`. The minimum value is `400`. This must be set upon database creation otherwise it cannot be updated without a manual terraform destroy-apply. -* `system_index` - (Computed) One or more `system_index` blocks as defined below. --- @@ -57,19 +56,21 @@ The `index` block supports the following: * `unique` - (Optional) Is the index unique or not? Defaults to `false`. ---- +## Attributes Reference -The `system_index` block supports the following: +The following attributes are exported: -* `keys` - (Computed) The list of system keys which are not settable for each Cosmos DB Mongo Collection. +* `id` - The ID of the Cosmos DB Mongo Collection. -* `unique` - (Computed) The unique status of index. +* `system_index` - One or more `system_index` blocks as defined below. -## Attributes Reference +--- -The following attributes are exported: +The `system_index` block supports the following: -* `id` - The ID of the Cosmos DB Mongo Collection. +* `keys` - The list of system keys which are not settable for each Cosmos DB Mongo Collection. + +* `unique` - The unique status of the index. ## Timeouts From 7571180fdb3dcb243ed1d2c9e2999f9357b85390 Mon Sep 17 00:00:00 2001 From: v-cheye Date: Tue, 14 Apr 2020 13:19:05 +0800 Subject: [PATCH 10/14] Update code --- .../resource_arm_cosmosdb_mongo_collection.go | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index d1c5dae28026..ad8afadc0eaf 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -399,30 +399,32 @@ func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[s if v.Key != nil && v.Key.Keys != nil { key := (*v.Key.Keys)[0] - // Updating `DocumentDBDefaultIndex` system index is not a supported scenario. - // `_id` system index is always unique. // As `DocumentDBDefaultIndex` and `_id` cannot be updated, so they would be moved into `system_index`. - if key == "_id" || key == "DocumentDBDefaultIndex" { + if key == "_id" { systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys) + // The system index `_id` is always unique but api returns nil and it would be converted to `false` by zero-value. So it has to be manually set as `true`. + systemIndex["unique"] = true + systemIndexes = append(systemIndexes, systemIndex) + } else if key == "DocumentDBDefaultIndex" { + // Updating system index `DocumentDBDefaultIndex` is not a supported scenario. + systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys) if v.Options != nil && v.Options.Unique != nil { systemIndex["unique"] = *v.Options.Unique } systemIndexes = append(systemIndexes, systemIndex) - } else { + } else if key == "_ts" && v.Options.ExpireAfterSeconds != nil { // As `ExpireAfterSeconds` only can be applied to system index `_ts`, so it would be set in `default_ttl_seconds`. - if key == "_ts" && v.Options.ExpireAfterSeconds != nil { - ttl = v.Options.ExpireAfterSeconds - } else { - index["keys"] = utils.FlattenStringSlice(v.Key.Keys) - - if v.Options != nil && v.Options.Unique != nil { - index["unique"] = *v.Options.Unique - } - - indexes = append(indexes, index) + ttl = v.Options.ExpireAfterSeconds + } else { + // The other settable indexes would be set in `index` + index["keys"] = utils.FlattenStringSlice(v.Key.Keys) + if v.Options != nil && v.Options.Unique != nil { + index["unique"] = *v.Options.Unique } + + indexes = append(indexes, index) } } } From d41bafff6543400c6604c81fb3be52d8883ebbf4 Mon Sep 17 00:00:00 2001 From: v-cheye Date: Tue, 14 Apr 2020 15:33:02 +0800 Subject: [PATCH 11/14] Update code --- .../resource_arm_cosmosdb_mongo_collection.go | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index ad8afadc0eaf..2470751f2da8 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -399,30 +399,39 @@ func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[s if v.Key != nil && v.Key.Keys != nil { key := (*v.Key.Keys)[0] + switch key { // As `DocumentDBDefaultIndex` and `_id` cannot be updated, so they would be moved into `system_index`. - if key == "_id" { + case "_id": systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys) // The system index `_id` is always unique but api returns nil and it would be converted to `false` by zero-value. So it has to be manually set as `true`. systemIndex["unique"] = true systemIndexes = append(systemIndexes, systemIndex) - } else if key == "DocumentDBDefaultIndex" { + case "DocumentDBDefaultIndex": // Updating system index `DocumentDBDefaultIndex` is not a supported scenario. systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys) + + isUnique := false if v.Options != nil && v.Options.Unique != nil { - systemIndex["unique"] = *v.Options.Unique + isUnique = *v.Options.Unique } + systemIndex["unique"] = isUnique systemIndexes = append(systemIndexes, systemIndex) - } else if key == "_ts" && v.Options.ExpireAfterSeconds != nil { - // As `ExpireAfterSeconds` only can be applied to system index `_ts`, so it would be set in `default_ttl_seconds`. - ttl = v.Options.ExpireAfterSeconds - } else { + case "_ts": + if v.Options != nil && v.Options.ExpireAfterSeconds != nil { + // As `ExpireAfterSeconds` only can be applied to system index `_ts`, so it would be set in `default_ttl_seconds`. + ttl = v.Options.ExpireAfterSeconds + } + default: // The other settable indexes would be set in `index` index["keys"] = utils.FlattenStringSlice(v.Key.Keys) + + isUnique := false if v.Options != nil && v.Options.Unique != nil { - index["unique"] = *v.Options.Unique + isUnique = *v.Options.Unique } + index["unique"] = isUnique indexes = append(indexes, index) } From cefe6af8789b1c2d53481ed167bc0f095d05ba3f Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 15 Apr 2020 15:29:38 +0800 Subject: [PATCH 12/14] Update code --- .../cosmos/resource_arm_cosmosdb_mongo_collection.go | 8 ++++---- .../tests/resource_arm_cosmosdb_mongo_collection_test.go | 2 +- website/docs/r/cosmosdb_mongo_collection.html.markdown | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index 2470751f2da8..939568fa17b1 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -103,7 +103,7 @@ func resourceArmCosmosDbMongoCollection() *schema.Resource { }, }, - "system_index": { + "system_indexes": { Type: schema.TypeList, Computed: true, Elem: &schema.Resource{ @@ -310,8 +310,8 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa if err := d.Set("index", indexes); err != nil { return fmt.Errorf("failed to set `index`: %+v", err) } - if err := d.Set("system_index", systemIndexes); err != nil { - return fmt.Errorf("failed to set `system_index`: %+v", err) + if err := d.Set("system_indexes", systemIndexes); err != nil { + return fmt.Errorf("failed to set `system_indexes`: %+v", err) } } } @@ -400,7 +400,7 @@ func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[s key := (*v.Key.Keys)[0] switch key { - // As `DocumentDBDefaultIndex` and `_id` cannot be updated, so they would be moved into `system_index`. + // As `DocumentDBDefaultIndex` and `_id` cannot be updated, so they would be moved into `system_indexes`. case "_id": systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys) // The system index `_id` is always unique but api returns nil and it would be converted to `false` by zero-value. So it has to be manually set as `true`. diff --git a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go index 39edf0097054..8605cc6aa0ec 100644 --- a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go +++ b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_mongo_collection_test.go @@ -135,7 +135,7 @@ func TestAccAzureRMCosmosDbMongoCollection_withIndex(t *testing.T) { testCheckAzureRMCosmosDbMongoCollectionExists(data.ResourceName), resource.TestCheckResourceAttr(data.ResourceName, "default_ttl_seconds", "707"), resource.TestCheckResourceAttr(data.ResourceName, "index.#", "3"), - resource.TestCheckResourceAttr(data.ResourceName, "system_index.#", "2"), + resource.TestCheckResourceAttr(data.ResourceName, "system_indexes.#", "2"), ), }, data.ImportStep(), diff --git a/website/docs/r/cosmosdb_mongo_collection.html.markdown b/website/docs/r/cosmosdb_mongo_collection.html.markdown index 219ed12b4350..3fdd56aadbd5 100644 --- a/website/docs/r/cosmosdb_mongo_collection.html.markdown +++ b/website/docs/r/cosmosdb_mongo_collection.html.markdown @@ -62,11 +62,11 @@ The following attributes are exported: * `id` - The ID of the Cosmos DB Mongo Collection. -* `system_index` - One or more `system_index` blocks as defined below. +* `system_indexes` - One or more `system_indexes` blocks as defined below. --- -The `system_index` block supports the following: +The `system_indexes` block supports the following: * `keys` - The list of system keys which are not settable for each Cosmos DB Mongo Collection. From cc4d2dde7bb7f11c943dfb94a7972a66352d2cc6 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Fri, 17 Apr 2020 10:04:25 +0800 Subject: [PATCH 13/14] Update code --- .../resource_arm_cosmosdb_mongo_collection.go | 23 ++++++++++--------- .../r/cosmosdb_mongo_collection.html.markdown | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index 939568fa17b1..d93a39a5dd0f 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -302,17 +302,15 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa d.Set("shard_key", k) } - if props.Indexes != nil { - indexes, systemIndexes, ttl := flattenCosmosMongoCollectionIndex(props.Indexes) - if err := d.Set("default_ttl_seconds", ttl); err != nil { - return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err) - } - if err := d.Set("index", indexes); err != nil { - return fmt.Errorf("failed to set `index`: %+v", err) - } - if err := d.Set("system_indexes", systemIndexes); err != nil { - return fmt.Errorf("failed to set `system_indexes`: %+v", err) - } + indexes, systemIndexes, ttl := flattenCosmosMongoCollectionIndex(props.Indexes) + if err := d.Set("default_ttl_seconds", ttl); err != nil { + return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err) + } + if err := d.Set("index", indexes); err != nil { + return fmt.Errorf("failed to set `index`: %+v", err) + } + if err := d.Set("system_indexes", systemIndexes); err != nil { + return fmt.Errorf("failed to set `system_indexes`: %+v", err) } } @@ -391,6 +389,9 @@ func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[s indexes := make([]map[string]interface{}, 0) systemIndexes := make([]map[string]interface{}, 0) var ttl *int32 + if input == nil { + return &indexes, &systemIndexes, ttl + } for _, v := range *input { index := map[string]interface{}{} diff --git a/website/docs/r/cosmosdb_mongo_collection.html.markdown b/website/docs/r/cosmosdb_mongo_collection.html.markdown index 3fdd56aadbd5..e71cdf464fbe 100644 --- a/website/docs/r/cosmosdb_mongo_collection.html.markdown +++ b/website/docs/r/cosmosdb_mongo_collection.html.markdown @@ -70,7 +70,7 @@ The `system_indexes` block supports the following: * `keys` - The list of system keys which are not settable for each Cosmos DB Mongo Collection. -* `unique` - The unique status of the index. +* `unique` - Identifies whether the table contains no duplicate values. ## Timeouts From 2170312a36a2f1d4c3850d650bcf1c43e6550ffa Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Fri, 17 Apr 2020 10:13:19 +0800 Subject: [PATCH 14/14] Update code --- .../services/cosmos/resource_arm_cosmosdb_mongo_collection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go index d93a39a5dd0f..6ff840c1edae 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_mongo_collection.go @@ -397,7 +397,7 @@ func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[s index := map[string]interface{}{} systemIndex := map[string]interface{}{} - if v.Key != nil && v.Key.Keys != nil { + if v.Key != nil && v.Key.Keys != nil && len(*v.Key.Keys) > 0 { key := (*v.Key.Keys)[0] switch key {