Skip to content

Commit

Permalink
Upgrade Cosmos version from 2020-04-01-preview to 2021-01-15 (#10926
Browse files Browse the repository at this point in the history
)

Co-authored-by: kt <[email protected]>
  • Loading branch information
yupwei68 and katbyte authored Apr 27, 2021
1 parent e6483aa commit 5c91f84
Show file tree
Hide file tree
Showing 62 changed files with 1,678 additions and 289 deletions.
2 changes: 1 addition & 1 deletion azurerm/internal/services/cosmos/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"log"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down
15 changes: 8 additions & 7 deletions azurerm/internal/services/cosmos/common/indexing_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package common

import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func expandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[

index := documentdb.CompositePath{
Path: utils.String(data["path"].(string)),
Order: documentdb.CompositePathSortOrder(data["order"].(string)),
Order: documentdb.CompositePathSortOrder(strings.ToLower(data["order"].(string))),
}
indexPairs = append(indexPairs, index)
}
Expand All @@ -73,7 +74,7 @@ func ExpandAzureRmCosmosDbIndexingPolicy(d *schema.ResourceData) *documentdb.Ind
}
input := i[0].(map[string]interface{})
policy := &documentdb.IndexingPolicy{}
policy.IndexingMode = documentdb.IndexingMode(input["indexing_mode"].(string))
policy.IndexingMode = documentdb.IndexingMode(strings.ToLower(input["indexing_mode"].(string)))
if v, ok := input["included_path"].([]interface{}); ok {
policy.IncludedPaths = expandAzureRmCosmosDBIndexingPolicyIncludedPaths(v)
}
Expand Down Expand Up @@ -123,7 +124,7 @@ func flattenCosmosDBIndexingPolicyCompositeIndex(input []documentdb.CompositePat

block := make(map[string]interface{})
block["path"] = path
block["order"] = string(v.Order)
block["order"] = strings.Title(string(v.Order))
indexPairs = append(indexPairs, block)
}

Expand Down Expand Up @@ -169,7 +170,7 @@ func FlattenAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPol
}

result := make(map[string]interface{})
result["indexing_mode"] = string(indexingPolicy.IndexingMode)
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)
Expand All @@ -186,11 +187,11 @@ func ValidateAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPo
// Ensure includedPaths or excludedPaths are not set if indexingMode is "None".
if indexingPolicy.IndexingMode == documentdb.None {
if indexingPolicy.IncludedPaths != nil {
return fmt.Errorf("included_path must not be set if indexing_mode is %q", documentdb.None)
return fmt.Errorf("included_path must not be set if indexing_mode is %q", strings.Title(string(documentdb.None)))
}

if indexingPolicy.ExcludedPaths != nil {
return fmt.Errorf("excluded_path must not be set if indexing_mode is %q", documentdb.None)
return fmt.Errorf("excluded_path must not be set if indexing_mode is %q", strings.Title(string(documentdb.None)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down
2 changes: 1 addition & 1 deletion azurerm/internal/services/cosmos/common/ip_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down
2 changes: 1 addition & 1 deletion azurerm/internal/services/cosmos/common/ip_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
)

var (
Expand Down
21 changes: 9 additions & 12 deletions azurerm/internal/services/cosmos/common/schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package common

import (
"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cosmos/validate"
Expand Down Expand Up @@ -93,13 +93,6 @@ func DatabaseAutoscaleSettingsSchema() *schema.Schema {
}
}

func ContainerAutoscaleSettingsSchema() *schema.Schema {
autoscaleSettingsDatabaseSchema := DatabaseAutoscaleSettingsSchema()
autoscaleSettingsDatabaseSchema.RequiredWith = []string{"partition_key_path"}

return autoscaleSettingsDatabaseSchema
}

func MongoCollectionAutoscaleSettingsSchema() *schema.Schema {
autoscaleSettingsDatabaseSchema := DatabaseAutoscaleSettingsSchema()
autoscaleSettingsDatabaseSchema.RequiredWith = []string{"shard_key"}
Expand All @@ -116,14 +109,16 @@ func CosmosDbIndexingPolicySchema() *schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// `automatic` is excluded as it is deprecated; see https://stackoverflow.com/a/58721386
// `indexing_mode` case changes from 2020-04-01 to 2021-01-15 issue https://github.com/Azure/azure-rest-api-specs/issues/14051
// todo: change to SDK constants and remove translation code in 3.0
"indexing_mode": {
Type: schema.TypeString,
Optional: true,
Default: documentdb.Consistent,
DiffSuppressFunc: suppress.CaseDifference, // Open issue https://github.com/Azure/azure-sdk-for-go/issues/6603
ValidateFunc: validation.StringInSlice([]string{
string(documentdb.Consistent),
string(documentdb.None),
"Consistent",
"None",
}, false),
},

Expand Down Expand Up @@ -171,15 +166,17 @@ func CosmosDbIndexingPolicySchema() *schema.Schema {
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: schema.TypeString,
Required: true,
// Workaround for Azure/azure-rest-api-specs#11222
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice(
[]string{
string(documentdb.Ascending),
string(documentdb.Descending),
"Ascending",
"Descending",
}, false),
},
},
Expand Down
2 changes: 1 addition & 1 deletion azurerm/internal/services/cosmos/common/throughput.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cosmos/common"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check"
Expand Down
29 changes: 16 additions & 13 deletions azurerm/internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/cosmos-db/mgmt/2020-04-01-preview/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -208,6 +208,7 @@ func resourceCosmosDbAccount() *schema.Resource {
"zone_redundant": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
Expand Down Expand Up @@ -821,21 +822,23 @@ func resourceCosmosDbAccountApiUpsert(client *documentdb.DatabaseAccountsClient,
return nil, "", fmt.Errorf("Error reading CosmosDB Account %q after create/update (Resource Group %q): %+v", name, resourceGroup, err2)
}
status := "Succeeded"
locations := append(*resp.ReadLocations, *resp.WriteLocations...)
for _, l := range locations {
if status = *l.ProvisioningState; status == "Creating" || status == "Updating" || status == "Deleting" {
break // return the first non successful status.
if props := resp.DatabaseAccountGetProperties; props != nil {
locations := append(*props.ReadLocations, *props.WriteLocations...)
for _, l := range locations {
if status = *l.ProvisioningState; status == "Creating" || status == "Updating" || status == "Deleting" {
break // return the first non successful status.
}
}
}

for _, desiredLocation := range *account.Locations {
for index, l := range locations {
if azure.NormalizeLocation(*desiredLocation.LocationName) == azure.NormalizeLocation(*l.LocationName) {
break
}
for _, desiredLocation := range *account.Locations {
for index, l := range locations {
if azure.NormalizeLocation(*desiredLocation.LocationName) == azure.NormalizeLocation(*l.LocationName) {
break
}

if (index + 1) == len(locations) {
return resp, "Updating", nil
if (index + 1) == len(locations) {
return resp, "Updating", nil
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ resource "azurerm_cosmosdb_account" "test" {
kind = "MongoDB"
offer_type = "Standard"
capabilities {
name = "EnableMongo"
}
consistency_policy {
consistency_level = "BoundedStaleness"
}
Expand Down
Loading

0 comments on commit 5c91f84

Please sign in to comment.