From fbb932079d815255eef688f5a32644843c7616f0 Mon Sep 17 00:00:00 2001 From: Aditya Choudhari Date: Sun, 24 Nov 2024 21:44:46 -0800 Subject: [PATCH 1/2] fix: Relationship commands --- cmd/ctrlc/root/api/create/create.go | 2 + .../jobtoresource/job-to-resource.go | 70 +++ .../api/create/relationship/relationship.go | 67 +-- .../resource-to-resource.go | 94 ++++ internal/api/client.gen.go | 412 ++++++++++++++++++ 5 files changed, 587 insertions(+), 58 deletions(-) create mode 100644 cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go create mode 100644 cmd/ctrlc/root/api/create/relationship/resourcetoresource/resource-to-resource.go diff --git a/cmd/ctrlc/root/api/create/create.go b/cmd/ctrlc/root/api/create/create.go index f96c96c..6796349 100644 --- a/cmd/ctrlc/root/api/create/create.go +++ b/cmd/ctrlc/root/api/create/create.go @@ -2,6 +2,7 @@ package create import ( "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/environment" + "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/relationship" "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/release" "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/releasechannel" "github.com/spf13/cobra" @@ -20,6 +21,7 @@ func NewCreateCmd() *cobra.Command { cmd.AddCommand(release.NewCreateReleaseCmd()) cmd.AddCommand(releasechannel.NewCreateReleaseChannelCmd()) cmd.AddCommand(environment.NewCreateEnvironmentCmd()) + cmd.AddCommand(relationship.NewRelationshipCmd()) return cmd } diff --git a/cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go b/cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go new file mode 100644 index 0000000..3c22b1b --- /dev/null +++ b/cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go @@ -0,0 +1,70 @@ +package jobtoresource + +import ( + "fmt" + + "github.com/MakeNowJust/heredoc/v2" + "github.com/ctrlplanedev/cli/internal/api" + "github.com/ctrlplanedev/cli/internal/cliutil" + "github.com/google/uuid" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func NewCreateRelationshipCmd() *cobra.Command { + var jobId string + var resourceIdentifier string + + cmd := &cobra.Command{ + Use: "job-to-resource [flags]", + Short: "Create a new relationship between a job and a resource", + Long: `Create a new relationship between a job and a resource.`, + Example: heredoc.Doc(` + # Create a new relationship between a job and a resource + $ ctrlc create relationship job-to-resource --job-id 123e4567-e89b-12d3-a456-426614174000 --resource-identifier my-resource + `), + PreRunE: func(cmd *cobra.Command, args []string) error { + if jobId == "" { + return fmt.Errorf("job-id is required") + } + + if resourceIdentifier == "" { + return fmt.Errorf("resource-identifier is required") + } + + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + apiURL := viper.GetString("url") + apiKey := viper.GetString("api-key") + + client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) + if err != nil { + return fmt.Errorf("failed to create relationship API client: %w", err) + } + + jobIdUUID, err := uuid.Parse(jobId) + if err != nil { + return fmt.Errorf("failed to parse job-id: %w", err) + } + + resp, err := client.CreateJobToResourceRelationship(cmd.Context(), api.CreateJobToResourceRelationshipJSONRequestBody{ + JobId: jobIdUUID, + ResourceIdentifier: resourceIdentifier, + }) + if err != nil { + return fmt.Errorf("failed to create job-to-resource relationship: %w", err) + } + + return cliutil.HandleOutput(cmd, resp) + }, + } + + cmd.Flags().StringVarP(&jobId, "job", "j", "", "ID of the job (required)") + cmd.Flags().StringVarP(&resourceIdentifier, "resource", "r", "", "Identifier of the resource (required)") + + cmd.MarkFlagRequired("job") + cmd.MarkFlagRequired("resource") + + return cmd +} diff --git a/cmd/ctrlc/root/api/create/relationship/relationship.go b/cmd/ctrlc/root/api/create/relationship/relationship.go index 544b98a..d536afc 100644 --- a/cmd/ctrlc/root/api/create/relationship/relationship.go +++ b/cmd/ctrlc/root/api/create/relationship/relationship.go @@ -1,72 +1,23 @@ package relationship import ( - "fmt" - - "github.com/MakeNowJust/heredoc/v2" - "github.com/ctrlplanedev/cli/internal/api" + "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/relationship/jobtoresource" + "github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/relationship/resourcetoresource" "github.com/spf13/cobra" - "github.com/spf13/viper" ) -func NewCreateRelationshipCmd() *cobra.Command { - var fromId string - var toId string - var fromType string - var toType string - +func NewRelationshipCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "relationship [flags]", - Short: "Create a new relationship", - Long: `Create a new relationship between two entities.`, - Example: heredoc.Doc(` - - `), - PreRunE: func(cmd *cobra.Command, args []string) error { - if fromType != "deployment" && fromType != "resource" { - return fmt.Errorf("from-type must be either 'deployment' or 'resource', got %s", fromType) - } - - if toType != "deployment" && toType != "resource" { - return fmt.Errorf("to-type must be either 'deployment' or 'resource', got %s", toType) - } - - if fromId == toId && fromType == toType { - return fmt.Errorf("from and to cannot be the same") - } - - if fromType == "deployment" && toType == "deployment" { - return fmt.Errorf("cannot create relationship between two deployments") - } - - return nil - }, + Use: "relationship ", + Short: "Create a relationship", + Long: `Create a relationship between two entities.`, RunE: func(cmd *cobra.Command, args []string) error { - apiURL := viper.GetString("url") - apiKey := viper.GetString("api-key") - - _, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) - if err != nil { - return fmt.Errorf("failed to create relationship API client: %w", err) - } - - - - // return cliutil.HandleOutput(cmd, response) - return nil + return cmd.Help() }, } - // Add flags - cmd.Flags().StringVarP(&fromId, "from", "f", "", "ID of the source resource (required)") - cmd.Flags().StringVarP(&toId, "to", "t", "", "ID of the target resource (required)") - cmd.Flags().StringVarP(&fromType, "from-type", "F", "", "Type of the source resource (must be 'deployment' or 'resource') (required)") - cmd.Flags().StringVarP(&toType, "to-type", "T", "", "Type of the target resource (must be 'deployment' or 'resource') (required)") - - cmd.MarkFlagRequired("from") - cmd.MarkFlagRequired("to") - cmd.MarkFlagRequired("from-type") - cmd.MarkFlagRequired("to-type") + cmd.AddCommand(resourcetoresource.NewCreateRelationshipCmd()) + cmd.AddCommand(jobtoresource.NewCreateRelationshipCmd()) return cmd } diff --git a/cmd/ctrlc/root/api/create/relationship/resourcetoresource/resource-to-resource.go b/cmd/ctrlc/root/api/create/relationship/resourcetoresource/resource-to-resource.go new file mode 100644 index 0000000..9299ea6 --- /dev/null +++ b/cmd/ctrlc/root/api/create/relationship/resourcetoresource/resource-to-resource.go @@ -0,0 +1,94 @@ +package resourcetoresource + +import ( + "fmt" + + "github.com/MakeNowJust/heredoc/v2" + "github.com/ctrlplanedev/cli/internal/api" + "github.com/ctrlplanedev/cli/internal/cliutil" + "github.com/google/uuid" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func NewCreateRelationshipCmd() *cobra.Command { + var fromIdentifier string + var toIdentifier string + var workspaceId string + var relationshipType string + + cmd := &cobra.Command{ + Use: "resource-to-resource [flags]", + Short: "Create a new relationship between two resources", + Long: `Create a new relationship between two resources.`, + Example: heredoc.Doc(` + # Create a new relationship between two resources + $ ctrlc create relationship resource-to-resource --from my-resource --to another-resource --workspace-id 123e4567-e89b-12d3-a456-426614174000 --type depends_on + `), + PreRunE: func(cmd *cobra.Command, args []string) error { + if workspaceId == "" { + return fmt.Errorf("workspace-id is required") + } + + if fromIdentifier == "" { + return fmt.Errorf("from is required") + } + + if toIdentifier == "" { + return fmt.Errorf("to is required") + } + + if relationshipType == "" { + return fmt.Errorf("type is required") + } + + if relationshipType != "associated_with" && relationshipType != "depends_on" { + return fmt.Errorf("type must be either 'associated_with' or 'depends_on', got %s", relationshipType) + } + + if fromIdentifier == toIdentifier { + return fmt.Errorf("from and to cannot be the same") + } + + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + apiURL := viper.GetString("url") + apiKey := viper.GetString("api-key") + + client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) + if err != nil { + return fmt.Errorf("failed to create relationship API client: %w", err) + } + + workspaceIdUUID, err := uuid.Parse(workspaceId) + if err != nil { + return fmt.Errorf("failed to parse workspace-id: %w", err) + } + + resp, err := client.CreateResourceToResourceRelationship(cmd.Context(), api.CreateResourceToResourceRelationshipJSONRequestBody{ + FromIdentifier: fromIdentifier, + ToIdentifier: toIdentifier, + WorkspaceId: workspaceIdUUID, + Type: relationshipType, + }) + if err != nil { + return fmt.Errorf("failed to create resource-to-resource relationship: %w", err) + } + + return cliutil.HandleOutput(cmd, resp) + }, + } + + cmd.Flags().StringVarP(&fromIdentifier, "from", "f", "", "Identifier of the source resource (required)") + cmd.Flags().StringVarP(&toIdentifier, "to", "t", "", "Identifier of the target resource (required)") + cmd.Flags().StringVarP(&workspaceId, "workspace", "w", "", "ID of the workspace (required)") + cmd.Flags().StringVarP(&relationshipType, "type", "T", "", "Type of the relationship (must be 'associated_with' or 'depends_on') (required)") + + cmd.MarkFlagRequired("from") + cmd.MarkFlagRequired("to") + cmd.MarkFlagRequired("workspace") + cmd.MarkFlagRequired("type") + + return cmd +} diff --git a/internal/api/client.gen.go b/internal/api/client.gen.go index 470dac0..e369c1f 100644 --- a/internal/api/client.gen.go +++ b/internal/api/client.gen.go @@ -68,6 +68,30 @@ type UpdateJobJSONBody struct { Status *string `json:"status,omitempty"` } +// CreateJobToResourceRelationshipJSONBody defines parameters for CreateJobToResourceRelationship. +type CreateJobToResourceRelationshipJSONBody struct { + // JobId Unique identifier of the job + JobId openapi_types.UUID `json:"jobId"` + + // ResourceIdentifier Unique identifier of the resource + ResourceIdentifier string `json:"resourceIdentifier"` +} + +// CreateResourceToResourceRelationshipJSONBody defines parameters for CreateResourceToResourceRelationship. +type CreateResourceToResourceRelationshipJSONBody struct { + // FromIdentifier The identifier of the resource to connect + FromIdentifier string `json:"fromIdentifier"` + + // ToIdentifier The identifier of the resource to connect to + ToIdentifier string `json:"toIdentifier"` + + // Type The type of relationship + Type string `json:"type"` + + // WorkspaceId The workspace ID + WorkspaceId openapi_types.UUID `json:"workspaceId"` +} + // CreateReleaseChannelJSONBody defines parameters for CreateReleaseChannel. type CreateReleaseChannelJSONBody struct { DeploymentId string `json:"deploymentId"` @@ -132,6 +156,12 @@ type UpdateJobAgentJSONRequestBody UpdateJobAgentJSONBody // UpdateJobJSONRequestBody defines body for UpdateJob for application/json ContentType. type UpdateJobJSONRequestBody UpdateJobJSONBody +// CreateJobToResourceRelationshipJSONRequestBody defines body for CreateJobToResourceRelationship for application/json ContentType. +type CreateJobToResourceRelationshipJSONRequestBody CreateJobToResourceRelationshipJSONBody + +// CreateResourceToResourceRelationshipJSONRequestBody defines body for CreateResourceToResourceRelationship for application/json ContentType. +type CreateResourceToResourceRelationshipJSONRequestBody CreateResourceToResourceRelationshipJSONBody + // CreateReleaseChannelJSONRequestBody defines body for CreateReleaseChannel for application/json ContentType. type CreateReleaseChannelJSONRequestBody CreateReleaseChannelJSONBody @@ -347,6 +377,16 @@ type ClientInterface interface { // AcknowledgeJob request AcknowledgeJob(ctx context.Context, jobId string, reqEditors ...RequestEditorFn) (*http.Response, error) + // CreateJobToResourceRelationshipWithBody request with any body + CreateJobToResourceRelationshipWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) + + CreateJobToResourceRelationship(ctx context.Context, body CreateJobToResourceRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) + + // CreateResourceToResourceRelationshipWithBody request with any body + CreateResourceToResourceRelationshipWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) + + CreateResourceToResourceRelationship(ctx context.Context, body CreateResourceToResourceRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) + // CreateReleaseChannelWithBody request with any body CreateReleaseChannelWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) @@ -562,6 +602,54 @@ func (c *Client) AcknowledgeJob(ctx context.Context, jobId string, reqEditors .. return c.Client.Do(req) } +func (c *Client) CreateJobToResourceRelationshipWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewCreateJobToResourceRelationshipRequestWithBody(c.Server, contentType, body) + 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) CreateJobToResourceRelationship(ctx context.Context, body CreateJobToResourceRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewCreateJobToResourceRelationshipRequest(c.Server, body) + 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) CreateResourceToResourceRelationshipWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewCreateResourceToResourceRelationshipRequestWithBody(c.Server, contentType, body) + 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) CreateResourceToResourceRelationship(ctx context.Context, body CreateResourceToResourceRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewCreateResourceToResourceRelationshipRequest(c.Server, body) + 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) CreateReleaseChannelWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewCreateReleaseChannelRequestWithBody(c.Server, contentType, body) if err != nil { @@ -1172,6 +1260,86 @@ func NewAcknowledgeJobRequest(server string, jobId string) (*http.Request, error return req, nil } +// NewCreateJobToResourceRelationshipRequest calls the generic CreateJobToResourceRelationship builder with application/json body +func NewCreateJobToResourceRelationshipRequest(server string, body CreateJobToResourceRelationshipJSONRequestBody) (*http.Request, error) { + var bodyReader io.Reader + buf, err := json.Marshal(body) + if err != nil { + return nil, err + } + bodyReader = bytes.NewReader(buf) + return NewCreateJobToResourceRelationshipRequestWithBody(server, "application/json", bodyReader) +} + +// NewCreateJobToResourceRelationshipRequestWithBody generates requests for CreateJobToResourceRelationship with any type of body +func NewCreateJobToResourceRelationshipRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/v1/relationship/job-to-resource") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("POST", queryURL.String(), body) + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", contentType) + + return req, nil +} + +// NewCreateResourceToResourceRelationshipRequest calls the generic CreateResourceToResourceRelationship builder with application/json body +func NewCreateResourceToResourceRelationshipRequest(server string, body CreateResourceToResourceRelationshipJSONRequestBody) (*http.Request, error) { + var bodyReader io.Reader + buf, err := json.Marshal(body) + if err != nil { + return nil, err + } + bodyReader = bytes.NewReader(buf) + return NewCreateResourceToResourceRelationshipRequestWithBody(server, "application/json", bodyReader) +} + +// NewCreateResourceToResourceRelationshipRequestWithBody generates requests for CreateResourceToResourceRelationship with any type of body +func NewCreateResourceToResourceRelationshipRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/v1/relationship/resource-to-resource") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("POST", queryURL.String(), body) + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", contentType) + + return req, nil +} + // NewCreateReleaseChannelRequest calls the generic CreateReleaseChannel builder with application/json body func NewCreateReleaseChannelRequest(server string, body CreateReleaseChannelJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader @@ -1734,6 +1902,16 @@ type ClientWithResponsesInterface interface { // AcknowledgeJobWithResponse request AcknowledgeJobWithResponse(ctx context.Context, jobId string, reqEditors ...RequestEditorFn) (*AcknowledgeJobResponse, error) + // CreateJobToResourceRelationshipWithBodyWithResponse request with any body + CreateJobToResourceRelationshipWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateJobToResourceRelationshipResponse, error) + + CreateJobToResourceRelationshipWithResponse(ctx context.Context, body CreateJobToResourceRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateJobToResourceRelationshipResponse, error) + + // CreateResourceToResourceRelationshipWithBodyWithResponse request with any body + CreateResourceToResourceRelationshipWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateResourceToResourceRelationshipResponse, error) + + CreateResourceToResourceRelationshipWithResponse(ctx context.Context, body CreateResourceToResourceRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateResourceToResourceRelationshipResponse, error) + // CreateReleaseChannelWithBodyWithResponse request with any body CreateReleaseChannelWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateReleaseChannelResponse, error) @@ -2184,6 +2362,78 @@ func (r AcknowledgeJobResponse) StatusCode() int { return 0 } +type CreateJobToResourceRelationshipResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *struct { + Message *string `json:"message,omitempty"` + } + JSON400 *struct { + Error *string `json:"error,omitempty"` + } + JSON404 *struct { + Error *string `json:"error,omitempty"` + } + JSON409 *struct { + Error *string `json:"error,omitempty"` + } + JSON500 *struct { + Error *string `json:"error,omitempty"` + } +} + +// Status returns HTTPResponse.Status +func (r CreateJobToResourceRelationshipResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r CreateJobToResourceRelationshipResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + +type CreateResourceToResourceRelationshipResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *struct { + Message *string `json:"message,omitempty"` + } + JSON400 *struct { + Error *string `json:"error,omitempty"` + } + JSON404 *struct { + Error *string `json:"error,omitempty"` + } + JSON409 *struct { + Error *string `json:"error,omitempty"` + } + JSON500 *struct { + Error *string `json:"error,omitempty"` + } +} + +// Status returns HTTPResponse.Status +func (r CreateResourceToResourceRelationshipResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r CreateResourceToResourceRelationshipResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + type CreateReleaseChannelResponse struct { Body []byte HTTPResponse *http.Response @@ -2688,6 +2938,40 @@ func (c *ClientWithResponses) AcknowledgeJobWithResponse(ctx context.Context, jo return ParseAcknowledgeJobResponse(rsp) } +// CreateJobToResourceRelationshipWithBodyWithResponse request with arbitrary body returning *CreateJobToResourceRelationshipResponse +func (c *ClientWithResponses) CreateJobToResourceRelationshipWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateJobToResourceRelationshipResponse, error) { + rsp, err := c.CreateJobToResourceRelationshipWithBody(ctx, contentType, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseCreateJobToResourceRelationshipResponse(rsp) +} + +func (c *ClientWithResponses) CreateJobToResourceRelationshipWithResponse(ctx context.Context, body CreateJobToResourceRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateJobToResourceRelationshipResponse, error) { + rsp, err := c.CreateJobToResourceRelationship(ctx, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseCreateJobToResourceRelationshipResponse(rsp) +} + +// CreateResourceToResourceRelationshipWithBodyWithResponse request with arbitrary body returning *CreateResourceToResourceRelationshipResponse +func (c *ClientWithResponses) CreateResourceToResourceRelationshipWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateResourceToResourceRelationshipResponse, error) { + rsp, err := c.CreateResourceToResourceRelationshipWithBody(ctx, contentType, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseCreateResourceToResourceRelationshipResponse(rsp) +} + +func (c *ClientWithResponses) CreateResourceToResourceRelationshipWithResponse(ctx context.Context, body CreateResourceToResourceRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateResourceToResourceRelationshipResponse, error) { + rsp, err := c.CreateResourceToResourceRelationship(ctx, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseCreateResourceToResourceRelationshipResponse(rsp) +} + // CreateReleaseChannelWithBodyWithResponse request with arbitrary body returning *CreateReleaseChannelResponse func (c *ClientWithResponses) CreateReleaseChannelWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateReleaseChannelResponse, error) { rsp, err := c.CreateReleaseChannelWithBody(ctx, contentType, body, reqEditors...) @@ -3328,6 +3612,134 @@ func ParseAcknowledgeJobResponse(rsp *http.Response) (*AcknowledgeJobResponse, e return response, nil } +// ParseCreateJobToResourceRelationshipResponse parses an HTTP response from a CreateJobToResourceRelationshipWithResponse call +func ParseCreateJobToResourceRelationshipResponse(rsp *http.Response) (*CreateJobToResourceRelationshipResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &CreateJobToResourceRelationshipResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest struct { + Message *string `json:"message,omitempty"` + } + 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 == 400: + var dest struct { + Error *string `json:"error,omitempty"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON400 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 404: + var dest struct { + Error *string `json:"error,omitempty"` + } + 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 == 409: + var dest struct { + Error *string `json:"error,omitempty"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON409 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest struct { + Error *string `json:"error,omitempty"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest + + } + + return response, nil +} + +// ParseCreateResourceToResourceRelationshipResponse parses an HTTP response from a CreateResourceToResourceRelationshipWithResponse call +func ParseCreateResourceToResourceRelationshipResponse(rsp *http.Response) (*CreateResourceToResourceRelationshipResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &CreateResourceToResourceRelationshipResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest struct { + Message *string `json:"message,omitempty"` + } + 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 == 400: + var dest struct { + Error *string `json:"error,omitempty"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON400 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 404: + var dest struct { + Error *string `json:"error,omitempty"` + } + 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 == 409: + var dest struct { + Error *string `json:"error,omitempty"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON409 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest struct { + Error *string `json:"error,omitempty"` + } + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest + + } + + return response, nil +} + // ParseCreateReleaseChannelResponse parses an HTTP response from a CreateReleaseChannelWithResponse call func ParseCreateReleaseChannelResponse(rsp *http.Response) (*CreateReleaseChannelResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) From f64a1dadbef7f2aac2a3fd1b39e3795c3d60416a Mon Sep 17 00:00:00 2001 From: Aditya Choudhari Date: Sun, 24 Nov 2024 21:52:19 -0800 Subject: [PATCH 2/2] fix example --- .../api/create/relationship/jobtoresource/job-to-resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go b/cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go index 3c22b1b..5977a03 100644 --- a/cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go +++ b/cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go @@ -21,7 +21,7 @@ func NewCreateRelationshipCmd() *cobra.Command { Long: `Create a new relationship between a job and a resource.`, Example: heredoc.Doc(` # Create a new relationship between a job and a resource - $ ctrlc create relationship job-to-resource --job-id 123e4567-e89b-12d3-a456-426614174000 --resource-identifier my-resource + $ ctrlc create relationship job-to-resource --job 123e4567-e89b-12d3-a456-426614174000 --resource my-resource `), PreRunE: func(cmd *cobra.Command, args []string) error { if jobId == "" {