Skip to content

Commit

Permalink
Merge pull request #2 from ctrlplanedev/relationship-cli-commands
Browse files Browse the repository at this point in the history
fix: Relationship commands
  • Loading branch information
adityachoudhari26 authored Nov 25, 2024
2 parents dd181db + f64a1da commit 688ad08
Show file tree
Hide file tree
Showing 5 changed files with 587 additions and 58 deletions.
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)
},
}

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

0 comments on commit 688ad08

Please sign in to comment.