Skip to content

Commit

Permalink
azurerm_cosmosdb_sql_container - support for default_ttl (#5492)
Browse files Browse the repository at this point in the history
Fixes #5451

Adds the default_ttl argument for the azurerm_cosmosdb_sql_container resource.

Also, fixes a bug causing resource updates to fail since id.Container was being passed in instead of id.ResourceGroup for one of the function arguments.
  • Loading branch information
aqche authored and katbyte committed Jan 25, 2020
1 parent 7ed463e commit 8e3b507
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
Expand Down Expand Up @@ -74,6 +75,13 @@ func resourceArmCosmosDbSQLContainer() *schema.Resource {
ValidateFunc: validate.CosmosThroughput,
},

"default_ttl": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntAtLeast(-1),
},

"unique_key": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -145,6 +153,10 @@ func resourceArmCosmosDbSQLContainerCreate(d *schema.ResourceData, meta interfac
}
}

if defaultTTL, hasTTL := d.GetOk("default_ttl"); hasTTL {
db.SQLContainerCreateUpdateProperties.Resource.DefaultTTL = utils.Int32(int32(defaultTTL.(int)))
}

if throughput, hasThroughput := d.GetOk("throughput"); hasThroughput {
db.SQLContainerCreateUpdateProperties.Options = map[string]*string{
"throughput": utils.String(strconv.Itoa(throughput.(int))),
Expand Down Expand Up @@ -208,7 +220,11 @@ func resourceArmCosmosDbSQLContainerUpdate(d *schema.ResourceData, meta interfac
}
}

future, err := client.CreateUpdateSQLContainer(ctx, id.Container, id.Account, id.Database, id.Container, db)
if defaultTTL, hasTTL := d.GetOk("default_ttl"); hasTTL {
db.SQLContainerCreateUpdateProperties.Resource.DefaultTTL = utils.Int32(int32(defaultTTL.(int)))
}

future, err := client.CreateUpdateSQLContainer(ctx, id.ResourceGroup, id.Account, id.Database, id.Container, db)
if err != nil {
return fmt.Errorf("Error issuing create/update request for Cosmos SQL Container %s (Account: %s, Database:%s): %+v", id.Container, id.Account, id.Database, err)
}
Expand Down Expand Up @@ -284,6 +300,10 @@ func resourceArmCosmosDbSQLContainerRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error setting `unique_key`: %+v", err)
}
}

if defaultTTL := props.DefaultTTL; defaultTTL != nil {
d.Set("default_ttl", defaultTTL)
}
}

throughputResp, err := client.GetSQLContainerThroughput(ctx, id.ResourceGroup, id.Account, id.Database, id.Container)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestAccAzureRMCosmosDbSqlContainer_update(t *testing.T) {
Config: testAccAzureRMCosmosDbSqlContainer_complete(data),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbSqlContainerExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "default_ttl", "500"),
resource.TestCheckResourceAttr(data.ResourceName, "throughput", "600"),
),
},
Expand All @@ -74,6 +75,7 @@ func TestAccAzureRMCosmosDbSqlContainer_update(t *testing.T) {
Config: testAccAzureRMCosmosDbSqlContainer_update(data),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbSqlContainerExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "default_ttl", "1000"),
resource.TestCheckResourceAttr(data.ResourceName, "throughput", "400"),
),
},
Expand Down Expand Up @@ -127,7 +129,7 @@ func testCheckAzureRMCosmosDbSqlContainerExists(resourceName string) resource.Te
resourceGroup := rs.Primary.Attributes["resource_group_name"]
database := rs.Primary.Attributes["database_name"]

resp, err := client.GetSQLContainer(ctx, resourceGroup, database, account, name)
resp, err := client.GetSQLContainer(ctx, resourceGroup, account, database, name)
if err != nil {
return fmt.Errorf("Bad: Get on cosmosAccountsClient: %+v", err)
}
Expand Down Expand Up @@ -168,7 +170,8 @@ resource "azurerm_cosmosdb_sql_container" "test" {
unique_key {
paths = ["/definition/id1", "/definition/id2"]
}
throughput = 600
default_ttl = 500
throughput = 600
}
`, testAccAzureRMCosmosDbSqlDatabase_basic(data), data.RandomInteger)
Expand All @@ -187,7 +190,8 @@ resource "azurerm_cosmosdb_sql_container" "test" {
unique_key {
paths = ["/definition/id1", "/definition/id2"]
}
throughput = 400
default_ttl = 1000
throughput = 400
}
`, testAccAzureRMCosmosDbSqlDatabase_basic(data), data.RandomInteger)
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cosmosdb_sql_container.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ The following arguments are supported:

* `throughput` - (Optional) The throughput of SQL container (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.

* `default_ttl` - (Optional) The default time to live of SQL container. If missing, items are not expired automatically. 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.

---
A `unique_key` block supports the following:

Expand Down

0 comments on commit 8e3b507

Please sign in to comment.