Skip to content

Commit e13f0d8

Browse files
authored
azurerm_cosmosdb_account modifying geo_location no longer triggers a recreation of the resource #7217
1 parent 2feded1 commit e13f0d8

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

azurerm/helpers/azure/location.go

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func SchemaLocationForDataSource() *schema.Schema {
1717
return location.SchemaComputed()
1818
}
1919

20+
func SchemaLocationWithoutForceNew() *schema.Schema {
21+
return location.SchemaWithoutForceNew()
22+
}
23+
2024
// azure.NormalizeLocation is a function which normalises human-readable region/location
2125
// names (e.g. "West US") to the values used and returned by the Azure API (e.g. "westus").
2226
// In state we track the API internal version as it is easier to go from the human form

azurerm/internal/location/schema.go

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ func SchemaComputed() *schema.Schema {
3636
}
3737
}
3838

39+
// Schema returns the Schema which should be used for Location fields
40+
// where these are Required and can be changed
41+
func SchemaWithoutForceNew() *schema.Schema {
42+
return &schema.Schema{
43+
Type: schema.TypeString,
44+
Required: true,
45+
ValidateFunc: EnhancedValidate,
46+
StateFunc: StateFunc,
47+
DiffSuppressFunc: DiffSuppressFunc,
48+
}
49+
}
50+
3951
func DiffSuppressFunc(_, old, new string, _ *schema.ResourceData) bool {
4052
return Normalize(old) == Normalize(new)
4153
}

azurerm/internal/services/cosmos/cosmosdb_account_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func resourceArmCosmosDbAccount() *schema.Resource {
151151
Computed: true,
152152
},
153153

154-
"location": azure.SchemaLocation(),
154+
"location": azure.SchemaLocationWithoutForceNew(),
155155

156156
"failover_priority": {
157157
Type: schema.TypeInt,

azurerm/internal/services/cosmos/tests/cosmosdb_account_resource_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,39 @@ func TestAccAzureRMCosmosDBAccount_capabilitiesUpdate(t *testing.T) {
342342
})
343343
}
344344

345+
func TestAccAzureRMCosmosDBAccount_geoLocationsUpdate(t *testing.T) {
346+
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test")
347+
348+
resource.ParallelTest(t, resource.TestCase{
349+
PreCheck: func() { acceptance.PreCheck(t) },
350+
Providers: acceptance.SupportedProviders,
351+
CheckDestroy: testCheckAzureRMCosmosDBAccountDestroy,
352+
Steps: []resource.TestStep{
353+
{
354+
Config: testAccAzureRMCosmosDBAccount_basic(data, "GlobalDocumentDB", documentdb.Eventual),
355+
Check: resource.ComposeAggregateTestCheckFunc(
356+
checkAccAzureRMCosmosDBAccount_basic(data, documentdb.Eventual, 1),
357+
),
358+
},
359+
data.ImportStep(),
360+
{
361+
Config: testAccAzureRMCosmosDBAccount_geoLocationUpdate(data, "GlobalDocumentDB", documentdb.Eventual),
362+
Check: resource.ComposeAggregateTestCheckFunc(
363+
checkAccAzureRMCosmosDBAccount_basic(data, documentdb.Eventual, 2),
364+
),
365+
},
366+
data.ImportStep(),
367+
{
368+
Config: testAccAzureRMCosmosDBAccount_basic(data, "GlobalDocumentDB", documentdb.Eventual),
369+
Check: resource.ComposeAggregateTestCheckFunc(
370+
checkAccAzureRMCosmosDBAccount_basic(data, documentdb.Eventual, 1),
371+
),
372+
},
373+
data.ImportStep(),
374+
},
375+
})
376+
}
377+
345378
func testCheckAzureRMCosmosDBAccountDestroy(s *terraform.State) error {
346379
conn := acceptance.AzureProvider.Meta().(*clients.Client).Cosmos.DatabaseClient
347380
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
@@ -644,6 +677,41 @@ resource "azurerm_cosmosdb_account" "test" {
644677
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), capeTf)
645678
}
646679

680+
func testAccAzureRMCosmosDBAccount_geoLocationUpdate(data acceptance.TestData, kind documentdb.DatabaseAccountKind, consistency documentdb.DefaultConsistencyLevel) string {
681+
return fmt.Sprintf(`
682+
provider "azurerm" {
683+
features {}
684+
}
685+
686+
resource "azurerm_resource_group" "test" {
687+
name = "acctestRG-cosmos-%d"
688+
location = "%s"
689+
}
690+
691+
resource "azurerm_cosmosdb_account" "test" {
692+
name = "acctest-ca-%d"
693+
location = azurerm_resource_group.test.location
694+
resource_group_name = azurerm_resource_group.test.name
695+
offer_type = "Standard"
696+
kind = "%s"
697+
698+
consistency_policy {
699+
consistency_level = "%s"
700+
}
701+
702+
geo_location {
703+
location = azurerm_resource_group.test.location
704+
failover_priority = 0
705+
}
706+
707+
geo_location {
708+
location = "%s"
709+
failover_priority = 1
710+
}
711+
}
712+
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency), data.Locations.Secondary)
713+
}
714+
647715
func checkAccAzureRMCosmosDBAccount_basic(data acceptance.TestData, consistency documentdb.DefaultConsistencyLevel, locationCount int) resource.TestCheckFunc {
648716
return resource.ComposeTestCheckFunc(
649717
testCheckAzureRMCosmosDBAccountExists(data.ResourceName),

0 commit comments

Comments
 (0)