Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add composite indexes support to CosmosDB SQL container #8792

Merged
merged 27 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
01965f0
Add composite indexes support to CosmosDB SQL container
favoretti Oct 8, 2020
5b3609b
Append to the right array...
favoretti Oct 8, 2020
b082bc6
Rework schema
favoretti Oct 8, 2020
0125d58
Updated documentation
favoretti Oct 8, 2020
a643b33
Make 'index' block non-optional inside 'composite_index' and set MinI…
favoretti Oct 8, 2020
cc36a9e
Fix naming
favoretti Oct 8, 2020
75ce33f
Another approach, hope I wrapped my brain around it a bit
favoretti Oct 8, 2020
9fad44a
Add required
favoretti Oct 8, 2020
10ba062
Try #34987
favoretti Oct 8, 2020
d05ec01
fmt
favoretti Oct 8, 2020
164a1f4
fix lint...
favoretti Oct 8, 2020
aa1b838
Fix expand to iterate over interface rather than ResourceData
favoretti Oct 9, 2020
22362ac
Fix casing in documentation
favoretti Oct 9, 2020
4f6d23c
Casing doesn't matter after all
favoretti Oct 9, 2020
e0ee49c
Lint
favoretti Oct 9, 2020
949f682
Update website/docs/r/cosmosdb_sql_container.html.markdown
favoretti Oct 14, 2020
00d11c3
Update azurerm/internal/services/cosmos/common/indexing_policy.go
favoretti Oct 14, 2020
7f15d11
Update azurerm/internal/services/cosmos/common/indexing_policy.go
favoretti Oct 14, 2020
1c22989
Update azurerm/internal/services/cosmos/common/schema.go
favoretti Oct 14, 2020
d397817
Update azurerm/internal/services/cosmos/common/schema.go
favoretti Oct 14, 2020
9c3cb6d
Update azurerm/internal/services/cosmos/common/schema.go
favoretti Oct 14, 2020
d23a447
Update azurerm/internal/services/cosmos/common/indexing_policy.go
favoretti Oct 14, 2020
37ff151
test case fixes, doc whitespace change
jackofallops Oct 15, 2020
7d0bdf0
typo in previous 'fix'
jackofallops Oct 15, 2020
5ed75dd
Apply workaround for casing
favoretti Oct 15, 2020
dad3d0a
move case ignore workaround to schema
jackofallops Oct 19, 2020
d68d250
gofmt
jackofallops Oct 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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