-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_backup_policy_jobs.go
574 lines (523 loc) · 20.8 KB
/
data_source_ibm_is_backup_policy_jobs.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMIsBackupPolicyJobs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsBackupPolicyJobsRead,
Schema: map[string]*schema.Schema{
"backup_policy_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The backup policy identifier.",
},
"source_id": {
Type: schema.TypeString,
Description: "Filters the collection to backup policy jobs with a source with the specified identifier",
Optional: true,
},
"target_snapshots_id": {
Type: schema.TypeSet,
ConflictsWith: []string{"target_snapshots_crn"},
Description: "Filters the collection to resources with the target snapshot with the specified identifier",
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Optional: true,
},
"target_snapshots_crn": {
Type: schema.TypeSet,
ConflictsWith: []string{"target_snapshots_id"},
Description: "Filters the collection to backup policy jobs with the target snapshot with the specified CRN",
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Optional: true,
},
"backup_policy_plan_id": {
Type: schema.TypeString,
Optional: true,
Description: "Filters the collection to backup policy jobs with the backup plan with the specified identifier.",
},
"status": {
Type: schema.TypeString,
Description: "Filters the collection to backup policy jobs with the specified status",
Optional: true,
},
"jobs": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of backup policy jobs.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auto_delete": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this backup policy job will be automatically deleted after it completes. At present, this is always `true`, but may be modifiable in the future.",
},
"auto_delete_after": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "If `auto_delete` is `true`, the days after completion that this backup policy job will be deleted. This value may be modifiable in the future.",
},
"backup_policy_plan": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The backup policy plan operated this backup policy job (may be[deleted](https://cloud.ibm.com/apidocs/vpc#deleted-resources)).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this backup policy plan.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this backup policy plan.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this backup policy plan.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
"completed_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the backup policy job was completed.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the backup policy job was created.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this backup policy job.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this backup policy job.",
},
"job_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The type of backup policy job.The enumerated values for this property will expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the backup policy job on which the unexpected property value was encountered.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
"source_volume": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The source volume this backup was created from (may be[deleted](https://cloud.ibm.com/apidocs/vpc#deleted-resources)).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this volume.",
},
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this volume.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this volume.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this volume.",
},
},
},
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The status of the backup policy job.The enumerated values for this property will expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the backup policy job on which the unexpected property value was encountered.",
},
"status_reasons": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The reasons for the current status (if any).The enumerated reason code values for this property will expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the resource on which the unexpected reason code was encountered.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the status reason:- `internal_error`: Internal error (contact IBM support)- `snapshot_pending`: Cannot delete backup (snapshot) in the `pending` lifecycle state- `snapshot_volume_limit`: The snapshot limit for the source volume has been reached- `source_volume_busy`: The source volume has `busy` set (after multiple retries).",
},
"message": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the status reason.",
},
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about this status reason.",
},
},
},
},
"target_snapshot": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The snapshot operated on by this backup policy job (may be[deleted](https://cloud.ibm.com/apidocs/vpc#deleted-resources)).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this snapshot.",
},
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this snapshot.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this snapshot.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this snapshot.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMIsBackupPolicyJobsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
listBackupPolicyJobsOptions := &vpcv1.ListBackupPolicyJobsOptions{}
listBackupPolicyJobsOptions.SetBackupPolicyID(d.Get("backup_policy_id").(string))
if sourceId, ok := d.GetOk("source_id"); ok {
listBackupPolicyJobsOptions.SetSourceID(sourceId.(string))
}
if targetSnapshotsId, ok := d.GetOk("target_snapshots_id"); ok {
targetSnapshotsIds := targetSnapshotsId.(*schema.Set)
if targetSnapshotsIds.Len() != 0 {
targetSnapshotsIdsArray := make([]string, targetSnapshotsIds.Len())
for i, key := range targetSnapshotsIds.List() {
keystr := key.(string)
targetSnapshotsIdsArray[i] = keystr
}
listBackupPolicyJobsOptions.SetTargetSnapshotsID(strings.Join(targetSnapshotsIdsArray, ","))
}
}
if targetSnapshotsCrn, ok := d.GetOk("target_snapshots_crn"); ok {
targetSnapshotsCrns := targetSnapshotsCrn.(*schema.Set)
if targetSnapshotsCrns.Len() != 0 {
targetSnapshotsCrnsArray := make([]string, targetSnapshotsCrns.Len())
for i, key := range targetSnapshotsCrns.List() {
keystr := key.(string)
targetSnapshotsCrnsArray[i] = keystr
}
listBackupPolicyJobsOptions.SetTargetSnapshotsCRN(strings.Join(targetSnapshotsCrnsArray, ","))
}
}
if status, ok := d.GetOk("status"); ok {
listBackupPolicyJobsOptions.SetStatus(status.(string))
}
if backupPolicyPlanId, ok := d.GetOk("backup_policy_plan_id"); ok {
listBackupPolicyJobsOptions.SetBackupPolicyPlanID(backupPolicyPlanId.(string))
}
// Support for pagination
start := ""
allrecs := []vpcv1.BackupPolicyJob{}
for {
if start != "" {
listBackupPolicyJobsOptions.Start = &start
}
backupPolicyJobCollection, response, err := vpcClient.ListBackupPolicyJobsWithContext(context, listBackupPolicyJobsOptions)
if err != nil {
log.Printf("[DEBUG] ListBackupPolicyJobsWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListBackupPolicyJobsWithContext failed %s\n%s", err, response))
}
if backupPolicyJobCollection != nil && *backupPolicyJobCollection.TotalCount == int64(0) {
break
}
start = flex.GetNext(backupPolicyJobCollection.Next)
allrecs = append(allrecs, backupPolicyJobCollection.Jobs...)
if start == "" {
break
}
}
d.SetId(dataSourceIBMIsBackupPolicyJobsID(d))
if allrecs != nil {
err = d.Set("jobs", dataSourceBackupPolicyJobCollectionFlattenJobs(allrecs))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting jobs %s", err))
}
}
return nil
}
// dataSourceIBMIsBackupPolicyJobsID returns a reasonable ID for the list.
func dataSourceIBMIsBackupPolicyJobsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceBackupPolicyJobCollectionFlattenJobs(result []vpcv1.BackupPolicyJob) (jobs []map[string]interface{}) {
for _, jobsItem := range result {
jobs = append(jobs, dataSourceBackupPolicyJobCollectionJobsToMap(jobsItem))
}
return jobs
}
func dataSourceBackupPolicyJobCollectionJobsToMap(jobsItem vpcv1.BackupPolicyJob) (jobsMap map[string]interface{}) {
// log.Println("Hi I am inside dataSourceBackupPolicyJobCollectionJobsToMap")
jobsMap = map[string]interface{}{}
if jobsItem.AutoDelete != nil {
jobsMap["auto_delete"] = jobsItem.AutoDelete
}
if jobsItem.AutoDeleteAfter != nil {
jobsMap["auto_delete_after"] = jobsItem.AutoDeleteAfter
}
if jobsItem.BackupPolicyPlan != nil {
backupPolicyPlanList := []map[string]interface{}{}
backupPolicyPlanMap := dataSourceBackupPolicyJobCollectionJobsBackupPolicyPlanToMap(*jobsItem.BackupPolicyPlan)
backupPolicyPlanList = append(backupPolicyPlanList, backupPolicyPlanMap)
jobsMap["backup_policy_plan"] = backupPolicyPlanList
}
if jobsItem.CompletedAt != nil {
jobsMap["completed_at"] = jobsItem.CompletedAt.String()
}
if jobsItem.CreatedAt != nil {
jobsMap["created_at"] = jobsItem.CreatedAt.String()
}
if jobsItem.Href != nil {
jobsMap["href"] = jobsItem.Href
}
if jobsItem.ID != nil {
jobsMap["id"] = jobsItem.ID
}
if jobsItem.JobType != nil {
jobsMap["job_type"] = jobsItem.JobType
}
if jobsItem.ResourceType != nil {
jobsMap["resource_type"] = jobsItem.ResourceType
}
log.Println("jobsItem.Source")
log.Println(jobsItem.Source)
if jobsItem.Source != nil {
sourceVolumeList := []map[string]interface{}{}
jobSource := jobsItem.Source.(*vpcv1.BackupPolicyJobSource)
sourceVolumeMap := dataSourceBackupPolicyJobCollectionJobsSourceVolumeToMap(*jobSource)
sourceVolumeList = append(sourceVolumeList, sourceVolumeMap)
jobsMap["source_volume"] = sourceVolumeList
}
if jobsItem.Status != nil {
jobsMap["status"] = jobsItem.Status
}
if jobsItem.StatusReasons != nil {
statusReasonsList := []map[string]interface{}{}
for _, statusReasonsItem := range jobsItem.StatusReasons {
statusReasonsList = append(statusReasonsList, dataSourceBackupPolicyJobCollectionJobsStatusReasonsToMap(statusReasonsItem))
}
jobsMap["status_reasons"] = statusReasonsList
}
log.Println("jobsItem.TargetSnapshot")
log.Println(jobsItem.TargetSnapshots)
if jobsItem.TargetSnapshots != nil {
targetSnapshotList := []map[string]interface{}{}
for _, targetSnapshotsItem := range jobsItem.TargetSnapshots {
targetSnapshotMap := dataSourceBackupPolicyJobCollectionJobsTargetSnapshotToMap(targetSnapshotsItem)
targetSnapshotList = append(targetSnapshotList, targetSnapshotMap)
}
jobsMap["target_snapshot"] = targetSnapshotList
}
// log.Println("jobsItem")
// log.Println(jobsItem)
return jobsMap
}
func dataSourceBackupPolicyJobCollectionJobsBackupPolicyPlanToMap(backupPolicyPlanItem vpcv1.BackupPolicyPlanReference) (backupPolicyPlanMap map[string]interface{}) {
backupPolicyPlanMap = map[string]interface{}{}
if backupPolicyPlanItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceBackupPolicyJobCollectionBackupPolicyPlanDeletedToMap(*backupPolicyPlanItem.Deleted)
deletedList = append(deletedList, deletedMap)
backupPolicyPlanMap["deleted"] = deletedList
}
if backupPolicyPlanItem.Href != nil {
backupPolicyPlanMap["href"] = backupPolicyPlanItem.Href
}
if backupPolicyPlanItem.ID != nil {
backupPolicyPlanMap["id"] = backupPolicyPlanItem.ID
}
if backupPolicyPlanItem.Name != nil {
backupPolicyPlanMap["name"] = backupPolicyPlanItem.Name
}
if backupPolicyPlanItem.ResourceType != nil {
backupPolicyPlanMap["resource_type"] = backupPolicyPlanItem.ResourceType
}
return backupPolicyPlanMap
}
func dataSourceBackupPolicyJobCollectionBackupPolicyPlanDeletedToMap(deletedItem vpcv1.BackupPolicyPlanReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceBackupPolicyJobCollectionJobsSourceVolumeToMap(sourceVolumeItem vpcv1.BackupPolicyJobSource) (sourceVolumeMap map[string]interface{}) {
sourceVolumeMap = map[string]interface{}{}
if sourceVolumeItem.CRN != nil {
sourceVolumeMap["crn"] = sourceVolumeItem.CRN
}
if sourceVolumeItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceBackupPolicyJobCollectionSourceVolumeDeletedToMap(*sourceVolumeItem.Deleted)
deletedList = append(deletedList, deletedMap)
sourceVolumeMap["deleted"] = deletedList
}
if sourceVolumeItem.Href != nil {
sourceVolumeMap["href"] = sourceVolumeItem.Href
}
if sourceVolumeItem.ID != nil {
sourceVolumeMap["id"] = sourceVolumeItem.ID
}
if sourceVolumeItem.Name != nil {
sourceVolumeMap["name"] = sourceVolumeItem.Name
}
return sourceVolumeMap
}
func dataSourceBackupPolicyJobCollectionSourceVolumeDeletedToMap(deletedItem vpcv1.VolumeReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceBackupPolicyJobCollectionJobsStatusReasonsToMap(statusReasonsItem vpcv1.BackupPolicyJobStatusReason) (statusReasonsMap map[string]interface{}) {
statusReasonsMap = map[string]interface{}{}
if statusReasonsItem.Code != nil {
statusReasonsMap["code"] = statusReasonsItem.Code
}
if statusReasonsItem.Message != nil {
statusReasonsMap["message"] = statusReasonsItem.Message
}
if statusReasonsItem.MoreInfo != nil {
statusReasonsMap["more_info"] = statusReasonsItem.MoreInfo
}
return statusReasonsMap
}
func dataSourceBackupPolicyJobCollectionJobsTargetSnapshotToMap(targetSnapshotItem vpcv1.SnapshotReference) (targetSnapshotMap map[string]interface{}) {
targetSnapshotMap = map[string]interface{}{}
if targetSnapshotItem.CRN != nil {
targetSnapshotMap["crn"] = targetSnapshotItem.CRN
}
if targetSnapshotItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceBackupPolicyJobCollectionTargetSnapshotDeletedToMap(*targetSnapshotItem.Deleted)
deletedList = append(deletedList, deletedMap)
targetSnapshotMap["deleted"] = deletedList
}
if targetSnapshotItem.Href != nil {
targetSnapshotMap["href"] = targetSnapshotItem.Href
}
if targetSnapshotItem.ID != nil {
targetSnapshotMap["id"] = targetSnapshotItem.ID
}
if targetSnapshotItem.Name != nil {
targetSnapshotMap["name"] = targetSnapshotItem.Name
}
if targetSnapshotItem.ResourceType != nil {
targetSnapshotMap["resource_type"] = targetSnapshotItem.ResourceType
}
return targetSnapshotMap
}
func dataSourceBackupPolicyJobCollectionTargetSnapshotDeletedToMap(deletedItem vpcv1.SnapshotReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceBackupPolicyJobCollectionNextToMap(nextItem vpcv1.BackupPolicyJobCollectionNext) (nextMap map[string]interface{}) {
nextMap = map[string]interface{}{}
if nextItem.Href != nil {
nextMap["href"] = nextItem.Href
}
return nextMap
}