From 2a91761350eda1eb3ee9175e0a5417b00fa71273 Mon Sep 17 00:00:00 2001 From: Austin Cheung Date: Thu, 23 Jan 2020 00:18:01 -0800 Subject: [PATCH 1/3] add default_ttl arg to azurerm_cosmosdb_sql_container --- .../resource_arm_cosmosdb_sql_container.go | 22 ++++++++++++++++++- ...esource_arm_cosmosdb_sql_container_test.go | 8 +++++-- .../r/cosmosdb_sql_container.html.markdown | 2 ++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_sql_container.go b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_sql_container.go index 1f8db7e30414..cf3fc51a6102 100644 --- a/azurerm/internal/services/cosmos/resource_arm_cosmosdb_sql_container.go +++ b/azurerm/internal/services/cosmos/resource_arm_cosmosdb_sql_container.go @@ -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" @@ -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, @@ -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))), @@ -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) } @@ -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) diff --git a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_sql_container_test.go b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_sql_container_test.go index 90c8689003ba..2acb54cbdb82 100644 --- a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_sql_container_test.go +++ b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_sql_container_test.go @@ -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"), ), }, @@ -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"), ), }, @@ -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) @@ -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) diff --git a/website/docs/r/cosmosdb_sql_container.html.markdown b/website/docs/r/cosmosdb_sql_container.html.markdown index f67169ec7d23..0f27f7fa7b06 100644 --- a/website/docs/r/cosmosdb_sql_container.html.markdown +++ b/website/docs/r/cosmosdb_sql_container.html.markdown @@ -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: From 093c55a93c85c45b0b5fca863abd54eebf3095e7 Mon Sep 17 00:00:00 2001 From: kt Date: Thu, 23 Jan 2020 08:13:24 -0800 Subject: [PATCH 2/3] Update cosmosdb_sql_container.html.markdown --- website/docs/r/cosmosdb_sql_container.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/cosmosdb_sql_container.html.markdown b/website/docs/r/cosmosdb_sql_container.html.markdown index 0f27f7fa7b06..81b73bfb332a 100644 --- a/website/docs/r/cosmosdb_sql_container.html.markdown +++ b/website/docs/r/cosmosdb_sql_container.html.markdown @@ -47,7 +47,7 @@ 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. +* `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: From 2373c1a0e87d838fa29c064cf9914bec2aaa9390 Mon Sep 17 00:00:00 2001 From: Austin Cheung Date: Fri, 24 Jan 2020 15:19:02 -0800 Subject: [PATCH 3/3] fix GetSQLContainer inputs --- .../cosmos/tests/resource_arm_cosmosdb_sql_container_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_sql_container_test.go b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_sql_container_test.go index 2acb54cbdb82..e2c796a3072b 100644 --- a/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_sql_container_test.go +++ b/azurerm/internal/services/cosmos/tests/resource_arm_cosmosdb_sql_container_test.go @@ -129,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) }