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_sql_container - support for default_ttl #5492

Merged
merged 4 commits into from
Jan 25, 2020
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
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out the reason the tests were failing was because the inputs were incorrect. it passed before b/c until recently database and account used the same naming format.

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