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_account modifying geo_location no longer triggers a recreation of the resource #7217

Merged
merged 2 commits into from Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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