Skip to content

Commit c281b3b

Browse files
committed
add resource upsert
1 parent 5da08f5 commit c281b3b

File tree

10 files changed

+433
-322
lines changed

10 files changed

+433
-322
lines changed

cmd/ctrlc/root/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create"
77
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get"
8+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/upsert"
89
"github.com/spf13/cobra"
910
"github.com/spf13/viper"
1011
)
@@ -39,6 +40,7 @@ func NewAPICmd() *cobra.Command {
3940

4041
cmd.AddCommand(get.NewGetCmd())
4142
cmd.AddCommand(create.NewCreateCmd())
43+
cmd.AddCommand(upsert.NewUpsertCmd())
4244

4345
return cmd
4446
}

cmd/ctrlc/root/api/create/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/environment"
55
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/release"
66
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/releasechannel"
7-
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/resources"
87
"github.com/spf13/cobra"
98
)
109

@@ -21,6 +20,6 @@ func NewCreateCmd() *cobra.Command {
2120
cmd.AddCommand(release.NewReleaseCmd())
2221
cmd.AddCommand(releasechannel.NewReleaseChannelCmd())
2322
cmd.AddCommand(environment.NewEnvironmentCmd())
24-
cmd.AddCommand(resources.NewResourcesCmd())
23+
2524
return cmd
2625
}

cmd/ctrlc/root/api/create/environment/environment.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func NewEnvironmentCmd() *cobra.Command {
1717
var releaseChannels []string
1818
var expiresIn string
1919
var system string
20-
var targetFilter string
20+
var resourceFilter string
2121
cmd := &cobra.Command{
2222
Use: "environment [flags]",
2323
Short: "Create a new environment",
@@ -42,12 +42,12 @@ func NewEnvironmentCmd() *cobra.Command {
4242
body.ReleaseChannels = &releaseChannels
4343
body.SystemId = system
4444

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

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

7777
cmd.MarkFlagRequired("name")
7878
cmd.MarkFlagRequired("system")

cmd/ctrlc/root/api/create/release/release.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewReleaseCmd() *cobra.Command {
4444
if createdAt != "" {
4545
t, err := time.Parse(time.RFC3339, createdAt)
4646
if err != nil {
47-
return fmt.Errorf("failed to parse created_at time: %w", err)
47+
return fmt.Errorf("failed to parse created_at time: %w", err)
4848
}
4949
parsedTime = &t
5050
}
@@ -57,7 +57,6 @@ func NewReleaseCmd() *cobra.Command {
5757
metadata["ctrlplane/links"] = string(linksJSON)
5858
}
5959

60-
6160
config := cliutil.ConvertConfigArrayToNestedMap(configArray)
6261
resp, err := client.CreateRelease(cmd.Context(), api.CreateReleaseJSONRequestBody{
6362
Version: versionFlag,

cmd/ctrlc/root/api/get/get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package get
22

33
import (
4-
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/target"
4+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/resource"
55
"github.com/spf13/cobra"
66
)
77

@@ -15,6 +15,6 @@ func NewGetCmd() *cobra.Command {
1515
},
1616
}
1717

18-
cmd.AddCommand(target.NewTargetCmd())
18+
cmd.AddCommand(resource.NewGetResourceCmd())
1919
return cmd
2020
}

cmd/ctrlc/root/api/get/target/target.go renamed to cmd/ctrlc/root/api/get/resource/resource.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package target
1+
package resource
22

33
import (
44
"fmt"
@@ -10,8 +10,8 @@ import (
1010
"github.com/spf13/viper"
1111
)
1212

13-
func NewTargetCmd() *cobra.Command {
14-
var targetId string
13+
func NewGetResourceCmd() *cobra.Command {
14+
var resourceId string
1515

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

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

4444
// Add flags
45-
cmd.Flags().StringVar(&targetId, "id", "", "ID of the target (required)")
45+
cmd.Flags().StringVar(&resourceId, "id", "", "ID of the target (required)")
4646
cmd.MarkFlagRequired("id")
4747

4848
return cmd

cmd/ctrlc/root/api/create/resources/resources.go renamed to cmd/ctrlc/root/api/upsert/resource/resource.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package resources
1+
package resource
22

33
import (
44
"encoding/json"
@@ -12,7 +12,7 @@ import (
1212
"github.com/spf13/viper"
1313
)
1414

15-
func NewResourcesCmd() *cobra.Command {
15+
func NewUpsertResourceCmd() *cobra.Command {
1616
var workspaceID string
1717
var name string
1818
var identifier string
@@ -21,17 +21,18 @@ func NewResourcesCmd() *cobra.Command {
2121
var metadata map[string]string
2222
var configArray map[string]string
2323
var links map[string]string
24+
var variables map[string]string
2425

2526
cmd := &cobra.Command{
26-
Use: "resources [flags]",
27-
Short: "Create a new resources",
28-
Long: `Create a new resources with the specified version and configuration.`,
27+
Use: "resource [flags]",
28+
Short: "Upsert a resource",
29+
Long: `Upsert a resource with the specified version and configuration.`,
2930
Example: heredoc.Doc(`
30-
# Create a new resource
31-
$ ctrlc create resources --version v1.0.0
31+
# Upsert a resource
32+
$ ctrlc upsert resource --version v1.0.0
3233
33-
# Create a new resource using Go template syntax
34-
$ ctrlc create resources --version v1.0.0 --template='{{.status.phase}}'
34+
# Upsert a resource using Go template syntax
35+
$ ctrlc upsert resource --version v1.0.0 --template='{{.status.phase}}'
3536
`),
3637
RunE: func(cmd *cobra.Command, args []string) error {
3738
apiURL := viper.GetString("url")
@@ -41,9 +42,7 @@ func NewResourcesCmd() *cobra.Command {
4142
return fmt.Errorf("failed to create API client: %w", err)
4243
}
4344

44-
// Convert configArray into a nested map[string]interface{}
4545
config := cliutil.ConvertConfigArrayToNestedMap(configArray)
46-
4746
if len(links) > 0 {
4847
linksJSON, err := json.Marshal(links)
4948
if err != nil {
@@ -52,20 +51,29 @@ func NewResourcesCmd() *cobra.Command {
5251
metadata["ctrlplane/links"] = string(linksJSON)
5352
}
5453

54+
variablesRequest := &[]api.Variable{}
55+
for k, v := range variables {
56+
sensitive := false
57+
vv := api.Variable_Value{}
58+
vv.SetString(v)
59+
60+
*variablesRequest = append(*variablesRequest, api.Variable{
61+
Key: k,
62+
Sensitive: &sensitive,
63+
Value: vv,
64+
})
65+
}
66+
5567
// Extrat into vars
56-
resp, err := client.UpsertTargets(cmd.Context(), api.UpsertTargetsJSONRequestBody{
57-
Targets: []struct {
68+
resp, err := client.UpsertResources(cmd.Context(), api.UpsertResourcesJSONRequestBody{
69+
Resources: []struct {
5870
Config map[string]interface{} `json:"config"`
5971
Identifier string `json:"identifier"`
6072
Kind string `json:"kind"`
6173
Metadata *map[string]string `json:"metadata,omitempty"`
6274
Name string `json:"name"`
63-
Variables *[]struct {
64-
Key string `json:"key"`
65-
Sensitive *bool `json:"sensitive,omitempty"`
66-
Value api.UpsertTargetsJSONBody_Targets_Variables_Value `json:"value"`
67-
} `json:"variables,omitempty"`
68-
Version string `json:"version"`
75+
Variables *[]api.Variable `json:"variables,omitempty"`
76+
Version string `json:"version"`
6977
}{
7078
{
7179
Version: version,
@@ -74,6 +82,7 @@ func NewResourcesCmd() *cobra.Command {
7482
Name: name,
7583
Kind: kind,
7684
Config: config,
85+
Variables: variablesRequest,
7786
},
7887
},
7988

@@ -94,6 +103,8 @@ func NewResourcesCmd() *cobra.Command {
94103
cmd.Flags().StringVar(&identifier, "identifier", "", "Identifier of the resource (required)")
95104
cmd.Flags().StringVar(&kind, "kind", "", "Kind of the resource (required)")
96105
cmd.Flags().StringVar(&version, "version", "", "Version of the resource (required)")
106+
107+
cmd.Flags().StringToStringVar(&variables, "var", make(map[string]string), "Variable key-value pairs (can be specified multiple times)")
97108
cmd.Flags().StringToStringVar(&metadata, "metadata", make(map[string]string), "Metadata key-value pairs (e.g. --metadata key=value)")
98109
cmd.Flags().StringToStringVar(&configArray, "config", make(map[string]string), "Config key-value pairs with nested values (can be specified multiple times)")
99110
cmd.Flags().StringToStringVar(&links, "link", make(map[string]string), "Links key-value pairs (can be specified multiple times)")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package upsert
2+
3+
import (
4+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/upsert/resource"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func NewUpsertCmd() *cobra.Command {
9+
cmd := &cobra.Command{
10+
Use: "upsert <command>",
11+
Short: "Upsert resources",
12+
Long: `Commands for upserting resources.`,
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
return cmd.Help()
15+
},
16+
}
17+
18+
cmd.AddCommand(resource.NewUpsertResourceCmd())
19+
20+
return cmd
21+
}

0 commit comments

Comments
 (0)