-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.go
351 lines (302 loc) · 9.79 KB
/
spec.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package pipeline
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/rh01/terraform-provider-tekton/tekton/schema/task"
tektonapiv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
)
func TektonPipelineSpecFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"params": {
Type: schema.TypeList,
Description: "Params is a list of input parameters required to run the task. Params must be supplied as inputs in PipelineRuns unless they declare a default value.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: tektonParamSpecFields(),
},
},
"display_name": {
Type: schema.TypeString,
Description: "DisplayName is a user-facing name of the task that may be used to populate a UI.",
Optional: true,
},
"description": {
Type: schema.TypeString,
Description: "Description is a user-facing description of the task that may be used to populate a UI.",
Optional: true,
},
"tasks": {
Type: schema.TypeList,
Description: "Tasks are the tasks of the build; each task is run sequentially with the source mounted into /workspace.",
Optional: true,
Elem: &schema.Resource{
Schema: task.TektonTaskFields(),
},
},
"workspaces": {
Type: schema.TypeList,
Description: "Workspaces are the volumes that this Pipeline requires.",
Optional: true,
Elem: &schema.Resource{
Schema: tektonPipelineWorkspaceDeclarationFields(),
},
},
"results": {
Type: schema.TypeList,
Description: "Results are values that this Pipeline can output",
Optional: true,
Elem: &schema.Resource{
Schema: tektonPipelineResultFields(),
},
},
}
}
func tektonPipelineResultFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "Name declares the name by which a result is referenced.",
Required: true,
},
"type": {
Type: schema.TypeString,
Description: "Type is the user-specified type of the result. The possible types are currently string, array and object, and string is the default.",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"string", "array", "object"}, false),
},
"description": {
Type: schema.TypeString,
Description: "Description is a user-facing description of the result that may be used to populate a UI.",
Optional: true,
},
"value": {
Type: schema.TypeList,
Description: "Value is the expression used to retrieve the value.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: tektonParamValueFields(),
},
},
}
}
func tektonPipelineSpecSchema() *schema.Schema {
fields := TektonPipelineSpecFields()
return &schema.Schema{
Type: schema.TypeList,
Description: fmt.Sprintf("TektonPipelineSpec describes how the proper TektonPipeline should look like."),
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: fields,
},
}
}
func tektonParamSpecFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "Name declares the name by which a parameter is referenced.",
Required: true,
},
"type": {
Type: schema.TypeString,
Description: "Type is the user-specified type of the parameter. The possible types are currently string, array and object, and string is the default.",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"string", "array", "object"}, false),
},
"description": {
Type: schema.TypeString,
Description: "Description is a user-facing description of the parameter that may be used to populate a UI.",
Optional: true,
},
"properties": {
Type: schema.TypeMap,
Description: "Properties is the JSON Schema properties to support key-value pairs parameter.",
Optional: true,
Elem: &schema.Resource{
Schema: tektonPropertySpecFields(),
},
},
"default": {
Type: schema.TypeList,
Description: "Default is the value a parameter takes if no input value is supplied. If default is set, a Pipeline may be executed without a supplied value for the parameter.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: tektonParamValueFields(),
},
},
}
}
func tektonPropertySpecFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Description: "Type is the user-specified type of the parameter. The possible types are currently string, array and object, and string is the default.",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"string", "array", "object"}, false),
},
}
}
func tektonParamValueFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Description: "Type is the user-specified type of the parameter. The possible types are currently string, array and object, and string is the default.",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"string", "array", "object"}, false),
},
"string_val": {
Type: schema.TypeString,
Description: "StringVal is a string value.",
Optional: true,
},
"array_val": {
Type: schema.TypeList,
Description: "ArrayVal is an array of strings.",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"object_val": {
Type: schema.TypeMap,
Description: "ObjectVal is a map of strings to strings.",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
}
}
func expandTektonPipelineSpec(task []interface{}) (tektonapiv1.PipelineSpec, error) {
ppSpec := tektonapiv1.PipelineSpec{}
if len(task) == 0 || task[0] == nil {
return ppSpec, nil
}
tktask := task[0].(map[string]interface{})
// display_name is a user-facing name of the pipeline
if v, ok := tktask["display_name"]; ok {
ppSpec.DisplayName = v.(string)
}
// description is a user-facing description of the pipeline
if v, ok := tktask["description"]; ok {
ppSpec.Description = v.(string)
}
// params
if v, ok := tktask["params"]; ok {
params := v.([]interface{})
for _, param := range params {
p := param.(map[string]interface{})
ppSpec.Params = append(ppSpec.Params, tektonapiv1.ParamSpec{
Name: p["name"].(string),
Type: tektonapiv1.ParamType(p["type"].(string)),
Description: p["description"].(string),
Default: expandTektonParamValue(p["default"].([]interface{})),
})
}
}
//results
if v, ok := tktask["results"]; ok {
results := v.([]interface{})
for _, res := range results {
r := res.(map[string]interface{})
ppSpec.Results = append(ppSpec.Results, tektonapiv1.PipelineResult{
Name: r["name"].(string),
Type: tektonapiv1.ResultsType(r["type"].(string)),
Description: r["description"].(string),
Value: tektonapiv1.ResultValue(*expandTektonParamValue(r["value"].([]interface{}))),
})
}
}
// workspaces
if v, ok := tktask["workspaces"]; ok {
workspaces := v.([]interface{})
for _, ws := range workspaces {
w := ws.(map[string]interface{})
ppSpec.Workspaces = append(ppSpec.Workspaces, tektonapiv1.PipelineWorkspaceDeclaration{
Name: w["name"].(string),
Description: w["description"].(string),
Optional: w["optional"].(bool),
})
}
}
return ppSpec, nil
}
func expandTektonParamValue(value []interface{}) *tektonapiv1.ParamValue {
if len(value) == 0 || value[0] == nil {
return nil
}
v := value[0].(map[string]interface{})
return &tektonapiv1.ParamValue{
Type: tektonapiv1.ParamType(v["type"].(string)),
StringVal: v["string_val"].(string),
ArrayVal: expandTektonArrayValue(v["array_val"].([]interface{})),
}
}
func expandTektonArrayValue(value []interface{}) []string {
var result []string
for _, v := range value {
result = append(result, v.(string))
}
return result
}
func flattenTektonPipelineSpec(in tektonapiv1.PipelineSpec) []interface{} {
att := make(map[string]interface{})
att["display_name"] = in.DisplayName
att["description"] = in.Description
att["params"] = flattenTektonParamSpec(in.Params)
att["results"] = flattenTektonPipelineResult(in.Results)
att["workspaces"] = flattenTektonPipelineWorkspaceDeclaration(in.Workspaces)
return []interface{}{att}
}
func flattenTektonParamSpec(in []tektonapiv1.ParamSpec) []interface{} {
var result []interface{}
for _, v := range in {
att := make(map[string]interface{})
att["name"] = v.Name
att["type"] = v.Type
att["description"] = v.Description
att["default"] = flattenTektonParamValue(v.Default)
result = append(result, att)
}
return result
}
func flattenTektonPipelineResult(in []tektonapiv1.PipelineResult) []interface{} {
var result []interface{}
for _, v := range in {
att := make(map[string]interface{})
att["name"] = v.Name
att["type"] = v.Type
att["description"] = v.Description
att["value"] = flattenTektonParamValue(&v.Value)
result = append(result, att)
}
return result
}
func flattenTektonPipelineWorkspaceDeclaration(in []tektonapiv1.PipelineWorkspaceDeclaration) []interface{} {
var result []interface{}
for _, v := range in {
att := make(map[string]interface{})
att["name"] = v.Name
att["description"] = v.Description
att["optional"] = v.Optional
result = append(result, att)
}
return result
}
func flattenTektonParamValue(in *tektonapiv1.ParamValue) []interface{} {
var result []interface{}
if in == nil {
return result
}
att := make(map[string]interface{})
att["type"] = in.Type
att["string_val"] = in.StringVal
att["array_val"] = in.ArrayVal
result = append(result, att)
return result
}