-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_backup_policies.go
473 lines (435 loc) · 15.7 KB
/
data_source_ibm_is_backup_policies.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
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"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 DataSourceIBMIsBackupPolicies() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsBackupPoliciesRead,
Schema: map[string]*schema.Schema{
"resource_group": {
Type: schema.TypeString,
Description: "Filters the collection to resources in the resource group with the specified identifier",
Optional: true,
},
"name": {
Type: schema.TypeString,
Description: "Filters the collection to resources with the exact specified name",
Optional: true,
},
"tag": {
Type: schema.TypeString,
Description: "Filters the collection to resources with the exact tag value",
Optional: true,
},
"backup_policies": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of backup policies.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the backup policy was created.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this backup policy.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this backup policy.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this backup policy.",
},
"lifecycle_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the backup policy.",
},
"last_job_completed_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the most recent job for this backup policy completed.",
},
"match_resource_types": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "A resource type this backup policy applies to. Resources that have both a matching type and a matching user tag will be subject to the backup policy.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"match_resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type this backup policy will apply to. Resources that have both a matching type and a matching user tag will be subject to the backup policy.",
},
"included_content": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The included content for backups created using this policy",
Elem: &schema.Schema{Type: schema.TypeString},
},
"match_user_tags": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The user tags this backup policy applies to. Resources that have both a matching user tag and a matching type will be subject to the backup policy.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this backup policy.",
},
"plans": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The plans for the backup policy.",
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 type of resource referenced.",
},
},
},
},
"resource_group": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The resource group for this backup policy.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this resource group.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this resource group.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this resource group.",
},
},
},
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
"health_reasons": {
Type: schema.TypeList,
Computed: true,
Description: "The reasons for the current health_state (if any).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the reason for this health state.",
},
"message": {
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the reason for this health state.",
},
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about the reason for this health state.",
},
},
},
},
"health_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The health of this resource",
},
"scope": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The scope for this backup policy.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this enterprise.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this enterprise or account.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMIsBackupPoliciesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
start := ""
matchBackupPolicies := []vpcv1.BackupPolicy{}
var resourceGroup string
if v, ok := d.GetOk("resource_group"); ok {
resourceGroup = v.(string)
}
var name string
if v, ok := d.GetOk("name"); ok {
name = v.(string)
}
var tag string
if v, ok := d.GetOk("tag"); ok {
tag = v.(string)
}
for {
listBackupPoliciesOptions := &vpcv1.ListBackupPoliciesOptions{}
if start != "" {
listBackupPoliciesOptions.Start = &start
}
if resourceGroup != "" {
listBackupPoliciesOptions.SetResourceGroupID(resourceGroup)
}
if name != "" {
listBackupPoliciesOptions.SetName(name)
}
if tag != "" {
listBackupPoliciesOptions.SetTag(tag)
}
backupPolicyCollection, response, err := sess.ListBackupPoliciesWithContext(context, listBackupPoliciesOptions)
if err != nil {
log.Printf("[DEBUG] ListBackupPoliciesWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] ListBackupPoliciesWithContext failed %s\n%s", err, response))
}
if backupPolicyCollection != nil && *backupPolicyCollection.TotalCount == int64(0) {
break
}
start = flex.GetNext(backupPolicyCollection.Next)
for _, backupPolicyInfo := range backupPolicyCollection.BackupPolicies {
backupPolicies := backupPolicyInfo.(*vpcv1.BackupPolicy)
matchBackupPolicies = append(matchBackupPolicies, *backupPolicies)
}
if start == "" {
break
}
}
d.SetId(dataSourceIBMIsBackupPoliciesID(d))
if matchBackupPolicies != nil {
err = d.Set("backup_policies", dataSourceBackupPolicyCollectionFlattenBackupPolicies(matchBackupPolicies))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting backup_policies %s", err))
}
}
return nil
}
// dataSourceIBMIsBackupPoliciesID returns a reasonable ID for the list.
func dataSourceIBMIsBackupPoliciesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceBackupPolicyCollectionFlattenBackupPolicies(result []vpcv1.BackupPolicy) (backupPolicies []map[string]interface{}) {
for _, backupPoliciesItem := range result {
backupPolicies = append(backupPolicies, dataSourceBackupPolicyCollectionBackupPoliciesToMap(backupPoliciesItem))
}
return backupPolicies
}
func dataSourceBackupPolicyCollectionBackupPoliciesToMap(backupPoliciesItem vpcv1.BackupPolicy) (backupPoliciesMap map[string]interface{}) {
backupPoliciesMap = map[string]interface{}{}
if backupPoliciesItem.CreatedAt != nil {
backupPoliciesMap["created_at"] = backupPoliciesItem.CreatedAt.String()
}
if backupPoliciesItem.CRN != nil {
backupPoliciesMap["crn"] = backupPoliciesItem.CRN
}
if backupPoliciesItem.Href != nil {
backupPoliciesMap["href"] = backupPoliciesItem.Href
}
if backupPoliciesItem.ID != nil {
backupPoliciesMap["id"] = backupPoliciesItem.ID
}
if backupPoliciesItem.LifecycleState != nil {
backupPoliciesMap["lifecycle_state"] = backupPoliciesItem.LifecycleState
}
if backupPoliciesItem.LastJobCompletedAt != nil {
backupPoliciesMap["last_job_completed_at"] = flex.DateTimeToString(backupPoliciesItem.LastJobCompletedAt)
}
if backupPoliciesItem.MatchResourceType != nil {
backupPoliciesMap["match_resource_types"] = []string{*backupPoliciesItem.MatchResourceType}
backupPoliciesMap["match_resource_type"] = *backupPoliciesItem.MatchResourceType
}
if backupPoliciesItem.MatchUserTags != nil {
backupPoliciesMap["match_user_tags"] = backupPoliciesItem.MatchUserTags
}
if backupPoliciesItem.IncludedContent != nil {
backupPoliciesMap["included_content"] = backupPoliciesItem.IncludedContent
}
if backupPoliciesItem.Name != nil {
backupPoliciesMap["name"] = backupPoliciesItem.Name
}
if backupPoliciesItem.Plans != nil {
plansList := []map[string]interface{}{}
for _, plansItem := range backupPoliciesItem.Plans {
plansList = append(plansList, dataSourceBackupPolicyCollectionBackupPoliciesPlansToMap(plansItem))
}
backupPoliciesMap["plans"] = plansList
}
if backupPoliciesItem.ResourceGroup != nil {
resourceGroupList := []map[string]interface{}{}
resourceGroupMap := dataSourceBackupPolicyCollectionBackupPoliciesResourceGroupToMap(*backupPoliciesItem.ResourceGroup)
resourceGroupList = append(resourceGroupList, resourceGroupMap)
backupPoliciesMap["resource_group"] = resourceGroupList
}
if backupPoliciesItem.ResourceType != nil {
backupPoliciesMap["resource_type"] = backupPoliciesItem.ResourceType
}
if backupPoliciesItem.HealthReasons != nil {
healthReasonsList := []map[string]interface{}{}
for _, healthReasonsItem := range backupPoliciesItem.HealthReasons {
healthReasonsList = append(healthReasonsList, dataSourceBackupPolicyCollectionPoliciesHealthReasonsToMap(healthReasonsItem))
}
backupPoliciesMap["health_reasons"] = healthReasonsList
}
if backupPoliciesItem.HealthState != nil {
backupPoliciesMap["health_state"] = backupPoliciesItem.HealthState
}
if backupPoliciesItem.Scope != nil {
scopeList := []map[string]interface{}{}
scopeMap := dataSourceBackupPolicyCollectionBackupPoliciesScopeToMap(*backupPoliciesItem.Scope.(*vpcv1.BackupPolicyScope))
scopeList = append(scopeList, scopeMap)
backupPoliciesMap["scope"] = scopeList
}
return backupPoliciesMap
}
func dataSourceBackupPolicyCollectionPoliciesHealthReasonsToMap(statusReasonsItem vpcv1.BackupPolicyHealthReason) (healthReasonsMap map[string]interface{}) {
healthReasonsMap = map[string]interface{}{}
if statusReasonsItem.Code != nil {
healthReasonsMap["code"] = statusReasonsItem.Code
}
if statusReasonsItem.Message != nil {
healthReasonsMap["message"] = statusReasonsItem.Message
}
if statusReasonsItem.MoreInfo != nil {
healthReasonsMap["more_info"] = statusReasonsItem.MoreInfo
}
return healthReasonsMap
}
func dataSourceBackupPolicyCollectionBackupPoliciesScopeToMap(scopeItem vpcv1.BackupPolicyScope) (scopeMap map[string]interface{}) {
scopeMap = map[string]interface{}{}
if scopeItem.CRN != nil {
scopeMap["crn"] = scopeItem.CRN
}
if scopeItem.ID != nil {
scopeMap["id"] = scopeItem.ID
}
if scopeItem.ResourceType != nil {
scopeMap["resource_type"] = scopeItem.ResourceType
}
return scopeMap
}
func dataSourceBackupPolicyCollectionBackupPoliciesPlansToMap(plansItem vpcv1.BackupPolicyPlanReference) (plansMap map[string]interface{}) {
plansMap = map[string]interface{}{}
if plansItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceBackupPolicyCollectionPlansDeletedToMap(*plansItem.Deleted)
deletedList = append(deletedList, deletedMap)
plansMap["deleted"] = deletedList
}
if plansItem.Href != nil {
plansMap["href"] = plansItem.Href
}
if plansItem.ID != nil {
plansMap["id"] = plansItem.ID
}
if plansItem.Name != nil {
plansMap["name"] = plansItem.Name
}
if plansItem.ResourceType != nil {
plansMap["resource_type"] = plansItem.ResourceType
}
return plansMap
}
func dataSourceBackupPolicyCollectionPlansDeletedToMap(deletedItem vpcv1.BackupPolicyPlanReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceBackupPolicyCollectionBackupPoliciesResourceGroupToMap(resourceGroupItem vpcv1.ResourceGroupReference) (resourceGroupMap map[string]interface{}) {
resourceGroupMap = map[string]interface{}{}
if resourceGroupItem.Href != nil {
resourceGroupMap["href"] = resourceGroupItem.Href
}
if resourceGroupItem.ID != nil {
resourceGroupMap["id"] = resourceGroupItem.ID
}
if resourceGroupItem.Name != nil {
resourceGroupMap["name"] = resourceGroupItem.Name
}
return resourceGroupMap
}