Skip to content

Commit

Permalink
azurerm_cosmosdb_mongo_collection - correctly read the _id index …
Browse files Browse the repository at this point in the history
…back when mongo 3.6 (#8690)

Co-authored-by: jackofallops <[email protected]>
Fix #8660
  • Loading branch information
yupwei68 authored Dec 30, 2020
1 parent 836beb8 commit 6bd58af
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func resourceArmCosmosDbMongoCollectionUpdate(d *schema.ResourceData, meta inter

func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Cosmos.MongoDbClient
accClient := meta.(*clients.Client).Cosmos.DatabaseClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand All @@ -301,6 +302,11 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa
d.Set("resource_group_name", id.ResourceGroup)
d.Set("account_name", id.DatabaseAccountName)
d.Set("database_name", id.MongodbDatabaseName)

accResp, err := accClient.Get(ctx, id.ResourceGroup, id.DatabaseAccountName)
if err != nil {
return fmt.Errorf("reading Cosmos Account %q : %+v", id.DatabaseAccountName, err)
}
if props := resp.MongoDBCollectionGetProperties; props != nil {
if res := props.Resource; res != nil {
d.Set("name", res.ID)
Expand All @@ -313,8 +319,18 @@ func resourceArmCosmosDbMongoCollectionRead(d *schema.ResourceData, meta interfa
for k := range res.ShardKey {
d.Set("shard_key", k)
}
accountIsVersion36 := false
if accProps := accResp.DatabaseAccountGetProperties; accProps != nil {
if capabilities := accProps.Capabilities; capabilities != nil {
for _, v := range *capabilities {
if v.Name != nil && *v.Name == "EnableMongo" {
accountIsVersion36 = true
}
}
}
}

indexes, systemIndexes, ttl := flattenCosmosMongoCollectionIndex(res.Indexes)
indexes, systemIndexes, ttl := flattenCosmosMongoCollectionIndex(res.Indexes, accountIsVersion36)
if err := d.Set("default_ttl_seconds", ttl); err != nil {
return fmt.Errorf("failed to set `default_ttl_seconds`: %+v", err)
}
Expand Down Expand Up @@ -399,7 +415,7 @@ func expandCosmosMongoCollectionIndex(indexes []interface{}, defaultTtl *int) *[
return &results
}

func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[string]interface{}, *[]map[string]interface{}, *int32) {
func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex, accountIsVersion36 bool) (*[]map[string]interface{}, *[]map[string]interface{}, *int32) {
indexes := make([]map[string]interface{}, 0)
systemIndexes := make([]map[string]interface{}, 0)
var ttl *int32
Expand All @@ -422,6 +438,13 @@ func flattenCosmosMongoCollectionIndex(input *[]documentdb.MongoIndex) (*[]map[s
systemIndex["unique"] = true

systemIndexes = append(systemIndexes, systemIndex)

if accountIsVersion36 {
index["keys"] = utils.FlattenStringSlice(v.Key.Keys)
index["unique"] = true
indexes = append(indexes, index)
}

case "DocumentDBDefaultIndex":
// Updating system index `DocumentDBDefaultIndex` is not a supported scenario.
systemIndex["keys"] = utils.FlattenStringSlice(v.Key.Keys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"testing"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
Expand Down Expand Up @@ -179,6 +180,25 @@ func TestAccAzureRMCosmosDbMongoCollection_autoscale(t *testing.T) {
})
}

func TestAccAzureRMCosmosDbMongoCollection_ver36(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_mongo_collection", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMCosmosDbMongoCollectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCosmosDbMongoCollection_ver36(data),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbMongoCollectionExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMCosmosDbMongoCollectionDestroy(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Cosmos.MongoDbClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
Expand Down Expand Up @@ -343,3 +363,27 @@ resource "azurerm_cosmosdb_mongo_collection" "test" {
}
`, testAccAzureRMCosmosDbMongoDatabase_basic(data), data.RandomInteger)
}

func testAccAzureRMCosmosDbMongoCollection_ver36(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
resource "azurerm_cosmosdb_mongo_database" "test" {
name = "acctest-%[2]d"
resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
account_name = azurerm_cosmosdb_account.test.name
}
resource "azurerm_cosmosdb_mongo_collection" "test" {
name = "acctest-%[2]d"
resource_group_name = azurerm_cosmosdb_mongo_database.test.resource_group_name
account_name = azurerm_cosmosdb_mongo_database.test.account_name
database_name = azurerm_cosmosdb_mongo_database.test.name
index {
keys = ["_id"]
unique = true
}
}
`, testAccAzureRMCosmosDBAccount_capabilities(data, documentdb.MongoDB, []string{"EnableMongo"}), data.RandomInteger)
}

0 comments on commit 6bd58af

Please sign in to comment.