diff --git a/azurerm/helpers/azure/location.go b/azurerm/helpers/azure/location.go index d28793566d57..bc247cace81d 100644 --- a/azurerm/helpers/azure/location.go +++ b/azurerm/helpers/azure/location.go @@ -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 diff --git a/azurerm/internal/location/schema.go b/azurerm/internal/location/schema.go index 21cef50f02de..97552711582b 100644 --- a/azurerm/internal/location/schema.go +++ b/azurerm/internal/location/schema.go @@ -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) } diff --git a/azurerm/internal/services/cosmos/cosmosdb_account_resource.go b/azurerm/internal/services/cosmos/cosmosdb_account_resource.go index 74fd6573ebb7..f2fe3cac9593 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_account_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_account_resource.go @@ -151,7 +151,7 @@ func resourceArmCosmosDbAccount() *schema.Resource { Computed: true, }, - "location": azure.SchemaLocation(), + "location": azure.SchemaLocationWithoutForceNew(), "failover_priority": { Type: schema.TypeInt, diff --git a/azurerm/internal/services/cosmos/tests/cosmosdb_account_resource_test.go b/azurerm/internal/services/cosmos/tests/cosmosdb_account_resource_test.go index 3caaf699dd94..15899aac35e0 100644 --- a/azurerm/internal/services/cosmos/tests/cosmosdb_account_resource_test.go +++ b/azurerm/internal/services/cosmos/tests/cosmosdb_account_resource_test.go @@ -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 @@ -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),