-
Notifications
You must be signed in to change notification settings - Fork 66
/
schema_variable.go
306 lines (283 loc) · 8.17 KB
/
schema_variable.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func expandVariable(d *schema.ResourceData) *variables.Variable {
name := d.Get("name").(string)
variable := variables.NewVariable(name)
if v, ok := d.GetOk("description"); ok {
variable.Description = v.(string)
}
if v, ok := d.GetOk("is_editable"); ok {
variable.IsEditable = v.(bool)
}
if v, ok := d.GetOk("is_sensitive"); ok {
variable.IsSensitive = v.(bool)
}
if v, ok := d.GetOk("type"); ok {
variable.Type = v.(string)
}
if v, ok := d.GetOk("scope"); ok {
variable.Scope = expandVariableScope(v)
}
if v, ok := d.GetOk("space_id"); ok {
variable.SpaceID = v.(string)
}
if variable.IsSensitive {
variable.Type = "Sensitive"
variable.Value = d.Get("sensitive_value").(string)
} else {
variable.Value = d.Get("value").(string)
}
if v, ok := d.GetOk("prompt"); ok {
variable.Prompt = expandPromptedVariableSettings(v)
}
variable.ID = d.Id()
return variable
}
func getVariableDataSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {
Computed: true,
Description: "The identifier of the variable to find.",
Type: schema.TypeString,
},
"name": {
Required: true,
Description: "The name of variable to find.",
Type: schema.TypeString,
},
"owner_id": {
Required: true,
Description: "Owner ID for the variable to find.",
Type: schema.TypeString,
},
"scope": {
Description: "As variable names can appear more than once under different scopes, a VariableScope must also be provided",
Elem: &schema.Resource{Schema: getVariableScopeSchema()},
Required: true,
Type: schema.TypeList,
MaxItems: 1,
},
"space_id": getQuerySpaceID(),
"description": {
Description: "The description of this variable.",
Computed: true,
Type: schema.TypeString,
},
"is_editable": {
Description: "Indicates whether or not this variable is considered editable.",
Computed: true,
Type: schema.TypeBool,
},
"is_sensitive": {
Description: "Indicates whether or not this resource is considered sensitive and should be kept secret.",
Computed: true,
Type: schema.TypeBool,
},
"type": {
Description: "The type of variable represented by this resource. Valid types are `AmazonWebServicesAccount`, `AzureAccount`, `GoogleCloudAccount`, `Certificate`, `Sensitive`, `String`, or `WorkerPool`.",
Computed: true,
Type: schema.TypeString,
},
"sensitive_value": {
Computed: true,
Sensitive: true,
Type: schema.TypeString,
},
"value": {
Computed: true,
Type: schema.TypeString,
},
"prompt": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": getDescriptionSchema("variable prompt option"),
"display_settings": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"control_type": {
Description: "The type of control for rendering this prompted variable. Valid types are `SingleLineText`, `MultiLineText`, `Checkbox`, `Select`.",
Required: true,
Type: schema.TypeString,
},
"select_option": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Description: "The select value",
Required: true,
Type: schema.TypeString,
},
"display_name": {
Description: "The display name for the select value",
Required: true,
Type: schema.TypeString,
},
},
},
Description: "If the `control_type` is `Select`, then this value defines an option.",
Optional: true,
Type: schema.TypeList,
},
},
},
Optional: true,
Type: schema.TypeList,
},
"is_required": {
Type: schema.TypeBool,
Optional: true,
},
"label": {
Type: schema.TypeString,
Optional: true,
},
},
},
Computed: true,
Type: schema.TypeList,
},
}
}
func getVariableSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"description": getDescriptionSchema("variable"),
"encrypted_value": {
Computed: true,
Type: schema.TypeString,
},
"is_editable": {
Default: true,
Description: "Indicates whether or not this variable is considered editable.",
Optional: true,
Type: schema.TypeBool,
},
"is_sensitive": getIsSensitiveSchema(),
"key_fingerprint": {
Computed: true,
Type: schema.TypeString,
},
"name": getNameSchema(true),
"owner_id": {
ConflictsWith: []string{"project_id"},
Optional: true,
Type: schema.TypeString,
},
"pgp_key": {
ForceNew: true,
Optional: true,
Sensitive: true,
Type: schema.TypeString,
},
"project_id": {
ConflictsWith: []string{"owner_id"},
Deprecated: "This attribute is deprecated; please use owner_id instead.",
Optional: true,
Type: schema.TypeString,
},
"prompt": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": getDescriptionSchema("variable prompt option"),
"display_settings": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"control_type": {
Description: "The type of control for rendering this prompted variable. Valid types are `SingleLineText`, `MultiLineText`, `Checkbox`, `Select`.",
Required: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{
"Checkbox",
"MultiLineText",
"Select",
"SingleLineText",
}, false)),
},
"select_option": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Description: "The select value",
Required: true,
Type: schema.TypeString,
},
"display_name": {
Description: "The display name for the select value",
Required: true,
Type: schema.TypeString,
},
},
},
Description: "If the `control_type` is `Select`, then this value defines an option.",
Optional: true,
Type: schema.TypeList,
},
},
},
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
"is_required": {
Type: schema.TypeBool,
Optional: true,
},
"label": {
Type: schema.TypeString,
Optional: true,
},
},
},
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
"scope": {
Elem: &schema.Resource{Schema: getVariableScopeSchema()},
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
"sensitive_value": {
ConflictsWith: []string{"value"},
Optional: true,
Sensitive: true,
Type: schema.TypeString,
},
"type": getVariableTypeSchema(),
"space_id": getSpaceIDSchema(),
"value": {
ConflictsWith: []string{"sensitive_value"},
Optional: true,
Type: schema.TypeString,
},
}
}
func setVariable(ctx context.Context, d *schema.ResourceData, variable *variables.Variable) error {
if d == nil || variable == nil {
return fmt.Errorf("error setting scope")
}
d.Set("description", variable.Description)
d.Set("is_editable", variable.IsEditable)
d.Set("is_sensitive", variable.IsSensitive)
d.Set("name", variable.Name)
d.Set("type", variable.Type)
if variable.IsSensitive {
d.Set("value", nil)
} else {
d.Set("value", variable.Value)
}
if err := d.Set("prompt", flattenPromptedVariableSettings(variable.Prompt)); err != nil {
return fmt.Errorf("error setting prompted config: %s", err)
}
if err := d.Set("scope", flattenVariableScope(variable.Scope)); err != nil {
return fmt.Errorf("error setting scope: %s", err)
}
d.SetId(variable.GetID())
return nil
}