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_mongo_collection: Fix the issue that expireAfterSeconds property could not be set to -1 #15736

Merged
merged 1 commit into from
Mar 17, 2022
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
17 changes: 10 additions & 7 deletions internal/services/cosmos/cosmosdb_mongo_collection_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion website/docs/r/cosmosdb_mongo_collection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down