-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e4f7ae5
Improve code for cosmosdb
1918181
Update code
e6b747d
Update code
06efb64
Update code
c201aee
Update code
9dffb47
Update code
d77aa84
Update code
a334dd0
Update code
4b828a0
Update code
7571180
Update code
d41baff
Update code
cefe6af
Update code
cc4d2dd
Update code
2170312
Update code
a7cbf7a
Merge remote-tracking branch 'upstream/master' into cosmosdb
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_indexes": { | ||
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{}, | ||
}, | ||
|
@@ -263,8 +302,15 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa | |
d.Set("shard_key", k) | ||
} | ||
|
||
if props.Indexes != nil { | ||
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_indexes", systemIndexes); err != nil { | ||
return fmt.Errorf("failed to set `system_indexes`: %+v", err) | ||
} | ||
} | ||
|
||
|
@@ -307,11 +353,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 +382,62 @@ 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) { | ||
indexes := make([]map[string]interface{}, 0) | ||
systemIndexes := make([]map[string]interface{}, 0) | ||
var ttl *int32 | ||
if input == nil { | ||
return &indexes, &systemIndexes, ttl | ||
} | ||
|
||
for _, v := range *input { | ||
index := map[string]interface{}{} | ||
systemIndex := map[string]interface{}{} | ||
|
||
if v.Key != nil && v.Key.Keys != nil && len(*v.Key.Keys) > 0 { | ||
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_indexes`. | ||
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 | ||
|
||
if keys := key.Keys; keys != nil && len(*keys) > 0 { | ||
k := (*keys)[0] | ||
systemIndexes = append(systemIndexes, systemIndex) | ||
case "DocumentDBDefaultIndex": | ||
// Updating system index `DocumentDBDefaultIndex` is not a supported scenario. | ||
systemIndex["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 | ||
} | ||
systemIndex["unique"] = isUnique | ||
|
||
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) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this method needs to account for
input
being nil hereThere 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