From 0dd5eb50c2ebe168a9667f429952f84741bfe266 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Tue, 11 May 2021 10:47:49 +0800 Subject: [PATCH 1/4] update --- .../services/cosmos/common/indexing_policy.go | 8 +- .../internal/services/cosmos/common/schema.go | 74 ++++++------ .../cosmos/cosmosdb_gremlin_graph_resource.go | 29 ++++- .../cosmosdb_gremlin_graph_resource_test.go | 107 +++++++++++++++--- .../r/cosmosdb_gremlin_graph.html.markdown | 23 ++++ 5 files changed, 186 insertions(+), 55 deletions(-) diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index e9ee909a6de7..7f3b065cb180 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -45,7 +45,7 @@ func expandAzureRmCosmosDBIndexingPolicyExcludedPaths(input []interface{}) *[]do return &paths } -func expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[][]documentdb.CompositePath { +func ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[][]documentdb.CompositePath { indexes := make([][]documentdb.CompositePath, 0) for _, i := range input { @@ -83,7 +83,7 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.Ind } if v, ok := input["composite_index"].([]interface{}); ok { - policy.CompositeIndexes = expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) + policy.CompositeIndexes = ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) } return policy } @@ -131,7 +131,7 @@ func flattenCosmosDBIndexingPolicyCompositeIndex(input []documentdb.CompositePat return indexPairs } -func flattenCosmosDBIndexingPolicyCompositeIndexes(input *[][]documentdb.CompositePath) []interface{} { +func FlattenCosmosDBIndexingPolicyCompositeIndexes(input *[][]documentdb.CompositePath) []interface{} { if input == nil { return []interface{}{} } @@ -173,7 +173,7 @@ func FlattenAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPol result["indexing_mode"] = strings.Title(string(indexingPolicy.IndexingMode)) result["included_path"] = flattenCosmosDBIndexingPolicyIncludedPaths(indexingPolicy.IncludedPaths) result["excluded_path"] = flattenCosmosDBIndexingPolicyExcludedPaths(indexingPolicy.ExcludedPaths) - result["composite_index"] = flattenCosmosDBIndexingPolicyCompositeIndexes(indexingPolicy.CompositeIndexes) + result["composite_index"] = FlattenCosmosDBIndexingPolicyCompositeIndexes(indexingPolicy.CompositeIndexes) results = append(results, result) return results diff --git a/azurerm/internal/services/cosmos/common/schema.go b/azurerm/internal/services/cosmos/common/schema.go index 65a873fb2cf4..ea319416ab87 100644 --- a/azurerm/internal/services/cosmos/common/schema.go +++ b/azurerm/internal/services/cosmos/common/schema.go @@ -150,41 +150,7 @@ func CosmosDbIndexingPolicySchema() *schema.Schema { }, }, }, - "composite_index": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "index": { - Type: schema.TypeList, - MinItems: 1, - Required: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "path": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringIsNotEmpty, - }, - // `order` case changes from 2020-04-01 to 2021-01-15, issue opened:https://github.com/Azure/azure-rest-api-specs/issues/14051 - // todo: change to SDK constants and remove translation code in 3.0 - "order": { - Type: schema.TypeString, - Required: true, - // Workaround for Azure/azure-rest-api-specs#11222 - DiffSuppressFunc: suppress.CaseDifference, - ValidateFunc: validation.StringInSlice( - []string{ - "Ascending", - "Descending", - }, false), - }, - }, - }, - }, - }, - }, - }, + "composite_index": CosmosDbIndexingPolicyCompositeIndexSchema(), }, }, } @@ -223,3 +189,41 @@ func ConflictResolutionPolicy() *schema.Schema { }, } } + +func CosmosDbIndexingPolicyCompositeIndexSchema() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "index": { + Type: schema.TypeList, + MinItems: 1, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "path": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + // `order` case changes from 2020-04-01 to 2021-01-15, issue opened:https://github.com/Azure/azure-rest-api-specs/issues/14051 + // todo: change to SDK constants and remove translation code in 3.0 + "order": { + Type: schema.TypeString, + Required: true, + // Workaround for Azure/azure-rest-api-specs#11222 + DiffSuppressFunc: suppress.CaseDifference, + ValidateFunc: validation.StringInSlice( + []string{ + "Ascending", + "Descending", + }, false), + }, + }, + }, + }, + }, + }, + } +} diff --git a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go index b3f9fbe547bc..29b0bf2e62f6 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go @@ -92,10 +92,16 @@ func resourceCosmosDbGremlinGraph() *schema.Resource { ValidateFunc: validation.StringIsNotEmpty, }, + "partition_key_version": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + ValidateFunc: validation.IntBetween(1, 2), + }, + "index_policy": { Type: schema.TypeList, - Required: true, - ForceNew: true, + Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "automatic": { @@ -136,6 +142,8 @@ func resourceCosmosDbGremlinGraph() *schema.Resource { }, Set: schema.HashString, }, + + "composite_index": common.CosmosDbIndexingPolicyCompositeIndexSchema(), }, }, }, @@ -202,6 +210,10 @@ func resourceCosmosDbGremlinGraphCreate(d *schema.ResourceData, meta interface{} if partitionkeypaths != "" { db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey = &documentdb.ContainerPartitionKey{ Paths: &[]string{partitionkeypaths}, + Kind: documentdb.PartitionKindHash, + } + if partitionKeyVersion, ok := d.GetOk("partition_key_version"); ok { + db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey.Version = utils.Int32(int32(partitionKeyVersion.(int))) } } @@ -280,6 +292,11 @@ func resourceCosmosDbGremlinGraphUpdate(d *schema.ResourceData, meta interface{} if partitionkeypaths != "" { db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey = &documentdb.ContainerPartitionKey{ Paths: &[]string{partitionkeypaths}, + Kind: documentdb.PartitionKindHash, + } + + if partitionKeyVersion, ok := d.GetOk("partition_key_version"); ok { + db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey.Version = utils.Int32(int32(partitionKeyVersion.(int))) } } @@ -356,6 +373,10 @@ func resourceCosmosDbGremlinGraphRead(d *schema.ResourceData, meta interface{}) d.Set("partition_key_path", (*paths)[0]) } } + + if version := pk.Version; version != nil { + d.Set("partition_key_version", version) + } } if ip := props.IndexingPolicy; ip != nil { @@ -434,6 +455,9 @@ func expandAzureRmCosmosDbGrelinGraphIndexingPolicy(d *schema.ResourceData) *doc IncludedPaths: expandAzureRmCosmosDbGrelimGraphIncludedPath(input), ExcludedPaths: expandAzureRmCosmosDbGremlinGraphExcludedPath(input), } + if v, ok := input["composite_index"].([]interface{}); ok { + policy.CompositeIndexes = common.ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) + } if automatic, ok := input["automatic"].(bool); ok { policy.Automatic = utils.Bool(automatic) @@ -505,6 +529,7 @@ func flattenAzureRmCosmosDBGremlinGraphIndexingPolicy(input *documentdb.Indexing indexPolicy["indexing_mode"] = strings.Title(string(input.IndexingMode)) indexPolicy["included_paths"] = schema.NewSet(schema.HashString, flattenAzureRmCosmosDBGremlinGraphIncludedPaths(input.IncludedPaths)) indexPolicy["excluded_paths"] = schema.NewSet(schema.HashString, flattenAzureRmCosmosDBGremlinGraphExcludedPaths(input.ExcludedPaths)) + indexPolicy["composite_index"] = common.FlattenCosmosDBIndexingPolicyCompositeIndexes(input.CompositeIndexes) return []interface{}{indexPolicy} } diff --git a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go index b0cda4e60133..478968ef5796 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go @@ -77,6 +77,20 @@ func TestAccCosmosDbGremlinGraph_indexPolicy(t *testing.T) { ), }, data.ImportStep(), + { + Config: r.indexPolicyCompositeIndex(data), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.indexPolicy(data), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), }) } @@ -138,6 +152,23 @@ func TestAccCosmosDbGremlinGraph_autoscale(t *testing.T) { }) } +func TestAccCosmosDbGremlinGraph_partition_key_version(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_gremlin_graph", "test") + r := CosmosGremlinGraphResource{} + + data.ResourceTest(t, r, []resource.TestStep{ + { + + Config: r.partition_key_version(data, 2), + Check: resource.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("partition_key_version").HasValue("2"), + ), + }, + data.ImportStep(), + }) +} + func (t CosmosGremlinGraphResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) { id, err := parse.GremlinGraphID(state.ID) if err != nil { @@ -163,13 +194,6 @@ resource "azurerm_cosmosdb_gremlin_graph" "test" { database_name = azurerm_cosmosdb_gremlin_database.test.name partition_key_path = "/test" throughput = 400 - - index_policy { - automatic = true - indexing_mode = "Consistent" - included_paths = ["/*"] - excluded_paths = ["/\"_etag\"/?"] - } } `, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger) } @@ -184,13 +208,6 @@ resource "azurerm_cosmosdb_gremlin_graph" "import" { account_name = azurerm_cosmosdb_account.test.name database_name = azurerm_cosmosdb_gremlin_database.test.name partition_key_path = azurerm_cosmosdb_gremlin_graph.test.partition_key_path - - index_policy { - automatic = true - indexing_mode = "Consistent" - included_paths = ["/*"] - excluded_paths = ["/\"_etag\"/?"] - } } `, r.basic(data)) } @@ -247,6 +264,53 @@ resource "azurerm_cosmosdb_gremlin_graph" "test" { `, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger) } +func (CosmosGremlinGraphResource) indexPolicyCompositeIndex(data acceptance.TestData) string { + return fmt.Sprintf(` +%[1]s + +resource "azurerm_cosmosdb_gremlin_graph" "test" { + name = "acctest-CGRPC-%[2]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 + + index_policy { + automatic = true + indexing_mode = "Consistent" + + composite_index { + index { + path = "/path1" + order = "Ascending" + } + index { + path = "/path2" + order = "Descending" + } + } + + composite_index { + index { + path = "/path3" + order = "Ascending" + } + index { + path = "/path4" + order = "Descending" + } + } + } + + conflict_resolution_policy { + mode = "LastWriterWins" + conflict_resolution_path = "/_ts" + } +} +`, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger) +} + func (CosmosGremlinGraphResource) update(data acceptance.TestData, throughput int, defaultTTL int) string { return fmt.Sprintf(` %[1]s @@ -298,3 +362,18 @@ resource "azurerm_cosmosdb_gremlin_graph" "test" { } `, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger, maxThroughput) } + +func (CosmosGremlinGraphResource) partition_key_version(data acceptance.TestData, version int) string { + return fmt.Sprintf(` +%[1]s + +resource "azurerm_cosmosdb_gremlin_graph" "test" { + name = "acctest-CGRPC-%[2]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" + partition_key_version = %[3]d +} +`, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger, version) +} diff --git a/website/docs/r/cosmosdb_gremlin_graph.html.markdown b/website/docs/r/cosmosdb_gremlin_graph.html.markdown index be2a52956ef9..1a25e90efaad 100644 --- a/website/docs/r/cosmosdb_gremlin_graph.html.markdown +++ b/website/docs/r/cosmosdb_gremlin_graph.html.markdown @@ -66,6 +66,8 @@ The following arguments are supported: * `partition_key_path` - (Required) Define a partition key. Changing this forces a new resource to be created. +* `partition_key_version` - (Optional) Define a partition key version. Changing this forces a new resource to be created. Possible values are `1 `and `2`. This should be set to `2` in order to use large partition keys. + * `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. * `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. @@ -98,6 +100,10 @@ An `index_policy` block supports the following: * `excluded_paths` - (Optional) List of paths to exclude from indexing. Required if `indexing_mode` is `Consistent` or `Lazy`. +* `composite_index` - (Optional) One or more `composite_index` blocks as defined below. + +--- + An `conflict_resolution_policy` block supports the following: * `mode` - (Required) Indicates the conflict resolution mode. Possible values include: `LastWriterWins`, `Custom`. @@ -106,10 +112,27 @@ An `conflict_resolution_policy` block supports the following: * `conflict_resolution_procedure` - (Optional) The procedure to resolve conflicts in the case of custom mode. +--- + An `unique_key` block supports the following: * `paths` - (Required) A list of paths to use for this unique key. +--- + +A `composite_index` block supports the following: + +* `index` - One or more `index` blocks as defined below. + +--- + +An `index` block supports the following: + +* `path` - Path for which the indexing behaviour applies to. + +* `order` - Order of the index. Possible values are `Ascending` or `Descending`. + + ## Attributes Reference The following attributes are exported: From 41a691291c3b4eaf8616f06cdf938049795d7509 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Tue, 11 May 2021 15:01:33 +0800 Subject: [PATCH 2/4] update --- .../services/cosmos/cosmosdb_gremlin_graph_resource.go | 4 ++++ .../cosmos/cosmosdb_gremlin_graph_resource_test.go | 7 ------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go index 29b0bf2e62f6..0703e5cca61a 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go @@ -102,6 +102,8 @@ func resourceCosmosDbGremlinGraph() *schema.Resource { "index_policy": { Type: schema.TypeList, Optional: true, + Computed: true, + MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "automatic": { @@ -126,6 +128,7 @@ func resourceCosmosDbGremlinGraph() *schema.Resource { "included_paths": { Type: schema.TypeSet, Optional: true, + Computed: true, Elem: &schema.Schema{ Type: schema.TypeString, ValidateFunc: validation.StringIsNotEmpty, @@ -136,6 +139,7 @@ func resourceCosmosDbGremlinGraph() *schema.Resource { "excluded_paths": { Type: schema.TypeSet, Optional: true, + Computed: true, Elem: &schema.Schema{ Type: schema.TypeString, ValidateFunc: validation.StringIsNotEmpty, diff --git a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go index 478968ef5796..c369b26088e9 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go @@ -84,13 +84,6 @@ func TestAccCosmosDbGremlinGraph_indexPolicy(t *testing.T) { ), }, data.ImportStep(), - { - Config: r.indexPolicy(data), - Check: resource.ComposeTestCheckFunc( - check.That(data.ResourceName).ExistsInAzure(r), - ), - }, - data.ImportStep(), }) } From e06a5b1602e7fc3333dec44cb554cfd449ba1bb9 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Thu, 27 May 2021 15:47:53 +0800 Subject: [PATCH 3/4] update --- .../cosmos/cosmosdb_gremlin_graph_resource.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go index aafc5e452705..9adaeef6dbd5 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go @@ -210,6 +210,10 @@ func resourceCosmosDbGremlinGraphCreate(d *pluginsdk.ResourceData, meta interfac if partitionkeypaths != "" { db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey = &documentdb.ContainerPartitionKey{ Paths: &[]string{partitionkeypaths}, + Kind: documentdb.PartitionKindHash, + } + if partitionKeyVersion, ok := d.GetOk("partition_key_version"); ok { + db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey.Version = utils.Int32(int32(partitionKeyVersion.(int))) } } @@ -288,6 +292,11 @@ func resourceCosmosDbGremlinGraphUpdate(d *pluginsdk.ResourceData, meta interfac if partitionkeypaths != "" { db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey = &documentdb.ContainerPartitionKey{ Paths: &[]string{partitionkeypaths}, + Kind: documentdb.PartitionKindHash, + } + + if partitionKeyVersion, ok := d.GetOk("partition_key_version"); ok { + db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey.Version = utils.Int32(int32(partitionKeyVersion.(int))) } } @@ -364,6 +373,10 @@ func resourceCosmosDbGremlinGraphRead(d *pluginsdk.ResourceData, meta interface{ d.Set("partition_key_path", (*paths)[0]) } } + + if version := pk.Version; version != nil { + d.Set("partition_key_version", version) + } } if ip := props.IndexingPolicy; ip != nil { @@ -442,6 +455,9 @@ func expandAzureRmCosmosDbGrelinGraphIndexingPolicy(d *pluginsdk.ResourceData) * IncludedPaths: expandAzureRmCosmosDbGrelimGraphIncludedPath(input), ExcludedPaths: expandAzureRmCosmosDbGremlinGraphExcludedPath(input), } + if v, ok := input["composite_index"].([]interface{}); ok { + policy.CompositeIndexes = common.ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) + } if automatic, ok := input["automatic"].(bool); ok { policy.Automatic = utils.Bool(automatic) From 5b89f5d24dff1ea2d30405b2c6be237566220769 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Thu, 27 May 2021 17:08:25 +0800 Subject: [PATCH 4/4] update --- .../internal/services/cosmos/cosmosdb_gremlin_graph_resource.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go index 9adaeef6dbd5..0d341f11fd39 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go @@ -126,6 +126,7 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource { "included_paths": { Type: pluginsdk.TypeSet, Optional: true, + Computed: true, Elem: &pluginsdk.Schema{ Type: pluginsdk.TypeString, ValidateFunc: validation.StringIsNotEmpty, @@ -136,6 +137,7 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource { "excluded_paths": { Type: pluginsdk.TypeSet, Optional: true, + Computed: true, Elem: &pluginsdk.Schema{ Type: pluginsdk.TypeString, ValidateFunc: validation.StringIsNotEmpty,