Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 58 additions & 49 deletions cmd/ctrlc/root/apply/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,69 +69,78 @@ func processDeployment(
}
}

func getDirectDeploymentVariableValue(value DirectDeploymentVariableValue) (api.DirectDeploymentVariableValue, error) {
directValue := api.DirectDeploymentVariableValue{
IsDefault: value.IsDefault,
Sensitive: value.Sensitive,
ResourceSelector: value.ResourceSelector,
Value: &api.DirectDeploymentVariableValue_Value{},
}

valueData, err := json.Marshal(value.Value)
if err != nil {
return api.DirectDeploymentVariableValue{}, err
}
if err := directValue.Value.UnmarshalJSON(valueData); err != nil {
return api.DirectDeploymentVariableValue{}, err
}
return directValue, nil
}

func getReferenceDeploymentVariableValue(value ReferenceDeploymentVariableValue) (api.ReferenceDeploymentVariableValue, error) {
referenceValue := api.ReferenceDeploymentVariableValue{
IsDefault: value.IsDefault,
ResourceSelector: value.ResourceSelector,
Path: value.Path,
Reference: value.Reference,
}

if value.DefaultValue != nil {
referenceValue.DefaultValue = &api.ReferenceDeploymentVariableValue_DefaultValue{}
valueData, err := json.Marshal(value.DefaultValue)
if err != nil {
return api.ReferenceDeploymentVariableValue{}, err
}
if err := referenceValue.DefaultValue.UnmarshalJSON(valueData); err != nil {
return api.ReferenceDeploymentVariableValue{}, err
}
}

return referenceValue, nil
}

func upsertDeploymentVariable(
ctx context.Context,
client *api.ClientWithResponses,
deploymentID uuid.UUID,
variable DeploymentVariable,
) {
vars := []api.VariableValue{}
for _, value := range variable.Values {
if value.Value != nil {
directValue := api.DeploymentVariableDirectValue{}
directValue.Default = value.Default
directValue.Sensitive = value.Sensitive
directValue.ValueType = "direct"
directValue.ResourceSelector = value.ResourceSelector

directValue.Value = api.DeploymentVariableDirectValue_Value{}
valueData, err := json.Marshal(*value.Value)
if err != nil {
log.Error("Failed to marshal direct value", "error", err)
continue
}
directValue.Value.UnmarshalJSON(valueData)

var varDirect api.VariableValue
varDirect.FromDeploymentVariableDirectValue(directValue)
vars = append(vars, varDirect)
directValues := []api.DirectDeploymentVariableValue{}
for _, value := range variable.DirectValues {
directValue, err := getDirectDeploymentVariableValue(value)
if err != nil {
log.Error("Failed to get direct deployment variable value", "error", err)
continue
}
directValues = append(directValues, directValue)
}

if value.Reference != nil && value.Path != nil {
referenceValue := api.DeploymentVariableReferenceValue{}
referenceValue.Default = value.Default
referenceValue.Reference = *value.Reference
referenceValue.Path = *value.Path
referenceValue.ResourceSelector = value.ResourceSelector
referenceValue.ValueType = "reference"

if value.DefaultValue != nil {
referenceValue.DefaultValue = &api.DeploymentVariableReferenceValue_DefaultValue{}
valueData, err := json.Marshal(*value.DefaultValue)
if err != nil {
log.Error("Failed to marshal default value", "error", err)
continue
}
referenceValue.DefaultValue.UnmarshalJSON(valueData)
}

varReference := api.VariableValue{}
varReference.FromDeploymentVariableReferenceValue(referenceValue)
vars = append(vars, varReference)
referenceValues := []api.ReferenceDeploymentVariableValue{}
for _, value := range variable.ReferenceValues {
referenceValue, err := getReferenceDeploymentVariableValue(value)
if err != nil {
log.Error("Failed to get reference deployment variable value", "error", err)
continue
}

log.Error("Unsupported variable value type", "type", variable.Values)
referenceValues = append(referenceValues, referenceValue)
}

log.Info("Creating deployment variable", "key", variable.Key, "values", len(vars))

body := api.CreateDeploymentVariableJSONRequestBody{
Key: variable.Key,
Description: variable.Description,
Values: &vars,
Config: variable.Config,
Key: variable.Key,
Description: variable.Description,
DirectValues: &directValues,
ReferenceValues: &referenceValues,
Config: variable.Config,
}

_, err := client.CreateDeploymentVariableWithResponse(ctx, deploymentID, body)
Expand Down
26 changes: 16 additions & 10 deletions cmd/ctrlc/root/apply/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@ type Environment struct {
Metadata *map[string]string `yaml:"metadata,omitempty"`
}

type DeploymentVariableValue struct {
Value *any `yaml:"value,omitempty"`
type DirectDeploymentVariableValue struct {
Value any `yaml:"value"`
Sensitive *bool `yaml:"sensitive,omitempty"`

DefaultValue *any `yaml:"defaultValue,omitempty"`
Reference *string `yaml:"reference,omitempty"`
Path *[]string `yaml:"path,omitempty"`
IsDefault *bool `yaml:"isDefault,omitempty"`
ResourceSelector *map[string]any `yaml:"resourceSelector,omitempty"`
}

type ReferenceDeploymentVariableValue struct {
Reference string `yaml:"reference"`
Path []string `yaml:"path"`
DefaultValue *any `yaml:"defaultValue,omitempty"`

Default *bool `yaml:"default,omitempty"`
IsDefault *bool `yaml:"isDefault,omitempty"`
ResourceSelector *map[string]any `yaml:"resourceSelector,omitempty"`
}

type DeploymentVariable struct {
Key string `yaml:"key"`
Config map[string]any `yaml:"config"`
Description *string `yaml:"description"`
Values []DeploymentVariableValue `yaml:"values"`
Key string `yaml:"key"`
Config map[string]any `yaml:"config"`
Description *string `yaml:"description"`
DirectValues []DirectDeploymentVariableValue `yaml:"directValues"`
ReferenceValues []ReferenceDeploymentVariableValue `yaml:"referenceValues"`
}

type ExitHook struct {
Expand Down
Loading