From 5a3e995ceb7b299bc262a27565b089c694298114 Mon Sep 17 00:00:00 2001 From: Max Englander Date: Fri, 20 Nov 2026 19:40:11 -0500 Subject: [PATCH 1/4] Add --client-tablet-targeting-policy to password create and update Pass client_tablet_targeting_policy through to the password create and update requests, and show it in password show and list. Co-Authored-By: Claude Opus 5.5 --- internal/cmd/password/create.go | 28 ++++---- internal/cmd/password/create_test.go | 30 +++++++++ internal/cmd/password/password.go | 88 ++++++++++++++------------ internal/cmd/password/password_test.go | 9 +++ internal/cmd/password/update.go | 32 +++++++--- internal/cmd/password/update_test.go | 33 +++++++++- internal/planetscale/passwords.go | 58 +++++++++-------- internal/planetscale/passwords_test.go | 75 ++++++++++++++++++++++ 8 files changed, 261 insertions(+), 92 deletions(-) diff --git a/internal/cmd/password/create.go b/internal/cmd/password/create.go index 8851432ed..482d6a83f 100644 --- a/internal/cmd/password/create.go +++ b/internal/cmd/password/create.go @@ -12,10 +12,11 @@ import ( func CreateCmd(ch *cmdutil.Helper) *cobra.Command { var flags struct { - role string - ttl cmdutil.TTLFlag - replica bool - readOnlyRegion string + role string + ttl cmdutil.TTLFlag + replica bool + readOnlyRegion string + clientTabletTargetingPolicy string } cmd := &cobra.Command{ @@ -83,14 +84,15 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { defer end() pass, err := client.Passwords.Create(cmd.Context(), &ps.DatabaseBranchPasswordRequest{ - Database: database, - Branch: branch, - Organization: ch.Config.Organization, - Name: name, - Role: flags.role, - TTL: int(flags.ttl.Value.Seconds()), - Replica: flags.replica, - ReadOnlyRegionID: readOnlyRegionID, + Database: database, + Branch: branch, + Organization: ch.Config.Organization, + Name: name, + Role: flags.role, + TTL: int(flags.ttl.Value.Seconds()), + Replica: flags.replica, + ReadOnlyRegionID: readOnlyRegionID, + ClientTabletTargetingPolicy: flags.clientTabletTargetingPolicy, }) if err != nil { switch cmdutil.ErrCode(err) { @@ -121,6 +123,8 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { cmd.Flags().BoolVar(&flags.replica, "replica", false, "When enabled, the password will route all reads to the branch's primary replicas and all read-only regions.") cmd.Flags().StringVar(&flags.readOnlyRegion, "read-only-region", "", "Create a password scoped to a Vitess read-only region (region slug, display name, or id). List regions with: pscale database read-only-regions list .") + cmd.Flags().StringVar(&flags.clientTabletTargetingPolicy, "client-tablet-targeting-policy", "", + "How a replica password handles a tablet type set by the client, such as USE @replica or mydb@replica. Allowed value: ignore.") return cmd } diff --git a/internal/cmd/password/create_test.go b/internal/cmd/password/create_test.go index f2c2c2675..1d6a742e4 100644 --- a/internal/cmd/password/create_test.go +++ b/internal/cmd/password/create_test.go @@ -334,6 +334,36 @@ func TestPassword_CreateCmd_Replica(t *testing.T) { c.Assert(svc.CreateFnInvoked, qt.IsTrue) } +func TestPassword_CreateCmd_ClientTabletTargetingPolicy(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + + svc := &mock.PasswordsService{ + CreateFn: func(ctx context.Context, req *ps.DatabaseBranchPasswordRequest) (*ps.DatabaseBranchPassword, error) { + c.Assert(req.Replica, qt.IsTrue) + c.Assert(req.ClientTabletTargetingPolicy, qt.Equals, "ignore") + return &ps.DatabaseBranchPassword{Name: "foo"}, nil + }, + } + + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Passwords: svc}, nil + }, + } + + cmd := CreateCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "production-replica-password", "--replica", "--client-tablet-targeting-policy", "ignore"}) + err := cmd.Execute() + + c.Assert(err, qt.IsNil) + c.Assert(svc.CreateFnInvoked, qt.IsTrue) +} + func TestPassword_CreateCmd_ReplicaWithoutRole(t *testing.T) { c := qt.New(t) diff --git a/internal/cmd/password/password.go b/internal/cmd/password/password.go index fe05129fd..f3c681fbe 100644 --- a/internal/cmd/password/password.go +++ b/internal/cmd/password/password.go @@ -94,30 +94,32 @@ func passwordSelector(args []string, name string) error { type Passwords []*Password type Password struct { - PublicID string `header:"id" json:"id"` - Name string `header:"name" json:"name"` - Branch string `header:"branch" json:"branch"` - Username string `header:"username" json:"username"` - Role string `header:"role" json:"role"` - RoleDesc string `header:"role description" json:"-"` - ConnectionType string `header:"connection type" json:"connection_type"` - TTL int `header:"ttl" json:"ttl"` - Remaining int `header:"ttl_remaining" json:"-"` - CreatedAt int64 `json:"created_at"` - Expired bool `header:"expired" json:"expired"` - orig *ps.DatabaseBranchPassword + PublicID string `header:"id" json:"id"` + Name string `header:"name" json:"name"` + Branch string `header:"branch" json:"branch"` + Username string `header:"username" json:"username"` + Role string `header:"role" json:"role"` + RoleDesc string `header:"role description" json:"-"` + ConnectionType string `header:"connection type" json:"connection_type"` + ClientTabletTargetingPolicy string `header:"client tablet targeting" json:"client_tablet_targeting_policy"` + TTL int `header:"ttl" json:"ttl"` + Remaining int `header:"ttl_remaining" json:"-"` + CreatedAt int64 `json:"created_at"` + Expired bool `header:"expired" json:"expired"` + orig *ps.DatabaseBranchPassword } type passwordWithoutTTL struct { - PublicID string `header:"id" json:"id"` - Name string `header:"name" json:"name"` - Branch string `header:"branch" json:"branch"` - Username string `header:"username" json:"username"` - Role string `header:"role" json:"role"` - RoleDesc string `header:"role description" json:"-"` - ConnectionType string `header:"connection type" json:"connection_type"` - CreatedAt int64 `json:"created_at"` - orig *ps.DatabaseBranchPassword + PublicID string `header:"id" json:"id"` + Name string `header:"name" json:"name"` + Branch string `header:"branch" json:"branch"` + Username string `header:"username" json:"username"` + Role string `header:"role" json:"role"` + RoleDesc string `header:"role description" json:"-"` + ConnectionType string `header:"connection type" json:"connection_type"` + ClientTabletTargetingPolicy string `header:"client tablet targeting" json:"client_tablet_targeting_policy"` + CreatedAt int64 `json:"created_at"` + orig *ps.DatabaseBranchPassword } type PasswordWithPlainText struct { @@ -163,32 +165,34 @@ func toPassword(password *ps.DatabaseBranchPassword) *Password { ttlRemaining = max(int(time.Until(password.ExpiresAt).Seconds()), 0) } return &Password{ - Name: password.Name, - Branch: password.Branch.Name, - PublicID: password.PublicID, - Username: password.Username, - Role: password.Role, - RoleDesc: toRoleDesc(password.Role), - ConnectionType: toConnectionTypeDesc(password), - TTL: password.TTL, - Remaining: ttlRemaining, - CreatedAt: toTimestamp(password.CreatedAt), - Expired: password.TTL > 0 && ttlRemaining == 0, - orig: password, + Name: password.Name, + Branch: password.Branch.Name, + PublicID: password.PublicID, + Username: password.Username, + Role: password.Role, + RoleDesc: toRoleDesc(password.Role), + ConnectionType: toConnectionTypeDesc(password), + ClientTabletTargetingPolicy: password.ClientTabletTargetingPolicy, + TTL: password.TTL, + Remaining: ttlRemaining, + CreatedAt: toTimestamp(password.CreatedAt), + Expired: password.TTL > 0 && ttlRemaining == 0, + orig: password, } } func toPasswordWithoutTTL(password *ps.DatabaseBranchPassword) *passwordWithoutTTL { return &passwordWithoutTTL{ - Name: password.Name, - Branch: password.Branch.Name, - PublicID: password.PublicID, - Username: password.Username, - Role: password.Role, - RoleDesc: toRoleDesc(password.Role), - ConnectionType: toConnectionTypeDesc(password), - CreatedAt: toTimestamp(password.CreatedAt), - orig: password, + Name: password.Name, + Branch: password.Branch.Name, + PublicID: password.PublicID, + Username: password.Username, + Role: password.Role, + RoleDesc: toRoleDesc(password.Role), + ConnectionType: toConnectionTypeDesc(password), + ClientTabletTargetingPolicy: password.ClientTabletTargetingPolicy, + CreatedAt: toTimestamp(password.CreatedAt), + orig: password, } } diff --git a/internal/cmd/password/password_test.go b/internal/cmd/password/password_test.go index 9ffaf4ab7..d75112f2d 100644 --- a/internal/cmd/password/password_test.go +++ b/internal/cmd/password/password_test.go @@ -18,3 +18,12 @@ func TestToConnectionTypeDesc(t *testing.T) { Region: ps.Region{Slug: "eu-west"}, }), qt.Equals, "Read-only region (eu-west)") } + +func TestToPasswordClientTabletTargetingPolicy(t *testing.T) { + c := qt.New(t) + + password := &ps.DatabaseBranchPassword{Replica: true, ClientTabletTargetingPolicy: "ignore"} + + c.Assert(toPassword(password).ClientTabletTargetingPolicy, qt.Equals, "ignore") + c.Assert(toPasswordWithoutTTL(password).ClientTabletTargetingPolicy, qt.Equals, "ignore") +} diff --git a/internal/cmd/password/update.go b/internal/cmd/password/update.go index d39282e48..047080ea7 100644 --- a/internal/cmd/password/update.go +++ b/internal/cmd/password/update.go @@ -13,25 +13,28 @@ import ( "github.com/spf13/cobra" ) -// UpdateCmd updates the name or IP restrictions of a branch password. It never -// changes the password secret; use create and delete, or renew, for that. +// UpdateCmd updates the name, IP restrictions or client tablet targeting policy +// of a branch password. It never changes the password secret; use create and +// delete, or renew, for that. func UpdateCmd(ch *cmdutil.Helper) *cobra.Command { var flags struct { - name string - newName string - cidrs []string + name string + newName string + cidrs []string + clientTabletTargetingPolicy string } cmd := &cobra.Command{ Use: "update []", - Short: "Update a branch password's name or IP restrictions", - Long: `Update a branch password's name or IP restrictions. + Short: "Update a branch password's name, IP restrictions or client tablet targeting policy", + Long: `Update a branch password's name, IP restrictions or client tablet targeting policy. This does not change the password itself. To rotate a password, create a new one and delete the old one.`, Example: ` pscale password update mydb main pscale_pw_xxx --new-name reporting pscale password update mydb main --name reporting --cidrs 10.0.0.0/8,192.168.1.1/32 - pscale password update mydb main --name reporting --cidrs ""`, + pscale password update mydb main --name reporting --cidrs "" + pscale password update mydb main --name reporting --client-tablet-targeting-policy ignore`, Args: cobra.RangeArgs(2, 3), RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() @@ -44,8 +47,9 @@ one and delete the old one.`, newNameSet := cmd.Flags().Changed("new-name") cidrsSet := cmd.Flags().Changed("cidrs") - if !newNameSet && !cidrsSet { - return errors.New("at least one of --new-name or --cidrs must be provided") + clientTabletTargetingPolicySet := cmd.Flags().Changed("client-tablet-targeting-policy") + if !newNameSet && !cidrsSet && !clientTabletTargetingPolicySet { + return errors.New("at least one of --new-name, --cidrs or --client-tablet-targeting-policy must be provided") } if newNameSet && flags.newName == "" { return errors.New("--new-name cannot be empty") @@ -79,6 +83,9 @@ one and delete the old one.`, cidrs := normalizeCIDRs(flags.cidrs) updateReq.CIDRs = &cidrs } + if clientTabletTargetingPolicySet { + updateReq.ClientTabletTargetingPolicy = flags.clientTabletTargetingPolicy + } end := ch.Printer.PrintProgress(fmt.Sprintf("Updating password %s in %s/%s", printer.BoldBlue(passwordId), printer.BoldBlue(database), printer.BoldBlue(branch))) @@ -107,6 +114,9 @@ one and delete the old one.`, ch.Printer.Printf("IP restrictions: %s\n", strings.Join(password.CIDRs, ", ")) } } + if clientTabletTargetingPolicySet { + ch.Printer.Printf("Client tablet targeting: %s\n", password.ClientTabletTargetingPolicy) + } return nil } @@ -118,6 +128,8 @@ one and delete the old one.`, cmd.Flags().StringVar(&flags.newName, "new-name", "", "New name for the password") cmd.Flags().StringSliceVar(&flags.cidrs, "cidrs", nil, "Replace the IP addresses and CIDR ranges allowed to use this password. Pass an empty value to remove all restrictions") + cmd.Flags().StringVar(&flags.clientTabletTargetingPolicy, "client-tablet-targeting-policy", "", + "How a replica password handles a tablet type set by the client, such as USE @replica or mydb@replica. Allowed values: apply, ignore.") return cmd } diff --git a/internal/cmd/password/update_test.go b/internal/cmd/password/update_test.go index 349c4d6cf..e503f7515 100644 --- a/internal/cmd/password/update_test.go +++ b/internal/cmd/password/update_test.go @@ -123,6 +123,37 @@ func TestPassword_UpdateCmdClearsCIDRs(t *testing.T) { c.Assert(svc.UpdateFnInvoked, qt.IsTrue) } +func TestPassword_UpdateCmdSetsClientTabletTargetingPolicy(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + + svc := &mock.PasswordsService{ + UpdateFn: func(ctx context.Context, req *ps.UpdateDatabaseBranchPasswordRequest) (*ps.DatabaseBranchPassword, error) { + c.Assert(req.Name, qt.Equals, "") + c.Assert(req.CIDRs, qt.IsNil) + c.Assert(req.ClientTabletTargetingPolicy, qt.Equals, "apply") + return &ps.DatabaseBranchPassword{PublicID: "pscale_pw_xxx", ClientTabletTargetingPolicy: "apply"}, nil + }, + } + + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Passwords: svc}, nil + }, + } + + cmd := UpdateCmd(ch) + cmd.SetArgs([]string{"planetscale", "development", "pscale_pw_xxx", "--client-tablet-targeting-policy", "apply"}) + err := cmd.Execute() + + c.Assert(err, qt.IsNil) + c.Assert(svc.UpdateFnInvoked, qt.IsTrue) +} + func TestPassword_UpdateCmdByName(t *testing.T) { c := qt.New(t) @@ -180,6 +211,6 @@ func TestPassword_UpdateCmdRequiresAFlag(t *testing.T) { err := cmd.Execute() c.Assert(err, qt.IsNotNil) - c.Assert(err.Error(), qt.Contains, "at least one of --new-name or --cidrs") + c.Assert(err.Error(), qt.Contains, "at least one of --new-name, --cidrs or --client-tablet-targeting-policy") c.Assert(svc.UpdateFnInvoked, qt.IsFalse) } diff --git a/internal/planetscale/passwords.go b/internal/planetscale/passwords.go index 2a43a11a4..be94bfaa6 100644 --- a/internal/planetscale/passwords.go +++ b/internal/planetscale/passwords.go @@ -9,22 +9,24 @@ import ( ) type DatabaseBranchPassword struct { - PublicID string `json:"id"` - Name string `json:"name"` - Hostname string `json:"access_host_url"` - Username string `json:"username"` - Role string `json:"role"` - Actor *Actor `json:"actor"` - Branch DatabaseBranch `json:"database_branch"` - Region Region `json:"region"` - CreatedAt time.Time `json:"created_at"` - DeletedAt time.Time `json:"deleted_at"` - ExpiresAt time.Time `json:"expires_at"` - PlainText string `json:"plain_text"` - TTL int `json:"ttl_seconds"` - Renewable bool `json:"renewable"` - Replica bool `json:"replica"` - CIDRs []string `json:"cidrs"` + PublicID string `json:"id"` + Name string `json:"name"` + Hostname string `json:"access_host_url"` + Username string `json:"username"` + Role string `json:"role"` + Actor *Actor `json:"actor"` + Branch DatabaseBranch `json:"database_branch"` + Region Region `json:"region"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` + ExpiresAt time.Time `json:"expires_at"` + PlainText string `json:"plain_text"` + TTL int `json:"ttl_seconds"` + Renewable bool `json:"renewable"` + Replica bool `json:"replica"` + CIDRs []string `json:"cidrs"` + ClientTabletTargetingPolicy string `json:"client_tablet_targeting_policy"` + ClientTabletTargetingPolicyLocked bool `json:"client_tablet_targeting_policy_locked"` // ReadOnlyRegion is set client-side when the password is known to be scoped to a // Vitess read-only region (the API does not currently return a dedicated flag). @@ -34,14 +36,15 @@ type DatabaseBranchPassword struct { // DatabaseBranchPasswordRequest encapsulates the request for creating/getting/deleting a // database branch password. type DatabaseBranchPasswordRequest struct { - Organization string `json:"-"` - Database string `json:"-"` - Branch string `json:"-"` - Role string `json:"role,omitempty"` - Name string `json:"name"` - TTL int `json:"ttl,omitempty"` - Replica bool `json:"replica,omitempty"` - ReadOnlyRegionID string `json:"read_only_region_id,omitempty"` + Organization string `json:"-"` + Database string `json:"-"` + Branch string `json:"-"` + Role string `json:"role,omitempty"` + Name string `json:"name"` + TTL int `json:"ttl,omitempty"` + Replica bool `json:"replica,omitempty"` + ReadOnlyRegionID string `json:"read_only_region_id,omitempty"` + ClientTabletTargetingPolicy string `json:"client_tablet_targeting_policy,omitempty"` } // ListDatabaseBranchPasswordRequest encapsulates the request for listing all passwords @@ -63,7 +66,7 @@ type GetDatabaseBranchPasswordRequest struct { } // UpdateDatabaseBranchPasswordRequest encapsulates the request for updating a -// password's name or IP restrictions. +// password's name, IP restrictions or client tablet targeting policy. type UpdateDatabaseBranchPasswordRequest struct { Organization string `json:"-"` Database string `json:"-"` @@ -74,7 +77,8 @@ type UpdateDatabaseBranchPasswordRequest struct { Name string `json:"name,omitempty"` // CIDRs replaces the IP restrictions when set. A pointer to an empty slice // clears them. - CIDRs *[]string `json:"cidrs,omitempty"` + CIDRs *[]string `json:"cidrs,omitempty"` + ClientTabletTargetingPolicy string `json:"client_tablet_targeting_policy,omitempty"` } // DeleteDatabaseBranchPasswordRequest encapsulates the request for deleting a password @@ -168,7 +172,7 @@ func (d *passwordsService) Get(ctx context.Context, getReq *GetDatabaseBranchPas return password, nil } -// Update an existing password's name or IP restrictions. +// Update an existing password's name, IP restrictions or client tablet targeting policy. func (d *passwordsService) Update(ctx context.Context, updateReq *UpdateDatabaseBranchPasswordRequest) (*DatabaseBranchPassword, error) { pathStr := passwordBranchAPIPath(updateReq.Organization, updateReq.Database, updateReq.Branch, updateReq.PasswordId) req, err := d.client.newRequest(http.MethodPatch, pathStr, updateReq) diff --git a/internal/planetscale/passwords_test.go b/internal/planetscale/passwords_test.go index bede98b55..857fd43bd 100644 --- a/internal/planetscale/passwords_test.go +++ b/internal/planetscale/passwords_test.go @@ -107,6 +107,52 @@ func TestPasswords_CreateReplica(t *testing.T) { c.Assert(password, qt.DeepEquals, want) } +func TestPasswords_CreateClientTabletTargetingPolicy(t *testing.T) { + c := qt.New(t) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var body map[string]any + c.Assert(json.NewDecoder(r.Body).Decode(&body), qt.IsNil) + c.Assert(body["replica"], qt.Equals, true) + c.Assert(body["client_tablet_targeting_policy"], qt.Equals, "ignore") + + w.WriteHeader(200) + out := fmt.Sprintf(`{ + "id": "%s", + "role": "reader", + "name": "planetscale-go-test-replica-password", + "replica": true, + "client_tablet_targeting_policy": "ignore", + "client_tablet_targeting_policy_locked": true +}`, testPasswordID) + _, err := w.Write([]byte(out)) + c.Assert(err, qt.IsNil) + })) + + client, err := NewClient(WithBaseURL(ts.URL)) + c.Assert(err, qt.IsNil) + + password, err := client.Passwords.Create(context.Background(), &DatabaseBranchPasswordRequest{ + Organization: "my-org", + Database: "my-db", + Branch: "my-branch", + Role: "reader", + Replica: true, + ClientTabletTargetingPolicy: "ignore", + }) + + want := &DatabaseBranchPassword{ + Name: "planetscale-go-test-replica-password", + PublicID: testPasswordID, + Role: "reader", + Replica: true, + ClientTabletTargetingPolicy: "ignore", + ClientTabletTargetingPolicyLocked: true, + } + + c.Assert(err, qt.IsNil) + c.Assert(password, qt.DeepEquals, want) +} + func TestPasswords_List(t *testing.T) { c := qt.New(t) @@ -411,6 +457,35 @@ func TestPasswords_UpdateClearsCIDRs(t *testing.T) { c.Assert(password.CIDRs, qt.HasLen, 0) } +func TestPasswords_UpdateClientTabletTargetingPolicy(t *testing.T) { + c := qt.New(t) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var body map[string]any + c.Assert(json.NewDecoder(r.Body).Decode(&body), qt.IsNil) + c.Assert(body, qt.DeepEquals, map[string]any{"client_tablet_targeting_policy": "apply"}) + + w.WriteHeader(200) + out := fmt.Sprintf(`{"id":"%s","name":"planetscale-go-test-password","client_tablet_targeting_policy":"apply"}`, testPasswordID) + _, err := w.Write([]byte(out)) + c.Assert(err, qt.IsNil) + })) + + client, err := NewClient(WithBaseURL(ts.URL)) + c.Assert(err, qt.IsNil) + + password, err := client.Passwords.Update(context.Background(), &UpdateDatabaseBranchPasswordRequest{ + Organization: "my-org", + Database: "planetscale-go-test-db", + Branch: "my-branch", + PasswordId: testPasswordID, + ClientTabletTargetingPolicy: "apply", + }) + + c.Assert(err, qt.IsNil) + c.Assert(password.ClientTabletTargetingPolicy, qt.Equals, "apply") +} + func TestPasswords_Renew(t *testing.T) { c := qt.New(t) From dcd7374b90cbd5eb639dd7ad573210e289cb5767 Mon Sep 17 00:00:00 2001 From: Max Englander Date: Sat, 21 Nov 2026 15:22:42 -0500 Subject: [PATCH 2/4] Accept apply or ignore for the policy on password create Passwords can switch between apply and ignore later, so drop the client_tablet_targeting_policy_locked field. Co-Authored-By: Claude Opus 5.5 --- internal/cmd/password/create.go | 2 +- internal/cmd/password/create_test.go | 50 ++++++++++++++------------ internal/planetscale/passwords.go | 35 +++++++++--------- internal/planetscale/passwords_test.go | 14 ++++---- 4 files changed, 51 insertions(+), 50 deletions(-) diff --git a/internal/cmd/password/create.go b/internal/cmd/password/create.go index 482d6a83f..392192f52 100644 --- a/internal/cmd/password/create.go +++ b/internal/cmd/password/create.go @@ -124,7 +124,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { cmd.Flags().StringVar(&flags.readOnlyRegion, "read-only-region", "", "Create a password scoped to a Vitess read-only region (region slug, display name, or id). List regions with: pscale database read-only-regions list .") cmd.Flags().StringVar(&flags.clientTabletTargetingPolicy, "client-tablet-targeting-policy", "", - "How a replica password handles a tablet type set by the client, such as USE @replica or mydb@replica. Allowed value: ignore.") + "How a replica password handles a tablet type set by the client, such as USE @replica or mydb@replica. Allowed values: apply, ignore.") return cmd } diff --git a/internal/cmd/password/create_test.go b/internal/cmd/password/create_test.go index 1d6a742e4..b7d71c6c8 100644 --- a/internal/cmd/password/create_test.go +++ b/internal/cmd/password/create_test.go @@ -335,33 +335,37 @@ func TestPassword_CreateCmd_Replica(t *testing.T) { } func TestPassword_CreateCmd_ClientTabletTargetingPolicy(t *testing.T) { - c := qt.New(t) + for _, policy := range []string{"apply", "ignore"} { + t.Run(policy, func(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + + svc := &mock.PasswordsService{ + CreateFn: func(ctx context.Context, req *ps.DatabaseBranchPasswordRequest) (*ps.DatabaseBranchPassword, error) { + c.Assert(req.Replica, qt.IsTrue) + c.Assert(req.ClientTabletTargetingPolicy, qt.Equals, policy) + return &ps.DatabaseBranchPassword{Name: "foo"}, nil + }, + } - format := printer.JSON - p := printer.NewPrinter(&format) + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Passwords: svc}, nil + }, + } - svc := &mock.PasswordsService{ - CreateFn: func(ctx context.Context, req *ps.DatabaseBranchPasswordRequest) (*ps.DatabaseBranchPassword, error) { - c.Assert(req.Replica, qt.IsTrue) - c.Assert(req.ClientTabletTargetingPolicy, qt.Equals, "ignore") - return &ps.DatabaseBranchPassword{Name: "foo"}, nil - }, - } + cmd := CreateCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "production-replica-password", "--replica", "--client-tablet-targeting-policy", policy}) + err := cmd.Execute() - ch := &cmdutil.Helper{ - Printer: p, - Config: &config.Config{Organization: "planetscale"}, - Client: func() (*ps.Client, error) { - return &ps.Client{Passwords: svc}, nil - }, + c.Assert(err, qt.IsNil) + c.Assert(svc.CreateFnInvoked, qt.IsTrue) + }) } - - cmd := CreateCmd(ch) - cmd.SetArgs([]string{"planetscale", "main", "production-replica-password", "--replica", "--client-tablet-targeting-policy", "ignore"}) - err := cmd.Execute() - - c.Assert(err, qt.IsNil) - c.Assert(svc.CreateFnInvoked, qt.IsTrue) } func TestPassword_CreateCmd_ReplicaWithoutRole(t *testing.T) { diff --git a/internal/planetscale/passwords.go b/internal/planetscale/passwords.go index be94bfaa6..45ace5e35 100644 --- a/internal/planetscale/passwords.go +++ b/internal/planetscale/passwords.go @@ -9,24 +9,23 @@ import ( ) type DatabaseBranchPassword struct { - PublicID string `json:"id"` - Name string `json:"name"` - Hostname string `json:"access_host_url"` - Username string `json:"username"` - Role string `json:"role"` - Actor *Actor `json:"actor"` - Branch DatabaseBranch `json:"database_branch"` - Region Region `json:"region"` - CreatedAt time.Time `json:"created_at"` - DeletedAt time.Time `json:"deleted_at"` - ExpiresAt time.Time `json:"expires_at"` - PlainText string `json:"plain_text"` - TTL int `json:"ttl_seconds"` - Renewable bool `json:"renewable"` - Replica bool `json:"replica"` - CIDRs []string `json:"cidrs"` - ClientTabletTargetingPolicy string `json:"client_tablet_targeting_policy"` - ClientTabletTargetingPolicyLocked bool `json:"client_tablet_targeting_policy_locked"` + PublicID string `json:"id"` + Name string `json:"name"` + Hostname string `json:"access_host_url"` + Username string `json:"username"` + Role string `json:"role"` + Actor *Actor `json:"actor"` + Branch DatabaseBranch `json:"database_branch"` + Region Region `json:"region"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` + ExpiresAt time.Time `json:"expires_at"` + PlainText string `json:"plain_text"` + TTL int `json:"ttl_seconds"` + Renewable bool `json:"renewable"` + Replica bool `json:"replica"` + CIDRs []string `json:"cidrs"` + ClientTabletTargetingPolicy string `json:"client_tablet_targeting_policy"` // ReadOnlyRegion is set client-side when the password is known to be scoped to a // Vitess read-only region (the API does not currently return a dedicated flag). diff --git a/internal/planetscale/passwords_test.go b/internal/planetscale/passwords_test.go index 857fd43bd..d8fcbbef5 100644 --- a/internal/planetscale/passwords_test.go +++ b/internal/planetscale/passwords_test.go @@ -121,8 +121,7 @@ func TestPasswords_CreateClientTabletTargetingPolicy(t *testing.T) { "role": "reader", "name": "planetscale-go-test-replica-password", "replica": true, - "client_tablet_targeting_policy": "ignore", - "client_tablet_targeting_policy_locked": true + "client_tablet_targeting_policy": "ignore" }`, testPasswordID) _, err := w.Write([]byte(out)) c.Assert(err, qt.IsNil) @@ -141,12 +140,11 @@ func TestPasswords_CreateClientTabletTargetingPolicy(t *testing.T) { }) want := &DatabaseBranchPassword{ - Name: "planetscale-go-test-replica-password", - PublicID: testPasswordID, - Role: "reader", - Replica: true, - ClientTabletTargetingPolicy: "ignore", - ClientTabletTargetingPolicyLocked: true, + Name: "planetscale-go-test-replica-password", + PublicID: testPasswordID, + Role: "reader", + Replica: true, + ClientTabletTargetingPolicy: "ignore", } c.Assert(err, qt.IsNil) From 71ca6770c1934118ed495f16c1ebc7d7266430bd Mon Sep 17 00:00:00 2001 From: Max Englander Date: Sat, 21 Nov 2026 16:04:20 -0500 Subject: [PATCH 3/4] Test a null client tablet targeting policy The API can return null for the policy. It decodes to an empty string, so show and list print an empty value. Co-Authored-By: Claude Opus 5.5 --- internal/planetscale/passwords_test.go | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/internal/planetscale/passwords_test.go b/internal/planetscale/passwords_test.go index d8fcbbef5..05b971314 100644 --- a/internal/planetscale/passwords_test.go +++ b/internal/planetscale/passwords_test.go @@ -381,6 +381,36 @@ func TestPasswords_Get(t *testing.T) { c.Assert(password, qt.DeepEquals, want) } +func TestPasswords_GetNullClientTabletTargetingPolicy(t *testing.T) { + c := qt.New(t) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + out := fmt.Sprintf(`{ + "id": "%s", + "role": "reader", + "name": "planetscale-go-test-replica-password", + "replica": true, + "client_tablet_targeting_policy": null +}`, testPasswordID) + _, err := w.Write([]byte(out)) + c.Assert(err, qt.IsNil) + })) + + client, err := NewClient(WithBaseURL(ts.URL)) + c.Assert(err, qt.IsNil) + + password, err := client.Passwords.Get(context.Background(), &GetDatabaseBranchPasswordRequest{ + Organization: "my-org", + Database: "planetscale-go-test-db", + Branch: "my-branch", + PasswordId: testPasswordID, + }) + + c.Assert(err, qt.IsNil) + c.Assert(password.ClientTabletTargetingPolicy, qt.Equals, "") +} + func TestPasswords_Update(t *testing.T) { c := qt.New(t) From e519e73b9b20545d75d7cf96761a0e4c0f62fc1f Mon Sep 17 00:00:00 2001 From: Max Englander Date: Sat, 21 Nov 2026 16:27:14 -0500 Subject: [PATCH 4/4] Test a password response without a client tablet targeting policy Cover the field being missing as well as null. Co-Authored-By: Claude Opus 5.5 --- internal/planetscale/passwords_test.go | 58 +++++++++++++++----------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/internal/planetscale/passwords_test.go b/internal/planetscale/passwords_test.go index 05b971314..fc1a1eb5b 100644 --- a/internal/planetscale/passwords_test.go +++ b/internal/planetscale/passwords_test.go @@ -381,34 +381,42 @@ func TestPasswords_Get(t *testing.T) { c.Assert(password, qt.DeepEquals, want) } -func TestPasswords_GetNullClientTabletTargetingPolicy(t *testing.T) { - c := qt.New(t) - - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - out := fmt.Sprintf(`{ +func TestPasswords_GetWithoutClientTabletTargetingPolicy(t *testing.T) { + for name, field := range map[string]string{ + "missing": "", + "null": `, + "client_tablet_targeting_policy": null`, + } { + t.Run(name, func(t *testing.T) { + c := qt.New(t) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + out := fmt.Sprintf(`{ "id": "%s", "role": "reader", "name": "planetscale-go-test-replica-password", - "replica": true, - "client_tablet_targeting_policy": null -}`, testPasswordID) - _, err := w.Write([]byte(out)) - c.Assert(err, qt.IsNil) - })) - - client, err := NewClient(WithBaseURL(ts.URL)) - c.Assert(err, qt.IsNil) - - password, err := client.Passwords.Get(context.Background(), &GetDatabaseBranchPasswordRequest{ - Organization: "my-org", - Database: "planetscale-go-test-db", - Branch: "my-branch", - PasswordId: testPasswordID, - }) - - c.Assert(err, qt.IsNil) - c.Assert(password.ClientTabletTargetingPolicy, qt.Equals, "") + "replica": true%s +}`, testPasswordID, field) + _, err := w.Write([]byte(out)) + c.Assert(err, qt.IsNil) + })) + defer ts.Close() + + client, err := NewClient(WithBaseURL(ts.URL)) + c.Assert(err, qt.IsNil) + + password, err := client.Passwords.Get(context.Background(), &GetDatabaseBranchPasswordRequest{ + Organization: "my-org", + Database: "planetscale-go-test-db", + Branch: "my-branch", + PasswordId: testPasswordID, + }) + + c.Assert(err, qt.IsNil) + c.Assert(password.ClientTabletTargetingPolicy, qt.Equals, "") + }) + } } func TestPasswords_Update(t *testing.T) {