-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #957 from planetscale/add-rollouts
Add rollout status
- Loading branch information
Showing
7 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package keyspace | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/planetscale/cli/internal/cmdutil" | ||
"github.com/planetscale/cli/internal/printer" | ||
ps "github.com/planetscale/planetscale-go/planetscale" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func RolloutStatusCmd(ch *cmdutil.Helper) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "rollout-status <database> <branch> <keyspace>", | ||
Short: "Show keyspace rollout status per shard", | ||
Args: cmdutil.RequiredArgs("database", "branch", "keyspace"), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
database, branch, keyspace := args[0], args[1], args[2] | ||
|
||
client, err := ch.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
end := ch.Printer.PrintProgress(fmt.Sprintf("Fetching rollout status for keyspace %s in %s/%s", printer.BoldBlue(keyspace), printer.BoldBlue(database), printer.BoldBlue(branch))) | ||
defer end() | ||
|
||
k, err := client.Keyspaces.RolloutStatus(ctx, &ps.KeyspaceRolloutStatusRequest{ | ||
Organization: ch.Config.Organization, | ||
Database: database, | ||
Branch: branch, | ||
Keyspace: keyspace, | ||
}) | ||
if err != nil { | ||
switch cmdutil.ErrCode(err) { | ||
case ps.ErrNotFound: | ||
return fmt.Errorf("keyspace %s does not exist in branch %s (database: %s, organization: %s)", printer.BoldBlue(keyspace), printer.BoldBlue(branch), printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization)) | ||
default: | ||
return cmdutil.HandleError(err) | ||
} | ||
} | ||
end() | ||
|
||
return ch.Printer.PrintResource(toShardRollouts(k)) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func toShardRollouts(keyspaceRollout *ps.KeyspaceRollout) []*ShardRollout { | ||
shards := make([]*ShardRollout, 0, len(keyspaceRollout.Shards)) | ||
|
||
for _, s := range keyspaceRollout.Shards { | ||
shards = append(shards, toShardRollout(s)) | ||
} | ||
|
||
return shards | ||
} | ||
|
||
type ShardRollout struct { | ||
Name string `header:"shard" json:"name"` | ||
State string `header:"state" json:"state"` | ||
|
||
LastRolloutStartedAt *int64 `header:"started,timestamp(ms|utc|human)" json:"last_rollout_started_at"` | ||
LastRolloutFinishedAt *int64 `header:"finished,timestamp(ms|utc|human)" json:"last_rollout_finished_at"` | ||
|
||
orig *ps.ShardRollout | ||
} | ||
|
||
func toShardRollout(sr ps.ShardRollout) *ShardRollout { | ||
var startedAt, finishedAt *time.Time | ||
if !sr.LastRolloutStartedAt.IsZero() { | ||
startedAt = &sr.LastRolloutStartedAt | ||
} | ||
if !sr.LastRolloutFinishedAt.IsZero() { | ||
finishedAt = &sr.LastRolloutFinishedAt | ||
} | ||
return &ShardRollout{ | ||
Name: sr.Name, | ||
State: cmdutil.SnakeToSentenceCase(sr.State), | ||
LastRolloutStartedAt: printer.GetMillisecondsIfExists(startedAt), | ||
LastRolloutFinishedAt: printer.GetMillisecondsIfExists(finishedAt), | ||
orig: &sr, | ||
} | ||
} | ||
|
||
func (s *ShardRollout) MarshalJSON() ([]byte, error) { | ||
return json.MarshalIndent(s.orig, "", " ") | ||
} | ||
|
||
func (s *ShardRollout) MarshalCSVValue() interface{} { | ||
return []*ShardRollout{s} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package keyspace | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
qt "github.com/frankban/quicktest" | ||
"github.com/planetscale/cli/internal/cmdutil" | ||
"github.com/planetscale/cli/internal/config" | ||
"github.com/planetscale/cli/internal/mock" | ||
"github.com/planetscale/cli/internal/printer" | ||
ps "github.com/planetscale/planetscale-go/planetscale" | ||
) | ||
|
||
func TestKeyspace_RolloutStatusCmd(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() | ||
|
||
kr := &ps.KeyspaceRollout{ | ||
Name: "sharded", | ||
State: "resizing", | ||
Shards: []ps.ShardRollout{ | ||
{ | ||
Name: "-80", | ||
State: "resizing", | ||
LastRolloutFinishedAt: time.Time{}, | ||
LastRolloutStartedAt: ts, | ||
}, | ||
{ | ||
Name: "80-", | ||
State: "resizing", | ||
LastRolloutFinishedAt: time.Time{}, | ||
LastRolloutStartedAt: ts, | ||
}, | ||
}, | ||
} | ||
|
||
expected := []ShardRollout{ | ||
{ | ||
Name: "-80", | ||
State: cmdutil.SnakeToSentenceCase("resizing"), | ||
LastRolloutFinishedAt: nil, | ||
LastRolloutStartedAt: printer.GetMillisecondsIfExists(&ts), | ||
orig: &kr.Shards[0], | ||
}, | ||
{ | ||
Name: "80-", | ||
State: cmdutil.SnakeToSentenceCase("resizing"), | ||
LastRolloutFinishedAt: nil, | ||
LastRolloutStartedAt: printer.GetMillisecondsIfExists(&ts), | ||
orig: &kr.Shards[1], | ||
}, | ||
} | ||
|
||
svc := &mock.KeyspacesService{ | ||
RolloutStatusFn: func(ctx context.Context, req *ps.KeyspaceRolloutStatusRequest) (*ps.KeyspaceRollout, error) { | ||
c.Assert(req.Database, qt.Equals, db) | ||
c.Assert(req.Organization, qt.Equals, org) | ||
c.Assert(req.Branch, qt.Equals, branch) | ||
c.Assert(req.Keyspace, qt.Equals, keyspace) | ||
|
||
return kr, nil | ||
}, | ||
} | ||
|
||
ch := &cmdutil.Helper{ | ||
Printer: p, | ||
Config: &config.Config{ | ||
Organization: org, | ||
}, | ||
Client: func() (*ps.Client, error) { | ||
return &ps.Client{ | ||
Keyspaces: svc, | ||
}, nil | ||
}, | ||
} | ||
|
||
cmd := RolloutStatusCmd(ch) | ||
cmd.SetArgs([]string{db, branch, keyspace}) | ||
err := cmd.Execute() | ||
c.Assert(err, qt.IsNil) | ||
c.Assert(svc.RolloutStatusFnInvoked, qt.IsTrue) | ||
c.Assert(buf.String(), qt.JSONEquals, expected) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters