Skip to content

Commit

Permalink
provider: support for the postgresql_flexible_server.restart_server_o…
Browse files Browse the repository at this point in the history
…n_configuration_value_change property (hashicorp#23811)

* Add PostgresqlFlexibleServer to features block

* Corrected Subscription feature test case name

* postgres-flex-server: make restarts on config changes optional; add docs

* Rewrite checks functions to reduce repetitive code

* postgres-flex-configuration: corrected tf-code formatting

* psql-flex-server-config: corrected formatting in test file with terrafmt
  • Loading branch information
igorkliushnikov authored and rizkybiz committed Feb 29, 2024
1 parent 0e54cc3 commit 96013f3
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 33 deletions.
3 changes: 3 additions & 0 deletions internal/features/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ func Default() UserFeatures {
Subscription: SubscriptionFeatures{
PreventCancellationOnDestroy: false,
},
PostgresqlFlexibleServer: PostgresqlFlexibleServerFeatures{
RestartServerOnConfigurationValueChange: true,
},
}
}
29 changes: 17 additions & 12 deletions internal/features/user_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
package features

type UserFeatures struct {
ApiManagement ApiManagementFeatures
AppConfiguration AppConfigurationFeatures
ApplicationInsights ApplicationInsightFeatures
CognitiveAccount CognitiveAccountFeatures
VirtualMachine VirtualMachineFeatures
VirtualMachineScaleSet VirtualMachineScaleSetFeatures
KeyVault KeyVaultFeatures
TemplateDeployment TemplateDeploymentFeatures
LogAnalyticsWorkspace LogAnalyticsWorkspaceFeatures
ResourceGroup ResourceGroupFeatures
ManagedDisk ManagedDiskFeatures
Subscription SubscriptionFeatures
ApiManagement ApiManagementFeatures
AppConfiguration AppConfigurationFeatures
ApplicationInsights ApplicationInsightFeatures
CognitiveAccount CognitiveAccountFeatures
VirtualMachine VirtualMachineFeatures
VirtualMachineScaleSet VirtualMachineScaleSetFeatures
KeyVault KeyVaultFeatures
TemplateDeployment TemplateDeploymentFeatures
LogAnalyticsWorkspace LogAnalyticsWorkspaceFeatures
ResourceGroup ResourceGroupFeatures
ManagedDisk ManagedDiskFeatures
Subscription SubscriptionFeatures
PostgresqlFlexibleServer PostgresqlFlexibleServerFeatures
}

type CognitiveAccountFeatures struct {
Expand Down Expand Up @@ -79,3 +80,7 @@ type AppConfigurationFeatures struct {
type SubscriptionFeatures struct {
PreventCancellationOnDestroy bool
}

type PostgresqlFlexibleServerFeatures struct {
RestartServerOnConfigurationValueChange bool
}
25 changes: 25 additions & 0 deletions internal/provider/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,21 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema {
},
},
},

"postgresql_flexible_server": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"restart_server_on_configuration_value_change": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
},
},
},
}

// this is a temporary hack to enable us to gradually add provider blocks to test configurations
Expand Down Expand Up @@ -481,5 +496,15 @@ func expandFeatures(input []interface{}) features.UserFeatures {
}
}

if raw, ok := val["postgresql_flexible_server"]; ok {
items := raw.([]interface{})
if len(items) > 0 {
subscriptionRaw := items[0].(map[string]interface{})
if v, ok := subscriptionRaw["restart_server_on_configuration_value_change"]; ok {
featuresMap.PostgresqlFlexibleServer.RestartServerOnConfigurationValueChange = v.(bool)
}
}
}

return featuresMap
}
86 changes: 85 additions & 1 deletion internal/provider/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func TestExpandFeatures(t *testing.T) {
Subscription: features.SubscriptionFeatures{
PreventCancellationOnDestroy: false,
},
PostgresqlFlexibleServer: features.PostgresqlFlexibleServerFeatures{
RestartServerOnConfigurationValueChange: true,
},
},
},
{
Expand Down Expand Up @@ -122,6 +125,11 @@ func TestExpandFeatures(t *testing.T) {
"expand_without_downtime": true,
},
},
"postgresql_flexible_server": []interface{}{
map[string]interface{}{
"restart_server_on_configuration_value_change": true,
},
},
"resource_group": []interface{}{
map[string]interface{}{
"prevent_deletion_if_contains_resources": true,
Expand Down Expand Up @@ -204,6 +212,9 @@ func TestExpandFeatures(t *testing.T) {
ForceDelete: true,
ScaleToZeroOnDelete: true,
},
PostgresqlFlexibleServer: features.PostgresqlFlexibleServerFeatures{
RestartServerOnConfigurationValueChange: true,
},
},
},
{
Expand Down Expand Up @@ -255,6 +266,11 @@ func TestExpandFeatures(t *testing.T) {
"expand_without_downtime": false,
},
},
"postgresql_flexible_server": []interface{}{
map[string]interface{}{
"restart_server_on_configuration_value_change": false,
},
},
"resource_group": []interface{}{
map[string]interface{}{
"prevent_deletion_if_contains_resources": false,
Expand Down Expand Up @@ -337,6 +353,9 @@ func TestExpandFeatures(t *testing.T) {
RollInstancesWhenRequired: false,
ScaleToZeroOnDelete: false,
},
PostgresqlFlexibleServer: features.PostgresqlFlexibleServerFeatures{
RestartServerOnConfigurationValueChange: false,
},
},
},
}
Expand Down Expand Up @@ -1237,7 +1256,7 @@ func TestExpandFeaturesSubscription(t *testing.T) {
},
},
{
Name: "No Downtime Resize Enabled",
Name: "Subscription cancellation on destroy is Disabled",
Input: []interface{}{
map[string]interface{}{
"subscription": []interface{}{
Expand All @@ -1263,3 +1282,68 @@ func TestExpandFeaturesSubscription(t *testing.T) {
}
}
}

func TestExpandFeaturesPosgresqlFlexibleServer(t *testing.T) {
testData := []struct {
Name string
Input []interface{}
EnvVars map[string]interface{}
Expected features.UserFeatures
}{
{
Name: "Empty Block",
Input: []interface{}{
map[string]interface{}{
"postgresql_flexible_server": []interface{}{},
},
},
Expected: features.UserFeatures{
PostgresqlFlexibleServer: features.PostgresqlFlexibleServerFeatures{
RestartServerOnConfigurationValueChange: true,
},
},
},
{
Name: "Postgresql Flexible Server restarts on configuration change is Enabled",
Input: []interface{}{
map[string]interface{}{
"postgresql_flexible_server": []interface{}{
map[string]interface{}{
"restart_server_on_configuration_value_change": true,
},
},
},
},
Expected: features.UserFeatures{
PostgresqlFlexibleServer: features.PostgresqlFlexibleServerFeatures{
RestartServerOnConfigurationValueChange: true,
},
},
},
{
Name: "Postgresql Flexible Server restarts on configuration change is Disabled",
Input: []interface{}{
map[string]interface{}{
"postgresql_flexible_server": []interface{}{
map[string]interface{}{
"restart_server_on_configuration_value_change": false,
},
},
},
},
Expected: features.UserFeatures{
PostgresqlFlexibleServer: features.PostgresqlFlexibleServerFeatures{
RestartServerOnConfigurationValueChange: false,
},
},
},
}

for _, testCase := range testData {
t.Logf("[DEBUG] Test Case: %q", testCase.Name)
result := expandFeatures(testCase.Input)
if !reflect.DeepEqual(result.Subscription, testCase.Expected.Subscription) {
t.Fatalf("Expected %+v but got %+v", result.Subscription, testCase.Expected.Subscription)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ func resourceFlexibleServerConfigurationUpdate(d *pluginsdk.ResourceData, meta i

if isDynamicConfig := props.IsDynamicConfig; isDynamicConfig != nil && !*isDynamicConfig {
if isReadOnly := props.IsReadOnly; isReadOnly != nil && !*isReadOnly {
restartClient := meta.(*clients.Client).Postgres.ServerRestartClient
restartServerId := serverrestart.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName)
if meta.(*clients.Client).Features.PostgresqlFlexibleServer.RestartServerOnConfigurationValueChange {
restartClient := meta.(*clients.Client).Postgres.ServerRestartClient
restartServerId := serverrestart.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName)

if err = restartClient.ServersRestartThenPoll(ctx, restartServerId, serverrestart.RestartParameter{}); err != nil {
return fmt.Errorf("restarting server %s: %+v", id, err)
if err = restartClient.ServersRestartThenPoll(ctx, restartServerId, serverrestart.RestartParameter{}); err != nil {
return fmt.Errorf("restarting server %s: %+v", id, err)
}
}
}
}
Expand Down Expand Up @@ -186,11 +188,13 @@ func resourceFlexibleServerConfigurationDelete(d *pluginsdk.ResourceData, meta i

if isDynamicConfig := props.IsDynamicConfig; isDynamicConfig != nil && !*isDynamicConfig {
if isReadOnly := props.IsReadOnly; isReadOnly != nil && !*isReadOnly {
restartClient := meta.(*clients.Client).Postgres.ServerRestartClient
restartServerId := serverrestart.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName)
if meta.(*clients.Client).Features.PostgresqlFlexibleServer.RestartServerOnConfigurationValueChange {
restartClient := meta.(*clients.Client).Postgres.ServerRestartClient
restartServerId := serverrestart.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName)

if err = restartClient.ServersRestartThenPoll(ctx, restartServerId, serverrestart.RestartParameter{}); err != nil {
return fmt.Errorf("restarting server %s: %+v", id, err)
if err = restartClient.ServersRestartThenPoll(ctx, restartServerId, serverrestart.RestartParameter{}); err != nil {
return fmt.Errorf("restarting server %s: %+v", id, err)
}
}
}
}
Expand Down
Loading

0 comments on commit 96013f3

Please sign in to comment.