From 757abf19e7b12fd54e912297c16fa8ea9ebf4aae Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Fri, 7 May 2021 10:26:04 +0800 Subject: [PATCH 1/9] update --- .../services/cosmos/common/indexing_policy.go | 111 +++++++++++++- .../internal/services/cosmos/common/schema.go | 57 ++++++++ .../cosmosdb_sql_container_resource_test.go | 137 ++++++++++++++++++ 3 files changed, 304 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index e9ee909a6de7..ec1adc86cb45 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -19,7 +19,8 @@ func expandAzureRmCosmosDBIndexingPolicyIncludedPaths(input []interface{}) *[]do for _, v := range input { includedPath := v.(map[string]interface{}) path := documentdb.IncludedPath{ - Path: utils.String(includedPath["path"].(string)), + Path: utils.String(includedPath["path"].(string)), + Indexes: expandAzureRmCosmosDBIndexingPolicyIncludedPathsIndex(includedPath["indexes"].([]interface{})), } includedPaths = append(includedPaths, path) @@ -28,6 +29,27 @@ func expandAzureRmCosmosDBIndexingPolicyIncludedPaths(input []interface{}) *[]do return &includedPaths } +func expandAzureRmCosmosDBIndexingPolicyIncludedPathsIndex(input []interface{}) *[]documentdb.Indexes { + if len(input) == 0 { + return nil + } + + var indexes []documentdb.Indexes + + for _, v := range input { + index := v.(map[string]interface{}) + path := documentdb.Indexes{ + DataType: documentdb.DataType(index["data_type"].(string)), + Precision: utils.Int32(int32(index["precision"].(int))), + Kind: documentdb.IndexKind(index["kind"].(string)), + } + + indexes = append(indexes, path) + } + + return &indexes +} + func expandAzureRmCosmosDBIndexingPolicyExcludedPaths(input []interface{}) *[]documentdb.ExcludedPath { if len(input) == 0 { return nil @@ -66,6 +88,30 @@ func expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[ return &indexes } +func expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec { + indexes := make([]documentdb.SpatialSpec, 0) + + for _, i := range input { + indexPair := i.(map[string]interface{}) + indexes = append(indexes, documentdb.SpatialSpec{ + Types: expandAzureRmCosmosDBIndexingPolicySpatialIndexesTypes(indexPair["types"].(*schema.Set).List()), + Path: utils.String(indexPair["path"].(string)), + }) + } + + return &indexes +} + +func expandAzureRmCosmosDBIndexingPolicySpatialIndexesTypes(input []interface{}) *[]documentdb.SpatialType { + types := make([]documentdb.SpatialType, 0) + + for _, i := range input { + types = append(types, documentdb.SpatialType(i.(string))) + } + + return &types +} + func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.IndexingPolicy { i := d.Get("indexing_policy").([]interface{}) @@ -85,6 +131,10 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.Ind if v, ok := input["composite_index"].([]interface{}); ok { policy.CompositeIndexes = expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) } + + if v, ok := input["spatial_indexes"].([]interface{}); ok { + policy.SpatialIndexes = expandAzureRmCosmosDBIndexingPolicySpatialIndexes(v) + } return policy } @@ -157,12 +207,70 @@ func flattenCosmosDBIndexingPolicyIncludedPaths(input *[]documentdb.IncludedPath for _, v := range *input { block := make(map[string]interface{}) block["path"] = v.Path + block["indexes"] = flattenCosmosDBIndexingPolicyIncludedPathsIndexes(v.Indexes) includedPaths = append(includedPaths, block) } return includedPaths } +func flattenCosmosDBIndexingPolicyIncludedPathsIndexes(input *[]documentdb.Indexes) []interface{} { + if input == nil { + return nil + } + + indexes := make([]interface{}, 0) + + for _, v := range *input { + var precision int32 + if v.Precision != nil { + precision = *v.Precision + } + indexes = append(indexes, map[string]interface{}{ + "data_type": string(v.DataType), + "kind": string(v.Kind), + "precision": precision, + }) + } + + return indexes +} + +func flattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} { + if input == nil { + return nil + } + + 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 +282,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_indexes"] = 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 65a873fb2cf4..56dd15cfc63d 100644 --- a/azurerm/internal/services/cosmos/common/schema.go +++ b/azurerm/internal/services/cosmos/common/schema.go @@ -128,6 +128,35 @@ func CosmosDbIndexingPolicySchema() *schema.Schema { Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ + "indexes": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "data_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(documentdb.Number), + string(documentdb.String), + }, false), + }, + "kind": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(documentdb.Hash), + string(documentdb.Range), + string(documentdb.Spatial), + }, false), + }, + "precision": { + Type: schema.TypeInt, + Optional: true, + }, + }, + }, + }, "path": { Type: schema.TypeString, Required: true, @@ -185,6 +214,34 @@ func CosmosDbIndexingPolicySchema() *schema.Schema { }, }, }, + + "spatial_indexes": { + Type: schema.TypeList, + Optional: true, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "path": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "types": { + Type: schema.TypeSet, + Required: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringInSlice([]string{ + string(documentdb.SpatialTypeLineString), + string(documentdb.SpatialTypeMultiPolygon), + string(documentdb.SpatialTypePoint), + string(documentdb.SpatialTypePolygon), + }, false), + }, + }, + }, + }, + }, }, }, } 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 03ab317d26a4..7a7b51e2f186 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go @@ -156,6 +156,20 @@ func TestAccCosmosDbSqlContainer_indexing_policy(t *testing.T) { check.That(data.ResourceName).ExistsInAzure(r), ), }, + { + + Config: r.indexing_policy_update_indexes(data, "/includedPath02/*", "/excludedPath02/?"), + Check: resource.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + { + + Config: r.indexing_policy_update_spatialIndexes(data, "/includedPath02/*", "/excludedPath02/?"), + Check: resource.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, data.ImportStep(), }) } @@ -413,6 +427,129 @@ resource "azurerm_cosmosdb_sql_container" "test" { `, CosmosSqlDatabaseResource{}.basic(data), data.RandomInteger, includedPath, excludedPath) } +func (CosmosSqlContainerResource) indexing_policy_update_indexes(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 = "/*" + indexes{ +data_type = "String" +kind = "Hash" +precision = 1 +} + + indexes{ +data_type = "Number" +kind = "Range" +} + } + + 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" + } + } + } +} +`, CosmosSqlDatabaseResource{}.basic(data), data.RandomInteger, includedPath, excludedPath) +} + +func (CosmosSqlContainerResource) indexing_policy_update_spatialIndexes(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_indexes { + path = "/path/*" + types = ["LineString","Point"] + } + +spatial_indexes { + path = "/2/*" + } + } +} +`, CosmosSqlDatabaseResource{}.basic(data), data.RandomInteger, includedPath, excludedPath) +} + func (CosmosSqlContainerResource) partition_key_version(data acceptance.TestData, version int) string { return fmt.Sprintf(` %[1]s From d9dc5711a283024b6039b3600bb771dad2e49e86 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Sat, 8 May 2021 09:54:50 +0800 Subject: [PATCH 2/9] update --- .../services/cosmos/common/indexing_policy.go | 86 +++---------------- .../internal/services/cosmos/common/schema.go | 44 +--------- .../cosmosdb_sql_container_resource_test.go | 76 ++-------------- .../r/cosmosdb_sql_container.html.markdown | 17 ++++ 4 files changed, 36 insertions(+), 187 deletions(-) diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index ec1adc86cb45..febac2cdfa11 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -19,8 +19,7 @@ func expandAzureRmCosmosDBIndexingPolicyIncludedPaths(input []interface{}) *[]do for _, v := range input { includedPath := v.(map[string]interface{}) path := documentdb.IncludedPath{ - Path: utils.String(includedPath["path"].(string)), - Indexes: expandAzureRmCosmosDBIndexingPolicyIncludedPathsIndex(includedPath["indexes"].([]interface{})), + Path: utils.String(includedPath["path"].(string)), } includedPaths = append(includedPaths, path) @@ -29,27 +28,6 @@ func expandAzureRmCosmosDBIndexingPolicyIncludedPaths(input []interface{}) *[]do return &includedPaths } -func expandAzureRmCosmosDBIndexingPolicyIncludedPathsIndex(input []interface{}) *[]documentdb.Indexes { - if len(input) == 0 { - return nil - } - - var indexes []documentdb.Indexes - - for _, v := range input { - index := v.(map[string]interface{}) - path := documentdb.Indexes{ - DataType: documentdb.DataType(index["data_type"].(string)), - Precision: utils.Int32(int32(index["precision"].(int))), - Kind: documentdb.IndexKind(index["kind"].(string)), - } - - indexes = append(indexes, path) - } - - return &indexes -} - func expandAzureRmCosmosDBIndexingPolicyExcludedPaths(input []interface{}) *[]documentdb.ExcludedPath { if len(input) == 0 { return nil @@ -90,11 +68,17 @@ func expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[ func expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec { indexes := make([]documentdb.SpatialSpec, 0) + 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: expandAzureRmCosmosDBIndexingPolicySpatialIndexesTypes(indexPair["types"].(*schema.Set).List()), + Types: &spatialTypes, Path: utils.String(indexPair["path"].(string)), }) } @@ -102,16 +86,6 @@ func expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]d return &indexes } -func expandAzureRmCosmosDBIndexingPolicySpatialIndexesTypes(input []interface{}) *[]documentdb.SpatialType { - types := make([]documentdb.SpatialType, 0) - - for _, i := range input { - types = append(types, documentdb.SpatialType(i.(string))) - } - - return &types -} - func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.IndexingPolicy { i := d.Get("indexing_policy").([]interface{}) @@ -132,7 +106,7 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.Ind policy.CompositeIndexes = expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) } - if v, ok := input["spatial_indexes"].([]interface{}); ok { + if v, ok := input["spatial_index"].([]interface{}); ok { policy.SpatialIndexes = expandAzureRmCosmosDBIndexingPolicySpatialIndexes(v) } return policy @@ -207,35 +181,12 @@ func flattenCosmosDBIndexingPolicyIncludedPaths(input *[]documentdb.IncludedPath for _, v := range *input { block := make(map[string]interface{}) block["path"] = v.Path - block["indexes"] = flattenCosmosDBIndexingPolicyIncludedPathsIndexes(v.Indexes) includedPaths = append(includedPaths, block) } return includedPaths } -func flattenCosmosDBIndexingPolicyIncludedPathsIndexes(input *[]documentdb.Indexes) []interface{} { - if input == nil { - return nil - } - - indexes := make([]interface{}, 0) - - for _, v := range *input { - var precision int32 - if v.Precision != nil { - precision = *v.Precision - } - indexes = append(indexes, map[string]interface{}{ - "data_type": string(v.DataType), - "kind": string(v.Kind), - "precision": precision, - }) - } - - return indexes -} - func flattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} { if input == nil { return nil @@ -249,28 +200,13 @@ func flattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec path = *v.Path } indexes = append(indexes, map[string]interface{}{ - "path": path, - "types": flattenCosmosDBIndexingPolicySpatialIndexesTypes(v.Types), + "path": path, }) } 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 { @@ -282,7 +218,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_indexes"] = 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 56dd15cfc63d..6786a03acb0a 100644 --- a/azurerm/internal/services/cosmos/common/schema.go +++ b/azurerm/internal/services/cosmos/common/schema.go @@ -128,35 +128,6 @@ func CosmosDbIndexingPolicySchema() *schema.Schema { Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "indexes": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "data_type": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - string(documentdb.Number), - string(documentdb.String), - }, false), - }, - "kind": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - string(documentdb.Hash), - string(documentdb.Range), - string(documentdb.Spatial), - }, false), - }, - "precision": { - Type: schema.TypeInt, - Optional: true, - }, - }, - }, - }, "path": { Type: schema.TypeString, Required: true, @@ -215,7 +186,7 @@ func CosmosDbIndexingPolicySchema() *schema.Schema { }, }, - "spatial_indexes": { + "spatial_index": { Type: schema.TypeList, Optional: true, Computed: true, @@ -226,19 +197,6 @@ func CosmosDbIndexingPolicySchema() *schema.Schema { Required: true, ValidateFunc: validation.StringIsNotEmpty, }, - "types": { - Type: schema.TypeSet, - Required: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - ValidateFunc: validation.StringInSlice([]string{ - string(documentdb.SpatialTypeLineString), - string(documentdb.SpatialTypeMultiPolygon), - string(documentdb.SpatialTypePoint), - string(documentdb.SpatialTypePolygon), - }, false), - }, - }, }, }, }, 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 7a7b51e2f186..bc30874d4f7a 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go @@ -158,14 +158,15 @@ func TestAccCosmosDbSqlContainer_indexing_policy(t *testing.T) { }, { - Config: r.indexing_policy_update_indexes(data, "/includedPath02/*", "/excludedPath02/?"), + Config: r.indexing_policy_update_spatialIndex(data, "/includedPath02/*", "/excludedPath02/?"), Check: resource.ComposeAggregateTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), }, + data.ImportStep(), { - Config: r.indexing_policy_update_spatialIndexes(data, "/includedPath02/*", "/excludedPath02/?"), + Config: r.basic(data), Check: resource.ComposeAggregateTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -427,69 +428,7 @@ resource "azurerm_cosmosdb_sql_container" "test" { `, CosmosSqlDatabaseResource{}.basic(data), data.RandomInteger, includedPath, excludedPath) } -func (CosmosSqlContainerResource) indexing_policy_update_indexes(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 = "/*" - indexes{ -data_type = "String" -kind = "Hash" -precision = 1 -} - - indexes{ -data_type = "Number" -kind = "Range" -} - } - - 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" - } - } - } -} -`, CosmosSqlDatabaseResource{}.basic(data), data.RandomInteger, includedPath, excludedPath) -} - -func (CosmosSqlContainerResource) indexing_policy_update_spatialIndexes(data acceptance.TestData, includedPath, excludedPath string) string { +func (CosmosSqlContainerResource) indexing_policy_update_spatialIndex(data acceptance.TestData, includedPath, excludedPath string) string { return fmt.Sprintf(` %[1]s @@ -537,13 +476,12 @@ resource "azurerm_cosmosdb_sql_container" "test" { } } - spatial_indexes { + spatial_index { path = "/path/*" - types = ["LineString","Point"] } -spatial_indexes { - path = "/2/*" + spatial_index { + path = "/test/to/all" } } } diff --git a/website/docs/r/cosmosdb_sql_container.html.markdown b/website/docs/r/cosmosdb_sql_container.html.markdown index fc810cabfff7..9c24ce83686d 100644 --- a/website/docs/r/cosmosdb_sql_container.html.markdown +++ b/website/docs/r/cosmosdb_sql_container.html.markdown @@ -85,6 +85,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`. @@ -95,18 +96,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. + +--- + +An `spatial_index` block supports the following: + +* `path` - (Required) Path for which the indexing behaviour applies to. + +--- + 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. From 6da01d432304440906c97f2c03729820d78d149b Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Sat, 8 May 2021 10:11:15 +0800 Subject: [PATCH 3/9] update --- .../internal/services/cosmos/common/indexing_policy.go | 8 +++++--- .../cosmos/cosmosdb_sql_container_resource_test.go | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index febac2cdfa11..545c1d80b6d7 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -67,6 +67,9 @@ func expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[ } func expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec { + if len(input) == 0 || input[0] == nil { + return nil + } indexes := make([]documentdb.SpatialSpec, 0) spatialTypes := []documentdb.SpatialType{ documentdb.SpatialTypeLineString, @@ -106,9 +109,8 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.Ind policy.CompositeIndexes = expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v) } - if v, ok := input["spatial_index"].([]interface{}); ok { - policy.SpatialIndexes = expandAzureRmCosmosDBIndexingPolicySpatialIndexes(v) - } + policy.SpatialIndexes = expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{})) + return policy } 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 bc30874d4f7a..3145e9b1eb90 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go @@ -481,7 +481,7 @@ resource "azurerm_cosmosdb_sql_container" "test" { } spatial_index { - path = "/test/to/all" + path = "/test/to/all/?" } } } From 9b98468f3762cdc05c6ac911c053e41c2bde4e7c Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Sat, 8 May 2021 10:47:34 +0800 Subject: [PATCH 4/9] update --- azurerm/internal/services/cosmos/common/indexing_policy.go | 2 +- azurerm/internal/services/cosmos/common/schema.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index 545c1d80b6d7..fcf7668f33cc 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -191,7 +191,7 @@ func flattenCosmosDBIndexingPolicyIncludedPaths(input *[]documentdb.IncludedPath func flattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} { if input == nil { - return nil + return []interface{}{} } indexes := make([]interface{}, 0) diff --git a/azurerm/internal/services/cosmos/common/schema.go b/azurerm/internal/services/cosmos/common/schema.go index 6786a03acb0a..95fd25f0bc16 100644 --- a/azurerm/internal/services/cosmos/common/schema.go +++ b/azurerm/internal/services/cosmos/common/schema.go @@ -189,7 +189,6 @@ func CosmosDbIndexingPolicySchema() *schema.Schema { "spatial_index": { Type: schema.TypeList, Optional: true, - Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "path": { From cf1d62dea413a7e74b381f83d5e74767b896d017 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Mon, 10 May 2021 09:54:42 +0800 Subject: [PATCH 5/9] update --- .../services/cosmos/common/indexing_policy.go | 17 ++++++++++++++++- .../internal/services/cosmos/common/schema.go | 8 ++++++++ .../docs/r/cosmosdb_sql_container.html.markdown | 6 ++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index fcf7668f33cc..01b75a98fd46 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -202,13 +202,28 @@ func flattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec path = *v.Path } indexes = append(indexes, map[string]interface{}{ - "path": path, + "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 { diff --git a/azurerm/internal/services/cosmos/common/schema.go b/azurerm/internal/services/cosmos/common/schema.go index 95fd25f0bc16..384c91b429dd 100644 --- a/azurerm/internal/services/cosmos/common/schema.go +++ b/azurerm/internal/services/cosmos/common/schema.go @@ -196,6 +196,14 @@ func CosmosDbIndexingPolicySchema() *schema.Schema { Required: true, ValidateFunc: validation.StringIsNotEmpty, }, + + "types": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, }, }, }, diff --git a/website/docs/r/cosmosdb_sql_container.html.markdown b/website/docs/r/cosmosdb_sql_container.html.markdown index 9c24ce83686d..b2eb4d2c91c7 100644 --- a/website/docs/r/cosmosdb_sql_container.html.markdown +++ b/website/docs/r/cosmosdb_sql_container.html.markdown @@ -146,6 +146,12 @@ The following attributes are exported: * `id` - The ID of the CosmosDB SQL Container. +--- + +An `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: From 2aa8f52422536f6c2c88a0baaa9c1b5d20e6ff7d Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Mon, 10 May 2021 09:56:17 +0800 Subject: [PATCH 6/9] update --- website/docs/r/cosmosdb_sql_container.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/cosmosdb_sql_container.html.markdown b/website/docs/r/cosmosdb_sql_container.html.markdown index b2eb4d2c91c7..debbf7226fae 100644 --- a/website/docs/r/cosmosdb_sql_container.html.markdown +++ b/website/docs/r/cosmosdb_sql_container.html.markdown @@ -100,7 +100,7 @@ An `indexing_policy` block supports the following: --- -An `spatial_index` block supports the following: +A `spatial_index` block supports the following: * `path` - (Required) Path for which the indexing behaviour applies to. @@ -148,7 +148,7 @@ The following attributes are exported: --- -An `spatial_index` block exports the following: +A `spatial_index` block exports the following: * `types` - A set of spatial types of the path. From d07644ae0b60b10e47bb6ebb99a6c298c81a62e4 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Mon, 17 May 2021 15:51:36 +0800 Subject: [PATCH 7/9] update --- azurerm/internal/services/cosmos/common/indexing_policy.go | 1 + website/docs/r/cosmosdb_sql_container.html.markdown | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/cosmos/common/indexing_policy.go b/azurerm/internal/services/cosmos/common/indexing_policy.go index 01b75a98fd46..ce39d23bd09d 100644 --- a/azurerm/internal/services/cosmos/common/indexing_policy.go +++ b/azurerm/internal/services/cosmos/common/indexing_policy.go @@ -71,6 +71,7 @@ func expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]d 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, diff --git a/website/docs/r/cosmosdb_sql_container.html.markdown b/website/docs/r/cosmosdb_sql_container.html.markdown index debbf7226fae..e0f8a01e80aa 100644 --- a/website/docs/r/cosmosdb_sql_container.html.markdown +++ b/website/docs/r/cosmosdb_sql_container.html.markdown @@ -102,7 +102,7 @@ An `indexing_policy` block supports the following: A `spatial_index` block supports the following: -* `path` - (Required) Path for which the indexing behaviour applies to. +* `path` - (Required) Path for which the indexing behaviour applies to. All spatial types including `LineString`, `MultiPolygon`, `Point`, and `Polygon` will be applied to the path. --- From d79fa690a482e9d317cb417b00eaaf5116b9b6f7 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Mon, 17 May 2021 15:52:59 +0800 Subject: [PATCH 8/9] update --- website/docs/r/cosmosdb_sql_container.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/cosmosdb_sql_container.html.markdown b/website/docs/r/cosmosdb_sql_container.html.markdown index e0f8a01e80aa..685a4cb1fae1 100644 --- a/website/docs/r/cosmosdb_sql_container.html.markdown +++ b/website/docs/r/cosmosdb_sql_container.html.markdown @@ -102,7 +102,7 @@ An `indexing_policy` block supports the following: A `spatial_index` block supports the following: -* `path` - (Required) Path for which the indexing behaviour applies to. All spatial types including `LineString`, `MultiPolygon`, `Point`, and `Polygon` will be applied to the path. +* `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. --- From 05cde283ab53cb56ad8ad7083a80ac6f616dcd4c Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Thu, 27 May 2021 15:53:58 +0800 Subject: [PATCH 9/9] update --- azurerm/internal/services/cosmos/common/schema.go | 14 +++++++------- .../cosmos/cosmosdb_sql_container_resource_test.go | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/azurerm/internal/services/cosmos/common/schema.go b/azurerm/internal/services/cosmos/common/schema.go index a9dc601157fb..1e9295bf1992 100644 --- a/azurerm/internal/services/cosmos/common/schema.go +++ b/azurerm/internal/services/cosmos/common/schema.go @@ -187,21 +187,21 @@ func CosmosDbIndexingPolicySchema() *pluginsdk.Schema { }, "spatial_index": { - Type: schema.TypeList, + Type: pluginsdk.TypeList, Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ "path": { - Type: schema.TypeString, + Type: pluginsdk.TypeString, Required: true, ValidateFunc: validation.StringIsNotEmpty, }, "types": { - Type: schema.TypeSet, + Type: pluginsdk.TypeSet, Computed: true, - Elem: &schema.Schema{ - Type: schema.TypeString, + 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 3615964dbd52..2660667269b5 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_container_resource_test.go @@ -175,7 +175,7 @@ func TestAccCosmosDbSqlContainer_indexing_policy(t *testing.T) { { Config: r.indexing_policy_update_spatialIndex(data, "/includedPath02/*", "/excludedPath02/?"), - Check: resource.ComposeAggregateTestCheckFunc( + Check: acceptance.ComposeAggregateTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), }, @@ -183,7 +183,7 @@ func TestAccCosmosDbSqlContainer_indexing_policy(t *testing.T) { { Config: r.basic(data), - Check: resource.ComposeAggregateTestCheckFunc( + Check: acceptance.ComposeAggregateTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), },