-
Notifications
You must be signed in to change notification settings - Fork 65
/
schema_phase.go
156 lines (133 loc) · 4.58 KB
/
schema_phase.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
package octopusdeploy
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/lifecycles"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func expandPhase(flattenedPhase interface{}) *lifecycles.Phase {
if flattenedPhase == nil {
return nil
}
if _, ok := flattenedPhase.(map[string]interface{}); !ok {
return nil
}
flattenedValues := flattenedPhase.(map[string]interface{})
if len(flattenedValues) == 0 {
return nil
}
name := flattenedValues["name"].(string)
phase := lifecycles.NewPhase(name)
if v, ok := flattenedValues["automatic_deployment_targets"]; ok {
phase.AutomaticDeploymentTargets = getSliceFromTerraformTypeList(v)
}
if v, ok := flattenedValues["is_optional_phase"]; ok {
phase.IsOptionalPhase = v.(bool)
}
if v, ok := flattenedValues["minimum_environments_before_promotion"]; ok {
if n, isInt32 := v.(int32); isInt32 {
phase.MinimumEnvironmentsBeforePromotion = n
} else {
phase.MinimumEnvironmentsBeforePromotion = int32(v.(int))
}
}
if v, ok := flattenedValues["optional_deployment_targets"]; ok {
phase.OptionalDeploymentTargets = getSliceFromTerraformTypeList(v)
}
if v, ok := flattenedValues["release_retention_policy"]; ok {
phase.ReleaseRetentionPolicy = expandRetentionPeriod(v)
}
if v, ok := flattenedValues["tentacle_retention_policy"]; ok {
phase.TentacleRetentionPolicy = expandRetentionPeriod(v)
}
if phase.AutomaticDeploymentTargets == nil {
phase.AutomaticDeploymentTargets = []string{}
}
if phase.OptionalDeploymentTargets == nil {
phase.OptionalDeploymentTargets = []string{}
}
return phase
}
func expandPhases(flattenedPhases interface{}) []*lifecycles.Phase {
if flattenedPhases == nil {
return nil
}
if _, ok := flattenedPhases.([]interface{}); !ok {
return nil
}
flattenedValues := flattenedPhases.([]interface{})
if len(flattenedValues) == 0 {
return nil
}
phases := []*lifecycles.Phase{}
for _, flattenedValues := range flattenedValues {
phases = append(phases, expandPhase(flattenedValues))
}
return phases
}
func flattenPhase(phase *lifecycles.Phase) interface{} {
if phase == nil {
return nil
}
flattenedPhase := make(map[string]interface{})
flattenedPhase["automatic_deployment_targets"] = flattenArray(phase.AutomaticDeploymentTargets)
flattenedPhase["id"] = phase.ID
flattenedPhase["is_optional_phase"] = phase.IsOptionalPhase
flattenedPhase["minimum_environments_before_promotion"] = int(phase.MinimumEnvironmentsBeforePromotion)
flattenedPhase["name"] = phase.Name
flattenedPhase["optional_deployment_targets"] = flattenArray(phase.OptionalDeploymentTargets)
if phase.ReleaseRetentionPolicy != nil {
flattenedPhase["release_retention_policy"] = flattenRetentionPeriod(phase.ReleaseRetentionPolicy)
}
if phase.TentacleRetentionPolicy != nil {
flattenedPhase["tentacle_retention_policy"] = flattenRetentionPeriod(phase.TentacleRetentionPolicy)
}
return flattenedPhase
}
func flattenPhases(phases []*lifecycles.Phase) []interface{} {
flattenedPhases := make([]interface{}, 0)
for _, phase := range phases {
flattenedPhases = append(flattenedPhases, flattenPhase(phase))
}
return flattenedPhases
}
func getPhaseSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"automatic_deployment_targets": {
Description: "Environment IDs in this phase that a release is automatically deployed to when it is eligible for this phase",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
"id": getIDSchema(),
"is_optional_phase": {
Default: false,
Description: "If false a release must be deployed to this phase before it can be deployed to the next phase.",
Optional: true,
Type: schema.TypeBool,
},
"minimum_environments_before_promotion": {
Default: 0,
Description: "The number of units required before a release can enter the next phase. If 0, all environments are required.",
Optional: true,
Type: schema.TypeInt,
},
"name": getNameSchema(true),
"optional_deployment_targets": {
Description: "Environment IDs in this phase that a release can be deployed to, but is not automatically deployed to",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
"release_retention_policy": {
Elem: &schema.Resource{Schema: getRetentionPeriodSchema()},
Optional: true,
MaxItems: 1,
Type: schema.TypeList,
},
"tentacle_retention_policy": {
Elem: &schema.Resource{Schema: getRetentionPeriodSchema()},
Optional: true,
MaxItems: 1,
Type: schema.TypeList,
},
}
}