From c36961ca5208437e0aedbc4bdca707e4f9071890 Mon Sep 17 00:00:00 2001 From: Dmitriy Ivolgin Date: Fri, 16 Aug 2024 10:34:40 -0700 Subject: [PATCH] Add api patch command (#407) --- cli/cmd/api_patch.go | 66 +++++++++++++++++++++++++++++++++++++++++++ cli/cmd/root.go | 1 + cli/cmd/runner.go | 5 ++-- pkg/kotsclient/api.go | 9 ++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 cli/cmd/api_patch.go diff --git a/cli/cmd/api_patch.go b/cli/cmd/api_patch.go new file mode 100644 index 000000000..3473b0def --- /dev/null +++ b/cli/cmd/api_patch.go @@ -0,0 +1,66 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/replicatedhq/replicated/pkg/kotsclient" + "github.com/spf13/cobra" +) + +func (r *runners) InitAPIPatch(parent *cobra.Command) *cobra.Command { + cmd := &cobra.Command{ + Use: "patch", + Short: "Make ad-hoc PATCH API calls to the Replicated API", + Long: `This is essentially like curl for the Replicated API, but +uses your local credentials and prints the response unmodified. + +We recommend piping the output to jq for easier reading. + +Pass the PATH of the request as the final argument. Do not include the host or version. + +Example: + replicated api patch /v3/customer/2VffY549paATVfHSGpJhjh6Ehpy -b '{"name":"Valuable Customer"}' + +`, + RunE: r.apiPatch, + SilenceUsage: true, + Args: cobra.ExactArgs(1), + } + parent.AddCommand(cmd) + + cmd.Flags().StringVarP(&r.args.apiPatchBody, "body", "b", "", "JSON body to send with the request") + + return cmd +} + +func (r *runners) apiPatch(cmd *cobra.Command, args []string) error { + path := args[0] + + if !strings.HasPrefix(args[0], "/") { + path = fmt.Sprintf("/%s", args[0]) + } + pathParts := strings.Split(path, "/") + // remove any empty parts + for i := len(pathParts) - 1; i >= 0; i-- { + if pathParts[i] == "" { + pathParts = append(pathParts[:i], pathParts[i+1:]...) + } + } + + // v1 and v2 paths use platform client, v3 uses kots client + // split the path on the first slash to determine which client to use + if pathParts[0] == "v1" { + + } else if pathParts[0] == "v3" { + kotsRestClient := kotsclient.VendorV3Client{HTTPClient: *r.platformAPI} + response, err := kotsRestClient.Patch(path, r.args.apiPatchBody) + if err != nil { + return err + } + + fmt.Printf("%s", response) + } + + return nil +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index af34b6d33..302d3f77c 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -262,6 +262,7 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i runCmds.InitAPIGet(apiCmd) runCmds.InitAPIPost(apiCmd) runCmds.InitAPIPut(apiCmd) + runCmds.InitAPIPatch(apiCmd) runCmds.rootCmd.SetUsageTemplate(rootCmdUsageTmpl) diff --git a/cli/cmd/runner.go b/cli/cmd/runner.go index 2d38ac9dc..810027ab2 100644 --- a/cli/cmd/runner.go +++ b/cli/cmd/runner.go @@ -283,8 +283,9 @@ type runnerArgs struct { loginEndpoint string - apiPostBody string - apiPutBody string + apiPostBody string + apiPutBody string + apiPatchBody string customerInspectCustomer string diff --git a/pkg/kotsclient/api.go b/pkg/kotsclient/api.go index 537af6266..cb10f49db 100644 --- a/pkg/kotsclient/api.go +++ b/pkg/kotsclient/api.go @@ -26,3 +26,12 @@ func (c *VendorV3Client) Put(path string, body string) ([]byte, error) { return resp, nil } + +func (c *VendorV3Client) Patch(path string, body string) ([]byte, error) { + resp, err := c.DoJSONWithoutUnmarshal("PATCH", path, body) + if err != nil { + return nil, err + } + + return resp, nil +}