From 5c1a239c90a91f93dbce84e7ce9094515aab1da1 Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Fri, 13 Nov 2026 13:18:09 +0100 Subject: [PATCH 1/7] Add --storage-iops and --storage-throughput to keyspace update-settings --- internal/cmd/keyspace/keyspace.go | 2 + internal/cmd/keyspace/settings.go | 8 ++ internal/cmd/keyspace/update_settings.go | 24 ++++- internal/cmd/keyspace/update_settings_test.go | 90 +++++++++++++++++++ internal/planetscale/keyspaces.go | 8 ++ 5 files changed, 131 insertions(+), 1 deletion(-) diff --git a/internal/cmd/keyspace/keyspace.go b/internal/cmd/keyspace/keyspace.go index 7ca665af..6ff38dff 100644 --- a/internal/cmd/keyspace/keyspace.go +++ b/internal/cmd/keyspace/keyspace.go @@ -87,6 +87,8 @@ type Storage struct { DiskScalingStrategy string `header:"disk scaling strategy" json:"disk_scaling_strategy"` StorageBytes string `header:"storage" json:"storage_bytes"` MaxStorageBytes string `header:"max storage" json:"max_storage_bytes"` + IOPS string `header:"iops" json:"iops"` + Throughput string `header:"throughput" json:"throughput"` } func toKeyspaces(keyspaces []*ps.Keyspace) []*Keyspace { diff --git a/internal/cmd/keyspace/settings.go b/internal/cmd/keyspace/settings.go index abbf3a21..b959ca2b 100644 --- a/internal/cmd/keyspace/settings.go +++ b/internal/cmd/keyspace/settings.go @@ -60,6 +60,8 @@ func toKeyspaceSettings(ks *ps.Keyspace) *KeyspaceSettings { DiskScalingStrategy: "not set", StorageBytes: "not set", MaxStorageBytes: "not set", + IOPS: "not set", + Throughput: "not set", }, orig: ks, } @@ -120,6 +122,12 @@ func toKeyspaceSettings(ks *ps.Keyspace) *KeyspaceSettings { settings.Storage.MaxStorageBytes += " (set by PlanetScale staff)" } } + if ks.Storage.IOPS != nil { + settings.Storage.IOPS = strconv.FormatInt(*ks.Storage.IOPS, 10) + } + if ks.Storage.ThroughputMiBs != nil { + settings.Storage.Throughput = fmt.Sprintf("%d MiB/s", *ks.Storage.ThroughputMiBs) + } } return settings diff --git a/internal/cmd/keyspace/update_settings.go b/internal/cmd/keyspace/update_settings.go index 855be348..65ea4a5d 100644 --- a/internal/cmd/keyspace/update_settings.go +++ b/internal/cmd/keyspace/update_settings.go @@ -33,6 +33,8 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { diskScalingStrategy string maxStorage string storage string + storageIOPS int64 + storageThroughput int64 interactive bool } @@ -70,7 +72,9 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { strategyChanged := cmd.Flags().Changed("disk-scaling-strategy") maxStorageChanged := cmd.Flags().Changed("max-storage") storageChanged := cmd.Flags().Changed("storage") - diskStorageChanged := strategyChanged || maxStorageChanged || storageChanged + storageIOPSChanged := cmd.Flags().Changed("storage-iops") + storageThroughputChanged := cmd.Flags().Changed("storage-throughput") + diskStorageChanged := strategyChanged || maxStorageChanged || storageChanged || storageIOPSChanged || storageThroughputChanged if !rdcChanged && !vrfChanged && !throttlerChanged && !maxRolloutChanged && !diskStorageChanged { ch.Printer.Println("No changes were requested. No update performed.") @@ -104,6 +108,14 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { } } + if storageIOPSChanged && flags.storageIOPS <= 0 { + return errors.New("--storage-iops must be greater than 0") + } + + if storageThroughputChanged && flags.storageThroughput <= 0 { + return errors.New("--storage-throughput must be greater than 0") + } + var storageBytes int64 if storageChanged { storageBytes, err = parseStorageSize(flags.storage) @@ -193,6 +205,14 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { if storageChanged { updateReq.Storage.StorageBytes = &storageBytes } + + if storageIOPSChanged { + updateReq.Storage.IOPS = &flags.storageIOPS + } + + if storageThroughputChanged { + updateReq.Storage.ThroughputMiBs = &flags.storageThroughput + } } k, err := updateKeyspaceSettings(ctx, client, updateReq) @@ -216,6 +236,8 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { cmd.Flags().StringVar(&flags.diskScalingStrategy, "disk-scaling-strategy", "grow", fmt.Sprintf("The disk scaling strategy (%s). 'grow' lets dedicated disks grow automatically up to --max-storage; 'disable' turns autoscaling off; 'shrink' recreates disks at --storage and then disables autoscaling.", strings.Join(diskScalingStrategies, ", "))) cmd.Flags().StringVar(&flags.maxStorage, "max-storage", "", "The maximum size that dedicated disks may autoscale to, in bytes or with a unit such as 200GiB or 1TiB. Must be at least 12 GiB and no smaller than the current disk size. Cannot be raised above the organization default (4 TiB, or 16 TiB for managed tenancy). Read-only while PlanetScale staff have raised it above that default.") cmd.Flags().StringVar(&flags.storage, "storage", "", "The disk size to recreate disks at, in bytes or with a unit such as 200GiB or 1TiB. Must be a multiple of 1 GiB. Only accepted when the disk scaling strategy is shrink, either passed with --disk-scaling-strategy or already set on the keyspace.") + cmd.Flags().Int64Var(&flags.storageIOPS, "storage-iops", 0, "The provisioned IOPS for each AWS gp3 disk, from 3000 up to 500 per GiB of disk size (at most 80000). Applied without replacing disks.") + cmd.Flags().Int64Var(&flags.storageThroughput, "storage-throughput", 0, "The provisioned throughput in MiB/s for each AWS gp3 disk, from 125 up to 0.25 per provisioned IOPS (at most 2000). Applied without replacing disks.") cmd.Flags().BoolVarP(&flags.interactive, "interactive", "i", false, "Run the command in interactive mode") _ = cmd.RegisterFlagCompletionFunc("disk-scaling-strategy", cobra.FixedCompletions(diskScalingStrategies, cobra.ShellCompDirectiveNoFileComp)) diff --git a/internal/cmd/keyspace/update_settings_test.go b/internal/cmd/keyspace/update_settings_test.go index a4fbb130..f4299860 100644 --- a/internal/cmd/keyspace/update_settings_test.go +++ b/internal/cmd/keyspace/update_settings_test.go @@ -1211,6 +1211,96 @@ func TestKeyspace_UpdateSettingsCmd_Storage(t *testing.T) { c.Assert(buf.String(), qt.JSONEquals, updatedKs) } +func TestKeyspace_UpdateSettingsCmd_StorageIOPS(t *testing.T) { + c := qt.New(t) + + var buf bytes.Buffer + format := printer.JSON + + p := printer.NewPrinter(&format) + p.SetResourceOutput(&buf) + + org := "planetscale" + db := "planetscale" + branch := "main" + keyspace := "sharded" + + ts := time.Now() + iops := int64(6000) + throughput := int64(500) + + updatedKs := &ps.Keyspace{ + ID: "ks1", + Name: keyspace, + CreatedAt: ts, + UpdatedAt: ts, + Storage: &ps.KeyspaceStorage{ + StorageBytes: 107374182400, + MaxStorageBytes: 4398046511104, + DiskScalingStrategy: "grow", + IOPS: &iops, + ThroughputMiBs: &throughput, + }, + } + + svc := &mock.KeyspacesService{ + UpdateSettingsFn: func(ctx context.Context, req *ps.UpdateKeyspaceSettingsRequest) (*ps.Keyspace, error) { + c.Assert(req.Storage, qt.Not(qt.IsNil)) + c.Assert(req.Storage.IOPS, qt.Not(qt.IsNil)) + c.Assert(*req.Storage.IOPS, qt.Equals, iops) + c.Assert(req.Storage.ThroughputMiBs, qt.Not(qt.IsNil)) + c.Assert(*req.Storage.ThroughputMiBs, qt.Equals, throughput) + c.Assert(req.Storage.DiskScalingStrategy, qt.IsNil) + c.Assert(req.Storage.MaxStorageBytes, qt.IsNil) + c.Assert(req.Storage.StorageBytes, qt.IsNil) + c.Assert(req.Throttler, qt.IsNil) + + return updatedKs, nil + }, + } + + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{ + Organization: org, + }, + Client: func() (*ps.Client, error) { + return &ps.Client{ + Keyspaces: svc, + }, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs([]string{db, branch, keyspace, "--storage-iops=6000", "--storage-throughput=500"}) + err := cmd.Execute() + c.Assert(err, qt.IsNil) + c.Assert(svc.UpdateSettingsFnInvoked, qt.IsTrue) + c.Assert(buf.String(), qt.JSONEquals, updatedKs) +} + +func TestKeyspace_UpdateSettingsCmd_StorageIOPSMustBePositive(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + + svc := &mock.KeyspacesService{} + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "sharded", "--storage-iops=0"}) + err := cmd.Execute() + c.Assert(err, qt.ErrorMatches, "--storage-iops must be greater than 0") + c.Assert(svc.UpdateSettingsFnInvoked, qt.IsFalse) +} + func TestKeyspace_UpdateSettingsCmd_MaxStorageAcceptsHumanReadableSize(t *testing.T) { c := qt.New(t) diff --git a/internal/planetscale/keyspaces.go b/internal/planetscale/keyspaces.go index d85d1877..d9ea03bf 100644 --- a/internal/planetscale/keyspaces.go +++ b/internal/planetscale/keyspaces.go @@ -45,6 +45,12 @@ type KeyspaceStorage struct { // DiskScalingStrategy is the disk scaling strategy: "grow", "disable" or // "shrink". DiskScalingStrategy string `json:"disk_scaling_strategy"` + // IOPS is the provisioned IOPS for each disk. Only returned when + // configurable IOPS are enabled for the organization. + IOPS *int64 `json:"iops,omitempty"` + // ThroughputMiBs is the provisioned throughput in MiB/s for each disk. + // Only returned when configurable IOPS are enabled for the organization. + ThroughputMiBs *int64 `json:"throughput_mibs,omitempty"` } type ReadOnlyRegionKeyspace struct { @@ -270,6 +276,8 @@ type KeyspaceStorageUpdate struct { DiskScalingStrategy *string `json:"disk_scaling_strategy,omitempty"` MaxStorageBytes *int64 `json:"max_storage_bytes,omitempty"` StorageBytes *int64 `json:"storage_bytes,omitempty"` + IOPS *int64 `json:"iops,omitempty"` + ThroughputMiBs *int64 `json:"throughput_mibs,omitempty"` } // KeyspacesService is an interface for interacting with the keyspace endpoints of the PlanetScale API From ac62b46f1c098603fb712d9697bf2e64689bcd0f Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Fri, 13 Nov 2026 14:48:44 +0100 Subject: [PATCH 2/7] Fail keyspace update-settings when IOPS or throughput are not applied The API ignores storage iops and throughput_mibs for organizations without configurable disk I/O, and API versions without the feature drop the unknown keys. Both still answer with success, so the command exited 0 although nothing changed. Check the returned keyspace and return an error when a requested value is missing or differs. --- internal/cmd/keyspace/update_settings.go | 44 +++++++++++ internal/cmd/keyspace/update_settings_test.go | 76 +++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/internal/cmd/keyspace/update_settings.go b/internal/cmd/keyspace/update_settings.go index 65ea4a5d..4d5b11de 100644 --- a/internal/cmd/keyspace/update_settings.go +++ b/internal/cmd/keyspace/update_settings.go @@ -222,6 +222,21 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { end() + // The API ignores IOPS and throughput for organizations without + // configurable disk I/O and still answers with success, so check + // that the returned keyspace has the requested values. + if storageIOPSChanged { + if err := checkStorageIOApplied("--storage-iops", flags.storageIOPS, storageIOPS(k)); err != nil { + return err + } + } + + if storageThroughputChanged { + if err := checkStorageIOApplied("--storage-throughput", flags.storageThroughput, storageThroughputMiBs(k)); err != nil { + return err + } + } + return ch.Printer.PrintResource(toKeyspaceSettings(k)) }, } @@ -417,6 +432,35 @@ func updateKeyspaceSettings(ctx context.Context, client *ps.Client, updateReq *p return k, nil } +func storageIOPS(k *ps.Keyspace) *int64 { + if k.Storage == nil { + return nil + } + return k.Storage.IOPS +} + +func storageThroughputMiBs(k *ps.Keyspace) *int64 { + if k.Storage == nil { + return nil + } + return k.Storage.ThroughputMiBs +} + +// checkStorageIOApplied returns an error when the keyspace returned by the API +// does not have the requested IOPS or throughput value. The API leaves the +// value out when configurable disk I/O is not enabled for the organization. +func checkStorageIOApplied(flag string, want int64, got *int64) error { + if got == nil { + return fmt.Errorf("%s was not applied: configurable disk IOPS and throughput are not enabled for this keyspace. Any other requested settings were applied", flag) + } + + if *got != want { + return fmt.Errorf("%s was not applied: the keyspace reports %d instead of %d. Any other requested settings were applied", flag, *got, want) + } + + return nil +} + // Helper function for translating the API value to a more semantic string. func constraintsToStrategy(constraint string) string { switch constraint { diff --git a/internal/cmd/keyspace/update_settings_test.go b/internal/cmd/keyspace/update_settings_test.go index f4299860..73a7dbc4 100644 --- a/internal/cmd/keyspace/update_settings_test.go +++ b/internal/cmd/keyspace/update_settings_test.go @@ -1301,6 +1301,82 @@ func TestKeyspace_UpdateSettingsCmd_StorageIOPSMustBePositive(t *testing.T) { c.Assert(svc.UpdateSettingsFnInvoked, qt.IsFalse) } +func TestKeyspace_UpdateSettingsCmd_StorageIONotApplied(t *testing.T) { + iops := int64(3000) + throughput := int64(125) + + tests := []struct { + name string + args []string + storage *ps.KeyspaceStorage + wantErr string + }{ + { + // The API leaves the values out when configurable disk I/O is + // not enabled for the organization. + name: "iops left out of the response", + args: []string{"--storage-iops=6000"}, + storage: &ps.KeyspaceStorage{DiskScalingStrategy: "grow"}, + wantErr: "--storage-iops was not applied: configurable disk IOPS and throughput are not enabled for this keyspace. Any other requested settings were applied", + }, + { + name: "throughput left out of the response", + args: []string{"--storage-throughput=500"}, + storage: &ps.KeyspaceStorage{DiskScalingStrategy: "grow"}, + wantErr: "--storage-throughput was not applied: configurable disk IOPS and throughput are not enabled for this keyspace. Any other requested settings were applied", + }, + { + name: "no storage in the response", + args: []string{"--storage-iops=6000"}, + wantErr: "--storage-iops was not applied: .*", + }, + { + name: "iops differs from the request", + args: []string{"--storage-iops=6000"}, + storage: &ps.KeyspaceStorage{IOPS: &iops, ThroughputMiBs: &throughput}, + wantErr: "--storage-iops was not applied: the keyspace reports 3000 instead of 6000. Any other requested settings were applied", + }, + { + name: "throughput differs from the request", + args: []string{"--storage-throughput=500"}, + storage: &ps.KeyspaceStorage{IOPS: &iops, ThroughputMiBs: &throughput}, + wantErr: "--storage-throughput was not applied: the keyspace reports 125 instead of 500. Any other requested settings were applied", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := qt.New(t) + + var buf bytes.Buffer + format := printer.JSON + p := printer.NewPrinter(&format) + p.SetResourceOutput(&buf) + + svc := &mock.KeyspacesService{ + UpdateSettingsFn: func(ctx context.Context, req *ps.UpdateKeyspaceSettingsRequest) (*ps.Keyspace, error) { + return &ps.Keyspace{ID: "ks1", Name: "sharded", Storage: tt.storage}, nil + }, + } + + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs(append([]string{"planetscale", "main", "sharded"}, tt.args...)) + err := cmd.Execute() + c.Assert(err, qt.ErrorMatches, tt.wantErr) + c.Assert(svc.UpdateSettingsFnInvoked, qt.IsTrue) + c.Assert(buf.String(), qt.Equals, "") + }) + } +} + func TestKeyspace_UpdateSettingsCmd_MaxStorageAcceptsHumanReadableSize(t *testing.T) { c := qt.New(t) From 1dd00d0d495d59fbdd2c5d25666ed04ccca4d91a Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Fri, 13 Nov 2026 14:49:31 +0100 Subject: [PATCH 3/7] Show keyspace IOPS limits and whether they can be configured The API returns iops_configurable, min_iops, max_iops and min_throughput_mibs next to the IOPS and throughput values. The CLI dropped them, so --format json had no way to pick a valid value. Keep the fields, and mark storage class defaults that cannot be changed in the settings table. Add wire tests for the IOPS request and response. --- internal/cmd/keyspace/settings.go | 10 ++++ internal/cmd/keyspace/settings_test.go | 49 ++++++++++++++++ internal/planetscale/keyspaces.go | 13 +++++ internal/planetscale/keyspaces_test.go | 79 ++++++++++++++++++++++++++ 4 files changed, 151 insertions(+) diff --git a/internal/cmd/keyspace/settings.go b/internal/cmd/keyspace/settings.go index b959ca2b..78a362bb 100644 --- a/internal/cmd/keyspace/settings.go +++ b/internal/cmd/keyspace/settings.go @@ -128,6 +128,16 @@ func toKeyspaceSettings(ks *ps.Keyspace) *KeyspaceSettings { if ks.Storage.ThroughputMiBs != nil { settings.Storage.Throughput = fmt.Sprintf("%d MiB/s", *ks.Storage.ThroughputMiBs) } + // The API reports the storage class default even when the values + // cannot be changed, so the table says so. + if ks.Storage.IOPSConfigurable != nil && !*ks.Storage.IOPSConfigurable { + if ks.Storage.IOPS != nil { + settings.Storage.IOPS += " (not configurable)" + } + if ks.Storage.ThroughputMiBs != nil { + settings.Storage.Throughput += " (not configurable)" + } + } } return settings diff --git a/internal/cmd/keyspace/settings_test.go b/internal/cmd/keyspace/settings_test.go index 48ce4f44..6b308397 100644 --- a/internal/cmd/keyspace/settings_test.go +++ b/internal/cmd/keyspace/settings_test.go @@ -265,4 +265,53 @@ func TestBuildKeyspaceSettings(t *testing.T) { staffSettings := toKeyspaceSettings(staffKs) c.Assert(staffSettings.Storage.MaxStorageBytes, qt.Equals, "8.0 TiB (set by PlanetScale staff)") + + // Configurable IOPS and throughput are shown as they are. + configurable := true + iops := int64(6000) + throughput := int64(500) + ioKs := &ps.Keyspace{ + ID: "ks1", + Name: "test", + Storage: &ps.KeyspaceStorage{ + IOPSConfigurable: &configurable, + IOPS: &iops, + ThroughputMiBs: &throughput, + }, + } + + ioSettings := toKeyspaceSettings(ioKs) + c.Assert(ioSettings.Storage.IOPS, qt.Equals, "6000") + c.Assert(ioSettings.Storage.Throughput, qt.Equals, "500 MiB/s") + + // Storage class defaults that cannot be changed are marked. + notConfigurable := false + fixedKs := &ps.Keyspace{ + ID: "ks1", + Name: "test", + Storage: &ps.KeyspaceStorage{ + IOPSConfigurable: ¬Configurable, + IOPS: &iops, + ThroughputMiBs: &throughput, + }, + } + + fixedSettings := toKeyspaceSettings(fixedKs) + c.Assert(fixedSettings.Storage.IOPS, qt.Equals, "6000 (not configurable)") + c.Assert(fixedSettings.Storage.Throughput, qt.Equals, "500 MiB/s (not configurable)") + + // Without a value there is nothing to mark. + fixedNilKs := &ps.Keyspace{ + ID: "ks1", + Name: "test", + Storage: &ps.KeyspaceStorage{ + IOPSConfigurable: ¬Configurable, + }, + } + + fixedNilSettings := toKeyspaceSettings(fixedNilKs) + c.Assert(fixedNilSettings.Storage.IOPS, qt.Equals, "not set") + c.Assert(fixedNilSettings.Storage.Throughput, qt.Equals, "not set") + c.Assert(nilSettings.Storage.IOPS, qt.Equals, "not set") + c.Assert(nilSettings.Storage.Throughput, qt.Equals, "not set") } diff --git a/internal/planetscale/keyspaces.go b/internal/planetscale/keyspaces.go index d9ea03bf..a0f0a5c3 100644 --- a/internal/planetscale/keyspaces.go +++ b/internal/planetscale/keyspaces.go @@ -51,6 +51,19 @@ type KeyspaceStorage struct { // ThroughputMiBs is the provisioned throughput in MiB/s for each disk. // Only returned when configurable IOPS are enabled for the organization. ThroughputMiBs *int64 `json:"throughput_mibs,omitempty"` + // IOPSConfigurable is true when IOPS and throughput can be configured for + // the keyspace's disks. The fields below are only returned when + // configurable IOPS are enabled for the organization, and the limits are + // null when IOPSConfigurable is false. + IOPSConfigurable *bool `json:"iops_configurable,omitempty"` + // MinIOPS is the minimum IOPS that can be configured. + MinIOPS *int64 `json:"min_iops,omitempty"` + // MaxIOPS is the maximum IOPS that can be configured for the current disk + // size. + MaxIOPS *int64 `json:"max_iops,omitempty"` + // MinThroughputMiBs is the minimum throughput in MiB/s that can be + // configured. The maximum is 0.25 MiB/s per provisioned IOPS, up to 2000. + MinThroughputMiBs *int64 `json:"min_throughput_mibs,omitempty"` } type ReadOnlyRegionKeyspace struct { diff --git a/internal/planetscale/keyspaces_test.go b/internal/planetscale/keyspaces_test.go index 048b3117..818cb817 100644 --- a/internal/planetscale/keyspaces_test.go +++ b/internal/planetscale/keyspaces_test.go @@ -633,3 +633,82 @@ func TestKeyspaces_UpdateSettingsStorage(t *testing.T) { c.Assert(keyspace.Storage.StorageBytes, qt.Equals, int64(214748364800)) c.Assert(keyspace.Storage.MaxStorageBytes, qt.Equals, int64(4398046511104)) } + +func TestKeyspaces_UpdateSettingsStorageIO(t *testing.T) { + c := qt.New(t) + + var body string + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + c.Assert(r.Method, qt.Equals, http.MethodPatch) + + raw, err := io.ReadAll(r.Body) + c.Assert(err, qt.IsNil) + body = string(raw) + + w.WriteHeader(200) + out := `{"type":"Keyspace","id":"thisisanid","name":"planetscale","storage":{"storage_bytes":107374182400,"max_storage_bytes":4398046511104,"disk_scaling_strategy":"grow","iops_configurable":true,"iops":6000,"throughput_mibs":500,"min_iops":3000,"max_iops":10000,"min_throughput_mibs":125}}` + _, err = w.Write([]byte(out)) + c.Assert(err, qt.IsNil) + })) + + client, err := NewClient(WithBaseURL(ts.URL)) + c.Assert(err, qt.IsNil) + + ctx := context.Background() + iops := int64(6000) + throughput := int64(500) + + keyspace, err := client.Keyspaces.UpdateSettings(ctx, &UpdateKeyspaceSettingsRequest{ + Organization: "foo", + Database: "bar", + Branch: "baz", + Keyspace: "qux", + Storage: &KeyspaceStorageUpdate{ + IOPS: &iops, + ThroughputMiBs: &throughput, + }, + }) + + c.Assert(err, qt.IsNil) + c.Assert(strings.TrimSpace(body), qt.Equals, `{"storage":{"iops":6000,"throughput_mibs":500}}`) + c.Assert(keyspace.Storage, qt.Not(qt.IsNil)) + c.Assert(*keyspace.Storage.IOPSConfigurable, qt.IsTrue) + c.Assert(*keyspace.Storage.IOPS, qt.Equals, int64(6000)) + c.Assert(*keyspace.Storage.ThroughputMiBs, qt.Equals, int64(500)) + c.Assert(*keyspace.Storage.MinIOPS, qt.Equals, int64(3000)) + c.Assert(*keyspace.Storage.MaxIOPS, qt.Equals, int64(10000)) + c.Assert(*keyspace.Storage.MinThroughputMiBs, qt.Equals, int64(125)) +} + +func TestKeyspaces_GetStorageIONotConfigurable(t *testing.T) { + c := qt.New(t) + + // Keyspaces that cannot configure IOPS still report the storage class + // default, with null limits. + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + out := `{"type":"Keyspace","id":"thisisanid","name":"planetscale","storage":{"disk_scaling_strategy":"grow","iops_configurable":false,"iops":null,"throughput_mibs":null,"min_iops":null,"max_iops":null,"min_throughput_mibs":null}}` + _, err := w.Write([]byte(out)) + c.Assert(err, qt.IsNil) + })) + + client, err := NewClient(WithBaseURL(ts.URL)) + c.Assert(err, qt.IsNil) + + keyspace, err := client.Keyspaces.Get(context.Background(), &GetKeyspaceRequest{ + Organization: "foo", + Database: "bar", + Branch: "baz", + Keyspace: "qux", + }) + + c.Assert(err, qt.IsNil) + c.Assert(keyspace.Storage, qt.Not(qt.IsNil)) + c.Assert(*keyspace.Storage.IOPSConfigurable, qt.IsFalse) + c.Assert(keyspace.Storage.IOPS, qt.IsNil) + c.Assert(keyspace.Storage.ThroughputMiBs, qt.IsNil) + c.Assert(keyspace.Storage.MinIOPS, qt.IsNil) + c.Assert(keyspace.Storage.MaxIOPS, qt.IsNil) + c.Assert(keyspace.Storage.MinThroughputMiBs, qt.IsNil) +} From b1ad48fe6d6a70d37ef5836a0af61525bd3cb873 Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Fri, 13 Nov 2026 14:50:54 +0100 Subject: [PATCH 4/7] Add --reset-storage-iops and --reset-storage-throughput The API clears an IOPS or throughput override when the value is null, so the storage class default applies again. The CLI could only send positive values, so an override could not be removed. Setting the default value explicitly is not the same: it keeps the override and diverges when the storage class default differs. Encode a reset as an explicit null, reject combining a value and a reset for the same setting, and check that the API applied the reset. --- internal/cmd/keyspace/update_settings.go | 38 ++++- internal/cmd/keyspace/update_settings_test.go | 150 ++++++++++++++++++ internal/planetscale/keyspaces.go | 38 +++++ internal/planetscale/keyspaces_test.go | 65 ++++++++ 4 files changed, 285 insertions(+), 6 deletions(-) diff --git a/internal/cmd/keyspace/update_settings.go b/internal/cmd/keyspace/update_settings.go index 4d5b11de..257fe646 100644 --- a/internal/cmd/keyspace/update_settings.go +++ b/internal/cmd/keyspace/update_settings.go @@ -35,6 +35,8 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { storage string storageIOPS int64 storageThroughput int64 + resetStorageIOPS bool + resetStorageThroughput bool interactive bool } @@ -74,7 +76,10 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { storageChanged := cmd.Flags().Changed("storage") storageIOPSChanged := cmd.Flags().Changed("storage-iops") storageThroughputChanged := cmd.Flags().Changed("storage-throughput") - diskStorageChanged := strategyChanged || maxStorageChanged || storageChanged || storageIOPSChanged || storageThroughputChanged + resetStorageIOPS := flags.resetStorageIOPS + resetStorageThroughput := flags.resetStorageThroughput + diskStorageChanged := strategyChanged || maxStorageChanged || storageChanged || + storageIOPSChanged || storageThroughputChanged || resetStorageIOPS || resetStorageThroughput if !rdcChanged && !vrfChanged && !throttlerChanged && !maxRolloutChanged && !diskStorageChanged { ch.Printer.Println("No changes were requested. No update performed.") @@ -213,6 +218,9 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { if storageThroughputChanged { updateReq.Storage.ThroughputMiBs = &flags.storageThroughput } + + updateReq.Storage.ResetIOPS = resetStorageIOPS + updateReq.Storage.ResetThroughputMiBs = resetStorageThroughput } k, err := updateKeyspaceSettings(ctx, client, updateReq) @@ -226,13 +234,25 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { // configurable disk I/O and still answers with success, so check // that the returned keyspace has the requested values. if storageIOPSChanged { - if err := checkStorageIOApplied("--storage-iops", flags.storageIOPS, storageIOPS(k)); err != nil { + if err := checkStorageIOApplied("--storage-iops", &flags.storageIOPS, storageIOPS(k)); err != nil { return err } } if storageThroughputChanged { - if err := checkStorageIOApplied("--storage-throughput", flags.storageThroughput, storageThroughputMiBs(k)); err != nil { + if err := checkStorageIOApplied("--storage-throughput", &flags.storageThroughput, storageThroughputMiBs(k)); err != nil { + return err + } + } + + if resetStorageIOPS { + if err := checkStorageIOApplied("--reset-storage-iops", nil, storageIOPS(k)); err != nil { + return err + } + } + + if resetStorageThroughput { + if err := checkStorageIOApplied("--reset-storage-throughput", nil, storageThroughputMiBs(k)); err != nil { return err } } @@ -253,7 +273,11 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { cmd.Flags().StringVar(&flags.storage, "storage", "", "The disk size to recreate disks at, in bytes or with a unit such as 200GiB or 1TiB. Must be a multiple of 1 GiB. Only accepted when the disk scaling strategy is shrink, either passed with --disk-scaling-strategy or already set on the keyspace.") cmd.Flags().Int64Var(&flags.storageIOPS, "storage-iops", 0, "The provisioned IOPS for each AWS gp3 disk, from 3000 up to 500 per GiB of disk size (at most 80000). Applied without replacing disks.") cmd.Flags().Int64Var(&flags.storageThroughput, "storage-throughput", 0, "The provisioned throughput in MiB/s for each AWS gp3 disk, from 125 up to 0.25 per provisioned IOPS (at most 2000). Applied without replacing disks.") + cmd.Flags().BoolVar(&flags.resetStorageIOPS, "reset-storage-iops", false, "Clear the IOPS override so the storage class default applies.") + cmd.Flags().BoolVar(&flags.resetStorageThroughput, "reset-storage-throughput", false, "Clear the throughput override so the storage class default applies.") cmd.Flags().BoolVarP(&flags.interactive, "interactive", "i", false, "Run the command in interactive mode") + cmd.MarkFlagsMutuallyExclusive("storage-iops", "reset-storage-iops") + cmd.MarkFlagsMutuallyExclusive("storage-throughput", "reset-storage-throughput") _ = cmd.RegisterFlagCompletionFunc("disk-scaling-strategy", cobra.FixedCompletions(diskScalingStrategies, cobra.ShellCompDirectiveNoFileComp)) @@ -449,13 +473,15 @@ func storageThroughputMiBs(k *ps.Keyspace) *int64 { // checkStorageIOApplied returns an error when the keyspace returned by the API // does not have the requested IOPS or throughput value. The API leaves the // value out when configurable disk I/O is not enabled for the organization. -func checkStorageIOApplied(flag string, want int64, got *int64) error { +// A nil want is a reset to the storage class default, which the CLI does not +// know, so then only the presence of the value is checked. +func checkStorageIOApplied(flag string, want, got *int64) error { if got == nil { return fmt.Errorf("%s was not applied: configurable disk IOPS and throughput are not enabled for this keyspace. Any other requested settings were applied", flag) } - if *got != want { - return fmt.Errorf("%s was not applied: the keyspace reports %d instead of %d. Any other requested settings were applied", flag, *got, want) + if want != nil && *got != *want { + return fmt.Errorf("%s was not applied: the keyspace reports %d instead of %d. Any other requested settings were applied", flag, *got, *want) } return nil diff --git a/internal/cmd/keyspace/update_settings_test.go b/internal/cmd/keyspace/update_settings_test.go index 73a7dbc4..c28861db 100644 --- a/internal/cmd/keyspace/update_settings_test.go +++ b/internal/cmd/keyspace/update_settings_test.go @@ -3,6 +3,7 @@ package keyspace import ( "bytes" "context" + "encoding/json" "errors" "testing" "time" @@ -1894,3 +1895,152 @@ func TestKeyspace_UpdateSettingsCmd_ErrorNotFound(t *testing.T) { c.Assert(svc.GetFnInvoked, qt.IsTrue) c.Assert(svc.UpdateSettingsFnInvoked, qt.IsFalse) } + +func TestKeyspace_UpdateSettingsCmd_ResetStorageIO(t *testing.T) { + iops := int64(3000) + throughput := int64(125) + + tests := []struct { + name string + args []string + wantBody string + }{ + { + name: "reset iops", + args: []string{"--reset-storage-iops"}, + wantBody: `{"storage":{"iops":null}}`, + }, + { + name: "reset throughput", + args: []string{"--reset-storage-throughput"}, + wantBody: `{"storage":{"throughput_mibs":null}}`, + }, + { + name: "reset both", + args: []string{"--reset-storage-iops", "--reset-storage-throughput"}, + wantBody: `{"storage":{"iops":null,"throughput_mibs":null}}`, + }, + { + name: "reset iops and set throughput", + args: []string{"--reset-storage-iops", "--storage-throughput=125"}, + wantBody: `{"storage":{"iops":null,"throughput_mibs":125}}`, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := qt.New(t) + + var buf bytes.Buffer + format := printer.JSON + p := printer.NewPrinter(&format) + p.SetResourceOutput(&buf) + + updatedKs := &ps.Keyspace{ + ID: "ks1", + Name: "sharded", + Storage: &ps.KeyspaceStorage{ + IOPS: &iops, + ThroughputMiBs: &throughput, + }, + } + + svc := &mock.KeyspacesService{ + UpdateSettingsFn: func(ctx context.Context, req *ps.UpdateKeyspaceSettingsRequest) (*ps.Keyspace, error) { + body, err := json.Marshal(struct { + Storage *ps.KeyspaceStorageUpdate `json:"storage"` + }{req.Storage}) + c.Assert(err, qt.IsNil) + c.Assert(string(body), qt.Equals, tt.wantBody) + return updatedKs, nil + }, + } + + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs(append([]string{"planetscale", "main", "sharded"}, tt.args...)) + err := cmd.Execute() + c.Assert(err, qt.IsNil) + c.Assert(svc.UpdateSettingsFnInvoked, qt.IsTrue) + c.Assert(buf.String(), qt.JSONEquals, updatedKs) + }) + } +} + +func TestKeyspace_UpdateSettingsCmd_ResetStorageIONotApplied(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + + // The API leaves the values out when configurable disk I/O is not + // enabled for the organization. + svc := &mock.KeyspacesService{ + UpdateSettingsFn: func(ctx context.Context, req *ps.UpdateKeyspaceSettingsRequest) (*ps.Keyspace, error) { + return &ps.Keyspace{ID: "ks1", Name: "sharded", Storage: &ps.KeyspaceStorage{DiskScalingStrategy: "grow"}}, nil + }, + } + + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "sharded", "--reset-storage-iops"}) + err := cmd.Execute() + c.Assert(err, qt.ErrorMatches, "--reset-storage-iops was not applied: configurable disk IOPS and throughput are not enabled for this keyspace. Any other requested settings were applied") +} + +func TestKeyspace_UpdateSettingsCmd_SetAndResetStorageIO(t *testing.T) { + tests := []struct { + name string + args []string + wantErr string + }{ + { + name: "iops", + args: []string{"--storage-iops=6000", "--reset-storage-iops"}, + wantErr: ".*\\[reset-storage-iops storage-iops\\] were all set", + }, + { + name: "throughput", + args: []string{"--storage-throughput=500", "--reset-storage-throughput"}, + wantErr: ".*\\[reset-storage-throughput storage-throughput\\] were all set", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + + svc := &mock.KeyspacesService{} + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs(append([]string{"planetscale", "main", "sharded"}, tt.args...)) + err := cmd.Execute() + c.Assert(err, qt.ErrorMatches, tt.wantErr) + c.Assert(svc.UpdateSettingsFnInvoked, qt.IsFalse) + }) + } +} diff --git a/internal/planetscale/keyspaces.go b/internal/planetscale/keyspaces.go index a0f0a5c3..831a8db2 100644 --- a/internal/planetscale/keyspaces.go +++ b/internal/planetscale/keyspaces.go @@ -2,6 +2,8 @@ package planetscale import ( "context" + "encoding/json" + "errors" "fmt" "net/http" "net/url" @@ -291,6 +293,42 @@ type KeyspaceStorageUpdate struct { StorageBytes *int64 `json:"storage_bytes,omitempty"` IOPS *int64 `json:"iops,omitempty"` ThroughputMiBs *int64 `json:"throughput_mibs,omitempty"` + + // ResetIOPS and ResetThroughputMiBs send null for the value, which clears + // the override so the storage class default applies. They cannot be + // combined with IOPS or ThroughputMiBs. + ResetIOPS bool `json:"-"` + ResetThroughputMiBs bool `json:"-"` +} + +func (u KeyspaceStorageUpdate) MarshalJSON() ([]byte, error) { + if u.ResetIOPS && u.IOPS != nil { + return nil, errors.New("cannot both set and reset iops") + } + if u.ResetThroughputMiBs && u.ThroughputMiBs != nil { + return nil, errors.New("cannot both set and reset throughput_mibs") + } + + // The alias drops this method, so the fields are encoded with their + // struct tags. + type alias KeyspaceStorageUpdate + b, err := json.Marshal(alias(u)) + if err != nil || (!u.ResetIOPS && !u.ResetThroughputMiBs) { + return b, err + } + + fields := map[string]json.RawMessage{} + if err := json.Unmarshal(b, &fields); err != nil { + return nil, err + } + if u.ResetIOPS { + fields["iops"] = json.RawMessage("null") + } + if u.ResetThroughputMiBs { + fields["throughput_mibs"] = json.RawMessage("null") + } + + return json.Marshal(fields) } // KeyspacesService is an interface for interacting with the keyspace endpoints of the PlanetScale API diff --git a/internal/planetscale/keyspaces_test.go b/internal/planetscale/keyspaces_test.go index 818cb817..059f18b2 100644 --- a/internal/planetscale/keyspaces_test.go +++ b/internal/planetscale/keyspaces_test.go @@ -712,3 +712,68 @@ func TestKeyspaces_GetStorageIONotConfigurable(t *testing.T) { c.Assert(keyspace.Storage.MaxIOPS, qt.IsNil) c.Assert(keyspace.Storage.MinThroughputMiBs, qt.IsNil) } + +func TestKeyspaceStorageUpdate_MarshalJSON(t *testing.T) { + iops := int64(6000) + throughput := int64(500) + strategy := "grow" + + tests := []struct { + name string + update KeyspaceStorageUpdate + want string + wantErr string + }{ + { + name: "empty", + update: KeyspaceStorageUpdate{}, + want: `{}`, + }, + { + name: "values", + update: KeyspaceStorageUpdate{IOPS: &iops, ThroughputMiBs: &throughput}, + want: `{"iops":6000,"throughput_mibs":500}`, + }, + { + name: "reset iops", + update: KeyspaceStorageUpdate{ResetIOPS: true}, + want: `{"iops":null}`, + }, + { + name: "reset throughput", + update: KeyspaceStorageUpdate{ResetThroughputMiBs: true}, + want: `{"throughput_mibs":null}`, + }, + { + name: "reset iops and set throughput and strategy", + update: KeyspaceStorageUpdate{DiskScalingStrategy: &strategy, ResetIOPS: true, ThroughputMiBs: &throughput}, + want: `{"disk_scaling_strategy":"grow","iops":null,"throughput_mibs":500}`, + }, + { + name: "set and reset iops", + update: KeyspaceStorageUpdate{IOPS: &iops, ResetIOPS: true}, + wantErr: ".*cannot both set and reset iops", + }, + { + name: "set and reset throughput", + update: KeyspaceStorageUpdate{ThroughputMiBs: &throughput, ResetThroughputMiBs: true}, + wantErr: ".*cannot both set and reset throughput_mibs", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := qt.New(t) + + // Encode through the request, the way the client sends it. + got, err := json.Marshal(&UpdateKeyspaceSettingsRequest{Storage: &tt.update}) + if tt.wantErr != "" { + c.Assert(err, qt.ErrorMatches, tt.wantErr) + return + } + + c.Assert(err, qt.IsNil) + c.Assert(string(got), qt.Equals, `{"storage":`+tt.want+`}`) + }) + } +} From c15ab7d5d0c61dcbd51bc4a352baadadcfa980e8 Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Fri, 13 Nov 2026 14:51:40 +0100 Subject: [PATCH 5/7] Correct the keyspace update-settings IOPS help text The help said IOPS go up to 80000, but the API caps them at 10000 unless larger cluster sizes are enabled for the organization. It also did not say that the flags only work on AWS gp3 disks with configurable disk I/O enabled. Point to the reported min_iops and max_iops. --- internal/cmd/keyspace/update_settings.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cmd/keyspace/update_settings.go b/internal/cmd/keyspace/update_settings.go index 257fe646..67296e64 100644 --- a/internal/cmd/keyspace/update_settings.go +++ b/internal/cmd/keyspace/update_settings.go @@ -271,8 +271,8 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { cmd.Flags().StringVar(&flags.diskScalingStrategy, "disk-scaling-strategy", "grow", fmt.Sprintf("The disk scaling strategy (%s). 'grow' lets dedicated disks grow automatically up to --max-storage; 'disable' turns autoscaling off; 'shrink' recreates disks at --storage and then disables autoscaling.", strings.Join(diskScalingStrategies, ", "))) cmd.Flags().StringVar(&flags.maxStorage, "max-storage", "", "The maximum size that dedicated disks may autoscale to, in bytes or with a unit such as 200GiB or 1TiB. Must be at least 12 GiB and no smaller than the current disk size. Cannot be raised above the organization default (4 TiB, or 16 TiB for managed tenancy). Read-only while PlanetScale staff have raised it above that default.") cmd.Flags().StringVar(&flags.storage, "storage", "", "The disk size to recreate disks at, in bytes or with a unit such as 200GiB or 1TiB. Must be a multiple of 1 GiB. Only accepted when the disk scaling strategy is shrink, either passed with --disk-scaling-strategy or already set on the keyspace.") - cmd.Flags().Int64Var(&flags.storageIOPS, "storage-iops", 0, "The provisioned IOPS for each AWS gp3 disk, from 3000 up to 500 per GiB of disk size (at most 80000). Applied without replacing disks.") - cmd.Flags().Int64Var(&flags.storageThroughput, "storage-throughput", 0, "The provisioned throughput in MiB/s for each AWS gp3 disk, from 125 up to 0.25 per provisioned IOPS (at most 2000). Applied without replacing disks.") + cmd.Flags().Int64Var(&flags.storageIOPS, "storage-iops", 0, "The provisioned IOPS for each disk. Only available for keyspaces on AWS gp3 disks when configurable disk I/O is enabled for the organization. At least 3000, and at most 500 per GiB of the smallest disk. Capped at 10000 unless larger cluster sizes are enabled for the organization, and at 80000 overall. 'keyspace settings --format json' shows min_iops and max_iops. Applied without replacing disks.") + cmd.Flags().Int64Var(&flags.storageThroughput, "storage-throughput", 0, "The provisioned throughput in MiB/s for each disk. Only available for keyspaces on AWS gp3 disks when configurable disk I/O is enabled for the organization. At least 125, and at most 0.25 MiB/s per provisioned IOPS, up to 2000. Applied without replacing disks.") cmd.Flags().BoolVar(&flags.resetStorageIOPS, "reset-storage-iops", false, "Clear the IOPS override so the storage class default applies.") cmd.Flags().BoolVar(&flags.resetStorageThroughput, "reset-storage-throughput", false, "Clear the throughput override so the storage class default applies.") cmd.Flags().BoolVarP(&flags.interactive, "interactive", "i", false, "Run the command in interactive mode") From 96821bb893eb0aff07d83629e864c7e50528524d Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Fri, 13 Nov 2026 14:52:32 +0100 Subject: [PATCH 6/7] Reject keyspace update-settings --interactive with other setting flags The interactive form returns before the other flags are read, so a command like `update-settings -i --storage-iops 6000` silently dropped the IOPS change. Mark --interactive mutually exclusive with every other flag. --- internal/cmd/keyspace/update_settings.go | 9 ++++ internal/cmd/keyspace/update_settings_test.go | 46 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/internal/cmd/keyspace/update_settings.go b/internal/cmd/keyspace/update_settings.go index 67296e64..91201dd6 100644 --- a/internal/cmd/keyspace/update_settings.go +++ b/internal/cmd/keyspace/update_settings.go @@ -15,6 +15,7 @@ import ( ps "github.com/planetscale/cli/internal/planetscale" "github.com/planetscale/cli/internal/printer" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) // diskScalingStrategies are the disk scaling strategies accepted by the @@ -279,6 +280,14 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { cmd.MarkFlagsMutuallyExclusive("storage-iops", "reset-storage-iops") cmd.MarkFlagsMutuallyExclusive("storage-throughput", "reset-storage-throughput") + // The interactive form only covers some settings and returns before the + // other flags are read, so they would be silently ignored. + cmd.Flags().VisitAll(func(f *pflag.Flag) { + if f.Name != "interactive" { + cmd.MarkFlagsMutuallyExclusive("interactive", f.Name) + } + }) + _ = cmd.RegisterFlagCompletionFunc("disk-scaling-strategy", cobra.FixedCompletions(diskScalingStrategies, cobra.ShellCompDirectiveNoFileComp)) return cmd diff --git a/internal/cmd/keyspace/update_settings_test.go b/internal/cmd/keyspace/update_settings_test.go index c28861db..8bdc7ef2 100644 --- a/internal/cmd/keyspace/update_settings_test.go +++ b/internal/cmd/keyspace/update_settings_test.go @@ -14,6 +14,7 @@ import ( "github.com/planetscale/cli/internal/mock" ps "github.com/planetscale/cli/internal/planetscale" "github.com/planetscale/cli/internal/printer" + "github.com/spf13/pflag" ) func TestKeyspace_UpdateSettingsCmd_OnlyVReplicationFlags(t *testing.T) { @@ -2044,3 +2045,48 @@ func TestKeyspace_UpdateSettingsCmd_SetAndResetStorageIO(t *testing.T) { }) } } + +func TestKeyspace_UpdateSettingsCmd_InteractiveRejectsOtherFlags(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + + svc := &mock.KeyspacesService{} + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + + var flags []string + cmd.Flags().VisitAll(func(f *pflag.Flag) { + if f.Name != "interactive" { + flags = append(flags, f.Name) + } + }) + c.Assert(flags, qt.Not(qt.HasLen), 0) + + for _, name := range flags { + c.Run(name, func(c *qt.C) { + value := "1" + if name == "disk-scaling-strategy" { + value = "grow" + } + if name == "replication-durability-constraints-strategy" { + value = "maximum" + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "sharded", "--interactive", "--" + name + "=" + value}) + err := cmd.Execute() + c.Assert(err, qt.ErrorMatches, `.*\[interactive `+name+`\] were all set.*|.*\[`+name+` interactive\] were all set.*`) + c.Assert(svc.GetFnInvoked, qt.IsFalse) + c.Assert(svc.UpdateSettingsFnInvoked, qt.IsFalse) + }) + } +} From aee65cd4561e3833601b501b7bfc9a9b19215b67 Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Fri, 13 Nov 2026 14:53:22 +0100 Subject: [PATCH 7/7] Cover keyspace IOPS and throughput flag validation in tests Test zero, negative and out of range values for both flags, and that a single flag leaves the other value out of the request so the API keeps it. Rename the happy path test, which covers throughput too. --- internal/cmd/keyspace/update_settings_test.go | 111 +++++++++++++++--- 1 file changed, 94 insertions(+), 17 deletions(-) diff --git a/internal/cmd/keyspace/update_settings_test.go b/internal/cmd/keyspace/update_settings_test.go index 8bdc7ef2..ba49e617 100644 --- a/internal/cmd/keyspace/update_settings_test.go +++ b/internal/cmd/keyspace/update_settings_test.go @@ -1213,7 +1213,7 @@ func TestKeyspace_UpdateSettingsCmd_Storage(t *testing.T) { c.Assert(buf.String(), qt.JSONEquals, updatedKs) } -func TestKeyspace_UpdateSettingsCmd_StorageIOPS(t *testing.T) { +func TestKeyspace_UpdateSettingsCmd_StorageIO(t *testing.T) { c := qt.New(t) var buf bytes.Buffer @@ -1281,26 +1281,103 @@ func TestKeyspace_UpdateSettingsCmd_StorageIOPS(t *testing.T) { c.Assert(buf.String(), qt.JSONEquals, updatedKs) } -func TestKeyspace_UpdateSettingsCmd_StorageIOPSMustBePositive(t *testing.T) { - c := qt.New(t) +func TestKeyspace_UpdateSettingsCmd_StorageIOMustBePositive(t *testing.T) { + tests := []struct { + arg string + wantErr string + }{ + {arg: "--storage-iops=0", wantErr: "--storage-iops must be greater than 0"}, + {arg: "--storage-iops=-1", wantErr: "--storage-iops must be greater than 0"}, + {arg: "--storage-throughput=0", wantErr: "--storage-throughput must be greater than 0"}, + {arg: "--storage-throughput=-1", wantErr: "--storage-throughput must be greater than 0"}, + {arg: "--storage-iops=9223372036854775808", wantErr: `invalid argument .* for "--storage-iops" flag: .*out of range`}, + } - format := printer.JSON - p := printer.NewPrinter(&format) + for _, tt := range tests { + t.Run(tt.arg, func(t *testing.T) { + c := qt.New(t) - svc := &mock.KeyspacesService{} - ch := &cmdutil.Helper{ - Printer: p, - Config: &config.Config{Organization: "planetscale"}, - Client: func() (*ps.Client, error) { - return &ps.Client{Keyspaces: svc}, nil - }, + format := printer.JSON + p := printer.NewPrinter(&format) + + svc := &mock.KeyspacesService{} + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "sharded", tt.arg}) + err := cmd.Execute() + c.Assert(err, qt.ErrorMatches, tt.wantErr) + c.Assert(svc.UpdateSettingsFnInvoked, qt.IsFalse) + }) } +} - cmd := UpdateSettingsCmd(ch) - cmd.SetArgs([]string{"planetscale", "main", "sharded", "--storage-iops=0"}) - err := cmd.Execute() - c.Assert(err, qt.ErrorMatches, "--storage-iops must be greater than 0") - c.Assert(svc.UpdateSettingsFnInvoked, qt.IsFalse) +// A single IOPS or throughput flag must leave the other value out of the +// request, so the API keeps its current value. +func TestKeyspace_UpdateSettingsCmd_SingleStorageIOFlag(t *testing.T) { + iops := int64(6000) + throughput := int64(250) + + tests := []struct { + arg string + wantBody string + }{ + {arg: "--storage-iops=6000", wantBody: `{"iops":6000}`}, + {arg: "--storage-throughput=250", wantBody: `{"throughput_mibs":250}`}, + } + + for _, tt := range tests { + t.Run(tt.arg, func(t *testing.T) { + c := qt.New(t) + + var buf bytes.Buffer + format := printer.JSON + p := printer.NewPrinter(&format) + p.SetResourceOutput(&buf) + + svc := &mock.KeyspacesService{ + UpdateSettingsFn: func(ctx context.Context, req *ps.UpdateKeyspaceSettingsRequest) (*ps.Keyspace, error) { + c.Assert(req.Throttler, qt.IsNil) + c.Assert(req.VReplicationFlags, qt.IsNil) + c.Assert(req.ReplicationDurabilityConstraints, qt.IsNil) + c.Assert(req.MaxRollout, qt.IsNil) + + body, err := json.Marshal(req.Storage) + c.Assert(err, qt.IsNil) + c.Assert(string(body), qt.Equals, tt.wantBody) + + return &ps.Keyspace{ + ID: "ks1", + Name: "sharded", + Storage: &ps.KeyspaceStorage{ + IOPS: &iops, + ThroughputMiBs: &throughput, + }, + }, nil + }, + } + + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := UpdateSettingsCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "sharded", tt.arg}) + err := cmd.Execute() + c.Assert(err, qt.IsNil) + c.Assert(svc.UpdateSettingsFnInvoked, qt.IsTrue) + }) + } } func TestKeyspace_UpdateSettingsCmd_StorageIONotApplied(t *testing.T) {