Skip to content

Commit

Permalink
add resource upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 14, 2024
1 parent 5da08f5 commit c281b3b
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 322 deletions.
2 changes: 2 additions & 0 deletions cmd/ctrlc/root/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/upsert"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -39,6 +40,7 @@ func NewAPICmd() *cobra.Command {

cmd.AddCommand(get.NewGetCmd())
cmd.AddCommand(create.NewCreateCmd())
cmd.AddCommand(upsert.NewUpsertCmd())

return cmd
}
3 changes: 1 addition & 2 deletions cmd/ctrlc/root/api/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/environment"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/release"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/releasechannel"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/resources"
"github.com/spf13/cobra"
)

Expand All @@ -21,6 +20,6 @@ func NewCreateCmd() *cobra.Command {
cmd.AddCommand(release.NewReleaseCmd())
cmd.AddCommand(releasechannel.NewReleaseChannelCmd())
cmd.AddCommand(environment.NewEnvironmentCmd())
cmd.AddCommand(resources.NewResourcesCmd())

return cmd
}
10 changes: 5 additions & 5 deletions cmd/ctrlc/root/api/create/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewEnvironmentCmd() *cobra.Command {
var releaseChannels []string
var expiresIn string
var system string
var targetFilter string
var resourceFilter string
cmd := &cobra.Command{
Use: "environment [flags]",
Short: "Create a new environment",
Expand All @@ -42,12 +42,12 @@ func NewEnvironmentCmd() *cobra.Command {
body.ReleaseChannels = &releaseChannels
body.SystemId = system

if targetFilter != "" {
if resourceFilter != "" {
var parsedFilter map[string]interface{}
if err := json.Unmarshal([]byte(targetFilter), &parsedFilter); err != nil {
if err := json.Unmarshal([]byte(resourceFilter), &parsedFilter); err != nil {
return fmt.Errorf("failed to parse target filter: %w", err)
}
body.TargetFilter = &parsedFilter
body.ResourceFilter = &parsedFilter
}

if expiresIn != "" {
Expand All @@ -72,7 +72,7 @@ func NewEnvironmentCmd() *cobra.Command {
cmd.Flags().StringVar(&system, "system", "", "ID of the system (required)")
cmd.Flags().StringVar(&expiresIn, "expires-in", "", "Expiration time in duration (e.g. 1h)")
cmd.Flags().StringSliceVar(&releaseChannels, "release-channel", []string{}, "Release channel in format <channelid>")
cmd.Flags().StringVar(&targetFilter, "target-filter", "", "Target filter as JSON string")
cmd.Flags().StringVar(&resourceFilter, "resource-filter", "", "Resource filter as JSON string")

cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("system")
Expand Down
3 changes: 1 addition & 2 deletions cmd/ctrlc/root/api/create/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewReleaseCmd() *cobra.Command {
if createdAt != "" {
t, err := time.Parse(time.RFC3339, createdAt)
if err != nil {
return fmt.Errorf("failed to parse created_at time: %w", err)
return fmt.Errorf("failed to parse created_at time: %w", err)
}
parsedTime = &t
}
Expand All @@ -57,7 +57,6 @@ func NewReleaseCmd() *cobra.Command {
metadata["ctrlplane/links"] = string(linksJSON)
}


config := cliutil.ConvertConfigArrayToNestedMap(configArray)
resp, err := client.CreateRelease(cmd.Context(), api.CreateReleaseJSONRequestBody{
Version: versionFlag,
Expand Down
4 changes: 2 additions & 2 deletions cmd/ctrlc/root/api/get/get.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package get

import (
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/target"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/resource"
"github.com/spf13/cobra"
)

Expand All @@ -15,6 +15,6 @@ func NewGetCmd() *cobra.Command {
},
}

cmd.AddCommand(target.NewTargetCmd())
cmd.AddCommand(resource.NewGetResourceCmd())
return cmd
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package target
package resource

import (
"fmt"
Expand All @@ -10,8 +10,8 @@ import (
"github.com/spf13/viper"
)

func NewTargetCmd() *cobra.Command {
var targetId string
func NewGetResourceCmd() *cobra.Command {
var resourceId string

cmd := &cobra.Command{
Use: "target [flags]",
Expand All @@ -32,7 +32,7 @@ func NewTargetCmd() *cobra.Command {
return fmt.Errorf("failed to create API client: %w", err)
}

resp, err := client.GetTarget(cmd.Context(), targetId)
resp, err := client.GetResource(cmd.Context(), resourceId)
if err != nil {
return fmt.Errorf("failed to get target: %w", err)
}
Expand All @@ -42,7 +42,7 @@ func NewTargetCmd() *cobra.Command {
}

// Add flags
cmd.Flags().StringVar(&targetId, "id", "", "ID of the target (required)")
cmd.Flags().StringVar(&resourceId, "id", "", "ID of the target (required)")
cmd.MarkFlagRequired("id")

return cmd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resources
package resource

import (
"encoding/json"
Expand All @@ -12,7 +12,7 @@ import (
"github.com/spf13/viper"
)

func NewResourcesCmd() *cobra.Command {
func NewUpsertResourceCmd() *cobra.Command {
var workspaceID string
var name string
var identifier string
Expand All @@ -21,17 +21,18 @@ func NewResourcesCmd() *cobra.Command {
var metadata map[string]string
var configArray map[string]string
var links map[string]string
var variables map[string]string

cmd := &cobra.Command{
Use: "resources [flags]",
Short: "Create a new resources",
Long: `Create a new resources with the specified version and configuration.`,
Use: "resource [flags]",
Short: "Upsert a resource",
Long: `Upsert a resource with the specified version and configuration.`,
Example: heredoc.Doc(`
# Create a new resource
$ ctrlc create resources --version v1.0.0
# Upsert a resource
$ ctrlc upsert resource --version v1.0.0
# Create a new resource using Go template syntax
$ ctrlc create resources --version v1.0.0 --template='{{.status.phase}}'
# Upsert a resource using Go template syntax
$ ctrlc upsert resource --version v1.0.0 --template='{{.status.phase}}'
`),
RunE: func(cmd *cobra.Command, args []string) error {
apiURL := viper.GetString("url")
Expand All @@ -41,9 +42,7 @@ func NewResourcesCmd() *cobra.Command {
return fmt.Errorf("failed to create API client: %w", err)
}

// Convert configArray into a nested map[string]interface{}
config := cliutil.ConvertConfigArrayToNestedMap(configArray)

if len(links) > 0 {
linksJSON, err := json.Marshal(links)
if err != nil {
Expand All @@ -52,20 +51,29 @@ func NewResourcesCmd() *cobra.Command {
metadata["ctrlplane/links"] = string(linksJSON)
}

variablesRequest := &[]api.Variable{}
for k, v := range variables {
sensitive := false
vv := api.Variable_Value{}
vv.SetString(v)

*variablesRequest = append(*variablesRequest, api.Variable{
Key: k,
Sensitive: &sensitive,
Value: vv,
})
}

// Extrat into vars
resp, err := client.UpsertTargets(cmd.Context(), api.UpsertTargetsJSONRequestBody{
Targets: []struct {
resp, err := client.UpsertResources(cmd.Context(), api.UpsertResourcesJSONRequestBody{
Resources: []struct {
Config map[string]interface{} `json:"config"`
Identifier string `json:"identifier"`
Kind string `json:"kind"`
Metadata *map[string]string `json:"metadata,omitempty"`
Name string `json:"name"`
Variables *[]struct {
Key string `json:"key"`
Sensitive *bool `json:"sensitive,omitempty"`
Value api.UpsertTargetsJSONBody_Targets_Variables_Value `json:"value"`
} `json:"variables,omitempty"`
Version string `json:"version"`
Variables *[]api.Variable `json:"variables,omitempty"`
Version string `json:"version"`
}{
{
Version: version,
Expand All @@ -74,6 +82,7 @@ func NewResourcesCmd() *cobra.Command {
Name: name,
Kind: kind,
Config: config,
Variables: variablesRequest,
},
},

Expand All @@ -94,6 +103,8 @@ func NewResourcesCmd() *cobra.Command {
cmd.Flags().StringVar(&identifier, "identifier", "", "Identifier of the resource (required)")
cmd.Flags().StringVar(&kind, "kind", "", "Kind of the resource (required)")
cmd.Flags().StringVar(&version, "version", "", "Version of the resource (required)")

cmd.Flags().StringToStringVar(&variables, "var", make(map[string]string), "Variable key-value pairs (can be specified multiple times)")
cmd.Flags().StringToStringVar(&metadata, "metadata", make(map[string]string), "Metadata key-value pairs (e.g. --metadata key=value)")
cmd.Flags().StringToStringVar(&configArray, "config", make(map[string]string), "Config key-value pairs with nested values (can be specified multiple times)")
cmd.Flags().StringToStringVar(&links, "link", make(map[string]string), "Links key-value pairs (can be specified multiple times)")
Expand Down
21 changes: 21 additions & 0 deletions cmd/ctrlc/root/api/upsert/upsert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package upsert

import (
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/upsert/resource"
"github.com/spf13/cobra"
)

func NewUpsertCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "upsert <command>",
Short: "Upsert resources",
Long: `Commands for upserting resources.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

cmd.AddCommand(resource.NewUpsertResourceCmd())

return cmd
}
Loading

0 comments on commit c281b3b

Please sign in to comment.