Skip to content

Commit

Permalink
Add ability to create replica passwords
Browse files Browse the repository at this point in the history
For: #112
  • Loading branch information
mscoutermarsh committed Feb 26, 2025
1 parent 6deb1a1 commit 28b9949
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 11 additions & 0 deletions internal/provider/password_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 10 additions & 7 deletions internal/provider/password_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},

Expand All @@ -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"),
),
Expand All @@ -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]),
Expand Down Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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}}"
Expand All @@ -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 ""
Expand Down

0 comments on commit 28b9949

Please sign in to comment.