Skip to content

Commit

Permalink
azurerm_postgresql_server: deprecate sku in favour of sku_name (#5376)
Browse files Browse the repository at this point in the history
partially addresses #1500
  • Loading branch information
katbyte committed Jan 13, 2020
1 parent 8d3416a commit 15ccb15
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 177 deletions.
138 changes: 107 additions & 31 deletions azurerm/internal/services/postgres/resource_arm_postgresql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"fmt"
"log"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -49,10 +50,43 @@ func resourceArmPostgreSQLServer() *schema.Resource {

"resource_group_name": azure.SchemaResourceGroupName(),

"sku_name": {
Type: schema.TypeString,
Optional: true, // required in 2.0
Computed: true, // remove in 2.0
ConflictsWith: []string{"sku"},
ValidateFunc: validation.StringInSlice([]string{
"B_Gen4_1",
"B_Gen4_2",
"B_Gen5_1",
"B_Gen5_2",
"GP_Gen4_2",
"GP_Gen4_4",
"GP_Gen4_8",
"GP_Gen4_16",
"GP_Gen4_32",
"GP_Gen5_2",
"GP_Gen5_4",
"GP_Gen5_8",
"GP_Gen5_16",
"GP_Gen5_32",
"GP_Gen5_64",
"MO_Gen5_2",
"MO_Gen5_4",
"MO_Gen5_8",
"MO_Gen5_16",
"MO_Gen5_32",
}, false),
},

// remove in 2.0
"sku": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
Computed: true,
ConflictsWith: []string{"sku_name"},
Deprecated: "This property has been deprecated in favour of the 'sku_name' property and will be removed in version 2.0 of the provider",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -229,13 +263,6 @@ func resourceArmPostgreSQLServerCreate(d *schema.ResourceData, meta interface{})
location := azure.NormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)

adminLogin := d.Get("administrator_login").(string)
adminLoginPassword := d.Get("administrator_login_password").(string)
sslEnforcement := d.Get("ssl_enforcement").(string)
version := d.Get("version").(string)
createMode := "Default"
t := d.Get("tags").(map[string]interface{})

if features.ShouldResourcesBeImported() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
Expand All @@ -249,21 +276,31 @@ func resourceArmPostgreSQLServerCreate(d *schema.ResourceData, meta interface{})
}
}

sku := expandAzureRmPostgreSQLServerSku(d)
storageProfile := expandAzureRmPostgreSQLStorageProfile(d)
var sku *postgresql.Sku
if b, ok := d.GetOk("sku_name"); ok {
var err error
sku, err = expandServerSkuName(b.(string))
if err != nil {
return fmt.Errorf("error expanding sku_name for PostgreSQL Server %s (Resource Group %q): %v", name, resourceGroup, err)
}
} else if _, ok := d.GetOk("sku"); ok {
sku = expandAzureRmPostgreSQLServerSku(d)
} else {
return fmt.Errorf("One of `sku` or `sku_name` must be set for PostgreSQL Server %q (Resource Group %q)", name, resourceGroup)
}

properties := postgresql.ServerForCreate{
Location: &location,
Properties: &postgresql.ServerPropertiesForDefaultCreate{
AdministratorLogin: utils.String(adminLogin),
AdministratorLoginPassword: utils.String(adminLoginPassword),
Version: postgresql.ServerVersion(version),
SslEnforcement: postgresql.SslEnforcementEnum(sslEnforcement),
StorageProfile: storageProfile,
CreateMode: postgresql.CreateMode(createMode),
AdministratorLogin: utils.String(d.Get("administrator_login").(string)),
AdministratorLoginPassword: utils.String(d.Get("administrator_login_password").(string)),
Version: postgresql.ServerVersion(d.Get("version").(string)),
SslEnforcement: postgresql.SslEnforcementEnum(d.Get("ssl_enforcement").(string)),
StorageProfile: expandAzureRmPostgreSQLStorageProfile(d),
CreateMode: postgresql.CreateMode("Default"),
},
Sku: sku,
Tags: tags.Expand(t),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

future, err := client.Create(ctx, resourceGroup, name, properties)
Expand Down Expand Up @@ -299,22 +336,28 @@ func resourceArmPostgreSQLServerUpdate(d *schema.ResourceData, meta interface{})
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

adminLoginPassword := d.Get("administrator_login_password").(string)
sslEnforcement := d.Get("ssl_enforcement").(string)
version := d.Get("version").(string)
sku := expandAzureRmPostgreSQLServerSku(d)
storageProfile := expandAzureRmPostgreSQLStorageProfile(d)
t := d.Get("tags").(map[string]interface{})
var sku *postgresql.Sku
if b, ok := d.GetOk("sku_name"); ok {
var err error
sku, err = expandServerSkuName(b.(string))
if err != nil {
return fmt.Errorf("error expanding sku_name for PostgreSQL Server %q (Resource Group %q): %v", name, resourceGroup, err)
}
} else if _, ok := d.GetOk("sku"); ok {
sku = expandAzureRmPostgreSQLServerSku(d)
} else {
return fmt.Errorf("One of `sku` or `sku_name` must be set for PostgreSQL Server %q (Resource Group %q)", name, resourceGroup)
}

properties := postgresql.ServerUpdateParameters{
ServerUpdateParametersProperties: &postgresql.ServerUpdateParametersProperties{
StorageProfile: storageProfile,
AdministratorLoginPassword: utils.String(adminLoginPassword),
Version: postgresql.ServerVersion(version),
SslEnforcement: postgresql.SslEnforcementEnum(sslEnforcement),
AdministratorLoginPassword: utils.String(d.Get("administrator_login_password").(string)),
Version: postgresql.ServerVersion(d.Get("version").(string)),
SslEnforcement: postgresql.SslEnforcementEnum(d.Get("ssl_enforcement").(string)),
StorageProfile: expandAzureRmPostgreSQLStorageProfile(d),
},
Sku: sku,
Tags: tags.Expand(t),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

future, err := client.Update(ctx, resourceGroup, name, properties)
Expand All @@ -330,7 +373,6 @@ func resourceArmPostgreSQLServerUpdate(d *schema.ResourceData, meta interface{})
if err != nil {
return fmt.Errorf("Error retrieving PostgreSQL Server %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read PostgreSQL Server %s (resource group %s) ID", name, resourceGroup)
}
Expand Down Expand Up @@ -377,6 +419,9 @@ func resourceArmPostgreSQLServerRead(d *schema.ResourceData, meta interface{}) e
if err := d.Set("sku", flattenPostgreSQLServerSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
}
if sku := resp.Sku; sku != nil {
d.Set("sku_name", sku.Name)
}

if err := d.Set("storage_profile", flattenPostgreSQLStorageProfile(resp.StorageProfile)); err != nil {
return fmt.Errorf("Error setting `storage_profile`: %+v", err)
Expand Down Expand Up @@ -420,6 +465,37 @@ func resourceArmPostgreSQLServerDelete(d *schema.ResourceData, meta interface{})
return nil
}

func expandServerSkuName(skuName string) (*postgresql.Sku, error) {
parts := strings.Split(skuName, "_")
if len(parts) != 3 {
return nil, fmt.Errorf("sku_name (%s) has the wrong number of parts (%d) after splitting on _", skuName, len(parts))
}

var tier postgresql.SkuTier
switch parts[0] {
case "B":
tier = postgresql.Basic
case "GP":
tier = postgresql.GeneralPurpose
case "MO":
tier = postgresql.MemoryOptimized
default:
return nil, fmt.Errorf("sku_name %s has unknown sku tier %s", skuName, parts[0])
}

capacity, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("cannot convert skuname %s capcity %s to int", skuName, parts[2])
}

return &postgresql.Sku{
Name: utils.String(skuName),
Tier: tier,
Capacity: utils.Int32(int32(capacity)),
Family: utils.String(parts[1]),
}, nil
}

func expandAzureRmPostgreSQLServerSku(d *schema.ResourceData) *postgresql.Sku {
skus := d.Get("sku").([]interface{})
sku := skus[0].(map[string]interface{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,7 @@ resource "azurerm_postgresql_server" "test" {
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku {
name = "GP_Gen5_2"
capacity = 2
tier = "GeneralPurpose"
family = "Gen5"
}
sku_name = "GP_Gen5_2"
storage_profile {
storage_mb = 51200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,7 @@ resource "azurerm_postgresql_server" "test" {
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku {
name = "GP_Gen5_2"
capacity = 2
tier = "GeneralPurpose"
family = "Gen5"
}
sku_name = "GP_Gen5_2"
storage_profile {
storage_mb = 51200
Expand Down Expand Up @@ -240,12 +235,7 @@ resource "azurerm_postgresql_server" "test" {
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku {
name = "GP_Gen5_2"
capacity = 2
tier = "GeneralPurpose"
family = "Gen5"
}
sku_name = "GP_Gen5_2"
storage_profile {
storage_mb = 51200
Expand Down Expand Up @@ -281,12 +271,7 @@ resource "azurerm_postgresql_server" "test" {
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku {
name = "GP_Gen5_2"
capacity = 2
tier = "GeneralPurpose"
family = "Gen5"
}
sku_name = "GP_Gen5_2"
storage_profile {
storage_mb = 51200
Expand Down Expand Up @@ -322,12 +307,7 @@ resource "azurerm_postgresql_server" "test" {
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku {
name = "GP_Gen5_2"
capacity = 2
tier = "GeneralPurpose"
family = "Gen5"
}
sku_name = "GP_Gen5_2"
storage_profile {
storage_mb = 51200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,7 @@ resource "azurerm_postgresql_server" "test" {
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku {
name = "GP_Gen5_2"
capacity = 2
tier = "GeneralPurpose"
family = "Gen5"
}
sku_name = "GP_Gen5_2"
storage_profile {
storage_mb = 51200
Expand Down
Loading

0 comments on commit 15ccb15

Please sign in to comment.