From d1de789e57d44a8591e1eab64ff4ec7a9dfea9fd Mon Sep 17 00:00:00 2001 From: Yuping Wei <56525716+yupwei68@users.noreply.github.com> Date: Wed, 19 May 2021 05:51:08 +0800 Subject: [PATCH] Support for `analytical_storage_ttl` in `azurerm_cosmosdb_cassandra_table` (#11755) Fix #11754 --- .../cosmosdb_cassandra_table_resource.go | 20 ++++- .../cosmosdb_cassandra_table_resource_test.go | 85 +++++++++++++++++++ .../r/cosmosdb_cassandra_table.html.markdown | 4 + 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_cassandra_table_resource.go b/azurerm/internal/services/cosmos/cosmosdb_cassandra_table_resource.go index 92b61d01ef5aa..23982be700820 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_cassandra_table_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_cassandra_table_resource.go @@ -59,6 +59,14 @@ func resourceCosmosDbCassandraTable() *schema.Resource { ValidateFunc: validation.IntAtLeast(-1), }, + "analytical_storage_ttl": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: -2, + ValidateFunc: validation.IntAtLeast(-1), + }, + "schema": common.CassandraTableSchemaPropertySchema(), "throughput": { @@ -115,6 +123,10 @@ func resourceCosmosDbCassandraTableCreate(d *schema.ResourceData, meta interface table.CassandraTableCreateUpdateProperties.Resource.DefaultTTL = utils.Int32(int32(defaultTTL.(int))) } + if analyticalTTL := d.Get("analytical_storage_ttl").(int); analyticalTTL != -2 { + table.CassandraTableCreateUpdateProperties.Resource.AnalyticalStorageTTL = utils.Int32(int32(analyticalTTL)) + } + if throughput, hasThroughput := d.GetOk("throughput"); hasThroughput { if throughput != 0 { table.CassandraTableCreateUpdateProperties.Options.Throughput = common.ConvertThroughputFromResourceData(throughput) @@ -184,7 +196,7 @@ func resourceCosmosDbCassandraTableUpdate(d *schema.ResourceData, meta interface if err != nil { if response.WasNotFound(throughputFuture.Response()) { return fmt.Errorf("setting Throughput for %s: %+v - "+ - "If the collection has not been created with an initial throughput, you cannot configure it later.", *id, err) + "If the collection has not been created with an initial throughput, you cannot configure it later", *id, err) } } @@ -229,6 +241,12 @@ func resourceCosmosDbCassandraTableRead(d *schema.ResourceData, meta interface{} d.Set("default_ttl", defaultTTL) } + analyticalTTL := -2 + if res.AnalyticalStorageTTL != nil { + analyticalTTL = int(*res.AnalyticalStorageTTL) + } + d.Set("analytical_storage_ttl", analyticalTTL) + if schema := res.Schema; schema != nil { d.Set("schema", flattenTableSchema(schema)) } diff --git a/azurerm/internal/services/cosmos/cosmosdb_cassandra_table_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_cassandra_table_resource_test.go index 663d5579a471d..d206da66dd33f 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_cassandra_table_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_cassandra_table_resource_test.go @@ -47,6 +47,22 @@ func TestAccCosmosDbCassandraTable_basic(t *testing.T) { }) } +func TestAccCosmosDbCassandraTable_analyticalStorageTTL(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_table", "test") + r := CosmosDBCassandraTableResource{} + + data.ResourceTest(t, r, []resource.TestStep{ + { + + Config: r.analyticalStorageTTL(data), + Check: resource.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (CosmosDBCassandraTableResource) basic(data acceptance.TestData) string { return fmt.Sprintf(` %[1]s @@ -73,3 +89,72 @@ resource "azurerm_cosmosdb_cassandra_table" "test" { } `, CosmosDbCassandraKeyspaceResource{}.basic(data), data.RandomInteger) } + +func (CosmosDBCassandraTableResource) analyticalStorageTTLTemplate(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-cosmos-%[1]d" + location = "%[2]s" +} + +resource "azurerm_cosmosdb_account" "test" { + name = "acctest-ca-%[1]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + offer_type = "Standard" + kind = "GlobalDocumentDB" + analytical_storage_enabled = true + + consistency_policy { + consistency_level = "Strong" + } + + capabilities { + name = "EnableCassandra" + } + + geo_location { + location = azurerm_resource_group.test.location + failover_priority = 0 + } +} + +resource "azurerm_cosmosdb_cassandra_keyspace" "test" { + name = "acctest-%[1]d" + resource_group_name = azurerm_cosmosdb_account.test.resource_group_name + account_name = azurerm_cosmosdb_account.test.name +} +`, data.RandomInteger, data.Locations.Primary) +} + +func (r CosmosDBCassandraTableResource) analyticalStorageTTL(data acceptance.TestData) string { + return fmt.Sprintf(` +%[1]s + +resource "azurerm_cosmosdb_cassandra_table" "test" { + name = "acctest-CCASST-%[2]d" + cassandra_keyspace_id = azurerm_cosmosdb_cassandra_keyspace.test.id + analytical_storage_ttl = 0 + + schema { + column { + name = "test1" + type = "ascii" + } + + column { + name = "test2" + type = "int" + } + + partition_key { + name = "test1" + } + } +} +`, r.analyticalStorageTTLTemplate(data), data.RandomInteger) +} diff --git a/website/docs/r/cosmosdb_cassandra_table.html.markdown b/website/docs/r/cosmosdb_cassandra_table.html.markdown index 9c12adb5ae50d..520096c055790 100644 --- a/website/docs/r/cosmosdb_cassandra_table.html.markdown +++ b/website/docs/r/cosmosdb_cassandra_table.html.markdown @@ -78,6 +78,10 @@ The following arguments are supported: * `throughput` - (Optional) The throughput of Cassandra KeySpace (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) Time to live of the Cosmos DB Cassandra table. Possible values are at least `-1`. `-1` means the Cassandra table never expires. + +* `analytical_storage_ttl` - (Optional) Time to live of the Analytical Storage. Possible values are at least `-1`. `-1` means the Analytical Storage never expires. Changing this forces a new resource to be created. + ~> **Note:** throughput has a maximum value of `1000000` unless a higher limit is requested via Azure Support * `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.