Skip to content

Commit

Permalink
azurerm_cosmosdb_account modifying geo_location no longer trigger…
Browse files Browse the repository at this point in the history
…s a recreation of the resource #7217
  • Loading branch information
mbfrahry committed Jun 9, 2020
1 parent 2feded1 commit e13f0d8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
4 changes: 4 additions & 0 deletions azurerm/helpers/azure/location.go
Expand Up @@ -17,6 +17,10 @@ func SchemaLocationForDataSource() *schema.Schema {
return location.SchemaComputed()
}

func SchemaLocationWithoutForceNew() *schema.Schema {
return location.SchemaWithoutForceNew()
}

// azure.NormalizeLocation is a function which normalises human-readable region/location
// names (e.g. "West US") to the values used and returned by the Azure API (e.g. "westus").
// In state we track the API internal version as it is easier to go from the human form
Expand Down
12 changes: 12 additions & 0 deletions azurerm/internal/location/schema.go
Expand Up @@ -36,6 +36,18 @@ func SchemaComputed() *schema.Schema {
}
}

// Schema returns the Schema which should be used for Location fields
// where these are Required and can be changed
func SchemaWithoutForceNew() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: EnhancedValidate,
StateFunc: StateFunc,
DiffSuppressFunc: DiffSuppressFunc,
}
}

func DiffSuppressFunc(_, old, new string, _ *schema.ResourceData) bool {
return Normalize(old) == Normalize(new)
}
Expand Down
Expand Up @@ -151,7 +151,7 @@ func resourceArmCosmosDbAccount() *schema.Resource {
Computed: true,
},

"location": azure.SchemaLocation(),
"location": azure.SchemaLocationWithoutForceNew(),

"failover_priority": {
Type: schema.TypeInt,
Expand Down
Expand Up @@ -342,6 +342,39 @@ func TestAccAzureRMCosmosDBAccount_capabilitiesUpdate(t *testing.T) {
})
}

func TestAccAzureRMCosmosDBAccount_geoLocationsUpdate(t *testing.T) {
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, "GlobalDocumentDB", documentdb.Eventual),
Check: resource.ComposeAggregateTestCheckFunc(
checkAccAzureRMCosmosDBAccount_basic(data, documentdb.Eventual, 1),
),
},
data.ImportStep(),
{
Config: testAccAzureRMCosmosDBAccount_geoLocationUpdate(data, "GlobalDocumentDB", documentdb.Eventual),
Check: resource.ComposeAggregateTestCheckFunc(
checkAccAzureRMCosmosDBAccount_basic(data, documentdb.Eventual, 2),
),
},
data.ImportStep(),
{
Config: testAccAzureRMCosmosDBAccount_basic(data, "GlobalDocumentDB", documentdb.Eventual),
Check: resource.ComposeAggregateTestCheckFunc(
checkAccAzureRMCosmosDBAccount_basic(data, documentdb.Eventual, 1),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMCosmosDBAccountDestroy(s *terraform.State) error {
conn := acceptance.AzureProvider.Meta().(*clients.Client).Cosmos.DatabaseClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
Expand Down Expand Up @@ -644,6 +677,41 @@ resource "azurerm_cosmosdb_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), capeTf)
}

func testAccAzureRMCosmosDBAccount_geoLocationUpdate(data acceptance.TestData, kind documentdb.DatabaseAccountKind, consistency documentdb.DefaultConsistencyLevel) string {
return fmt.Sprintf(`
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"
consistency_policy {
consistency_level = "%s"
}
geo_location {
location = azurerm_resource_group.test.location
failover_priority = 0
}
geo_location {
location = "%s"
failover_priority = 1
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency), data.Locations.Secondary)
}

func checkAccAzureRMCosmosDBAccount_basic(data acceptance.TestData, consistency documentdb.DefaultConsistencyLevel, locationCount int) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
testCheckAzureRMCosmosDBAccountExists(data.ResourceName),
Expand Down

0 comments on commit e13f0d8

Please sign in to comment.