-
Notifications
You must be signed in to change notification settings - Fork 1
fix: Relationship commands #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
cmd/ctrlc/root/api/create/relationship/jobtoresource/job-to-resource.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 123e4567-e89b-12d3-a456-426614174000 --resource 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <command>", | ||
| 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 | ||
| } |
94 changes: 94 additions & 0 deletions
94
cmd/ctrlc/root/api/create/relationship/resourcetoresource/resource-to-resource.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance API response error handling
The current implementation doesn't check the API response status code or provide detailed error information from the response body.
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 fmt.Errorf("API request failed: %w", err) + } + + if resp.StatusCode() >= 400 { + return fmt.Errorf("API returned error (status %d): %s", resp.StatusCode(), resp.Body) }