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_mysql_server: deprecate sku in favour of sku_name #5377

Merged
merged 5 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 108 additions & 30 deletions azurerm/internal/services/mysql/resource_arm_mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"fmt"
"log"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -50,10 +51,43 @@ func resourceArmMySqlServer() *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 @@ -226,13 +260,6 @@ func resourceArmMySqlServerCreate(d *schema.ResourceData, meta interface{}) erro
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() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
Expand All @@ -246,21 +273,31 @@ func resourceArmMySqlServerCreate(d *schema.ResourceData, meta interface{}) erro
}
}

sku := expandMySQLServerSku(d)
storageProfile := expandMySQLStorageProfile(d)
var sku *mysql.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 MySQL Server %q (Resource Group %q): %v", name, resourceGroup, err)
}
} else if _, ok := d.GetOk("sku"); ok {
sku = expandMySQLServerSku(d)
} else {
return fmt.Errorf("One of `sku` or `sku_name` must be set for MySQL Server %q (Resource Group %q)", name, resourceGroup)
}

properties := mysql.ServerForCreate{
Location: &location,
Properties: &mysql.ServerPropertiesForDefaultCreate{
AdministratorLogin: utils.String(adminLogin),
AdministratorLoginPassword: utils.String(adminLoginPassword),
Version: mysql.ServerVersion(version),
SslEnforcement: mysql.SslEnforcementEnum(sslEnforcement),
StorageProfile: storageProfile,
CreateMode: mysql.CreateMode(createMode),
AdministratorLogin: utils.String(d.Get("administrator_login").(string)),
AdministratorLoginPassword: utils.String(d.Get("administrator_login_password").(string)),
Version: mysql.ServerVersion(d.Get("version").(string)),
SslEnforcement: mysql.SslEnforcementEnum(d.Get("ssl_enforcement").(string)),
StorageProfile: expandMySQLStorageProfile(d),
CreateMode: mysql.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 @@ -296,22 +333,28 @@ func resourceArmMySqlServerUpdate(d *schema.ResourceData, meta interface{}) erro
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 := expandMySQLServerSku(d)
storageProfile := expandMySQLStorageProfile(d)
t := d.Get("tags").(map[string]interface{})
var sku *mysql.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 MySQL Server %q (Resource Group %q): %v", name, resourceGroup, err)
}
} else if _, ok := d.GetOk("sku"); ok {
sku = expandMySQLServerSku(d)
} else {
return fmt.Errorf("One of `sku` or `sku_name` must be set for MySQL Server %q (Resource Group %q)", name, resourceGroup)
}

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

future, err := client.Update(ctx, resourceGroup, name, properties)
Expand Down Expand Up @@ -366,6 +409,10 @@ func resourceArmMySqlServerRead(d *schema.ResourceData, meta interface{}) error
d.Set("location", azure.NormalizeLocation(*location))
}

if sku := resp.Sku; sku != nil {
d.Set("sku_name", sku.Name)
}

d.Set("administrator_login", resp.AdministratorLogin)
d.Set("version", string(resp.Version))
d.Set("ssl_enforcement", string(resp.SslEnforcement))
Expand Down Expand Up @@ -408,6 +455,37 @@ func resourceArmMySqlServerDelete(d *schema.ResourceData, meta interface{}) erro
return nil
}

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

var tier mysql.SkuTier
switch parts[0] {
case "B":
tier = mysql.Basic
case "GP":
tier = mysql.GeneralPurpose
case "MO":
tier = mysql.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 &mysql.Sku{
Name: utils.String(skuName),
Tier: tier,
Capacity: utils.Int32(int32(capacity)),
Family: utils.String(parts[1]),
}, nil
}

func expandMySQLServerSku(d *schema.ResourceData) *mysql.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 @@ -215,12 +215,7 @@ resource "azurerm_mysql_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 @@ -166,12 +166,7 @@ resource "azurerm_mysql_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 @@ -221,12 +216,7 @@ resource "azurerm_mysql_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 @@ -262,12 +252,7 @@ resource "azurerm_mysql_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 @@ -127,12 +127,7 @@ resource "azurerm_mysql_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