Skip to content

Commit

Permalink
Merge pull request #345 from replicatedhq/joshd/sc-82847/ability-to-e…
Browse files Browse the repository at this point in the history
…xtend-cluster-ttl-after-creation

Adding command to update ttl
  • Loading branch information
jdewinne authored Dec 8, 2023
2 parents e2bae77 + d43311d commit 02cc272
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cli/cmd/cluster_update.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 48 additions & 0 deletions cli/cmd/cluster_update_ttl.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ type runnerArgs struct {
upgradeClusterDryRun bool
upgradeClusterWaitDuration time.Duration

updateClusterTTL string

prepareClusterID string
prepareClusterName string
prepareClusterKubernetesDistribution string
Expand Down
40 changes: 40 additions & 0 deletions pkg/kotsclient/cluster_update_ttl.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 02cc272

Please sign in to comment.