-
Notifications
You must be signed in to change notification settings - Fork 66
/
schema_deployment_step.go
243 lines (213 loc) · 9.53 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package octopusdeploy
import (
"context"
"fmt"
"sort"
"strings"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/deployments"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func expandDeploymentStep(ctx context.Context, flattenedStep map[string]interface{}) *deployments.DeploymentStep {
name := flattenedStep["name"].(string)
step := deployments.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 = deployments.DeploymentStepConditionType(condition.(string))
}
if conditionExpression, ok := flattenedStep["condition_expression"]; ok {
step.Properties["Octopus.Step.ConditionVariableExpression"] = core.NewPropertyValue(conditionExpression.(string), false)
}
if packageRequirement, ok := flattenedStep["package_requirement"]; ok {
step.PackageRequirement = deployments.DeploymentStepPackageRequirement(packageRequirement.(string))
}
if startTrigger, ok := flattenedStep["start_trigger"]; ok {
step.StartTrigger = deployments.DeploymentStepStartTrigger(startTrigger.(string))
}
if targetRoles, ok := flattenedStep["target_roles"]; ok {
step.Properties["Octopus.Action.TargetRoles"] = core.NewPropertyValue(strings.Join(getSliceFromTerraformTypeList(targetRoles), ","), false)
}
if windowSize, ok := flattenedStep["window_size"]; ok {
step.Properties["Octopus.Action.MaxParallelism"] = core.NewPropertyValue(windowSize.(string), false)
}
var sort_order map[string]int = make(map[string]int)
step_expansion := func(step_type_name string, step_type_action func(map[string]interface{}) *deployments.DeploymentAction) {
if v, ok := flattenedStep[step_type_name]; ok {
for _, tfAction := range v.([]interface{}) {
flattenedAction := tfAction.(map[string]interface{})
action := step_type_action(flattenedAction)
step.Actions = append(step.Actions, action)
// Pull out the sort_order if it exists. This is used to sort the actions later
if posn, ok := flattenedAction["sort_order"].(int); ok && posn >= 0 {
name := flattenedAction["name"].(string)
sort_order[name] = posn
}
}
}
}
step_expansion("action", expandAction)
step_expansion("manual_intervention_action", expandManualInterventionAction)
step_expansion("apply_terraform_template_action", expandApplyTerraformTemplateAction)
step_expansion("deploy_package_action", expandDeployPackageAction)
step_expansion("deploy_windows_service_action", expandDeployWindowsServiceAction)
step_expansion("run_script_action", expandRunScriptAction)
step_expansion("run_kubectl_script_action", expandRunKubectlScriptAction)
step_expansion("deploy_kubernetes_secret_action", expandDeployKubernetesSecretAction)
// Now that we have extracted all the steps off each of the properties into a single array, sort the array by the sort_order if provided
if len(sort_order) > 0 {
sort_order_entries := make(map[int][]string)
// Validate there are no duplicate sort_order entries
for step_name, sort_order := range sort_order {
sort_order_entries[sort_order] = append(sort_order_entries[sort_order], step_name)
}
for _, matching_names := range sort_order_entries {
if len(matching_names) > 1 {
tflog.Warn(ctx, fmt.Sprintf("The following actions have the same sort_order: %v", matching_names))
}
}
// Validate that every step has a sort_order
if len(sort_order) != len(step.Actions) {
tflog.Warn(ctx, fmt.Sprintf("Not all actions on step '%s' have a `sort_order` parameter so they may be sorted in an unexpected order", step.Name))
}
sort.SliceStable(step.Actions, func(i, j int) bool {
return sort_order[step.Actions[i].Name] < sort_order[step.Actions[j].Name]
})
}
return step
}
func flattenDeploymentSteps(deploymentSteps []*deployments.DeploymentStep) []map[string]interface{} {
if deploymentSteps == nil {
return nil
}
var flattenedDeploymentSteps = make([]map[string]interface{}, len(deploymentSteps))
for key, deploymentStep := range deploymentSteps {
flattenedDeploymentStep := map[string]interface{}{}
flattenedDeploymentStep["condition"] = deploymentStep.Condition
flattenedDeploymentStep["id"] = deploymentStep.ID
flattenedDeploymentStep["name"] = deploymentStep.Name
flattenedDeploymentStep["package_requirement"] = deploymentStep.PackageRequirement
flattenedDeploymentStep["properties"] = flattenProperties(deploymentStep.Properties)
flattenedDeploymentStep["start_trigger"] = deploymentStep.StartTrigger
for propertyName, propertyValue := range deploymentStep.Properties {
switch propertyName {
case "Octopus.Action.TargetRoles":
flattenedDeploymentStep["target_roles"] = strings.Split(propertyValue.Value, ",")
case "Octopus.Action.MaxParallelism":
flattenedDeploymentStep["window_size"] = propertyValue.Value
case "Octopus.Step.ConditionVariableExpression":
flattenedDeploymentStep["condition_expression"] = propertyValue.Value
}
}
flatten_action_func := func(step_type_name string, i int, fp func(*deployments.DeploymentAction) map[string]interface{}) {
if _, ok := flattenedDeploymentStep[step_type_name]; !ok {
flattenedDeploymentStep[step_type_name] = make([]map[string]interface{}, 0)
}
action := fp(deploymentStep.Actions[i])
action["sort_order"] = i + 1
flattenedDeploymentStep[step_type_name] = append(flattenedDeploymentStep[step_type_name].([]map[string]interface{}), action)
}
for i := range deploymentStep.Actions {
switch deploymentStep.Actions[i].ActionType {
case "Octopus.KubernetesDeploySecret":
flatten_action_func("deploy_kubernetes_secret_action", i, flattenDeployKubernetesSecretAction)
case "Octopus.KubernetesRunScript":
flatten_action_func("run_kubectl_script_action", i, flattenKubernetesRunScriptAction)
case "Octopus.Manual":
flatten_action_func("manual_intervention_action", i, flattenManualInterventionAction)
case "Octopus.Script":
flatten_action_func("run_script_action", i, flattenRunScriptAction)
case "Octopus.TentaclePackage":
flatten_action_func("deploy_package_action", i, flattenDeployPackageAction)
case "Octopus.TerraformApply":
flatten_action_func("apply_terraform_template_action", i, flattenApplyTerraformTemplateAction)
case "Octopus.WindowsService":
flatten_action_func("deploy_windows_service_action", i, flattenDeployWindowsServiceAction)
default:
flatten_action_func("action", i, flattenDeploymentAction)
}
}
flattenedDeploymentSteps[key] = flattenedDeploymentStep
}
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,
}
}