Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/ctrlc/root/api/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
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
}
67 changes: 9 additions & 58 deletions cmd/ctrlc/root/api/create/relationship/relationship.go
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
}
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)
},
Comment on lines +55 to +80
Copy link
Contributor

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)
 			}

Committable suggestion skipped: line range outside the PR's diff.

}

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
}
Loading