-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding cmd for creating postgres addon (#378)
* Adding cmd for creating postgres addon * fix cluster addon ls for postgres * Include postgres addon data * fix descriptions * version can default to latest * use default error * removing username and password * unhide addons
- Loading branch information
Showing
8 changed files
with
190 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/replicated/cli/print" | ||
"github.com/replicatedhq/replicated/pkg/kotsclient" | ||
"github.com/replicatedhq/replicated/pkg/platformclient" | ||
"github.com/replicatedhq/replicated/pkg/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type clusterAddonCreatePostgresArgs struct { | ||
version string | ||
diskGiB int64 | ||
instanceType string | ||
|
||
clusterID string | ||
waitDuration time.Duration | ||
dryRun bool | ||
outputFormat string | ||
} | ||
|
||
func (r *runners) InitClusterAddonCreatePostgres(parent *cobra.Command) *cobra.Command { | ||
args := clusterAddonCreatePostgresArgs{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "postgres CLUSTER_ID", | ||
Short: "Create a Postgres database for a cluster", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(_ *cobra.Command, cmdArgs []string) error { | ||
args.clusterID = cmdArgs[0] | ||
return r.clusterAddonCreatePostgresCreateRun(args) | ||
}, | ||
} | ||
parent.AddCommand(cmd) | ||
|
||
_ = clusterAddonCreatePostgresFlags(cmd, &args) | ||
|
||
return cmd | ||
} | ||
|
||
func clusterAddonCreatePostgresFlags(cmd *cobra.Command, args *clusterAddonCreatePostgresArgs) error { | ||
cmd.Flags().StringVar(&args.version, "version", "", "The Postgres version to create") | ||
cmd.Flags().Int64Var(&args.diskGiB, "disk", 200, "Disk Size (GiB) for the Postgres database") | ||
cmd.Flags().StringVar(&args.instanceType, "instance-type", "db.t3.micro", "The type of instance to use for the Postgres database") | ||
|
||
cmd.Flags().DurationVar(&args.waitDuration, "wait", 0, "Wait duration for add-on to be ready before exiting (leave empty to not wait)") | ||
cmd.Flags().BoolVar(&args.dryRun, "dry-run", false, "Simulate creation to verify that your inputs are valid without actually creating an add-on") | ||
cmd.Flags().StringVar(&args.outputFormat, "output", "table", "The output format to use. One of: json|table|wide (default: table)") | ||
return nil | ||
} | ||
|
||
func (r *runners) clusterAddonCreatePostgresCreateRun(args clusterAddonCreatePostgresArgs) error { | ||
opts := kotsclient.CreateClusterAddonPostgresOpts{ | ||
ClusterID: args.clusterID, | ||
Version: args.version, | ||
DiskGiB: args.diskGiB, | ||
InstanceType: args.instanceType, | ||
DryRun: args.dryRun, | ||
} | ||
|
||
addon, err := r.createAndWaitForClusterAddonCreatePostgres(opts, args.waitDuration) | ||
if err != nil { | ||
if errors.Cause(err) == ErrWaitDurationExceeded { | ||
defer func() { | ||
os.Exit(124) | ||
}() | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
if opts.DryRun { | ||
_, err := fmt.Fprintln(r.w, "Dry run succeeded.") | ||
return err | ||
} | ||
|
||
return print.Addon(args.outputFormat, r.w, addon) | ||
} | ||
|
||
func (r *runners) createAndWaitForClusterAddonCreatePostgres(opts kotsclient.CreateClusterAddonPostgresOpts, waitDuration time.Duration) (*types.ClusterAddon, error) { | ||
addon, err := r.kotsAPI.CreateClusterAddonPostgres(opts) | ||
if errors.Cause(err) == platformclient.ErrForbidden { | ||
return nil, ErrCompatibilityMatrixTermsNotAccepted | ||
} else if err != nil { | ||
return nil, errors.Wrap(err, "create cluster add-on postgres") | ||
} | ||
|
||
if opts.DryRun { | ||
return addon, nil | ||
} | ||
|
||
// if the wait flag was provided, we poll the api until the add-on is ready, or a timeout | ||
if waitDuration > 0 { | ||
return waitForAddon(r.kotsAPI, opts.ClusterID, addon.ID, waitDuration) | ||
} | ||
|
||
return addon, nil | ||
} |
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,60 @@ | ||
package kotsclient | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/replicated/pkg/platformclient" | ||
"github.com/replicatedhq/replicated/pkg/types" | ||
) | ||
|
||
type CreateClusterAddonPostgresOpts struct { | ||
ClusterID string | ||
Version string | ||
DiskGiB int64 | ||
InstanceType string | ||
DryRun bool | ||
} | ||
|
||
type CreateClusterAddonPostgresRequest struct { | ||
Version string `json:"version"` | ||
DiskGiB int64 `json:"disk_gib"` | ||
InstanceType string `json:"instance_type"` | ||
} | ||
|
||
func (c *VendorV3Client) CreateClusterAddonPostgres(opts CreateClusterAddonPostgresOpts) (*types.ClusterAddon, error) { | ||
req := CreateClusterAddonPostgresRequest{ | ||
Version: opts.Version, | ||
DiskGiB: opts.DiskGiB, | ||
InstanceType: opts.InstanceType, | ||
} | ||
return c.doCreateClusterAddonPostgresRequest(opts.ClusterID, req, opts.DryRun) | ||
} | ||
|
||
func (c *VendorV3Client) doCreateClusterAddonPostgresRequest(clusterID string, req CreateClusterAddonPostgresRequest, dryRun bool) (*types.ClusterAddon, error) { | ||
resp := CreateClusterAddonObjectStoreResponse{} | ||
endpoint := fmt.Sprintf("/v3/cluster/%s/addons/postgres", clusterID) | ||
if dryRun { | ||
endpoint = fmt.Sprintf("%s?dry-run=true", endpoint) | ||
} | ||
err := c.DoJSON("POST", endpoint, http.StatusCreated, req, &resp) | ||
if err != nil { | ||
// if err is APIError and the status code is 400, then we have a validation error | ||
// and we can return the validation error | ||
if apiErr, ok := errors.Cause(err).(platformclient.APIError); ok { | ||
if apiErr.StatusCode == http.StatusBadRequest { | ||
errResp := &CreateClusterAddonErrorResponse{} | ||
if jsonErr := json.Unmarshal(apiErr.Body, errResp); jsonErr != nil { | ||
return nil, fmt.Errorf("unmarshal error response: %w", err) | ||
} | ||
return nil, errors.New(errResp.Message) | ||
} | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
return resp.Addon, nil | ||
} |
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