diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index fa4b506471e4..138bfa85401a 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -66,6 +66,30 @@ func ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[ return &indexes } +func expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec { + if len(input) == 0 || input[0] == nil { + return nil + } + indexes := make([]documentdb.SpatialSpec, 0) + // no matter what spatial types are updated, all types will be set and returned from service + spatialTypes := []documentdb.SpatialType{ + documentdb.SpatialTypeLineString, + documentdb.SpatialTypeMultiPolygon, + documentdb.SpatialTypePoint, + documentdb.SpatialTypePolygon, + } + + for _, i := range input { + indexPair := i.(map[string]interface{}) + indexes = append(indexes, documentdb.SpatialSpec{ + Types: &spatialTypes, + Path: utils.String(indexPair["path"].(string)), + }) + } + + return &indexes +} + func ExpandAzureRmCosmosDbIndexingPolicy(d *pluginsdk.ResourceData) *documentdb.IndexingPolicy { i := d.Get("indexing_policy").([]interface{}) @@ -85,6 +109,9 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *pluginsdk.ResourceData) *documentdb. if v, ok := input["composite_index"].([]interface{}); ok { policy.CompositeIndexes = ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) } + + policy.SpatialIndexes = expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{})) + return policy } @@ -163,6 +190,41 @@ func flattenCosmosDBIndexingPolicyIncludedPaths(input *[]documentdb.IncludedPath return includedPaths } +func flattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} { + if input == nil { + return []interface{}{} + } + + indexes := make([]interface{}, 0) + + for _, v := range *input { + var path string + if v.Path != nil { + path = *v.Path + } + indexes = append(indexes, map[string]interface{}{ + "path": path, + "types": flattenCosmosDBIndexingPolicySpatialIndexesTypes(v.Types), + }) + } + + return indexes +} + +func flattenCosmosDBIndexingPolicySpatialIndexesTypes(input *[]documentdb.SpatialType) []interface{} { + if input == nil { + return nil + } + + types := make([]interface{}, 0) + + for _, v := range *input { + types = append(types, string(v)) + } + + return types +} + func FlattenAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPolicy) []interface{} { results := make([]interface{}, 0) if indexingPolicy == nil { @@ -174,6 +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) 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 dc04eacb206d..0684c756aa5c 100644 --- a/azurerm/internal/services/cosmos/common/schema.go +++ b/azurerm/internal/services/cosmos/common/schema.go @@ -151,6 +151,28 @@ 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, + }, + }, + }, + }, + }, }, }, } diff --git a/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go index 59ff56110730..2660667269b5 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go @@ -172,6 +172,21 @@ func TestAccCosmosDbSqlContainer_indexing_policy(t *testing.T) { check.That(data.ResourceName).ExistsInAzure(r), ), }, + { + + Config: r.indexing_policy_update_spatialIndex(data, "/includedPath02/*", "/excludedPath02/?"), + Check: acceptance.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + + Config: r.basic(data), + Check: acceptance.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, data.ImportStep(), }) } @@ -450,6 +465,66 @@ resource "azurerm_cosmosdb_sql_container" "test" { `, CosmosSqlDatabaseResource{}.basic(data), data.RandomInteger, includedPath, excludedPath) } +func (CosmosSqlContainerResource) indexing_policy_update_spatialIndex(data acceptance.TestData, includedPath, excludedPath string) string { + return fmt.Sprintf(` +%[1]s + +resource "azurerm_cosmosdb_sql_container" "test" { + name = "acctest-CSQLC-%[2]d" + resource_group_name = azurerm_cosmosdb_account.test.resource_group_name + account_name = azurerm_cosmosdb_account.test.name + database_name = azurerm_cosmosdb_sql_database.test.name + partition_key_path = "/definition/id" + + indexing_policy { + indexing_mode = "Consistent" + + included_path { + path = "/*" + } + + included_path { + path = "%s" + } + + excluded_path { + path = "%s" + } + + 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/?" + } + } +} +`, CosmosSqlDatabaseResource{}.basic(data), data.RandomInteger, includedPath, excludedPath) +} + func (CosmosSqlContainerResource) partition_key_version(data acceptance.TestData, version int) string { return fmt.Sprintf(` %[1]s diff --git a/website/docs/r/cosmosdb_sql_container.html.markdown b/website/docs/r/cosmosdb_sql_container.html.markdown index 7d11bf990997..58bbfda62efb 100644 --- a/website/docs/r/cosmosdb_sql_container.html.markdown +++ b/website/docs/r/cosmosdb_sql_container.html.markdown @@ -87,6 +87,7 @@ A `unique_key` block supports the following: * `paths` - (Required) A list of paths to use for this unique key. +--- An `indexing_policy` block supports the following: * `indexing_mode` - (Optional) Indicates the indexing mode. Possible values include: `Consistent` and `None`. Defaults to `Consistent`. @@ -97,18 +98,34 @@ An `indexing_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 `included_path` block supports the following: * `path` - Path for which the indexing behaviour applies to. +--- + An `excluded_path` block supports the following: * `path` - Path that is excluded from indexing. +--- + 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. @@ -131,6 +148,12 @@ The following attributes are exported: * `id` - The ID of the CosmosDB SQL Container. +--- + +A `spatial_index` block exports the following: + +* `types` - A set of spatial types of the path. + ## Timeouts The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: