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..78a362bb 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,22 @@ 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) + } + // 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/cmd/keyspace/update_settings.go b/internal/cmd/keyspace/update_settings.go index 855be348..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 @@ -33,6 +34,10 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { diskScalingStrategy string maxStorage string storage string + storageIOPS int64 + storageThroughput int64 + resetStorageIOPS bool + resetStorageThroughput bool interactive bool } @@ -70,7 +75,12 @@ 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") + 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.") @@ -104,6 +114,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 +211,17 @@ 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 + } + + updateReq.Storage.ResetIOPS = resetStorageIOPS + updateReq.Storage.ResetThroughputMiBs = resetStorageThroughput } k, err := updateKeyspaceSettings(ctx, client, updateReq) @@ -202,6 +231,33 @@ 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 + } + } + + 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 + } + } + return ch.Printer.PrintResource(toKeyspaceSettings(k)) }, } @@ -216,7 +272,21 @@ 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 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") + 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)) @@ -395,6 +465,37 @@ 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. +// 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 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 +} + // 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 a4fbb130..ba49e617 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" @@ -13,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) { @@ -1211,6 +1213,249 @@ func TestKeyspace_UpdateSettingsCmd_Storage(t *testing.T) { c.Assert(buf.String(), qt.JSONEquals, updatedKs) } +func TestKeyspace_UpdateSettingsCmd_StorageIO(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_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`}, + } + + for _, tt := range tests { + t.Run(tt.arg, 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([]string{"planetscale", "main", "sharded", tt.arg}) + err := cmd.Execute() + c.Assert(err, qt.ErrorMatches, tt.wantErr) + 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) { + 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) @@ -1728,3 +1973,197 @@ 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) + }) + } +} + +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) + }) + } +} diff --git a/internal/planetscale/keyspaces.go b/internal/planetscale/keyspaces.go index d85d1877..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" @@ -45,6 +47,25 @@ 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"` + // 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 { @@ -270,6 +291,44 @@ 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"` + + // 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 048b3117..059f18b2 100644 --- a/internal/planetscale/keyspaces_test.go +++ b/internal/planetscale/keyspaces_test.go @@ -633,3 +633,147 @@ 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) +} + +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+`}`) + }) + } +}