Skip to content

Commit 5d075ea

Browse files
Merge pull request #32 from ctrlplanedev/refactor-deployment-variable-cli
refactor: deployment variables cli
2 parents 37956e6 + 920d338 commit 5d075ea

File tree

3 files changed

+594
-357
lines changed

3 files changed

+594
-357
lines changed

cmd/ctrlc/root/apply/deployment.go

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -69,69 +69,78 @@ func processDeployment(
6969
}
7070
}
7171

72+
func getDirectDeploymentVariableValue(value DirectDeploymentVariableValue) (api.DirectDeploymentVariableValue, error) {
73+
directValue := api.DirectDeploymentVariableValue{
74+
IsDefault: value.IsDefault,
75+
Sensitive: value.Sensitive,
76+
ResourceSelector: value.ResourceSelector,
77+
Value: &api.DirectDeploymentVariableValue_Value{},
78+
}
79+
80+
valueData, err := json.Marshal(value.Value)
81+
if err != nil {
82+
return api.DirectDeploymentVariableValue{}, err
83+
}
84+
if err := directValue.Value.UnmarshalJSON(valueData); err != nil {
85+
return api.DirectDeploymentVariableValue{}, err
86+
}
87+
return directValue, nil
88+
}
89+
90+
func getReferenceDeploymentVariableValue(value ReferenceDeploymentVariableValue) (api.ReferenceDeploymentVariableValue, error) {
91+
referenceValue := api.ReferenceDeploymentVariableValue{
92+
IsDefault: value.IsDefault,
93+
ResourceSelector: value.ResourceSelector,
94+
Path: value.Path,
95+
Reference: value.Reference,
96+
}
97+
98+
if value.DefaultValue != nil {
99+
referenceValue.DefaultValue = &api.ReferenceDeploymentVariableValue_DefaultValue{}
100+
valueData, err := json.Marshal(value.DefaultValue)
101+
if err != nil {
102+
return api.ReferenceDeploymentVariableValue{}, err
103+
}
104+
if err := referenceValue.DefaultValue.UnmarshalJSON(valueData); err != nil {
105+
return api.ReferenceDeploymentVariableValue{}, err
106+
}
107+
}
108+
109+
return referenceValue, nil
110+
}
111+
72112
func upsertDeploymentVariable(
73113
ctx context.Context,
74114
client *api.ClientWithResponses,
75115
deploymentID uuid.UUID,
76116
variable DeploymentVariable,
77117
) {
78-
vars := []api.VariableValue{}
79-
for _, value := range variable.Values {
80-
if value.Value != nil {
81-
directValue := api.DeploymentVariableDirectValue{}
82-
directValue.Default = value.Default
83-
directValue.Sensitive = value.Sensitive
84-
directValue.ValueType = "direct"
85-
directValue.ResourceSelector = value.ResourceSelector
86-
87-
directValue.Value = api.DeploymentVariableDirectValue_Value{}
88-
valueData, err := json.Marshal(*value.Value)
89-
if err != nil {
90-
log.Error("Failed to marshal direct value", "error", err)
91-
continue
92-
}
93-
directValue.Value.UnmarshalJSON(valueData)
94-
95-
var varDirect api.VariableValue
96-
varDirect.FromDeploymentVariableDirectValue(directValue)
97-
vars = append(vars, varDirect)
118+
directValues := []api.DirectDeploymentVariableValue{}
119+
for _, value := range variable.DirectValues {
120+
directValue, err := getDirectDeploymentVariableValue(value)
121+
if err != nil {
122+
log.Error("Failed to get direct deployment variable value", "error", err)
98123
continue
99124
}
125+
directValues = append(directValues, directValue)
126+
}
100127

101-
if value.Reference != nil && value.Path != nil {
102-
referenceValue := api.DeploymentVariableReferenceValue{}
103-
referenceValue.Default = value.Default
104-
referenceValue.Reference = *value.Reference
105-
referenceValue.Path = *value.Path
106-
referenceValue.ResourceSelector = value.ResourceSelector
107-
referenceValue.ValueType = "reference"
108-
109-
if value.DefaultValue != nil {
110-
referenceValue.DefaultValue = &api.DeploymentVariableReferenceValue_DefaultValue{}
111-
valueData, err := json.Marshal(*value.DefaultValue)
112-
if err != nil {
113-
log.Error("Failed to marshal default value", "error", err)
114-
continue
115-
}
116-
referenceValue.DefaultValue.UnmarshalJSON(valueData)
117-
}
118-
119-
varReference := api.VariableValue{}
120-
varReference.FromDeploymentVariableReferenceValue(referenceValue)
121-
vars = append(vars, varReference)
128+
referenceValues := []api.ReferenceDeploymentVariableValue{}
129+
for _, value := range variable.ReferenceValues {
130+
referenceValue, err := getReferenceDeploymentVariableValue(value)
131+
if err != nil {
132+
log.Error("Failed to get reference deployment variable value", "error", err)
122133
continue
123134
}
124-
125-
log.Error("Unsupported variable value type", "type", variable.Values)
135+
referenceValues = append(referenceValues, referenceValue)
126136
}
127137

128-
log.Info("Creating deployment variable", "key", variable.Key, "values", len(vars))
129-
130138
body := api.CreateDeploymentVariableJSONRequestBody{
131-
Key: variable.Key,
132-
Description: variable.Description,
133-
Values: &vars,
134-
Config: variable.Config,
139+
Key: variable.Key,
140+
Description: variable.Description,
141+
DirectValues: &directValues,
142+
ReferenceValues: &referenceValues,
143+
Config: variable.Config,
135144
}
136145

137146
_, err := client.CreateDeploymentVariableWithResponse(ctx, deploymentID, body)

cmd/ctrlc/root/apply/types.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,29 @@ type Environment struct {
2525
Metadata *map[string]string `yaml:"metadata,omitempty"`
2626
}
2727

28-
type DeploymentVariableValue struct {
29-
Value *any `yaml:"value,omitempty"`
28+
type DirectDeploymentVariableValue struct {
29+
Value any `yaml:"value"`
3030
Sensitive *bool `yaml:"sensitive,omitempty"`
3131

32-
DefaultValue *any `yaml:"defaultValue,omitempty"`
33-
Reference *string `yaml:"reference,omitempty"`
34-
Path *[]string `yaml:"path,omitempty"`
32+
IsDefault *bool `yaml:"isDefault,omitempty"`
33+
ResourceSelector *map[string]any `yaml:"resourceSelector,omitempty"`
34+
}
35+
36+
type ReferenceDeploymentVariableValue struct {
37+
Reference string `yaml:"reference"`
38+
Path []string `yaml:"path"`
39+
DefaultValue *any `yaml:"defaultValue,omitempty"`
3540

36-
Default *bool `yaml:"default,omitempty"`
41+
IsDefault *bool `yaml:"isDefault,omitempty"`
3742
ResourceSelector *map[string]any `yaml:"resourceSelector,omitempty"`
3843
}
3944

4045
type DeploymentVariable struct {
41-
Key string `yaml:"key"`
42-
Config map[string]any `yaml:"config"`
43-
Description *string `yaml:"description"`
44-
Values []DeploymentVariableValue `yaml:"values"`
46+
Key string `yaml:"key"`
47+
Config map[string]any `yaml:"config"`
48+
Description *string `yaml:"description"`
49+
DirectValues []DirectDeploymentVariableValue `yaml:"directValues"`
50+
ReferenceValues []ReferenceDeploymentVariableValue `yaml:"referenceValues"`
4551
}
4652

4753
type ExitHook struct {

0 commit comments

Comments
 (0)