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

Add lock for the write operation of azurerm_postgresql_configuration #14619

Merged
merged 3 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2020-01-01/postgresql"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/postgres/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/postgres/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -69,6 +70,9 @@ func resourcePostgreSQLConfigurationCreateUpdate(d *pluginsdk.ResourceData, meta
serverName := d.Get("server_name").(string)
value := d.Get("value").(string)

locks.ByName(serverName, postgreSQLServerResourceName)
defer locks.UnlockByName(serverName, postgreSQLServerResourceName)

properties := postgresql.Configuration{
ConfigurationProperties: &postgresql.ConfigurationProperties{
Value: utils.String(value),
Expand Down Expand Up @@ -142,6 +146,9 @@ func resourcePostgreSQLConfigurationDelete(d *pluginsdk.ResourceData, meta inter
return err
}

locks.ByName(id.ServerName, postgreSQLServerResourceName)
defer locks.UnlockByName(id.ServerName, postgreSQLServerResourceName)

// "delete" = resetting this to the default value
resp, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/postgres/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -78,6 +79,21 @@ func TestAccPostgreSQLConfiguration_deadlockTimeout(t *testing.T) {
})
}

func TestAccPostgreSQLConfiguration_multiplePostgreSQLConfigurations(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_postgresql_configuration", "test")
r := PostgreSQLConfigurationResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.multiplePostgreSQLConfigurations(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r PostgreSQLConfigurationResource) checkReset(configurationName string) acceptance.ClientCheckFunc {
return func(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) error {
id, err := parse.ServerID(state.Attributes["id"])
Expand Down Expand Up @@ -198,3 +214,79 @@ resource "azurerm_postgresql_server" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (r PostgreSQLConfigurationResource) multiplePostgreSQLConfigurations(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_postgresql_configuration" "test" {
name = "idle_in_transaction_session_timeout"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = "60"
}

resource "azurerm_postgresql_configuration" "test2" {
name = "log_autovacuum_min_duration"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = "10"
}

resource "azurerm_postgresql_configuration" "test3" {
name = "log_lock_waits"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = "on"
}

resource "azurerm_postgresql_configuration" "test4" {
name = "log_min_duration_statement"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = "10"
}

resource "azurerm_postgresql_configuration" "test5" {
name = "log_statement"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = "ddl"
}

resource "azurerm_postgresql_configuration" "test6" {
name = "pg_stat_statements.track"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = "top"
}

resource "azurerm_postgresql_configuration" "test7" {
name = "pg_qs.query_capture_mode"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = "top"
}

resource "azurerm_postgresql_configuration" "test8" {
name = "pgms_wait_sampling.query_capture_mode"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = "all"
}

resource "azurerm_postgresql_configuration" "test9" {
name = "pg_qs.max_query_text_length"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = 10000
}

resource "azurerm_postgresql_configuration" "test10" {
name = "pg_qs.retention_period_in_days"
resource_group_name = azurerm_postgresql_server.test.resource_group_name
server_name = azurerm_postgresql_server.test.name
value = 30
}
`, r.empty(data))
}