1- package resources
1+ package resource
22
33import (
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)" )
0 commit comments