/
data_source_ibm_cd_tekton_pipeline_trigger.go
478 lines (444 loc) · 17 KB
/
data_source_ibm_cd_tekton_pipeline_trigger.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package cdtektonpipeline
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/continuous-delivery-go-sdk/cdtektonpipelinev2"
)
func DataSourceIBMCdTektonPipelineTrigger() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMCdTektonPipelineTriggerRead,
Schema: map[string]*schema.Schema{
"pipeline_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The Tekton pipeline ID.",
},
"trigger_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The trigger ID.",
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Trigger type.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Trigger name.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "API URL for interacting with the trigger. Only included when fetching the list of pipeline triggers.",
},
"event_listener": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Event listener name. The name of the event listener to which the trigger is associated. The event listeners are defined in the definition repositories of the Tekton pipeline.",
},
"properties": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Optional trigger properties used to override or supplement the pipeline properties when triggering a pipeline run.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Property name.",
},
"value": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Property value. Any string value is valid.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "API URL for interacting with the trigger property.",
},
"enum": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Options for `single_select` property type. Only needed for `single_select` property type.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Property type.",
},
"path": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A dot notation path for `integration` type properties only, that selects a value from the tool integration. If left blank the full tool integration data will be used.",
},
},
},
},
"tags": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Optional trigger tags array.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"worker": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Details of the worker used to run the trigger.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Name of the worker. Computed based on the worker ID.",
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Type of the worker. Computed based on the worker ID.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "ID of the worker.",
},
},
},
},
"max_concurrent_runs": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "Defines the maximum number of concurrent runs for this trigger. If omitted then the concurrency limit is disabled for this trigger.",
},
"enabled": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Flag whether the trigger is enabled.",
},
"favorite": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Mark the trigger as a favorite.",
},
"source": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Source repository for a Git trigger. Only required for Git triggers. The referenced repository URL must match the URL of a repository tool integration in the parent toolchain. Obtain the list of integrations from the toolchain API https://cloud.ibm.com/apidocs/toolchain#list-tools.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The only supported source type is \"git\", indicating that the source is a git repository.",
},
"properties": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Properties of the source, which define the URL of the repository and a branch or pattern.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "URL of the repository to which the trigger is listening.",
},
"branch": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Name of a branch from the repo. One of branch or pattern must be specified, but only one or the other.",
},
"pattern": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The pattern of Git branch or tag to which to listen. You can specify a glob pattern such as '!test' or '*master' to match against multiple tags/branches in the repository. The glob pattern used must conform to Bash 4.3 specifications, see bash documentation for more info: https://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching. One of branch or pattern must be specified, but only one or the other.",
},
"blind_connection": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "True if the repository server is not addressable on the public internet. IBM Cloud will not be able to validate the connection details you provide.",
},
"hook_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "ID of the webhook from the repo. Computed upon creation of the trigger.",
},
"tool": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Reference to the repository tool in the parent toolchain.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "ID of the repository tool instance in the parent toolchain.",
},
},
},
},
},
},
},
},
},
},
"events": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Only needed for Git triggers. List of events to which a Git trigger listens. Choose one or more from: 'push', 'pull_request' and 'pull_request_closed'. For SCM repositories that use 'merge request' events, such events map to the equivalent 'pull request' events.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"cron": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Only needed for timer triggers. Cron expression that indicates when this trigger will activate. Maximum frequency is every 5 minutes. The string is based on UNIX crontab syntax: minute, hour, day of month, month, day of week. Example: 0 *_/2 * * * - every 2 hours.",
},
"timezone": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Only used for timer triggers. Specify the timezone used for this timer trigger, which will ensure the cron activates this trigger relative to the specified timezone. If no timezone is specified, the default timezone used is UTC. Valid timezones are those listed in the IANA timezone database, https://www.iana.org/time-zones.",
},
"secret": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Only needed for generic webhook trigger type. Secret used to start generic webhook trigger.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Secret type.",
},
"value": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Secret value, not needed if secret type is `internal_validation`.",
},
"source": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Secret location, not needed if secret type is `internal_validation`.",
},
"key_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Secret name, not needed if type is `internal_validation`.",
},
"algorithm": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Algorithm used for `digest_matches` secret type. Only needed for `digest_matches` secret type.",
},
},
},
},
"webhook_url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Webhook URL that can be used to trigger pipeline runs.",
},
},
}
}
func dataSourceIBMCdTektonPipelineTriggerRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cdTektonPipelineClient, err := meta.(conns.ClientSession).CdTektonPipelineV2()
if err != nil {
return diag.FromErr(err)
}
getTektonPipelineTriggerOptions := &cdtektonpipelinev2.GetTektonPipelineTriggerOptions{}
getTektonPipelineTriggerOptions.SetPipelineID(d.Get("pipeline_id").(string))
getTektonPipelineTriggerOptions.SetTriggerID(d.Get("trigger_id").(string))
TriggerIntf, response, err := cdTektonPipelineClient.GetTektonPipelineTriggerWithContext(context, getTektonPipelineTriggerOptions)
if err != nil {
log.Printf("[DEBUG] GetTektonPipelineTriggerWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetTektonPipelineTriggerWithContext failed %s\n%s", err, response))
}
trigger := TriggerIntf.(*cdtektonpipelinev2.Trigger)
d.SetId(fmt.Sprintf("%s/%s", *getTektonPipelineTriggerOptions.PipelineID, *getTektonPipelineTriggerOptions.TriggerID))
if err = d.Set("type", trigger.Type); err != nil {
return diag.FromErr(fmt.Errorf("Error setting type: %s", err))
}
if err = d.Set("name", trigger.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
if err = d.Set("href", trigger.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("event_listener", trigger.EventListener); err != nil {
return diag.FromErr(fmt.Errorf("Error setting event_listener: %s", err))
}
if trigger.Tags != nil {
if err = d.Set("tags", trigger.Tags); err != nil {
return diag.FromErr(fmt.Errorf("Error setting tags: %s", err))
}
}
properties := []map[string]interface{}{}
if trigger.Properties != nil {
for _, modelItem := range trigger.Properties {
modelMap, err := dataSourceIBMCdTektonPipelineTriggerTriggerPropertyToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
properties = append(properties, modelMap)
}
}
if err = d.Set("properties", properties); err != nil {
return diag.FromErr(fmt.Errorf("Error setting properties %s", err))
}
if trigger.Events != nil {
if err = d.Set("events", trigger.Events); err != nil {
return diag.FromErr(fmt.Errorf("Error setting events: %s", err))
}
}
worker := []map[string]interface{}{}
if trigger.Worker != nil {
modelMap, err := dataSourceIBMCdTektonPipelineTriggerWorkerToMap(trigger.Worker)
if err != nil {
return diag.FromErr(err)
}
worker = append(worker, modelMap)
}
if err = d.Set("worker", worker); err != nil {
return diag.FromErr(fmt.Errorf("Error setting worker %s", err))
}
if err = d.Set("max_concurrent_runs", flex.IntValue(trigger.MaxConcurrentRuns)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting max_concurrent_runs: %s", err))
}
if err = d.Set("enabled", trigger.Enabled); err != nil {
return diag.FromErr(fmt.Errorf("Error setting enabled: %s", err))
}
if err = d.Set("favorite", trigger.Favorite); err != nil {
return diag.FromErr(fmt.Errorf("Error setting favorite: %s", err))
}
source := []map[string]interface{}{}
if trigger.Source != nil {
modelMap, err := dataSourceIBMCdTektonPipelineTriggerTriggerSourceToMap(trigger.Source)
if err != nil {
return diag.FromErr(err)
}
source = append(source, modelMap)
}
if err = d.Set("source", source); err != nil {
return diag.FromErr(fmt.Errorf("Error setting source %s", err))
}
if err = d.Set("cron", trigger.Cron); err != nil {
return diag.FromErr(fmt.Errorf("Error setting cron: %s", err))
}
if err = d.Set("timezone", trigger.Timezone); err != nil {
return diag.FromErr(fmt.Errorf("Error setting timezone: %s", err))
}
secret := []map[string]interface{}{}
if trigger.Secret != nil {
modelMap, err := dataSourceIBMCdTektonPipelineTriggerGenericSecretToMap(trigger.Secret)
if err != nil {
return diag.FromErr(err)
}
secret = append(secret, modelMap)
}
if err = d.Set("secret", secret); err != nil {
return diag.FromErr(fmt.Errorf("Error setting secret %s", err))
}
if err = d.Set("webhook_url", trigger.WebhookURL); err != nil {
return diag.FromErr(fmt.Errorf("Error setting webhook_url: %s", err))
}
return nil
}
func dataSourceIBMCdTektonPipelineTriggerTriggerPropertyToMap(model *cdtektonpipelinev2.TriggerProperty) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["name"] = model.Name
if model.Value != nil {
modelMap["value"] = model.Value
}
if model.Href != nil {
modelMap["href"] = model.Href
}
if model.Enum != nil {
modelMap["enum"] = model.Enum
}
modelMap["type"] = model.Type
if model.Path != nil {
modelMap["path"] = model.Path
}
return modelMap, nil
}
func dataSourceIBMCdTektonPipelineTriggerWorkerToMap(model *cdtektonpipelinev2.Worker) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Name != nil {
modelMap["name"] = model.Name
}
if model.Type != nil {
modelMap["type"] = model.Type
}
modelMap["id"] = model.ID
return modelMap, nil
}
func dataSourceIBMCdTektonPipelineTriggerTriggerSourceToMap(model *cdtektonpipelinev2.TriggerSource) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["type"] = model.Type
propertiesMap, err := dataSourceIBMCdTektonPipelineTriggerTriggerSourcePropertiesToMap(model.Properties)
if err != nil {
return modelMap, err
}
modelMap["properties"] = []map[string]interface{}{propertiesMap}
return modelMap, nil
}
func dataSourceIBMCdTektonPipelineTriggerTriggerSourcePropertiesToMap(model *cdtektonpipelinev2.TriggerSourceProperties) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["url"] = model.URL
if model.Branch != nil {
modelMap["branch"] = model.Branch
}
if model.Pattern != nil {
modelMap["pattern"] = model.Pattern
}
modelMap["blind_connection"] = model.BlindConnection
if model.HookID != nil {
modelMap["hook_id"] = model.HookID
}
toolMap, err := dataSourceIBMCdTektonPipelineTriggerToolToMap(model.Tool)
if err != nil {
return modelMap, err
}
modelMap["tool"] = []map[string]interface{}{toolMap}
return modelMap, nil
}
func dataSourceIBMCdTektonPipelineTriggerToolToMap(model *cdtektonpipelinev2.Tool) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["id"] = model.ID
return modelMap, nil
}
func dataSourceIBMCdTektonPipelineTriggerGenericSecretToMap(model *cdtektonpipelinev2.GenericSecret) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Type != nil {
modelMap["type"] = model.Type
}
if model.Value != nil {
modelMap["value"] = model.Value
}
if model.Source != nil {
modelMap["source"] = model.Source
}
if model.KeyName != nil {
modelMap["key_name"] = model.KeyName
}
if model.Algorithm != nil {
modelMap["algorithm"] = model.Algorithm
}
return modelMap, nil
}