Skip to content

Commit

Permalink
Update postgres flexserver api version to 2021-06-01
Browse files Browse the repository at this point in the history
  noteworthy changes:
   - cmk_enabled is not exposed in 2021-06-01
   - now also postgres version 13 is available
  • Loading branch information
Nick Metz committed Jun 29, 2021
1 parent d42d6bf commit 1ca987a
Show file tree
Hide file tree
Showing 30 changed files with 1,630 additions and 459 deletions.
2 changes: 1 addition & 1 deletion azurerm/internal/services/postgres/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2020-01-01/postgresql"
"github.com/Azure/azure-sdk-for-go/services/preview/postgresql/mgmt/2020-02-14-preview/postgresqlflexibleservers"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2021-06-01/postgresqlflexibleservers"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/postgresql/mgmt/2020-02-14-preview/postgresqlflexibleservers"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2021-06-01/postgresqlflexibleservers"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
Expand Down Expand Up @@ -63,11 +63,6 @@ func dataSourcePostgresqlFlexibleServer() *pluginsdk.Resource {
Computed: true,
},

"cmk_enabled": {
Type: pluginsdk.TypeString,
Computed: true,
},

"fqdn": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -108,19 +103,14 @@ func dataSourceArmPostgresqlFlexibleServerRead(d *pluginsdk.ResourceData, meta i
d.Set("location", location.NormalizeNilable(resp.Location))
if props := resp.ServerProperties; props != nil {
d.Set("administrator_login", props.AdministratorLogin)
d.Set("storage_mb", props.StorageProfile.StorageMB)
d.Set("storage_mb", (*props.Storage.StorageSizeGB * 1024))
d.Set("version", props.Version)
d.Set("cmk_enabled", props.ByokEnforcement)
d.Set("fqdn", props.FullyQualifiedDomainName)
d.Set("public_network_access_enabled", props.PublicNetworkAccess == postgresqlflexibleservers.ServerPublicNetworkAccessStateEnabled)

if props.DelegatedSubnetArguments != nil {
d.Set("delegated_subnet_id", props.DelegatedSubnetArguments.SubnetArmResourceID)
}
d.Set("public_network_access_enabled", props.Network.PublicNetworkAccess == postgresqlflexibleservers.ServerPublicNetworkAccessStateEnabled)
d.Set("backup_retention_days", props.Backup.BackupRetentionDays)

if storage := props.StorageProfile; storage != nil {
d.Set("storage_mb", storage.StorageMB)
d.Set("backup_retention_days", storage.BackupRetentionDays)
if props.Network.DelegatedSubnetResourceID != nil {
d.Set("delegated_subnet_id", props.Network.DelegatedSubnetResourceID)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestAccDataSourcePostgresqlflexibleServer_basic(t *testing.T) {
check.That(data.ResourceName).Key("storage_mb").Exists(),
check.That(data.ResourceName).Key("version").Exists(),
check.That(data.ResourceName).Key("backup_retention_days").Exists(),
check.That(data.ResourceName).Key("cmk_enabled").Exists(),
check.That(data.ResourceName).Key("fqdn").Exists(),
check.That(data.ResourceName).Key("public_network_access_enabled").Exists(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/postgresql/mgmt/2020-02-14-preview/postgresqlflexibleservers"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2021-06-01/postgresqlflexibleservers"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/postgres/parse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/postgresql/mgmt/2020-02-14-preview/postgresqlflexibleservers"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2021-06-01/postgresqlflexibleservers"
"github.com/Azure/go-autorest/autorest/date"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
Expand All @@ -22,6 +22,11 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

const (
ServerMaintenanceWindowEnabled = "Enabled"
ServerMaintenanceWindowDisabled = "Disabled"
)

func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourcePostgresqlFlexibleServerCreate,
Expand Down Expand Up @@ -88,8 +93,9 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(postgresqlflexibleservers.OneOne),
string(postgresqlflexibleservers.OneTwo),
string(postgresqlflexibleservers.ServerVersionOneOne),
string(postgresqlflexibleservers.ServerVersionOneTwo),
string(postgresqlflexibleservers.ServerVersionOneThree),
}, false),
},

Expand All @@ -100,8 +106,8 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(postgresqlflexibleservers.Default),
string(postgresqlflexibleservers.PointInTimeRestore),
string(postgresqlflexibleservers.CreateModeDefault),
string(postgresqlflexibleservers.CreateModePointInTimeRestore),
}, false),
},

Expand Down Expand Up @@ -163,11 +169,6 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {
ValidateFunc: validation.IntBetween(7, 35),
},

"cmk_enabled": {
Type: pluginsdk.TypeString,
Computed: true,
},

"fqdn": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -205,7 +206,7 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte

createMode := d.Get("create_mode").(string)

if postgresqlflexibleservers.CreateMode(createMode) == postgresqlflexibleservers.PointInTimeRestore {
if postgresqlflexibleservers.CreateMode(createMode) == postgresqlflexibleservers.CreateModePointInTimeRestore {
if _, ok := d.GetOk("source_server_id"); !ok {
return fmt.Errorf("`source_server_id` is required when `create_mode` is `PointInTimeRestore`")
}
Expand All @@ -214,7 +215,7 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte
}
}

if createMode == "" || postgresqlflexibleservers.CreateMode(createMode) == postgresqlflexibleservers.Default {
if createMode == "" || postgresqlflexibleservers.CreateMode(createMode) == postgresqlflexibleservers.CreateModeDefault {
if _, ok := d.GetOk("administrator_login"); !ok {
return fmt.Errorf("`administrator_login` is required when `create_mode` is `Default`")
}
Expand All @@ -240,10 +241,11 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte
parameters := postgresqlflexibleservers.Server{
Location: utils.String(location.Normalize(d.Get("location").(string))),
ServerProperties: &postgresqlflexibleservers.ServerProperties{
CreateMode: postgresqlflexibleservers.CreateMode(d.Get("create_mode").(string)),
DelegatedSubnetArguments: expandArmServerServerPropertiesDelegatedSubnetArguments(d.Get("delegated_subnet_id").(string)),
Version: postgresqlflexibleservers.ServerVersion(d.Get("version").(string)),
StorageProfile: expandArmServerStorageProfile(d),
CreateMode: postgresqlflexibleservers.CreateMode(d.Get("create_mode").(string)),
Network: expandArmServerNetwork(d),
Version: postgresqlflexibleservers.ServerVersion(d.Get("version").(string)),
Storage: expandArmServerStorage(d),
Backup: expandArmServerBackup(d),
},
Sku: sku,
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
Expand All @@ -262,13 +264,7 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte
}

if v, ok := d.GetOk("source_server_id"); ok && v.(string) != "" {
sourceServer, err := parse.FlexibleServerID(v.(string))
if err != nil {
return err
}
parameters.ServerProperties.SourceSubscriptionID = utils.String(sourceServer.SubscriptionId)
parameters.ServerProperties.SourceResourceGroupName = utils.String(sourceServer.ResourceGroup)
parameters.ServerProperties.SourceServerName = utils.String(sourceServer.Name)
parameters.SourceServerResourceID = utils.String(v.(string))
}

pointInTimeUTC := d.Get("point_in_time_restore_time_in_utc").(string)
Expand Down Expand Up @@ -337,25 +333,28 @@ func resourcePostgresqlFlexibleServerRead(d *pluginsdk.ResourceData, meta interf
if props := resp.ServerProperties; props != nil {
d.Set("administrator_login", props.AdministratorLogin)
d.Set("zone", props.AvailabilityZone)
if props.SourceServerName != nil && props.SourceSubscriptionID != nil && props.SourceResourceGroupName != nil {
d.Set("source_server_id", parse.NewFlexibleServerID(*props.SourceSubscriptionID, *props.SourceResourceGroupName, *props.SourceServerName).ID())
}
d.Set("version", props.Version)
d.Set("cmk_enabled", props.ByokEnforcement)
d.Set("fqdn", props.FullyQualifiedDomainName)
d.Set("public_network_access_enabled", props.PublicNetworkAccess == postgresqlflexibleservers.ServerPublicNetworkAccessStateEnabled)
d.Set("public_network_access_enabled", props.Network.PublicNetworkAccess == postgresqlflexibleservers.ServerPublicNetworkAccessStateEnabled)

if props.DelegatedSubnetArguments != nil {
d.Set("delegated_subnet_id", props.DelegatedSubnetArguments.SubnetArmResourceID)
if props.SourceServerResourceID != nil {
d.Set("source_server_id", props.SourceServerResourceID)
}

if props.Network.DelegatedSubnetResourceID != nil {
d.Set("delegated_subnet_id", props.Network.DelegatedSubnetResourceID)
}

if err := d.Set("maintenance_window", flattenArmServerMaintenanceWindow(props.MaintenanceWindow)); err != nil {
return fmt.Errorf("setting `maintenance_window`: %+v", err)
}

if storage := props.StorageProfile; storage != nil {
d.Set("storage_mb", storage.StorageMB)
d.Set("backup_retention_days", storage.BackupRetentionDays)
if storage := props.Storage; storage != nil {
d.Set("storage_mb", (*storage.StorageSizeGB * 1024))
}

if backup := props.Backup; backup != nil {
d.Set("backup_retention_days", backup.BackupRetentionDays)
}
}

Expand Down Expand Up @@ -388,8 +387,12 @@ func resourcePostgresqlFlexibleServerUpdate(d *pluginsdk.ResourceData, meta inte
parameters.ServerPropertiesForUpdate.AdministratorLoginPassword = utils.String(d.Get("administrator_password").(string))
}

if d.HasChange("backup_retention_days") || d.HasChange("storage_mb") {
parameters.ServerPropertiesForUpdate.StorageProfile = expandArmServerStorageProfile(d)
if d.HasChange("storage_mb") {
parameters.ServerPropertiesForUpdate.Storage = expandArmServerStorage(d)
}

if d.HasChange("backup_retention_days") {
parameters.ServerPropertiesForUpdate.Backup = expandArmServerBackup(d)
}

if d.HasChange("maintenance_window") {
Expand Down Expand Up @@ -441,26 +444,26 @@ func resourcePostgresqlFlexibleServerDelete(d *pluginsdk.ResourceData, meta inte
return nil
}

func expandArmServerServerPropertiesDelegatedSubnetArguments(input string) *postgresqlflexibleservers.ServerPropertiesDelegatedSubnetArguments {
if len(input) == 0 {
return nil
}
func expandArmServerNetwork(d *pluginsdk.ResourceData) *postgresqlflexibleservers.Network {
network := postgresqlflexibleservers.Network{}

return &postgresqlflexibleservers.ServerPropertiesDelegatedSubnetArguments{
SubnetArmResourceID: utils.String(input),
if v, ok := d.GetOk("delegated_subnet_id"); ok {
network.DelegatedSubnetResourceID = utils.String(v.(string))
}

return &network
}

func expandArmServerMaintenanceWindow(input []interface{}) *postgresqlflexibleservers.MaintenanceWindow {
if len(input) == 0 {
return &postgresqlflexibleservers.MaintenanceWindow{
CustomWindow: utils.String(string(postgresqlflexibleservers.Disabled)),
CustomWindow: utils.String(ServerMaintenanceWindowDisabled),
}
}
v := input[0].(map[string]interface{})

maintenanceWindow := postgresqlflexibleservers.MaintenanceWindow{
CustomWindow: utils.String(string(postgresqlflexibleservers.Enabled)),
CustomWindow: utils.String(ServerMaintenanceWindowEnabled),
StartHour: utils.Int32(int32(v["start_hour"].(int))),
StartMinute: utils.Int32(int32(v["start_minute"].(int))),
DayOfWeek: utils.Int32(int32(v["day_of_week"].(int))),
Expand All @@ -469,20 +472,26 @@ func expandArmServerMaintenanceWindow(input []interface{}) *postgresqlflexiblese
return &maintenanceWindow
}

func expandArmServerStorageProfile(d *pluginsdk.ResourceData) *postgresqlflexibleservers.StorageProfile {
storage := postgresqlflexibleservers.StorageProfile{}

if v, ok := d.GetOk("backup_retention_days"); ok {
storage.BackupRetentionDays = utils.Int32(int32(v.(int)))
}
func expandArmServerStorage(d *pluginsdk.ResourceData) *postgresqlflexibleservers.Storage {
storage := postgresqlflexibleservers.Storage{}

if v, ok := d.GetOk("storage_mb"); ok {
storage.StorageMB = utils.Int32(int32(v.(int)))
storage.StorageSizeGB = utils.Int32(int32(v.(int) / 1024))
}

return &storage
}

func expandArmServerBackup(d *pluginsdk.ResourceData) *postgresqlflexibleservers.Backup {
backup := postgresqlflexibleservers.Backup{}

if v, ok := d.GetOk("backup_retention_days"); ok {
backup.BackupRetentionDays = utils.Int32(int32(v.(int)))
}

return &backup
}

func expandFlexibleServerSku(name string) (*postgresqlflexibleservers.Sku, error) {
if name == "" {
return nil, nil
Expand All @@ -492,11 +501,11 @@ func expandFlexibleServerSku(name string) (*postgresqlflexibleservers.Sku, error
var tier postgresqlflexibleservers.SkuTier
switch strings.TrimSuffix(parts[0], "_") {
case "B":
tier = postgresqlflexibleservers.Burstable
tier = postgresqlflexibleservers.SkuTierBurstable
case "GP":
tier = postgresqlflexibleservers.GeneralPurpose
tier = postgresqlflexibleservers.SkuTierGeneralPurpose
case "MO":
tier = postgresqlflexibleservers.MemoryOptimized
tier = postgresqlflexibleservers.SkuTierMemoryOptimized
default:
return nil, fmt.Errorf("sku_name %s has unknown sku tier %s", name, parts[0])
}
Expand All @@ -514,11 +523,11 @@ func flattenFlexibleServerSku(sku *postgresqlflexibleservers.Sku) (string, error

var tier string
switch sku.Tier {
case postgresqlflexibleservers.Burstable:
case postgresqlflexibleservers.SkuTierBurstable:
tier = "B"
case postgresqlflexibleservers.GeneralPurpose:
case postgresqlflexibleservers.SkuTierGeneralPurpose:
tier = "GP"
case postgresqlflexibleservers.MemoryOptimized:
case postgresqlflexibleservers.SkuTierMemoryOptimized:
tier = "MO"
default:
return "", fmt.Errorf("sku_name has unknown sku tier %s", sku.Tier)
Expand All @@ -528,7 +537,7 @@ func flattenFlexibleServerSku(sku *postgresqlflexibleservers.Sku) (string, error
}

func flattenArmServerMaintenanceWindow(input *postgresqlflexibleservers.MaintenanceWindow) []interface{} {
if input == nil || input.CustomWindow == nil || *input.CustomWindow == string(postgresqlflexibleservers.Disabled) {
if input == nil || input.CustomWindow == nil || *input.CustomWindow == string(ServerMaintenanceWindowDisabled) {
return make([]interface{}, 0)
}

Expand Down
Loading

0 comments on commit 1ca987a

Please sign in to comment.