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

azurerm_cosmosdb_mongo_collection - correctly read the _id index back when mongo 3.6 #8690

Merged
merged 8 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
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,12 @@ 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)
}
d.Set("database_name", id.MongodbDatabaseName)
if props := resp.MongoDBCollectionGetProperties; props != nil {
if res := props.Resource; res != nil {
d.Set("name", res.ID)
Expand All @@ -313,8 +320,16 @@ 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 {
for _, v := range *accProps.Capabilities {
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
if *v.Name == "EnableMongo" {
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
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 +414,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 +437,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 {
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
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)
}