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_flexible_server_configuration: support setting gtid_mode as specified #28496

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -4,8 +4,10 @@
package mysql

import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
Expand Down Expand Up @@ -70,12 +72,6 @@ func resourceMySQLFlexibleServerConfigurationCreate(d *pluginsdk.ResourceData, m

log.Printf("[INFO] preparing arguments for AzureRM MySQL Configuration creation.")

payload := configurations.Configuration{
Properties: &configurations.ConfigurationProperties{
Value: pointer.To(d.Get("value").(string)),
},
}

// NOTE: this resource intentionally doesn't support Requires Import
// since a fallback route is created by default

Expand All @@ -84,8 +80,20 @@ func resourceMySQLFlexibleServerConfigurationCreate(d *pluginsdk.ResourceData, m
locks.ByName(id.FlexibleServerName, mysqlFlexibleServerResourceName)
defer locks.UnlockByName(id.FlexibleServerName, mysqlFlexibleServerResourceName)

if err := client.UpdateThenPoll(ctx, id, payload); err != nil {
return fmt.Errorf("creating %s: %v", id, err)
if strings.EqualFold(id.ConfigurationName, "gtid_mode") {
if err := mysqlFlexibleServerConfigurationUpdateGITDMode(ctx, client, id, d.Get("value").(string)); err != nil {
return fmt.Errorf("creating GTID mode: %v", err)
}
} else {
payload := configurations.Configuration{
Properties: &configurations.ConfigurationProperties{
Value: pointer.To(d.Get("value").(string)),
},
}

if err := client.UpdateThenPoll(ctx, id, payload); err != nil {
return fmt.Errorf("creating %s: %v", id, err)
}
}

d.SetId(id.ID())
Expand All @@ -107,14 +115,20 @@ func resourceMySQLFlexibleServerConfigurationUpdate(d *pluginsdk.ResourceData, m
locks.ByName(id.FlexibleServerName, mysqlFlexibleServerResourceName)
defer locks.UnlockByName(id.FlexibleServerName, mysqlFlexibleServerResourceName)

payload := configurations.Configuration{
Properties: &configurations.ConfigurationProperties{
Value: pointer.To(d.Get("value").(string)),
},
}
if strings.EqualFold(id.ConfigurationName, "gtid_mode") {
if err := mysqlFlexibleServerConfigurationUpdateGITDMode(ctx, client, *id, d.Get("value").(string)); err != nil {
return fmt.Errorf("updating GTID mode: %v", err)
}
} else {
payload := configurations.Configuration{
Properties: &configurations.ConfigurationProperties{
Value: pointer.To(d.Get("value").(string)),
},
}

if err := client.UpdateThenPoll(ctx, *id, payload); err != nil {
return fmt.Errorf("updating %s: %v", id, err)
if err := client.UpdateThenPoll(ctx, *id, payload); err != nil {
return fmt.Errorf("updating %s: %v", id, err)
}
}

return resourceMySQLFlexibleServerConfigurationRead(d, meta)
Expand Down Expand Up @@ -188,3 +202,42 @@ func resourceMySQLFlexibleServerConfigurationDelete(d *pluginsdk.ResourceData, m

return nil
}

func mysqlFlexibleServerConfigurationUpdateGITDMode(ctx context.Context, client *configurations.ConfigurationsClient, id configurations.ConfigurationId, value string) error {
gtidSeq := []string{"OFF", "OFF_PERMISSIVE", "ON_PERMISSIVE", "ON"}
currentValue := "OFF"
resp, _ := client.Get(ctx, id)
if resp.Model != nil && resp.Model.Properties != nil && resp.Model.Properties.Value != nil {
currentValue = pointer.From(resp.Model.Properties.Value)
}

curIdx, toIdx := 0, 0
for idx, v := range gtidSeq {
if v == currentValue {
curIdx = idx
}

if v == value {
toIdx = idx
}
}

if toIdx < curIdx {
return fmt.Errorf("cannot set `gtid_mode` from %s to %s", currentValue, value)
}

for _, v := range gtidSeq[curIdx+1 : toIdx+1] {
payload := configurations.Configuration{
Properties: &configurations.ConfigurationProperties{
Value: pointer.To(v),
},
}

log.Printf("[DEBUG] updating `gtid_mode` of %s to %s", id, v)
if err := client.UpdateThenPoll(ctx, id, payload); err != nil {
return fmt.Errorf("updating `gtid_mode` of %s: %v", id, err)
}
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ func TestAccMySQLFlexibleServerConfiguration_interactiveTimeout(t *testing.T) {
})
}

func TestAccMySQLFlexibleServerConfiguration_GTIDMode(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mysql_flexible_server_configuration", "test")
r := MySQLFlexibleServerConfigurationResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.gtidMode(data, "OFF_PERMISSIVE"),
Check: acceptance.ComposeTestCheckFunc(
data.CheckWithClient(r.checkValue("OFF_PERMISSIVE")),
),
},
data.ImportStep(),
{
Config: r.gtidMode(data, "ON"),
Check: acceptance.ComposeTestCheckFunc(
data.CheckWithClient(r.checkValue("ON")),
),
},
data.ImportStep(),
})
}

func TestAccMySQLFlexibleServerConfiguration_logSlowAdminStatements(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mysql_flexible_server_configuration", "test")
r := MySQLFlexibleServerConfigurationResource{}
Expand Down Expand Up @@ -203,6 +225,27 @@ func (r MySQLFlexibleServerConfigurationResource) interactiveTimeout(data accept
return r.template(data, "interactive_timeout", "30")
}

func (r MySQLFlexibleServerConfigurationResource) gtidMode(data acceptance.TestData, value string) string {
return fmt.Sprintf(`
%s

resource "azurerm_mysql_flexible_server_configuration" "test1" {
name = "enforce_gtid_consistency"
resource_group_name = azurerm_resource_group.test.name
server_name = azurerm_mysql_flexible_server.test.name
value = "ON"
}

resource "azurerm_mysql_flexible_server_configuration" "test" {
name = "gtid_mode"
resource_group_name = "${azurerm_resource_group.test.name}"
server_name = "${azurerm_mysql_flexible_server.test.name}"
value = "%s"
depends_on = [azurerm_mysql_flexible_server_configuration.test1]
}
`, r.empty(data), value)
}

func (r MySQLFlexibleServerConfigurationResource) logSlowAdminStatements(data acceptance.TestData) string {
return r.template(data, "log_slow_admin_statements", "on")
}
Expand Down
Loading