Skip to content

Commit

Permalink
feat(cosmosdb_sql_container): including indexing_policy setting for…
Browse files Browse the repository at this point in the history
… CosmosDB SQL Container (#402)

* feat: including indexing_policy setting on CosmosDB SQL Container
  • Loading branch information
andrea-deri authored Jan 16, 2025
1 parent e57ed12 commit cf14027
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cosmosdb_sql_container/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ locals {
partition_key_path = "/fiscalCode"
autoscale_settings = {
max_throughput = 6000
},
}
indexing_policy = {
composite_indexes = [
[
{ path: "/field1" },
{ path: "/field10" },
],
[
{ path: "/field2", order: "descending" },
{ path: "/nested/field" },
{ path: "/field3", order: "ascending" },
]
]
}
},

]
Expand All @@ -41,6 +54,7 @@ module "core_cosmosdb_containers" {
database_name = module.core_cosmos_db.name
partition_key_path = each.value.partition_key_path
throughput = lookup(each.value, "throughput", null)
indexing_policy = lookup(each.value, "indexing_policy", null)}

autoscale_settings = lookup(each.value, "autoscale_settings", null)

Expand Down Expand Up @@ -75,6 +89,7 @@ No modules.
| <a name="input_autoscale_settings"></a> [autoscale\_settings](#input\_autoscale\_settings) | Autoscale settings for collection | <pre>object({<br/> max_throughput = number<br/> })</pre> | `null` | no |
| <a name="input_database_name"></a> [database\_name](#input\_database\_name) | The name of the Cosmos DB SQL Database to create the container within. | `string` | n/a | yes |
| <a name="input_default_ttl"></a> [default\_ttl](#input\_default\_ttl) | The default time to live of SQL container. If missing, items are not expired automatically. | `number` | `null` | no |
| <a name="input_indexing_policy"></a> [indexing\_policy](#input\_indexing\_policy) | The configuration of indexes on collection | <pre>object({<br/> # The indexing strategy. Valid options are: consistent, none<br/> indexing_mode = optional(string, "consistent"),<br/><br/> # One or more paths for which the indexing behaviour applies to. Either included_path or excluded_path must contain the all-path string ('/*')<br/> included_paths = optional(list(string), ["/*"]),<br/><br/> # One or more paths that are excluded from indexing. Either included_path or excluded_path must contain the all-path string ('/*')<br/> excluded_paths = optional(list(string), []),<br/><br/> # One or more path that define complex indexes. There can be multiple composite indexes on same indexing policy<br/> composite_indexes = optional(list(list(object(<br/> {<br/> # The path of the field to be included in the composite index<br/> path = string<br/><br/> # The sort of single field in indexing structure. Valid options are: ascending, descending<br/> order = optional(string, "ascending")<br/> }<br/> ))), []),<br/> })</pre> | `null` | no |
| <a name="input_name"></a> [name](#input\_name) | The name of the Cosmos DB instance. | `string` | n/a | yes |
| <a name="input_partition_key_path"></a> [partition\_key\_path](#input\_partition\_key\_path) | Define a partition key. | `string` | `null` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | The name of the resource group in which the Cosmos DB SQL | `string` | n/a | yes |
Expand Down
41 changes: 41 additions & 0 deletions cosmosdb_sql_container/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,47 @@ resource "azurerm_cosmosdb_sql_container" "this" {
throughput = var.throughput
default_ttl = var.default_ttl

dynamic "indexing_policy" {
for_each = var.indexing_policy == null ? [] : [var.indexing_policy]
# including indexing policy, if defined
content {
# including "indexing mode" by variable
indexing_mode = var.indexing_policy.indexing_mode
# including list of "included path", if present
dynamic "included_path" {
for_each = var.indexing_policy.included_paths
iterator = path
content {
path = path.value
}
}
# including list of "excluded path", if present
dynamic "excluded_path" {
for_each = var.indexing_policy.excluded_paths
iterator = path
content {
path = path.value
}
}
# including list of "composite index", if present
dynamic "composite_index" {
for_each = var.indexing_policy.composite_indexes
iterator = single_composite_index
content {
# include path component on each single composite index
dynamic "index" {
for_each = single_composite_index.value
iterator = single_index
content {
order = single_index.value.order
path = single_index.value.path
}
}
}
}
}
}

dynamic "unique_key" {
for_each = var.unique_key_paths
content {
Expand Down
26 changes: 26 additions & 0 deletions cosmosdb_sql_container/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,29 @@ variable "autoscale_settings" {
default = null
description = "Autoscale settings for collection"
}

variable "indexing_policy" {
type = object({
# The indexing strategy. Valid options are: consistent, none
indexing_mode = optional(string, "consistent"),

# One or more paths for which the indexing behaviour applies to. Either included_path or excluded_path must contain the all-path string ('/*')
included_paths = optional(list(string), ["/*"]),

# One or more paths that are excluded from indexing. Either included_path or excluded_path must contain the all-path string ('/*')
excluded_paths = optional(list(string), []),

# One or more path that define complex indexes. There can be multiple composite indexes on same indexing policy
composite_indexes = optional(list(list(object(
{
# The path of the field to be included in the composite index
path = string

# The sort of single field in indexing structure. Valid options are: ascending, descending
order = optional(string, "ascending")
}
))), []),
})
default = null
description = "The configuration of indexes on collection"
}

0 comments on commit cf14027

Please sign in to comment.