Skip to content
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
55 changes: 50 additions & 5 deletions internal/services/mysql/mysql_flexible_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/mysql/2023-12-30/serverfailover"
"github.com/hashicorp/go-azure-sdk/resource-manager/mysql/2023-12-30/servers"
"github.com/hashicorp/go-azure-sdk/resource-manager/privatedns/2024-06-01/privatezones"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
pluginSdkValidation "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
Expand Down Expand Up @@ -56,6 +59,10 @@ func resourceMysqlFlexibleServer() *pluginsdk.Resource {
return err
}),

ValidateRawResourceConfigFuncs: []schema.ValidateRawResourceConfigFunc{
pluginSdkValidation.PreferWriteOnlyAttribute(cty.GetAttrPath("administrator_password"), cty.GetAttrPath("administrator_password_wo")),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Expand All @@ -77,10 +84,25 @@ func resourceMysqlFlexibleServer() *pluginsdk.Resource {
},

"administrator_password": {
Type: pluginsdk.TypeString,
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validate.FlexibleServerAdministratorPassword,
ConflictsWith: []string{"administrator_password_wo"},
},

"administrator_password_wo": {
Type: pluginsdk.TypeString,
Optional: true,
WriteOnly: true,
ConflictsWith: []string{"administrator_password"},
RequiredWith: []string{"administrator_password_wo_version"},
},

"administrator_password_wo_version": {
Type: pluginsdk.TypeInt,
Optional: true,
Sensitive: true,
ValidateFunc: validate.FlexibleServerAdministratorPassword,
RequiredWith: []string{"administrator_password_wo"},
},

"backup_retention_days": {
Expand Down Expand Up @@ -338,6 +360,11 @@ func resourceMysqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta interface
return tf.ImportAsExistsError("azurerm_mysql_flexible_server", id.ID())
}

woPassword, err := pluginsdk.GetWriteOnly(d, "administrator_password_wo", cty.String)
if err != nil {
return err
}

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

if _, ok := d.GetOk("replication_role"); ok {
Expand All @@ -360,9 +387,11 @@ func resourceMysqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta interface
if _, ok := d.GetOk("administrator_login"); !ok {
return fmt.Errorf("`administrator_login` is required when `create_mode` is `Default`")
}
if _, ok := d.GetOk("administrator_password"); !ok {
return fmt.Errorf("`administrator_password` is required when `create_mode` is `Default`")

if _, ok := d.GetOk("administrator_password"); !ok && woPassword.IsNull() {
return fmt.Errorf("`administrator_password_wo` or `administrator_password` is required when `create_mode` is `Default`")
}

if _, ok := d.GetOk("sku_name"); !ok {
return fmt.Errorf("`sku_name` is required when `create_mode` is `Default`")
}
Expand Down Expand Up @@ -404,6 +433,10 @@ func resourceMysqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta interface
parameters.Properties.AdministratorLoginPassword = utils.String(v.(string))
}

if !woPassword.IsNull() {
parameters.Properties.AdministratorLoginPassword = pointer.To(woPassword.AsString())
}

if v, ok := d.GetOk("zone"); ok && v.(string) != "" {
parameters.Properties.AvailabilityZone = utils.String(v.(string))
}
Expand Down Expand Up @@ -557,6 +590,8 @@ func resourceMysqlFlexibleServerRead(d *pluginsdk.ResourceData, meta interface{}
}
d.Set("sku_name", sku)

d.Set("administrator_password_wo_version", d.Get("administrator_password_wo_version").(int))

return tags.FlattenAndSet(d, model.Tags)
}

Expand Down Expand Up @@ -674,6 +709,16 @@ func resourceMysqlFlexibleServerUpdate(d *pluginsdk.ResourceData, meta interface
parameters.Properties.AdministratorLoginPassword = utils.String(d.Get("administrator_password").(string))
}

if d.HasChange("administrator_password_wo_version") {
woPassword, err := pluginsdk.GetWriteOnly(d, "administrator_password_wo", cty.String)
if err != nil {
return err
}
if !woPassword.IsNull() {
parameters.Properties.AdministratorLoginPassword = pointer.To(woPassword.AsString())
}
}

if d.HasChange("backup_retention_days") || d.HasChange("geo_redundant_backup_enabled") {
parameters.Properties.Backup = expandArmServerBackup(d)
}
Expand Down
82 changes: 78 additions & 4 deletions internal/services/mysql/mysql_flexible_server_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
"testing"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/mysql/2023-12-30/servers"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
"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/provider/framework"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type MySqlFlexibleServerResource struct{}
Expand Down Expand Up @@ -473,7 +477,7 @@ func TestAccMySqlFlexibleServer_updateToCustomerManagedKey(t *testing.T) {
})
}

// this test can fail with a uninformative error, tracked here https://github.com/Azure/azure-rest-api-specs/issues/22980
// this test can fail with an uninformative error, tracked here https://github.com/Azure/azure-rest-api-specs/issues/22980
func TestAccMySqlFlexibleServer_enableGeoRedundantBackup(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mysql_flexible_server", "test")
r := MySqlFlexibleServerResource{}
Expand Down Expand Up @@ -511,6 +515,59 @@ func TestAccMySqlFlexibleServer_identity(t *testing.T) {
})
}

func TestAccMySqlFlexibleServer_writeOnlyPassword(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mysql_flexible_server", "test")
r := MySqlFlexibleServerResource{}

resource.ParallelTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.11.0-beta1"))),
},
ProtoV5ProviderFactories: framework.ProtoV5ProviderFactoriesInit(context.Background(), "azurerm"),
Steps: []resource.TestStep{
{
Config: r.writeOnlyPassword(data, "a-secret-from-kv", 1),
Check: check.That(data.ResourceName).ExistsInAzure(r),
},
data.ImportStep("administrator_password_wo_version"),
{
Config: r.writeOnlyPassword(data, "a-secret-from-kv-updated", 2),
Check: check.That(data.ResourceName).ExistsInAzure(r),
},
data.ImportStep("administrator_password_wo_version"),
},
})
}

func TestAccMySqlFlexibleServer_updateToWriteOnlyPassword(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mysql_flexible_server", "test")
r := MySqlFlexibleServerResource{}

resource.ParallelTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.11.0-beta1"))),
},
ProtoV5ProviderFactories: framework.ProtoV5ProviderFactoriesInit(context.Background(), "azurerm"),
Steps: []resource.TestStep{
{
Config: r.basic(data),
Check: check.That(data.ResourceName).ExistsInAzure(r),
},
data.ImportStep("administrator_password"),
{
Config: r.writeOnlyPassword(data, "a-secret-from-kv", 1),
Check: check.That(data.ResourceName).ExistsInAzure(r),
},
data.ImportStep("administrator_password", "administrator_password_wo_version"),
{
Config: r.basic(data),
Check: check.That(data.ResourceName).ExistsInAzure(r),
},
data.ImportStep("administrator_password"),
},
})
}

func (MySqlFlexibleServerResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := servers.ParseFlexibleServerID(state.ID)
if err != nil {
Expand All @@ -522,7 +579,7 @@ func (MySqlFlexibleServerResource) Exists(ctx context.Context, clients *clients.
return nil, fmt.Errorf("retrieving %s: %+v", id.ID(), err)
}

return utils.Bool(resp.Model != nil && resp.Model.Properties != nil), nil
return pointer.To(resp.Model != nil && resp.Model.Properties != nil), nil
}

func (MySqlFlexibleServerResource) template(data acceptance.TestData) string {
Expand All @@ -549,7 +606,6 @@ resource "azurerm_mysql_flexible_server" "test" {
administrator_login = "_admin_Terraform_892123456789312"
administrator_password = "QAZwsx123"
sku_name = "B_Standard_B1s"
zone = "1"
}
`, r.template(data), data.RandomInteger)
}
Expand Down Expand Up @@ -1285,3 +1341,21 @@ resource "azurerm_mysql_flexible_server" "test" {
}
`, r.template(data), data.RandomString, data.RandomInteger)
}

func (r MySqlFlexibleServerResource) writeOnlyPassword(data acceptance.TestData, secret string, version int) string {
return fmt.Sprintf(`
%s

%s

resource "azurerm_mysql_flexible_server" "test" {
name = "acctest-fs-%[3]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
administrator_login = "_admin_Terraform_892123456789312"
administrator_password_wo = ephemeral.azurerm_key_vault_secret.test.value
administrator_password_wo_version = %[4]d
sku_name = "B_Standard_B1s"
}
`, r.template(data), acceptance.WriteOnlyKeyVaultSecretTemplate(data, secret), data.RandomInteger, version)
}
6 changes: 5 additions & 1 deletion website/docs/r/mysql_flexible_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ The following arguments are supported:

* `administrator_login` - (Optional) The Administrator login for the MySQL Flexible Server. Required when `create_mode` is `Default`. Changing this forces a new MySQL Flexible Server to be created.

* `administrator_password` - (Optional) The Password associated with the `administrator_login` for the MySQL Flexible Server. Required when `create_mode` is `Default`.
* `administrator_password` - (Optional) The Password associated with the `administrator_login` for the MySQL Flexible Server. Either `administrator_password` or `administrator_password_wo` is required when `create_mode` is `Default`.

* `administrator_password_wo` - (Optional, Write-Only) The Password associated with the `administrator_login` for the MySQL Flexible Server. Either `administrator_password_wo` or `administrator_password` is required when `create_mode` is `Default`.

* `administrator_password_wo_version` - (Optional) An integer value used to trigger an update for `administrator_password_wo`. This property should be incremented when updating `administrator_password_wo`.

* `backup_retention_days` - (Optional) The backup retention days for the MySQL Flexible Server. Possible values are between `1` and `35` days. Defaults to `7`.

Expand Down