diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index 138bfa85401a..5e6ced0260e6 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -66,7 +66,7 @@ func ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[ return &indexes } -func expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec { +func ExpandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec { if len(input) == 0 || input[0] == nil { return nil } @@ -110,7 +110,7 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *pluginsdk.ResourceData) *documentdb. policy.CompositeIndexes = ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) } - policy.SpatialIndexes = expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{})) + policy.SpatialIndexes = ExpandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{})) return policy } @@ -190,7 +190,7 @@ func flattenCosmosDBIndexingPolicyIncludedPaths(input *[]documentdb.IncludedPath return includedPaths } -func flattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} { +func FlattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} { if input == nil { return []interface{}{} } @@ -236,7 +236,7 @@ func FlattenAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPol result["included_path"] = flattenCosmosDBIndexingPolicyIncludedPaths(indexingPolicy.IncludedPaths) result["excluded_path"] = flattenCosmosDBIndexingPolicyExcludedPaths(indexingPolicy.ExcludedPaths) result["composite_index"] = FlattenCosmosDBIndexingPolicyCompositeIndexes(indexingPolicy.CompositeIndexes) - result["spatial_index"] = flattenCosmosDBIndexingPolicySpatialIndexes(indexingPolicy.SpatialIndexes) + result["spatial_index"] = FlattenCosmosDBIndexingPolicySpatialIndexes(indexingPolicy.SpatialIndexes) 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 0684c756aa5c..1e749ac06f73 100644 --- a/azurerm/internal/services/cosmos/common/schema.go +++ b/azurerm/internal/services/cosmos/common/schema.go @@ -152,27 +152,7 @@ func CosmosDbIndexingPolicySchema() *pluginsdk.Schema { }, "composite_index": CosmosDbIndexingPolicyCompositeIndexSchema(), - "spatial_index": { - Type: pluginsdk.TypeList, - Optional: true, - Elem: &pluginsdk.Resource{ - Schema: map[string]*pluginsdk.Schema{ - "path": { - Type: pluginsdk.TypeString, - Required: true, - ValidateFunc: validation.StringIsNotEmpty, - }, - - "types": { - Type: pluginsdk.TypeSet, - Computed: true, - Elem: &pluginsdk.Schema{ - Type: pluginsdk.TypeString, - }, - }, - }, - }, - }, + "spatial_index": CosmosDbIndexingPolicySpatialIndexSchema(), }, }, } @@ -249,3 +229,27 @@ func CosmosDbIndexingPolicyCompositeIndexSchema() *pluginsdk.Schema { }, } } + +func CosmosDbIndexingPolicySpatialIndexSchema() *pluginsdk.Schema { + return &pluginsdk.Schema{ + Type: pluginsdk.TypeList, + Optional: true, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "path": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "types": { + Type: pluginsdk.TypeSet, + Computed: true, + Elem: &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + }, + }, + }, + }, + } +} diff --git a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go index 0d341f11fd39..708546607e45 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource.go @@ -146,6 +146,8 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource { }, "composite_index": common.CosmosDbIndexingPolicyCompositeIndexSchema(), + + "spatial_index": common.CosmosDbIndexingPolicySpatialIndexSchema(), }, }, }, @@ -461,6 +463,8 @@ func expandAzureRmCosmosDbGrelinGraphIndexingPolicy(d *pluginsdk.ResourceData) * policy.CompositeIndexes = common.ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) } + policy.SpatialIndexes = common.ExpandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{})) + if automatic, ok := input["automatic"].(bool); ok { policy.Automatic = utils.Bool(automatic) } @@ -532,6 +536,7 @@ func flattenAzureRmCosmosDBGremlinGraphIndexingPolicy(input *documentdb.Indexing indexPolicy["included_paths"] = pluginsdk.NewSet(pluginsdk.HashString, flattenAzureRmCosmosDBGremlinGraphIncludedPaths(input.IncludedPaths)) indexPolicy["excluded_paths"] = pluginsdk.NewSet(pluginsdk.HashString, flattenAzureRmCosmosDBGremlinGraphExcludedPaths(input.ExcludedPaths)) indexPolicy["composite_index"] = common.FlattenCosmosDBIndexingPolicyCompositeIndexes(input.CompositeIndexes) + indexPolicy["spatial_index"] = common.FlattenCosmosDBIndexingPolicySpatialIndexes(input.SpatialIndexes) 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 b1b4f9f8a31d..6661c4e050b1 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_gremlin_graph_resource_test.go @@ -83,6 +83,20 @@ func TestAccCosmosDbGremlinGraph_indexPolicy(t *testing.T) { ), }, data.ImportStep(), + { + Config: r.indexPolicySpatialIndex(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), }) } @@ -260,6 +274,61 @@ func (CosmosGremlinGraphResource) indexPolicyCompositeIndex(data acceptance.Test 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" + } + } + + spatial_index { + path = "/path/*" + } + + spatial_index { + path = "/test/to/all/?" + } + } + + conflict_resolution_policy { + mode = "LastWriterWins" + conflict_resolution_path = "/_ts" + } +} +`, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger) +} + +func (CosmosGremlinGraphResource) indexPolicySpatialIndex(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 diff --git a/website/docs/r/cosmosdb_gremlin_graph.html.markdown b/website/docs/r/cosmosdb_gremlin_graph.html.markdown index 1a25e90efaad..8f180373b85c 100644 --- a/website/docs/r/cosmosdb_gremlin_graph.html.markdown +++ b/website/docs/r/cosmosdb_gremlin_graph.html.markdown @@ -102,6 +102,14 @@ An `index_policy` block supports the following: * `composite_index` - (Optional) One or more `composite_index` blocks as defined below. +* `spatial_index` - (Optional) One or more `spatial_index` blocks as defined below. + +--- + +A `spatial_index` block supports the following: + +* `path` - (Required) Path for which the indexing behaviour applies to. According to the service design, all spatial types including `LineString`, `MultiPolygon`, `Point`, and `Polygon` will be applied to the path. + --- An `conflict_resolution_policy` block supports the following: