Skip to content

Commit

Permalink
Fix Cosmos zone redundant update (#9485)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvendingoldo authored Nov 26, 2020
1 parent b97390b commit 85fff15
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -488,17 +488,19 @@ func resourceArmCosmosDbAccountUpdate(d *schema.ResourceData, meta interface{})

// get existing locations (if exists)
resp, err := client.Get(ctx, resourceGroup, name)

if err != nil {
return fmt.Errorf("Error making Read request on AzureRM CosmosDB Account '%s': %s", name, err)
}

oldLocations := make([]documentdb.Location, 0)
oldLocationsMap := map[string]documentdb.Location{}
for _, l := range *resp.FailoverPolicies {
for _, l := range *resp.Locations {
location := documentdb.Location{
ID: l.ID,
LocationName: l.LocationName,
FailoverPriority: l.FailoverPriority,
IsZoneRedundant: l.IsZoneRedundant,
}

oldLocations = append(oldLocations, location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,36 @@ func testAccAzureRMCosmosDBAccount_zoneRedundantWith(t *testing.T, kind document
})
}

func testAccAzureRMCosmosDBAccount_zoneRedundant_updateWith(t *testing.T, kind documentdb.DatabaseAccountKind) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMCosmosDBAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCosmosDBAccount_basic(data, kind, documentdb.Eventual),
Check: resource.ComposeAggregateTestCheckFunc(
checkAccAzureRMCosmosDBAccount_basic(data, documentdb.Eventual, 1),
),
},
data.ImportStep(),
{
Config: testAccAzureRMCosmosDBAccount_zoneRedundantUpdate(data, kind, documentdb.Eventual),
Check: resource.ComposeAggregateTestCheckFunc(
checkAccAzureRMCosmosDBAccount_basic(data, documentdb.Eventual, 2),
),
},
data.ImportStep(),
},
})
}

func TestAccAzureRMCosmosDBAccount_zoneRedundant_update_mongo(t *testing.T) {
testAccAzureRMCosmosDBAccount_zoneRedundant_updateWith(t, documentdb.MongoDB)
}

func TestAccAzureRMCosmosDBAccount_update_mongo(t *testing.T) {
testAccAzureRMCosmosDBAccount_updateWith(t, documentdb.MongoDB)
}
Expand All @@ -284,7 +314,6 @@ func TestAccAzureRMCosmosDBAccount_update_global(t *testing.T) {
func TestAccAzureRMCosmosDBAccount_update_parse(t *testing.T) {
testAccAzureRMCosmosDBAccount_updateWith(t, documentdb.Parse)
}

func testAccAzureRMCosmosDBAccount_updateWith(t *testing.T, kind documentdb.DatabaseAccountKind) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test")

Expand Down Expand Up @@ -908,6 +937,63 @@ resource "azurerm_cosmosdb_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency), data.Locations.Secondary)
}

func testAccAzureRMCosmosDBAccount_zoneRedundantUpdate(data acceptance.TestData, kind documentdb.DatabaseAccountKind, consistency documentdb.DefaultConsistencyLevel) string {
return fmt.Sprintf(`
variable "geo_location" {
type = list(object({
location = string
failover_priority = string
zone_redundant = bool
}))
default = [
{
location = "%s"
failover_priority = 0
zone_redundant = false
},
{
location = "%s"
failover_priority = 1
zone_redundant = true
}
]
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-cosmos-%d"
location = "%s"
}
resource "azurerm_cosmosdb_account" "test" {
name = "acctest-ca-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
offer_type = "Standard"
kind = "%s"
enable_multiple_write_locations = true
enable_automatic_failover = true
consistency_policy {
consistency_level = "%s"
}
dynamic "geo_location" {
for_each = var.geo_location
content {
location = geo_location.value.location
failover_priority = geo_location.value.failover_priority
zone_redundant = geo_location.value.zone_redundant
}
}
}
`, data.Locations.Primary, data.Locations.Secondary, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency))
}

func testAccAzureRMCosmosDBAccount_vNetFiltersPreReqs(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down

0 comments on commit 85fff15

Please sign in to comment.