From d43311d606a535814d28b4f9883fe1c9c2371245 Mon Sep 17 00:00:00 2001 From: Josh De Winne Date: Thu, 7 Dec 2023 16:49:26 -0800 Subject: [PATCH] Adding command to update ttl --- cli/cmd/cluster_update.go | 16 ++++++++++ cli/cmd/cluster_update_ttl.go | 48 ++++++++++++++++++++++++++++ cli/cmd/root.go | 3 ++ cli/cmd/runner.go | 2 ++ pkg/kotsclient/cluster_update_ttl.go | 40 +++++++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 cli/cmd/cluster_update.go create mode 100644 cli/cmd/cluster_update_ttl.go create mode 100644 pkg/kotsclient/cluster_update_ttl.go diff --git a/cli/cmd/cluster_update.go b/cli/cmd/cluster_update.go new file mode 100644 index 00000000..681c8d0c --- /dev/null +++ b/cli/cmd/cluster_update.go @@ -0,0 +1,16 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +func (r *runners) InitClusterUpdateCommand(parent *cobra.Command) *cobra.Command { + cmd := &cobra.Command{ + Use: "update", + Short: "Update cluster settings", + Long: `cluster update can be used to update cluster settings`, + } + parent.AddCommand(cmd) + + return cmd +} diff --git a/cli/cmd/cluster_update_ttl.go b/cli/cmd/cluster_update_ttl.go new file mode 100644 index 00000000..e7721cd6 --- /dev/null +++ b/cli/cmd/cluster_update_ttl.go @@ -0,0 +1,48 @@ +package cmd + +import ( + "github.com/pkg/errors" + "github.com/replicatedhq/replicated/cli/print" + "github.com/replicatedhq/replicated/pkg/kotsclient" + "github.com/replicatedhq/replicated/pkg/platformclient" + "github.com/spf13/cobra" +) + +func (r *runners) InitClusterUpdateTTL(parent *cobra.Command) *cobra.Command { + cmd := &cobra.Command{ + Use: "ttl", + Short: "Update TTL for a test clusters", + Long: `Update TTL for a test clusters`, + Args: cobra.ExactArgs(1), + RunE: r.updateClusterTTL, + SilenceUsage: true, + } + parent.AddCommand(cmd) + + cmd.Flags().StringVar(&r.args.updateClusterTTL, "ttl", "", "Cluster TTL (duration, max 48h)") + + cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)") + + _ = cmd.MarkFlagRequired("ttl") + + return cmd +} + +func (r *runners) updateClusterTTL(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return errors.New("cluster id is required") + } + clusterID := args[0] + + opts := kotsclient.UpdateClusterTTLOpts{ + TTL: r.args.updateClusterTTL, + } + cl, err := r.kotsAPI.UpdateClusterTTL(clusterID, opts) + if errors.Cause(err) == platformclient.ErrForbidden { + return ErrCompatibilityMatrixTermsNotAccepted + } else if err != nil { + return errors.Wrap(err, "update cluster ttl") + } + + return print.Cluster(r.outputFormat, r.w, cl) +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 1fd76ea7..38d0d7b9 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -225,6 +225,9 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i runCmds.InitClusterVersions(clusterCmd) clusterPrepareCmd := runCmds.InitClusterPrepare(clusterCmd) + clusterUpdateCmd := runCmds.InitClusterUpdateCommand(clusterCmd) + runCmds.InitClusterUpdateTTL(clusterUpdateCmd) + runCmds.InitLoginCommand(runCmds.rootCmd) runCmds.InitLogoutCommand(runCmds.rootCmd) diff --git a/cli/cmd/runner.go b/cli/cmd/runner.go index 682bff2b..de8a8f8a 100644 --- a/cli/cmd/runner.go +++ b/cli/cmd/runner.go @@ -177,6 +177,8 @@ type runnerArgs struct { upgradeClusterDryRun bool upgradeClusterWaitDuration time.Duration + updateClusterTTL string + prepareClusterID string prepareClusterName string prepareClusterKubernetesDistribution string diff --git a/pkg/kotsclient/cluster_update_ttl.go b/pkg/kotsclient/cluster_update_ttl.go new file mode 100644 index 00000000..d4a62c99 --- /dev/null +++ b/pkg/kotsclient/cluster_update_ttl.go @@ -0,0 +1,40 @@ +package kotsclient + +import ( + "fmt" + "net/http" + + "github.com/replicatedhq/replicated/pkg/types" +) + +type UpdateClusterTTLRequest struct { + TTL string `json:"ttl"` +} + +type UpdateClusterTTLResponse struct { + Cluster *types.Cluster `json:"cluster"` + Errors []string `json:"errors"` +} + +type UpdateClusterTTLOpts struct { + TTL string +} + +func (c *VendorV3Client) UpdateClusterTTL(clusterID string, opts UpdateClusterTTLOpts) (*types.Cluster, error) { + req := UpdateClusterTTLRequest{ + TTL: opts.TTL, + } + + return c.doUpdateClusterTTLRequest(clusterID, req) +} + +func (c *VendorV3Client) doUpdateClusterTTLRequest(clusterID string, req UpdateClusterTTLRequest) (*types.Cluster, error) { + resp := UpdateClusterTTLResponse{} + endpoint := fmt.Sprintf("/v3/cluster/%s/ttl", clusterID) + err := c.DoJSON("PUT", endpoint, http.StatusOK, req, &resp) + if err != nil { + return nil, err + } + + return resp.Cluster, nil +}