Skip to content

Commit

Permalink
Add composite indexes support to CosmosDB SQL container (#8792)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Harvey <[email protected]>
Co-authored-by: jackofallops <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2020
1 parent 06aa2f8 commit e584f97
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
62 changes: 62 additions & 0 deletions azurerm/internal/services/cosmos/common/indexing_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ func expandAzureRmCosmosDBIndexingPolicyExcludedPaths(input []interface{}) *[]do
return &paths
}

func expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[][]documentdb.CompositePath {
indexes := make([][]documentdb.CompositePath, 0)

for _, i := range input {
indexPairs := make([]documentdb.CompositePath, 0)
indexPair := i.(map[string]interface{})
for _, idxPair := range indexPair["index"].([]interface{}) {
data := idxPair.(map[string]interface{})

index := documentdb.CompositePath{
Path: utils.String(data["path"].(string)),
Order: documentdb.CompositePathSortOrder(data["order"].(string)),
}
indexPairs = append(indexPairs, index)
}
indexes = append(indexes, indexPairs)
}

return &indexes
}

func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.IndexingPolicy {
i := d.Get("indexing_policy").([]interface{})

Expand All @@ -60,6 +81,9 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.Ind
policy.ExcludedPaths = expandAzureRmCosmosDBIndexingPolicyExcludedPaths(v)
}

if v, ok := input["composite_index"].([]interface{}); ok {
policy.CompositeIndexes = expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v)
}
return policy
}

Expand All @@ -85,6 +109,43 @@ func flattenCosmosDBIndexingPolicyExcludedPaths(input *[]documentdb.ExcludedPath
return excludedPaths
}

func flattenCosmosDBIndexingPolicyCompositeIndex(input []documentdb.CompositePath) []interface{} {
if input == nil {
return []interface{}{}
}

indexPairs := make([]interface{}, 0)
for _, v := range input {
path := ""
if v.Path != nil {
path = *v.Path
}

block := make(map[string]interface{})
block["path"] = path
block["order"] = string(v.Order)
indexPairs = append(indexPairs, block)
}

return indexPairs
}

func flattenCosmosDBIndexingPolicyCompositeIndexes(input *[][]documentdb.CompositePath) []interface{} {
if input == nil {
return []interface{}{}
}

indexes := make([]interface{}, 0)

for _, v := range *input {
block := make(map[string][]interface{})
block["index"] = flattenCosmosDBIndexingPolicyCompositeIndex(v)
indexes = append(indexes, block)
}

return indexes
}

func flattenCosmosDBIndexingPolicyIncludedPaths(input *[]documentdb.IncludedPath) []interface{} {
if input == nil {
return nil
Expand All @@ -111,6 +172,7 @@ func FlattenAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPol
result["indexing_mode"] = string(indexingPolicy.IndexingMode)
result["included_path"] = flattenCosmosDBIndexingPolicyIncludedPaths(indexingPolicy.IncludedPaths)
result["excluded_path"] = flattenCosmosDBIndexingPolicyExcludedPaths(indexingPolicy.ExcludedPaths)
result["composite_index"] = flattenCosmosDBIndexingPolicyCompositeIndexes(indexingPolicy.CompositeIndexes)

results = append(results, result)
return results
Expand Down
33 changes: 33 additions & 0 deletions azurerm/internal/services/cosmos/common/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ 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": {
Type: schema.TypeString,
Required: true,
// Workaround for Azure/azure-rest-api-specs#11222
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice(
[]string{
string(documentdb.Ascending),
string(documentdb.Descending),
}, false),
},
},
},
},
},
},
},
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,27 @@ resource "azurerm_cosmosdb_sql_container" "test" {
excluded_path {
path = "/testing/id2/*"
}
composite_index {
index {
path = "/path1"
order = "Descending"
}
index {
path = "/path2"
order = "Ascending"
}
}
composite_index {
index {
path = "/path3"
order = "Ascending"
}
index {
path = "/path4"
order = "Descending"
}
}
}
}
`, testAccAzureRMCosmosDbSqlDatabase_basic(data), data.RandomInteger)
Expand Down Expand Up @@ -293,6 +314,28 @@ resource "azurerm_cosmosdb_sql_container" "test" {
excluded_path {
path = "/testing/id1/*"
}
composite_index {
index {
path = "/path1"
order = "Ascending"
}
index {
path = "/path2"
order = "Descending"
}
}
composite_index {
index {
path = "/path3"
order = "Ascending"
}
index {
path = "/path4"
order = "Descending"
}
}
}
}
`, testAccAzureRMCosmosDbSqlDatabase_basic(data), data.RandomInteger)
Expand Down Expand Up @@ -338,6 +381,28 @@ resource "azurerm_cosmosdb_sql_container" "test" {
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"
}
}
}
}
`, testAccAzureRMCosmosDbSqlDatabase_basic(data), data.RandomInteger, includedPath, excludedPath)
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/cosmosdb_sql_container.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ An `indexing_policy` block supports the following:

* `excluded_path` - (Optional) One or more `excluded_path` blocks as defined below. Either `included_path` or `excluded_path` must contain the `path` `/*`

* `composite_index` - (Optional) One or more `composite_index` blocks as defined below.

An `included_path` block supports the following:

* `path` - Path for which the indexing behavior applies to.
Expand All @@ -96,6 +98,15 @@ 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 behavior applies to.

* `order` - Order of the index. Possible values are `Ascending` or `Descending`.

## Attributes Reference

Expand Down

0 comments on commit e584f97

Please sign in to comment.