From fa559d73fa86424121abbe4f0ce855068bdc2e1f Mon Sep 17 00:00:00 2001 From: Aditya Choudhari Date: Wed, 20 Nov 2024 20:32:08 -0800 Subject: [PATCH] fix: Delete release channel --- cmd/ctrlc/root/api/delete/delete.go | 2 + .../delete/releasechannel/release-channel.go | 48 ++++++ internal/api/client.gen.go | 156 ++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 cmd/ctrlc/root/api/delete/releasechannel/release-channel.go diff --git a/cmd/ctrlc/root/api/delete/delete.go b/cmd/ctrlc/root/api/delete/delete.go index d277c49..5267671 100644 --- a/cmd/ctrlc/root/api/delete/delete.go +++ b/cmd/ctrlc/root/api/delete/delete.go @@ -2,6 +2,7 @@ package delete import ( "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete/environment" + "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete/releasechannel" "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete/resource" "github.com/spf13/cobra" ) @@ -18,6 +19,7 @@ func NewDeleteCmd() *cobra.Command { cmd.AddCommand(resource.NewDeleteResourceCmd()) cmd.AddCommand(environment.NewDeleteEnvironmentCmd()) + cmd.AddCommand(releasechannel.NewDeleteReleaseChannelCmd()) return cmd } diff --git a/cmd/ctrlc/root/api/delete/releasechannel/release-channel.go b/cmd/ctrlc/root/api/delete/releasechannel/release-channel.go new file mode 100644 index 0000000..f4cd8eb --- /dev/null +++ b/cmd/ctrlc/root/api/delete/releasechannel/release-channel.go @@ -0,0 +1,48 @@ +package releasechannel + +import ( + "fmt" + + "github.com/MakeNowJust/heredoc/v2" + "github.com/ctrlplanedev/cli/internal/api" + "github.com/ctrlplanedev/cli/internal/cliutil" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func NewDeleteReleaseChannelCmd() *cobra.Command { + var deploymentId string + var name string + + cmd := &cobra.Command{ + Use: "release-channel [flags]", + Short: "Delete a release channel", + Long: `Delete a release channel by specifying a deployment ID and a name.`, + Example: heredoc.Doc(` + $ ctrlc delete release-channel --deployment 123e4567-e89b-12d3-a456-426614174000 --name mychannel + `), + RunE: func(cmd *cobra.Command, args []string) error { + if deploymentId == "" || name == "" { + return fmt.Errorf("deployment and name are required") + } + + apiURL := viper.GetString("url") + apiKey := viper.GetString("api-key") + client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) + if err != nil { + return fmt.Errorf("failed to create API client: %w", err) + } + resp, err := client.DeleteReleaseChannel(cmd.Context(), deploymentId, name) + if err != nil { + return fmt.Errorf("failed to delete release channel: %w", err) + } + + return cliutil.HandleOutput(cmd, resp) + }, + } + + cmd.Flags().StringVar(&deploymentId, "deployment", "", "Deployment ID") + cmd.Flags().StringVar(&name, "name", "", "Release channel name") + + return cmd +} diff --git a/internal/api/client.gen.go b/internal/api/client.gen.go index 588df02..56b483f 100644 --- a/internal/api/client.gen.go +++ b/internal/api/client.gen.go @@ -308,6 +308,9 @@ func WithRequestEditorFn(fn RequestEditorFn) ClientOption { // The interface specification for the client above. type ClientInterface interface { + // DeleteReleaseChannel request + DeleteReleaseChannel(ctx context.Context, deploymentId string, name string, reqEditors ...RequestEditorFn) (*http.Response, error) + // CreateEnvironmentWithBody request with any body CreateEnvironmentWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) @@ -391,6 +394,18 @@ type ClientInterface interface { GetResourceByIdentifier(ctx context.Context, workspaceId string, identifier string, reqEditors ...RequestEditorFn) (*http.Response, error) } +func (c *Client) DeleteReleaseChannel(ctx context.Context, deploymentId string, name string, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewDeleteReleaseChannelRequest(c.Server, deploymentId, name) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + func (c *Client) CreateEnvironmentWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewCreateEnvironmentRequestWithBody(c.Server, contentType, body) if err != nil { @@ -751,6 +766,47 @@ func (c *Client) GetResourceByIdentifier(ctx context.Context, workspaceId string return c.Client.Do(req) } +// NewDeleteReleaseChannelRequest generates requests for DeleteReleaseChannel +func NewDeleteReleaseChannelRequest(server string, deploymentId string, name string) (*http.Request, error) { + var err error + + var pathParam0 string + + pathParam0, err = runtime.StyleParamWithLocation("simple", false, "deploymentId", runtime.ParamLocationPath, deploymentId) + if err != nil { + return nil, err + } + + var pathParam1 string + + pathParam1, err = runtime.StyleParamWithLocation("simple", false, "name", runtime.ParamLocationPath, name) + if err != nil { + return nil, err + } + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/v1/deployments/%s/release-channels/name/%s", pathParam0, pathParam1) + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("DELETE", queryURL.String(), nil) + if err != nil { + return nil, err + } + + return req, nil +} + // NewCreateEnvironmentRequest calls the generic CreateEnvironment builder with application/json body func NewCreateEnvironmentRequest(server string, body CreateEnvironmentJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader @@ -1639,6 +1695,9 @@ func WithBaseURL(baseURL string) ClientOption { // ClientWithResponsesInterface is the interface specification for the client with responses above. type ClientWithResponsesInterface interface { + // DeleteReleaseChannelWithResponse request + DeleteReleaseChannelWithResponse(ctx context.Context, deploymentId string, name string, reqEditors ...RequestEditorFn) (*DeleteReleaseChannelResponse, error) + // CreateEnvironmentWithBodyWithResponse request with any body CreateEnvironmentWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateEnvironmentResponse, error) @@ -1722,6 +1781,39 @@ type ClientWithResponsesInterface interface { GetResourceByIdentifierWithResponse(ctx context.Context, workspaceId string, identifier string, reqEditors ...RequestEditorFn) (*GetResourceByIdentifierResponse, error) } +type DeleteReleaseChannelResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *struct { + Message string `json:"message"` + } + JSON403 *struct { + Error string `json:"error"` + } + JSON404 *struct { + Error string `json:"error"` + } + JSON500 *struct { + Error string `json:"error"` + } +} + +// Status returns HTTPResponse.Status +func (r DeleteReleaseChannelResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r DeleteReleaseChannelResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + type CreateEnvironmentResponse struct { Body []byte HTTPResponse *http.Response @@ -2475,6 +2567,15 @@ func (r GetResourceByIdentifierResponse) StatusCode() int { return 0 } +// DeleteReleaseChannelWithResponse request returning *DeleteReleaseChannelResponse +func (c *ClientWithResponses) DeleteReleaseChannelWithResponse(ctx context.Context, deploymentId string, name string, reqEditors ...RequestEditorFn) (*DeleteReleaseChannelResponse, error) { + rsp, err := c.DeleteReleaseChannel(ctx, deploymentId, name, reqEditors...) + if err != nil { + return nil, err + } + return ParseDeleteReleaseChannelResponse(rsp) +} + // CreateEnvironmentWithBodyWithResponse request with arbitrary body returning *CreateEnvironmentResponse func (c *ClientWithResponses) CreateEnvironmentWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateEnvironmentResponse, error) { rsp, err := c.CreateEnvironmentWithBody(ctx, contentType, body, reqEditors...) @@ -2737,6 +2838,61 @@ func (c *ClientWithResponses) GetResourceByIdentifierWithResponse(ctx context.Co return ParseGetResourceByIdentifierResponse(rsp) } +// ParseDeleteReleaseChannelResponse parses an HTTP response from a DeleteReleaseChannelWithResponse call +func ParseDeleteReleaseChannelResponse(rsp *http.Response) (*DeleteReleaseChannelResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &DeleteReleaseChannelResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest struct { + Message string `json:"message"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON200 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 403: + var dest struct { + Error string `json:"error"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON403 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 404: + var dest struct { + Error string `json:"error"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON404 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest struct { + Error string `json:"error"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest + + } + + return response, nil +} + // ParseCreateEnvironmentResponse parses an HTTP response from a CreateEnvironmentWithResponse call func ParseCreateEnvironmentResponse(rsp *http.Response) (*CreateEnvironmentResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body)