diff --git a/internal/services/cosmos/cosmosdb_mongo_collection_resource.go b/internal/services/cosmos/cosmosdb_mongo_collection_resource.go index 850ba1700b85..e3c2f95ad3c7 100644 --- a/internal/services/cosmos/cosmosdb_mongo_collection_resource.go +++ b/internal/services/cosmos/cosmosdb_mongo_collection_resource.go @@ -79,9 +79,12 @@ func resourceCosmosDbMongoCollection() *pluginsdk.Resource { // default TTL is simply an index on _ts with expireAfterOption, given we can't seem to set TTLs on a given index lets expose this to match the portal "default_ttl_seconds": { - Type: pluginsdk.TypeInt, - Optional: true, - ValidateFunc: validation.IntAtLeast(-1), + Type: pluginsdk.TypeInt, + Optional: true, + ValidateFunc: validation.All( + validation.IntAtLeast(-1), + validation.IntNotInSlice([]int{0}), + ), }, "analytical_storage_ttl": { @@ -163,8 +166,8 @@ func resourceCosmosDbMongoCollectionCreate(d *pluginsdk.ResourceData, meta inter } var ttl *int - if v := d.Get("default_ttl_seconds").(int); v > 0 { - ttl = utils.Int(v) + if v, ok := d.GetOk("default_ttl_seconds"); ok { + ttl = utils.Int(v.(int)) } indexes, hasIdKey := expandCosmosMongoCollectionIndex(d.Get("index").(*pluginsdk.Set).List(), ttl) @@ -232,8 +235,8 @@ func resourceCosmosDbMongoCollectionUpdate(d *pluginsdk.ResourceData, meta inter } var ttl *int - if v := d.Get("default_ttl_seconds").(int); v > 0 { - ttl = utils.Int(v) + if v, ok := d.GetOk("default_ttl_seconds"); ok { + ttl = utils.Int(v.(int)) } indexes, hasIdKey := expandCosmosMongoCollectionIndex(d.Get("index").(*pluginsdk.Set).List(), ttl) diff --git a/internal/services/cosmos/cosmosdb_mongo_collection_resource_test.go b/internal/services/cosmos/cosmosdb_mongo_collection_resource_test.go index 76ee7b9cc12c..e2d9a1bdd9f8 100644 --- a/internal/services/cosmos/cosmosdb_mongo_collection_resource_test.go +++ b/internal/services/cosmos/cosmosdb_mongo_collection_resource_test.go @@ -49,6 +49,21 @@ func TestAccCosmosDbMongoCollection_complete(t *testing.T) { }) } +func TestAccCosmosDbMongoCollection_neverExpires(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_mongo_collection", "test") + r := CosmosMongoCollectionResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.neverExpires(data), + Check: acceptance.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func TestAccCosmosDbMongoCollection_update(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_cosmosdb_mongo_collection", "test") r := CosmosMongoCollectionResource{} @@ -463,3 +478,23 @@ resource "azurerm_cosmosdb_mongo_collection" "test" { } `, CosmosMongoDatabaseResource{}.basic(data), data.RandomInteger) } + +func (CosmosMongoCollectionResource) neverExpires(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 + + index { + keys = ["_id"] + unique = true + } + + default_ttl_seconds = -1 +} +`, CosmosMongoDatabaseResource{}.basic(data), data.RandomInteger) +} diff --git a/website/docs/r/cosmosdb_mongo_collection.html.markdown b/website/docs/r/cosmosdb_mongo_collection.html.markdown index 725c4b9f1016..35b2299b7243 100644 --- a/website/docs/r/cosmosdb_mongo_collection.html.markdown +++ b/website/docs/r/cosmosdb_mongo_collection.html.markdown @@ -48,9 +48,9 @@ The following arguments are supported: * `name` - (Required) Specifies the name of the Cosmos DB Mongo Collection. Changing this forces a new resource to be created. * `resource_group_name` - (Required) The name of the resource group in which the Cosmos DB Mongo Collection is created. Changing this forces a new resource to be created. * `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 `-1` or `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. * `analytical_storage_ttl` - (Optional) The default time to live of Analytical Storage for this Mongo Collection. If present and the value is set to `-1`, it is equal to infinity, and items don’t expire by default. If present and the value is set to some number `n` – items will expire `n` seconds after their last modified time. +* `default_ttl_seconds` - (Optional) The default Time To Live in seconds. If the value is `-1`, items are not automatically expired. * # `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. * `autoscale_settings` - (Optional) An `autoscale_settings` block as defined below. This must be set upon database creation otherwise it cannot be updated without a manual terraform destroy-apply.