Skip to content

Commit

Permalink
Little refactor to increase readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Aris van Ommeren committed Mar 1, 2021
1 parent 388b4a4 commit 0671126
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
61 changes: 29 additions & 32 deletions azurerm/internal/services/postgres/postgresql_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,15 +585,12 @@ func resourcePostgreSQLServerUpdate(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("parsing Postgres Server ID : %v", err)
}

// Locks for scaling of replica functionality
// Locks for upscaling of replicas
mode := postgresql.CreateMode(d.Get("create_mode").(string))
source := d.Get("creation_source_server_id").(string)

primaryID := id.String()
if mode == postgresql.CreateModeReplica {
primaryID = source
primaryID = d.Get("creation_source_server_id").(string)
}

locks.ByID(primaryID)
defer locks.UnlockByID(primaryID)

Expand All @@ -602,6 +599,33 @@ func resourcePostgreSQLServerUpdate(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("expanding `sku_name` for PostgreSQL Server %s (Resource Group %q): %v", id.Name, id.ResourceGroup, err)
}

if d.HasChange("sku_name") && mode != postgresql.CreateModeReplica {
oldRaw, newRaw := d.GetChange("sku_name")
old := oldRaw.(string)
new := newRaw.(string)

if indexOfSku(old) < indexOfSku(new) {
listReplicas, err := replicasClient.ListByServer(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("request error for list of replicas for PostgreSQL Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

propertiesReplica := postgresql.ServerUpdateParameters{
Sku: sku,
}
for _, replica := range *listReplicas.Value {
future, err := client.Update(ctx, id.ResourceGroup, *replica.Name, propertiesReplica)
if err != nil {
return fmt.Errorf("upscaling PostgreSQL Server Replica %q (Resource Group %q): %+v", *replica.Name, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for update of PostgreSQL Server Replica %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}
}
}

publicAccess := postgresql.PublicNetworkAccessEnumEnabled
if v := d.Get("public_network_access_enabled"); !v.(bool) {
publicAccess = postgresql.PublicNetworkAccessEnumDisabled
Expand All @@ -628,33 +652,6 @@ func resourcePostgreSQLServerUpdate(d *schema.ResourceData, meta interface{}) er
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if d.HasChange("sku_name") && mode != postgresql.CreateModeReplica {
oldRaw, newRaw := d.GetChange("sku_name")
old := oldRaw.(string)
new := newRaw.(string)

if indexOfSku(old) < indexOfSku(new) {
propertiesReplica := postgresql.ServerUpdateParameters{
Sku: sku,
}

listReplicas, err := replicasClient.ListByServer(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("request error for list of replicas for PostgreSQL Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
for _, replica := range *listReplicas.Value {
future, err := client.Update(ctx, id.ResourceGroup, *replica.Name, propertiesReplica)
if err != nil {
return fmt.Errorf("updating PostgreSQL Server Replica %q (Resource Group %q): %+v", *replica.Name, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for update of PostgreSQL Server Replica %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}
}
}

future, err := client.Update(ctx, id.ResourceGroup, id.Name, properties)
if err != nil {
return fmt.Errorf("updating PostgreSQL Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func TestAccPostgreSQLServer_scaleReplica(t *testing.T) {
r := PostgreSQLServerResource{}
data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.scaleableReplica(data, "11", "GP_Gen5_2"),
Config: r.scaleableReplica(data, "GP_Gen5_2", "11"),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("GP_Gen5_2"),
Expand All @@ -336,7 +336,7 @@ func TestAccPostgreSQLServer_scaleReplica(t *testing.T) {
},
data.ImportStep("administrator_login_password"),
{
Config: r.scaleableReplica(data, "11", "GP_Gen5_4"),
Config: r.scaleableReplica(data, "GP_Gen5_4", "11"),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("GP_Gen5_4"),
Expand All @@ -346,7 +346,7 @@ func TestAccPostgreSQLServer_scaleReplica(t *testing.T) {
},
data.ImportStep("administrator_login_password"),
{
Config: r.scaleableReplica(data, "11", "GP_Gen5_2"),
Config: r.scaleableReplica(data, "GP_Gen5_2", "11"),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("GP_Gen5_2"),
Expand Down Expand Up @@ -890,7 +890,7 @@ resource "azurerm_postgresql_server" "test" {
`, data.RandomInteger, data.Locations.Primary, version, tlsVersion)
}

func (r PostgreSQLServerResource) scaleableReplica(data acceptance.TestData, version string, sku string) string {
func (r PostgreSQLServerResource) scaleableReplica(data acceptance.TestData, sku string, version string) string {
return fmt.Sprintf(`
%[1]s
Expand All @@ -899,13 +899,13 @@ resource "azurerm_postgresql_server" "replica" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
version = "%[3]s"
sku_name = "%[4]s"
sku_name = "%[3]s"
version = "%[4]s"
create_mode = "Replica"
creation_source_server_id = azurerm_postgresql_server.test.id
ssl_enforcement_enabled = true
}
`, r.template(data, sku, version), data.RandomInteger, version, sku)
`, r.template(data, sku, version), data.RandomInteger, sku, version)
}

0 comments on commit 0671126

Please sign in to comment.