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_gremlin_graph - support new property analytical_storage_ttl #22179

Merged
merged 11 commits into from
Jun 16, 2023
29 changes: 29 additions & 0 deletions internal/services/cosmos/cosmosdb_gremlin_graph_resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cosmos

import (
"context"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -69,6 +70,15 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource {
ValidateFunc: validate.CosmosEntityName,
},

"analytical_storage_ttl": {
Type: pluginsdk.TypeInt,
Optional: true,
ValidateFunc: validation.All(
validation.IntBetween(-1, 2147483647),
validation.IntNotInSlice([]int{0}),
),
},

"default_ttl": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand Down Expand Up @@ -172,6 +182,13 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource {
},
},
},

CustomizeDiff: pluginsdk.CustomDiffWithAll(
// `analytical_storage_ttl` can't be disabled once it's enabled
pluginsdk.ForceNewIfChange("analytical_storage_ttl", func(ctx context.Context, old, new, _ interface{}) bool {
return (old.(int) == -1 || (old.(int) >= 1 && old.(int) <= 2147483647)) && new.(int) == 0
}),
),
}
}

Expand Down Expand Up @@ -205,6 +222,10 @@ func resourceCosmosDbGremlinGraphCreate(d *pluginsdk.ResourceData, meta interfac
},
}

if v, ok := d.GetOk("analytical_storage_ttl"); ok {
db.Properties.Resource.AnalyticalStorageTtl = utils.Int64(int64(v.(int)))
}

if partitionkeypaths != "" {
partitionKindHash := cosmosdb.PartitionKindHash
db.Properties.Resource.PartitionKey = &cosmosdb.ContainerPartitionKey{
Expand Down Expand Up @@ -293,6 +314,10 @@ func resourceCosmosDbGremlinGraphUpdate(d *pluginsdk.ResourceData, meta interfac
}
}

if v, ok := d.GetOk("analytical_storage_ttl"); ok {
db.Properties.Resource.AnalyticalStorageTtl = utils.Int64(int64(v.(int)))
}

if defaultTTL, hasDefaultTTL := d.GetOk("default_ttl"); hasDefaultTTL {
db.Properties.Resource.DefaultTtl = utils.Int64(int64(defaultTTL.(int)))
}
Expand Down Expand Up @@ -382,6 +407,10 @@ func resourceCosmosDbGremlinGraphRead(d *pluginsdk.ResourceData, meta interface{
}
}

if v := props.AnalyticalStorageTtl; v != nil {
d.Set("analytical_storage_ttl", v)
}

if defaultTTL := props.DefaultTtl; defaultTTL != nil {
d.Set("default_ttl", defaultTTL)
}
Expand Down
73 changes: 73 additions & 0 deletions internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,28 @@ func TestAccCosmosDbGremlinGraph_serverless(t *testing.T) {
})
}

func TestAccCosmosDbGremlinGraph_analyticalStorageTtl(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_gremlin_graph", "test")
r := CosmosGremlinGraphResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.analyticalStorageTtl(data, -1),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.analyticalStorageTtl(data, 2),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t CosmosGremlinGraphResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := cosmosdb.ParseGraphID(state.ID)
if err != nil {
Expand Down Expand Up @@ -465,3 +487,54 @@ resource "azurerm_cosmosdb_gremlin_graph" "test" {
}
`, CosmosGremlinDatabaseResource{}.serverless(data), data.RandomInteger)
}

func (CosmosGremlinGraphResource) analyticalStorageTtl(data acceptance.TestData, analyticalStorageTtl int64) 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 = "EnableGremlin"
}

geo_location {
location = azurerm_resource_group.test.location
failover_priority = 0
}
}

resource "azurerm_cosmosdb_gremlin_database" "test" {
name = "acctest-db-%[1]d"
resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
account_name = azurerm_cosmosdb_account.test.name
}

resource "azurerm_cosmosdb_gremlin_graph" "test" {
name = "acctest-CGRPC-%[1]d"
resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
account_name = azurerm_cosmosdb_account.test.name
database_name = azurerm_cosmosdb_gremlin_database.test.name
partition_key_path = "/test"
throughput = 400
analytical_storage_ttl = %[3]d
}
`, data.RandomInteger, data.Locations.Primary, analyticalStorageTtl)
}
4 changes: 4 additions & 0 deletions website/docs/r/cosmosdb_gremlin_graph.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ The following arguments are supported:

* `throughput` - (Optional) The throughput of the Gremlin graph (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.

* `analytical_storage_ttl` - (Optional) The time to live of Analytical Storage for this Cosmos DB Gremlin Graph. Possible values are between `-1` to `2147483647` not including `0`. If present and the value is set to `-1`, it means never expire.

~> **Note:** Disabling `analytical_storage_ttl` will force a new resource to be created since it can't be disabled once it's enabled.

* `default_ttl` - (Optional) The default time to live (TTL) of the Gremlin graph. If the value is missing or set to "-1", items don’t expire.

* `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. Requires `partition_key_path` to be set.
Expand Down