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

Support for spatial_index in azurerm_cosmos_gremlin_graph #12176

Merged
merged 13 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions azurerm/internal/services/cosmos/common/indexing_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[
return &indexes
}

func expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec {
func ExpandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec {
if len(input) == 0 || input[0] == nil {
return nil
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *pluginsdk.ResourceData) *documentdb.
policy.CompositeIndexes = ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v)
}

policy.SpatialIndexes = expandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{}))
policy.SpatialIndexes = ExpandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{}))

return policy
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func flattenCosmosDBIndexingPolicyIncludedPaths(input *[]documentdb.IncludedPath
return includedPaths
}

func flattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} {
func FlattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} {
if input == nil {
return []interface{}{}
}
Expand Down Expand Up @@ -236,7 +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)
result["spatial_index"] = FlattenCosmosDBIndexingPolicySpatialIndexes(indexingPolicy.SpatialIndexes)

results = append(results, result)
return results
Expand Down
46 changes: 25 additions & 21 deletions azurerm/internal/services/cosmos/common/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,7 @@ 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,
},
},
},
},
},
"spatial_index": CosmosDbIndexingPolicySpatialIndexSchema(),
},
},
}
Expand Down Expand Up @@ -249,3 +229,27 @@ func CosmosDbIndexingPolicyCompositeIndexSchema() *pluginsdk.Schema {
},
}
}

func CosmosDbIndexingPolicySpatialIndexSchema() *pluginsdk.Schema {
return &pluginsdk.Schema{
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,
},
},
},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource {
},

"composite_index": common.CosmosDbIndexingPolicyCompositeIndexSchema(),

"spatial_index": common.CosmosDbIndexingPolicySpatialIndexSchema(),
},
},
},
Expand Down Expand Up @@ -461,6 +463,8 @@ func expandAzureRmCosmosDbGrelinGraphIndexingPolicy(d *pluginsdk.ResourceData) *
policy.CompositeIndexes = common.ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v)
}

policy.SpatialIndexes = common.ExpandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{}))

if automatic, ok := input["automatic"].(bool); ok {
policy.Automatic = utils.Bool(automatic)
}
Expand Down Expand Up @@ -532,6 +536,7 @@ func flattenAzureRmCosmosDBGremlinGraphIndexingPolicy(input *documentdb.Indexing
indexPolicy["included_paths"] = pluginsdk.NewSet(pluginsdk.HashString, flattenAzureRmCosmosDBGremlinGraphIncludedPaths(input.IncludedPaths))
indexPolicy["excluded_paths"] = pluginsdk.NewSet(pluginsdk.HashString, flattenAzureRmCosmosDBGremlinGraphExcludedPaths(input.ExcludedPaths))
indexPolicy["composite_index"] = common.FlattenCosmosDBIndexingPolicyCompositeIndexes(input.CompositeIndexes)
indexPolicy["spatial_index"] = common.FlattenCosmosDBIndexingPolicySpatialIndexes(input.SpatialIndexes)

return []interface{}{indexPolicy}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ func TestAccCosmosDbGremlinGraph_indexPolicy(t *testing.T) {
),
},
data.ImportStep(),
{
Config: r.indexPolicySpatialIndex(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

Expand Down Expand Up @@ -260,6 +274,61 @@ func (CosmosGremlinGraphResource) indexPolicyCompositeIndex(data acceptance.Test
return fmt.Sprintf(`
%[1]s

resource "azurerm_cosmosdb_gremlin_graph" "test" {
name = "acctest-CGRPC-%[2]d"
resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
account_name = azurerm_cosmosdb_account.test.name
database_name = azurerm_cosmosdb_gremlin_database.test.name
partition_key_path = "/test"
throughput = 400

index_policy {
automatic = true
indexing_mode = "Consistent"

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/?"
}
}

conflict_resolution_policy {
mode = "LastWriterWins"
conflict_resolution_path = "/_ts"
}
}
`, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger)
}

func (CosmosGremlinGraphResource) indexPolicySpatialIndex(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_cosmosdb_gremlin_graph" "test" {
name = "acctest-CGRPC-%[2]d"
resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/cosmosdb_gremlin_graph.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ An `index_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 `conflict_resolution_policy` block supports the following:
Expand Down