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
cosmosdb_mongo_collection - support index
& system_index
#6426
Changes from 11 commits
e4f7ae5
1918181
e6b747d
06efb64
c201aee
9dffb47
d77aa84
a334dd0
4b828a0
7571180
d41baff
cefe6af
cc4d2dd
2170312
a7cbf7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,45 @@ func resourceArmCosmosDbMongoCollection() *schema.Resource { | |
Computed: true, | ||
ValidateFunc: validate.CosmosThroughput, | ||
}, | ||
|
||
"index": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"keys": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
|
||
"unique": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"system_index": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"keys": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
|
||
"unique": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -121,7 +160,7 @@ func resourceArmCosmosDbMongoCollectionCreate(d *schema.ResourceData, meta inter | |
MongoDBCollectionCreateUpdateProperties: &documentdb.MongoDBCollectionCreateUpdateProperties{ | ||
Resource: &documentdb.MongoDBCollectionResource{ | ||
ID: &name, | ||
Indexes: expandCosmosMongoCollectionIndexes(ttl), | ||
Indexes: expandCosmosMongoCollectionIndex(d.Get("index").(*schema.Set).List(), ttl), | ||
}, | ||
Options: map[string]*string{}, | ||
}, | ||
|
@@ -181,7 +220,7 @@ func resourceArmCosmosDbMongoCollectionUpdate(d *schema.ResourceData, meta inter | |
MongoDBCollectionCreateUpdateProperties: &documentdb.MongoDBCollectionCreateUpdateProperties{ | ||
Resource: &documentdb.MongoDBCollectionResource{ | ||
ID: &id.Collection, | ||
Indexes: expandCosmosMongoCollectionIndexes(ttl), | ||
Indexes: expandCosmosMongoCollectionIndex(d.Get("index").(*schema.Set).List(), ttl), | ||
}, | ||
Options: map[string]*string{}, | ||
}, | ||
|
@@ -264,7 +303,16 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa | |
} | ||
|
||
if props.Indexes != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to ensure these fields are always set, so we can remove the if statement here, since the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
d.Set("default_ttl_seconds", flattenCosmosMongoCollectionIndexes(props.Indexes)) | ||
indexes, systemIndexes, ttl := flattenCosmosMongoCollectionIndex(props.Indexes) | ||
if err := d.Set("default_ttl_seconds", ttl); err != nil { | ||
return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err) | ||
} | ||
if err := d.Set("index", indexes); err != nil { | ||
return fmt.Errorf("failed to set `index`: %+v", err) | ||
} | ||
if err := d.Set("system_index", systemIndexes); err != nil { | ||
return fmt.Errorf("failed to set `system_index`: %+v", err) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -307,11 +355,26 @@ func resourceArmCosmosDbMongoCollectionDelete(d *schema.ResourceData, meta inter | |
return nil | ||
} | ||
|
||
func expandCosmosMongoCollectionIndexes(defaultTtl *int) *[]documentdb.MongoIndex { | ||
outputs := make([]documentdb.MongoIndex, 0) | ||
func expandCosmosMongoCollectionIndex(indexes []interface{}, defaultTtl *int) *[]documentdb.MongoIndex { | ||
results := make([]documentdb.MongoIndex, 0) | ||
|
||
if len(indexes) != 0 { | ||
for _, v := range indexes { | ||
index := v.(map[string]interface{}) | ||
|
||
results = append(results, documentdb.MongoIndex{ | ||
Key: &documentdb.MongoIndexKeys{ | ||
Keys: utils.ExpandStringSlice(index["keys"].(*schema.Set).List()), | ||
}, | ||
Options: &documentdb.MongoIndexOptions{ | ||
Unique: utils.Bool(index["unique"].(bool)), | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
if defaultTtl != nil { | ||
outputs = append(outputs, documentdb.MongoIndex{ | ||
results = append(results, documentdb.MongoIndex{ | ||
Key: &documentdb.MongoIndexKeys{ | ||
Keys: &[]string{"_ts"}, | ||
}, | ||
|
@@ -321,24 +384,59 @@ func expandCosmosMongoCollectionIndexes(defaultTtl *int) *[]documentdb.MongoInde | |
}) | ||
} | ||
|
||
return &outputs | ||
return &results | ||
} | ||
|
||
func flattenCosmosMongoCollectionIndexes(indexes *[]documentdb.MongoIndex) *int { | ||
var ttl int | ||
for _, i := range *indexes { | ||
if key := i.Key; key != nil { | ||
var ttlInner int32 | ||
func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[string]interface{}, *[]map[string]interface{}, *int32) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method needs to account for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
indexes := make([]map[string]interface{}, 0) | ||
systemIndexes := make([]map[string]interface{}, 0) | ||
var ttl *int32 | ||
|
||
for _, v := range *input { | ||
index := map[string]interface{}{} | ||
systemIndex := map[string]interface{}{} | ||
|
||
if v.Key != nil && v.Key.Keys != nil { | ||
key := (*v.Key.Keys)[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's a crash here if there's 0 items in this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
|
||
switch key { | ||
// As `DocumentDBDefaultIndex` and `_id` cannot be updated, so they would be moved into `system_index`. | ||
case "_id": | ||
systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys) | ||
// The system index `_id` is always unique but api returns nil and it would be converted to `false` by zero-value. So it has to be manually set as `true`. | ||
systemIndex["unique"] = true | ||
|
||
systemIndexes = append(systemIndexes, systemIndex) | ||
case "DocumentDBDefaultIndex": | ||
// Updating system index `DocumentDBDefaultIndex` is not a supported scenario. | ||
systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys) | ||
|
||
isUnique := false | ||
if v.Options != nil && v.Options.Unique != nil { | ||
isUnique = *v.Options.Unique | ||
} | ||
systemIndex["unique"] = isUnique | ||
|
||
if keys := key.Keys; keys != nil && len(*keys) > 0 { | ||
k := (*keys)[0] | ||
systemIndexes = append(systemIndexes, systemIndex) | ||
case "_ts": | ||
if v.Options != nil && v.Options.ExpireAfterSeconds != nil { | ||
// As `ExpireAfterSeconds` only can be applied to system index `_ts`, so it would be set in `default_ttl_seconds`. | ||
ttl = v.Options.ExpireAfterSeconds | ||
} | ||
default: | ||
// The other settable indexes would be set in `index` | ||
index["keys"] = utils.FlattenStringSlice(v.Key.Keys) | ||
|
||
if k == "_ts" { | ||
ttl = int(ttlInner) | ||
isUnique := false | ||
if v.Options != nil && v.Options.Unique != nil { | ||
isUnique = *v.Options.Unique | ||
} | ||
index["unique"] = isUnique | ||
|
||
indexes = append(indexes, index) | ||
} | ||
} | ||
} | ||
|
||
return &ttl | ||
return &indexes, &systemIndexes, ttl | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,14 +45,33 @@ The following arguments are supported: | |
* `database_name` - (Required) The name of the Cosmos DB Mongo Database in which the Cosmos DB Mongo Collection is created. Changing this forces a new resource to be created. | ||
* `default_ttl_seconds` - (Required) The default Time To Live in seconds. If the value is `0` items are not automatically expired. | ||
* `shard_key` - (Required) The name of the key to partition on for sharding. There must not be any other unique index keys. | ||
* `index` - (Optional) One or more `index` blocks as defined below. | ||
* `throughput` - (Optional) The throughput of the MongoDB collection (RU/s). Must be set in increments of `100`. The minimum value is `400`. This must be set upon database creation otherwise it cannot be updated without a manual terraform destroy-apply. | ||
|
||
--- | ||
|
||
The `index` block supports the following: | ||
|
||
* `keys` - (Required) Specifies the list of user settable keys for each Cosmos DB Mongo Collection. | ||
|
||
* `unique` - (Optional) Is the index unique or not? Defaults to `false`. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The ID of the Cosmos DB Mongo Collection. | ||
|
||
* `system_index` - One or more `system_index` blocks as defined below. | ||
|
||
--- | ||
|
||
The `system_index` block supports the following: | ||
|
||
* `keys` - The list of system keys which are not settable for each Cosmos DB Mongo Collection. | ||
|
||
* `unique` - The unique status of the index. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a better description here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
|
||
## Timeouts | ||
|
||
The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be
system_indexes
as it'll never be used as seperate blocks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated