-
Notifications
You must be signed in to change notification settings - Fork 61
Add pscale branch resize for Postgres branches
#1276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+198
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,126 @@ | ||
| package branch | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| "github.com/planetscale/cli/internal/printer" | ||
| ps "github.com/planetscale/planetscale-go/planetscale" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // ResizeCmd resizes a Postgres branch's cluster. | ||
| func ResizeCmd(ch *cmdutil.Helper) *cobra.Command { | ||
| var flags struct { | ||
| clusterSize string | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "resize <database> <branch>", | ||
| Short: "Resize a Postgres branch's cluster", | ||
| Args: cmdutil.RequiredArgs("database", "branch"), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| database, branch := args[0], args[1] | ||
|
|
||
| if flags.clusterSize == "" { | ||
| return errors.New("the --cluster-size flag is required") | ||
| } | ||
|
|
||
| client, err := ch.Client() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| db, err := client.Databases.Get(ctx, &ps.GetDatabaseRequest{ | ||
| Organization: ch.Config.Organization, | ||
| Database: database, | ||
| }) | ||
| if err != nil { | ||
| switch cmdutil.ErrCode(err) { | ||
| case ps.ErrNotFound: | ||
| return fmt.Errorf("database %s does not exist in organization %s", printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization)) | ||
| default: | ||
| return cmdutil.HandleError(err) | ||
| } | ||
| } | ||
|
|
||
| if db.Kind == "mysql" { | ||
| return fmt.Errorf("branch resize is only supported for PostgreSQL databases. To resize a MySQL keyspace, use %s", printer.BoldBlue("pscale keyspace resize")) | ||
| } | ||
|
|
||
| end := ch.Printer.PrintProgress(fmt.Sprintf("Resizing branch %s in %s to %s...", printer.BoldBlue(branch), printer.BoldBlue(database), printer.BoldBlue(flags.clusterSize))) | ||
| defer end() | ||
|
|
||
| change, err := client.PostgresBranches.Resize(ctx, &ps.ResizePostgresBranchRequest{ | ||
| Organization: ch.Config.Organization, | ||
| Database: database, | ||
| Branch: branch, | ||
| ClusterSize: flags.clusterSize, | ||
| }) | ||
| if err != nil { | ||
| switch cmdutil.ErrCode(err) { | ||
| case ps.ErrNotFound: | ||
| return fmt.Errorf("database %s or branch %s does not exist in organization %s", printer.BoldBlue(database), printer.BoldBlue(branch), printer.BoldBlue(ch.Config.Organization)) | ||
| default: | ||
| return cmdutil.HandleError(err) | ||
| } | ||
| } | ||
| end() | ||
|
|
||
| // A nil change request means the branch is already configured with | ||
| // the requested cluster size (the API responded 204 No Content). | ||
| if change == nil { | ||
| if ch.Printer.Format() == printer.Human { | ||
| ch.Printer.Printf("Branch %s is already configured with cluster size %s.\n", printer.BoldBlue(branch), printer.BoldBlue(flags.clusterSize)) | ||
| } else { | ||
| // In non-human formats there is no resource to print, so note | ||
| // the no-op on stderr to keep stdout clean for scripts. | ||
| fmt.Fprintf(cmd.ErrOrStderr(), "Branch %s is already configured with cluster size %s; no changes applied.\n", branch, flags.clusterSize) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| if ch.Printer.Format() == printer.Human { | ||
| ch.Printer.Printf("Resize of branch %s to %s started (state: %s).\n", printer.BoldBlue(branch), printer.BoldBlue(change.ClusterDisplayName), printer.BoldBlue(change.State)) | ||
| return nil | ||
| } | ||
|
|
||
| return ch.Printer.PrintResource(toPostgresBranchResize(change)) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&flags.clusterSize, "cluster-size", "", "New cluster size for the branch (a fully-qualified SKU name, e.g. PS_10_GCP_X86). Use 'pscale size cluster list --engine postgresql' to see the valid sizes.") | ||
| cmd.RegisterFlagCompletionFunc("cluster-size", func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| return cmdutil.PostgresBranchClusterSizesCompletionFunc(ch, cmd, args, toComplete) | ||
| }) | ||
|
nickvanw marked this conversation as resolved.
|
||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| type postgresBranchResize struct { | ||
| ID string `header:"id" json:"id"` | ||
| State string `header:"state" json:"state"` | ||
| ClusterSize string `header:"cluster_size" json:"cluster_size"` | ||
| PreviousClusterSize string `header:"previous_cluster_size" json:"previous_cluster_size"` | ||
| Replicas int `header:"replicas" json:"replicas"` | ||
|
|
||
| orig *ps.PostgresBranchClusterResizeRequest | ||
| } | ||
|
|
||
| func toPostgresBranchResize(c *ps.PostgresBranchClusterResizeRequest) *postgresBranchResize { | ||
| return &postgresBranchResize{ | ||
| ID: c.ID, | ||
| State: c.State, | ||
| ClusterSize: c.ClusterDisplayName, | ||
| PreviousClusterSize: c.PreviousClusterDisplayName, | ||
| Replicas: c.Replicas, | ||
| orig: c, | ||
| } | ||
| } | ||
|
|
||
| func (p *postgresBranchResize) MarshalJSON() ([]byte, error) { | ||
| return json.MarshalIndent(p.orig, "", " ") | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.