From 28b99499a72bc4812c0160457060aed827d75cf1 Mon Sep 17 00:00:00 2001 From: Mike Coutermarsh Date: Wed, 26 Feb 2025 18:24:13 -0500 Subject: [PATCH] Add ability to create replica passwords For: https://github.com/planetscale/terraform-provider-planetscale/issues/112 --- internal/provider/password_resource.go | 11 +++++++++++ internal/provider/password_resource_test.go | 17 ++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/internal/provider/password_resource.go b/internal/provider/password_resource.go index 888245a..f3694e8 100644 --- a/internal/provider/password_resource.go +++ b/internal/provider/password_resource.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/float64planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" @@ -53,6 +54,7 @@ type passwordResourceModel struct { DeletedAt types.String `tfsdk:"deleted_at"` ExpiresAt types.String `tfsdk:"expires_at"` Region types.Object `tfsdk:"region"` + Replica types.Bool `tfsdk:"replica"` Renewable types.Bool `tfsdk:"renewable"` Role types.String `tfsdk:"role"` Cidrs types.List `tfsdk:"cidrs"` @@ -98,6 +100,7 @@ func passwordResourceFromClient(ctx context.Context, password *planetscale.Passw Id: types.StringValue(password.Id), Region: region, Renewable: types.BoolValue(password.Renewable), + Replica: types.BoolValue(password.Replica), Role: types.StringValue(password.Role), TtlSeconds: types.Float64Value(password.TtlSeconds), Username: types.StringPointerValue(password.Username), @@ -316,6 +319,14 @@ func (r *passwordResource) Schema(ctx context.Context, req resource.SchemaReques Description: "Whether or not the password can be renewed.", Computed: true, }, + "replica": schema.BoolAttribute{ + Description: "Whether or not the password is a replica.", + Optional: true, + Computed: true, + PlanModifiers: []planmodifier.Bool{ + boolplanmodifier.RequiresReplaceIfConfigured(), + }, + }, "username": schema.StringAttribute{ Description: "The username for the password.", Computed: true, diff --git a/internal/provider/password_resource_test.go b/internal/provider/password_resource_test.go index ae1c602..6ce2996 100644 --- a/internal/provider/password_resource_test.go +++ b/internal/provider/password_resource_test.go @@ -114,13 +114,14 @@ func TestAccPasswordResource(t *testing.T) { Steps: []resource.TestStep{ // Create and Read testing { - Config: testAccPasswordResourceConfig(dbName, branchName, passwdName, ipv4Cidr), + Config: testAccPasswordResourceConfig(dbName, branchName, passwdName, ipv4Cidr, true), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("planetscale_password.test", "name", passwdName), resource.TestCheckResourceAttr("planetscale_password.test", "role", "admin"), resource.TestCheckResourceAttr("planetscale_password.test", "branch", branchName), resource.TestCheckResourceAttr("planetscale_password.test", "cidrs.#", "1"), resource.TestCheckTypeSetElemAttr("planetscale_password.test", "cidrs.*", ipv4Cidr[0]), + resource.TestCheckResourceAttr("planetscale_password.test", "replica", "true"), ), }, @@ -147,7 +148,7 @@ func TestAccPasswordResource(t *testing.T) { // Update 'name' attribute: { - Config: testAccPasswordResourceConfig(dbName, branchName, passwdName+"-new", ipv4Cidr), + Config: testAccPasswordResourceConfig(dbName, branchName, passwdName+"-new", ipv4Cidr, false), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("planetscale_password.test", "name", passwdName+"-new"), ), @@ -165,7 +166,7 @@ func TestAccPasswordResource(t *testing.T) { // Update `cidrs` with multiple ipv4 ranges { - Config: testAccPasswordResourceConfig(dbName, branchName, passwdName+"-new", multiIPv4Cidrs), + Config: testAccPasswordResourceConfig(dbName, branchName, passwdName+"-new", multiIPv4Cidrs, false), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("planetscale_password.test", "cidrs.#", "2"), resource.TestCheckTypeSetElemAttr("planetscale_password.test", "cidrs.*", multiIPv4Cidrs[0]), @@ -197,7 +198,7 @@ func TestAccPasswordResource(t *testing.T) { // Test removal of `cidrs` { - Config: testAccPasswordResourceConfig(dbName, branchName, passwdName+"-new", nil), + Config: testAccPasswordResourceConfig(dbName, branchName, passwdName+"-new", nil, false), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckNoResourceAttr("planetscale_password.test", "cidrs"), ), @@ -272,7 +273,7 @@ func TestAccPasswordResource_OutOfBandDelete(t *testing.T) { Steps: []resource.TestStep{ // Create and Read testing { - Config: testAccPasswordResourceConfig(dbName, branchName, passwdName, nil), + Config: testAccPasswordResourceConfig(dbName, branchName, passwdName, nil, false), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("planetscale_password.test", "role", "admin"), resource.TestCheckResourceAttr("planetscale_password.test", "branch", branchName), @@ -311,7 +312,7 @@ func TestAccPasswordResource_OutOfBandDelete(t *testing.T) { }) } -func testAccPasswordResourceConfig(dbName, branchName, passwdName string, cidrs []string) string { +func testAccPasswordResourceConfig(dbName, branchName, passwdName string, cidrs []string, replica bool) string { const tmpl = ` resource "planetscale_database" "test" { name = "{{.DBName}}" @@ -332,17 +333,19 @@ resource "planetscale_password" "test" { organization = "{{.Org}}" database = planetscale_database.test.name branch = planetscale_branch.test.name + replica = {{.Replica}} {{if .CIDRs}}cidrs = {{.CIDRs}}{{end}} } ` t := template.Must(template.New("config").Parse(tmpl)) var buf bytes.Buffer - err := t.Execute(&buf, map[string]string{ + err := t.Execute(&buf, map[string]interface{}{ "Org": testAccOrg, "DBName": dbName, "BranchName": branchName, "PasswdName": passwdName, "CIDRs": terraformStringList(cidrs), + "Replica": replica, }) if err != nil { return ""