-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ctrlplanedev/relationship-cli-commands
fix: Relationship commands
- Loading branch information
Showing
5 changed files
with
587 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 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 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.