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 composite_index and partition_key_version in azurerm_cosmosdb_gremlin_graph #11693

Merged
merged 5 commits into from
Jun 10, 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 @@ -45,7 +45,7 @@ func expandAzureRmCosmosDBIndexingPolicyExcludedPaths(input []interface{}) *[]do
return &paths
}

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

for _, i := range input {
Expand Down Expand Up @@ -83,7 +83,7 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *pluginsdk.ResourceData) *documentdb.
}

if v, ok := input["composite_index"].([]interface{}); ok {
policy.CompositeIndexes = expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v)
policy.CompositeIndexes = ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v)
}
return policy
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func flattenCosmosDBIndexingPolicyCompositeIndex(input []documentdb.CompositePat
return indexPairs
}

func flattenCosmosDBIndexingPolicyCompositeIndexes(input *[][]documentdb.CompositePath) []interface{} {
func FlattenCosmosDBIndexingPolicyCompositeIndexes(input *[][]documentdb.CompositePath) []interface{} {
if input == nil {
return []interface{}{}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func FlattenAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPol
result["indexing_mode"] = strings.Title(string(indexingPolicy.IndexingMode))
result["included_path"] = flattenCosmosDBIndexingPolicyIncludedPaths(indexingPolicy.IncludedPaths)
result["excluded_path"] = flattenCosmosDBIndexingPolicyExcludedPaths(indexingPolicy.ExcludedPaths)
result["composite_index"] = flattenCosmosDBIndexingPolicyCompositeIndexes(indexingPolicy.CompositeIndexes)
result["composite_index"] = FlattenCosmosDBIndexingPolicyCompositeIndexes(indexingPolicy.CompositeIndexes)

results = append(results, result)
return results
Expand Down
74 changes: 39 additions & 35 deletions azurerm/internal/services/cosmos/common/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,41 +150,7 @@ func CosmosDbIndexingPolicySchema() *pluginsdk.Schema {
},
},
},
"composite_index": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"index": {
Type: pluginsdk.TypeList,
MinItems: 1,
Required: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"path": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
// `order` case changes from 2020-04-01 to 2021-01-15, issue opened:https://github.com/Azure/azure-rest-api-specs/issues/14051
// todo: change to SDK constants and remove translation code in 3.0
"order": {
Type: pluginsdk.TypeString,
Required: true,
// Workaround for Azure/azure-rest-api-specs#11222
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice(
[]string{
"Ascending",
"Descending",
}, false),
},
},
},
},
},
},
},
"composite_index": CosmosDbIndexingPolicyCompositeIndexSchema(),
},
},
}
Expand Down Expand Up @@ -223,3 +189,41 @@ func ConflictResolutionPolicy() *pluginsdk.Schema {
},
}
}

func CosmosDbIndexingPolicyCompositeIndexSchema() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"index": {
Type: pluginsdk.TypeList,
MinItems: 1,
Required: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"path": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
// `order` case changes from 2020-04-01 to 2021-01-15, issue opened:https://github.com/Azure/azure-rest-api-specs/issues/14051
// todo: change to SDK constants and remove translation code in 3.0
"order": {
Type: pluginsdk.TypeString,
Required: true,
// Workaround for Azure/azure-rest-api-specs#11222
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice(
[]string{
"Ascending",
"Descending",
}, false),
},
},
},
},
},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,18 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource {
ValidateFunc: validation.StringIsNotEmpty,
},

"partition_key_version": {
Type: pluginsdk.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 2),
},

"index_policy": {
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"automatic": {
Expand All @@ -118,6 +126,7 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource {
"included_paths": {
Type: pluginsdk.TypeSet,
Optional: true,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
Expand All @@ -128,12 +137,15 @@ func resourceCosmosDbGremlinGraph() *pluginsdk.Resource {
"excluded_paths": {
Type: pluginsdk.TypeSet,
Optional: true,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
Set: pluginsdk.HashString,
},

"composite_index": common.CosmosDbIndexingPolicyCompositeIndexSchema(),
},
},
},
Expand Down Expand Up @@ -200,6 +212,10 @@ func resourceCosmosDbGremlinGraphCreate(d *pluginsdk.ResourceData, meta interfac
if partitionkeypaths != "" {
db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey = &documentdb.ContainerPartitionKey{
Paths: &[]string{partitionkeypaths},
Kind: documentdb.PartitionKindHash,
}
if partitionKeyVersion, ok := d.GetOk("partition_key_version"); ok {
db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey.Version = utils.Int32(int32(partitionKeyVersion.(int)))
}
}

Expand Down Expand Up @@ -278,6 +294,11 @@ func resourceCosmosDbGremlinGraphUpdate(d *pluginsdk.ResourceData, meta interfac
if partitionkeypaths != "" {
db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey = &documentdb.ContainerPartitionKey{
Paths: &[]string{partitionkeypaths},
Kind: documentdb.PartitionKindHash,
}

if partitionKeyVersion, ok := d.GetOk("partition_key_version"); ok {
db.GremlinGraphCreateUpdateProperties.Resource.PartitionKey.Version = utils.Int32(int32(partitionKeyVersion.(int)))
}
}

Expand Down Expand Up @@ -354,6 +375,10 @@ func resourceCosmosDbGremlinGraphRead(d *pluginsdk.ResourceData, meta interface{
d.Set("partition_key_path", (*paths)[0])
}
}

if version := pk.Version; version != nil {
d.Set("partition_key_version", version)
}
}

if ip := props.IndexingPolicy; ip != nil {
Expand Down Expand Up @@ -432,6 +457,9 @@ func expandAzureRmCosmosDbGrelinGraphIndexingPolicy(d *pluginsdk.ResourceData) *
IncludedPaths: expandAzureRmCosmosDbGrelimGraphIncludedPath(input),
ExcludedPaths: expandAzureRmCosmosDbGremlinGraphExcludedPath(input),
}
if v, ok := input["composite_index"].([]interface{}); ok {
policy.CompositeIndexes = common.ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v)
}

if automatic, ok := input["automatic"].(bool); ok {
policy.Automatic = utils.Bool(automatic)
Expand Down Expand Up @@ -503,6 +531,7 @@ func flattenAzureRmCosmosDBGremlinGraphIndexingPolicy(input *documentdb.Indexing
indexPolicy["indexing_mode"] = strings.Title(string(input.IndexingMode))
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)

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

Expand Down Expand Up @@ -137,6 +144,23 @@ func TestAccCosmosDbGremlinGraph_autoscale(t *testing.T) {
})
}

func TestAccCosmosDbGremlinGraph_partition_key_version(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_gremlin_graph", "test")
r := CosmosGremlinGraphResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{

Config: r.partition_key_version(data, 2),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("partition_key_version").HasValue("2"),
),
},
data.ImportStep(),
})
}

func (t CosmosGremlinGraphResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.GremlinGraphID(state.ID)
if err != nil {
Expand All @@ -162,13 +186,6 @@ resource "azurerm_cosmosdb_gremlin_graph" "test" {
database_name = azurerm_cosmosdb_gremlin_database.test.name
partition_key_path = "/test"
throughput = 400

index_policy {
automatic = true
indexing_mode = "Consistent"
included_paths = ["/*"]
excluded_paths = ["/\"_etag\"/?"]
}
}
`, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger)
}
Expand All @@ -183,13 +200,6 @@ resource "azurerm_cosmosdb_gremlin_graph" "import" {
account_name = azurerm_cosmosdb_account.test.name
database_name = azurerm_cosmosdb_gremlin_database.test.name
partition_key_path = azurerm_cosmosdb_gremlin_graph.test.partition_key_path

index_policy {
automatic = true
indexing_mode = "Consistent"
included_paths = ["/*"]
excluded_paths = ["/\"_etag\"/?"]
}
}
`, r.basic(data))
}
Expand Down Expand Up @@ -246,6 +256,53 @@ resource "azurerm_cosmosdb_gremlin_graph" "test" {
`, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger)
}

func (CosmosGremlinGraphResource) indexPolicyCompositeIndex(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
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"
}
}
}

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

func (CosmosGremlinGraphResource) update(data acceptance.TestData, throughput int, defaultTTL int) string {
return fmt.Sprintf(`
%[1]s
Expand Down Expand Up @@ -297,3 +354,18 @@ resource "azurerm_cosmosdb_gremlin_graph" "test" {
}
`, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger, maxThroughput)
}

func (CosmosGremlinGraphResource) partition_key_version(data acceptance.TestData, version int) 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
account_name = azurerm_cosmosdb_account.test.name
database_name = azurerm_cosmosdb_gremlin_database.test.name
partition_key_path = "/test"
partition_key_version = %[3]d
}
`, CosmosGremlinDatabaseResource{}.basic(data), data.RandomInteger, version)
}
Loading