-
Notifications
You must be signed in to change notification settings - Fork 65
/
schema_deployment_step.go
229 lines (204 loc) · 8.82 KB
/
schema_deployment_step.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package octopusdeploy
import (
"strings"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func expandDeploymentStep(flattenedStep map[string]interface{}) *octopusdeploy.DeploymentStep {
name := flattenedStep["name"].(string)
step := octopusdeploy.NewDeploymentStep(name)
// properties MUST be serialized first
if properties, ok := flattenedStep["properties"]; ok {
step.Properties = expandProperties(properties)
}
if condition, ok := flattenedStep["condition"]; ok {
step.Condition = octopusdeploy.DeploymentStepConditionType(condition.(string))
}
if conditionExpression, ok := flattenedStep["condition_expression"]; ok {
step.Properties["Octopus.Step.ConditionVariableExpression"] = octopusdeploy.NewPropertyValue(conditionExpression.(string), false)
}
if packageRequirement, ok := flattenedStep["package_requirement"]; ok {
step.PackageRequirement = octopusdeploy.DeploymentStepPackageRequirement(packageRequirement.(string))
}
if startTrigger, ok := flattenedStep["start_trigger"]; ok {
step.StartTrigger = octopusdeploy.DeploymentStepStartTrigger(startTrigger.(string))
}
if targetRoles, ok := flattenedStep["target_roles"]; ok {
step.Properties["Octopus.Action.TargetRoles"] = octopusdeploy.NewPropertyValue(strings.Join(getSliceFromTerraformTypeList(targetRoles), ","), false)
}
if windowSize, ok := flattenedStep["window_size"]; ok {
step.Properties["Octopus.Action.MaxParallelism"] = octopusdeploy.NewPropertyValue(windowSize.(string), false)
}
if v, ok := flattenedStep["action"]; ok {
for _, tfAction := range v.([]interface{}) {
action := expandAction(tfAction.(map[string]interface{}))
step.Actions = append(step.Actions, action)
}
}
if v, ok := flattenedStep["manual_intervention_action"]; ok {
for _, tfAction := range v.([]interface{}) {
action := expandManualInterventionAction(tfAction.(map[string]interface{}))
step.Actions = append(step.Actions, action)
}
}
if v, ok := flattenedStep["apply_terraform_template_action"]; ok {
for _, tfAction := range v.([]interface{}) {
action := expandApplyTerraformTemplateAction(tfAction.(map[string]interface{}))
step.Actions = append(step.Actions, action)
}
}
if v, ok := flattenedStep["deploy_package_action"]; ok {
for _, tfAction := range v.([]interface{}) {
action := expandDeployPackageAction(tfAction.(map[string]interface{}))
step.Actions = append(step.Actions, action)
}
}
if v, ok := flattenedStep["deploy_windows_service_action"]; ok {
for _, tfAction := range v.([]interface{}) {
action := expandDeployWindowsServiceAction(tfAction.(map[string]interface{}))
step.Actions = append(step.Actions, action)
}
}
if v, ok := flattenedStep["run_script_action"]; ok {
for _, tfAction := range v.([]interface{}) {
action := expandRunScriptAction(tfAction.(map[string]interface{}))
step.Actions = append(step.Actions, action)
}
}
if v, ok := flattenedStep["run_kubectl_script_action"]; ok {
for _, tfAction := range v.([]interface{}) {
action := expandRunKubectlScriptAction(tfAction.(map[string]interface{}))
step.Actions = append(step.Actions, action)
}
}
if v, ok := flattenedStep["deploy_kubernetes_secret_action"]; ok {
for _, tfAction := range v.([]interface{}) {
action := expandDeployKubernetesSecretAction(tfAction.(map[string]interface{}))
step.Actions = append(step.Actions, action)
}
}
return step
}
func flattenDeploymentSteps(deploymentSteps []octopusdeploy.DeploymentStep) []map[string]interface{} {
if deploymentSteps == nil {
return nil
}
var flattenedDeploymentSteps = make([]map[string]interface{}, len(deploymentSteps))
for key, deploymentStep := range deploymentSteps {
flattenedDeploymentSteps[key] = map[string]interface{}{}
flattenedDeploymentSteps[key]["condition"] = deploymentStep.Condition
flattenedDeploymentSteps[key]["id"] = deploymentStep.ID
flattenedDeploymentSteps[key]["name"] = deploymentStep.Name
flattenedDeploymentSteps[key]["package_requirement"] = deploymentStep.PackageRequirement
flattenedDeploymentSteps[key]["properties"] = flattenProperties(deploymentStep.Properties)
flattenedDeploymentSteps[key]["start_trigger"] = deploymentStep.StartTrigger
for propertyName, propertyValue := range deploymentStep.Properties {
switch propertyName {
case "Octopus.Action.TargetRoles":
flattenedDeploymentSteps[key]["target_roles"] = strings.Split(propertyValue.Value, ",")
case "Octopus.Action.MaxParallelism":
flattenedDeploymentSteps[key]["window_size"] = propertyValue.Value
case "Octopus.Step.ConditionVariableExpression":
flattenedDeploymentSteps[key]["condition_expression"] = propertyValue.Value
}
}
for _, action := range deploymentStep.Actions {
switch action.ActionType {
case "Octopus.KubernetesDeploySecret":
flattenedDeploymentSteps[key]["deploy_kubernetes_secret_action"] = []interface{}{flattenDeployKubernetesSecretAction(action)}
case "Octopus.KubernetesRunScript":
flattenedDeploymentSteps[key]["run_kubectl_script_action"] = []interface{}{flattenKubernetesRunScriptAction(action)}
case "Octopus.Manual":
flattenedDeploymentSteps[key]["manual_intervention_action"] = []interface{}{flattenManualInterventionAction(action)}
case "Octopus.Script":
flattenedDeploymentSteps[key]["run_script_action"] = []interface{}{flattenRunScriptAction(action)}
case "Octopus.TentaclePackage":
flattenedDeploymentSteps[key]["deploy_package_action"] = []interface{}{flattenDeployPackageAction(action)}
case "Octopus.TerraformApply":
flattenedDeploymentSteps[key]["apply_terraform_template_action"] = []interface{}{flattenApplyTerraformTemplateAction(action)}
case "Octopus.WindowsService":
flattenedDeploymentSteps[key]["deploy_windows_service_action"] = []interface{}{flattenDeployWindowsServiceAction(action)}
default:
flattenedDeploymentSteps[key]["action"] = []interface{}{flattenDeploymentAction(action)}
}
}
}
return flattenedDeploymentSteps
}
func getDeploymentStepSchema() *schema.Schema {
return &schema.Schema{
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": getDeploymentActionSchema(),
"apply_terraform_template_action": getApplyTerraformTemplateActionSchema(),
"condition": {
Default: "Success",
Description: "When to run the step, one of 'Success', 'Failure', 'Always' or 'Variable'",
Optional: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{
"Always",
"Failure",
"Success",
"Variable",
}, false)),
},
"condition_expression": {
Computed: true,
Description: "The expression to evaluate to determine whether to run this step when 'condition' is 'Variable'",
Optional: true,
Type: schema.TypeString,
},
"deploy_kubernetes_secret_action": getDeployKubernetesSecretActionSchema(),
"deploy_package_action": getDeployPackageActionSchema(),
"deploy_windows_service_action": getDeployWindowsServiceActionSchema(),
"id": getIDSchema(),
"manual_intervention_action": getManualInterventionActionSchema(),
"name": getNameSchema(true),
"package_requirement": {
Default: "LetOctopusDecide",
Description: "Whether to run this step before or after package acquisition (if possible)",
Optional: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{
"AfterPackageAcquisition",
"BeforePackageAcquisition",
"LetOctopusDecide",
}, false)),
},
"properties": {
Computed: true,
Optional: true,
Type: schema.TypeMap,
},
"run_kubectl_script_action": getRunKubectlScriptSchema(),
"run_script_action": getRunScriptActionSchema(),
"start_trigger": {
Default: "StartAfterPrevious",
Description: "Whether to run this step after the previous step ('StartAfterPrevious') or at the same time as the previous step ('StartWithPrevious')",
Optional: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{
"StartAfterPrevious",
"StartWithPrevious",
}, false)),
},
"target_roles": {
Computed: true,
Description: "The roles that this step run against, or runs on behalf of",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
"window_size": {
Description: "The maximum number of targets to deploy to simultaneously",
Optional: true,
Type: schema.TypeString,
},
},
},
Optional: true,
Type: schema.TypeList,
}
}