-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_backup_policy_plans.go
316 lines (289 loc) · 10.6 KB
/
data_source_ibm_is_backup_policy_plans.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
// 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 DataSourceIBMIsBackupPolicyPlans() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsBackupPolicyPlansRead,
Schema: map[string]*schema.Schema{
"backup_policy_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The backup policy identifier.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The unique user-defined name for this backup policy plan.",
},
"plans": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of backup policy plans.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"active": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the plan is active.",
},
"attach_user_tags": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The user tags to attach to backups (snapshots) created by this plan.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"copy_user_tags": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether to copy the source's user tags to the created backups (snapshots).",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the backup policy plan was created.",
},
"cron_spec": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The cron specification for the backup schedule.",
},
"deletion_trigger": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_after": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The maximum number of days to keep each backup after creation.",
},
"delete_over_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The maximum number of recent backups to keep. If absent, there is no maximum.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this backup policy plan.",
},
"clone_policy": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_snapshots": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The maximum number of recent snapshots (per source) that will keep clones.",
},
"zones": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "The zone this backup policy plan will create snapshot clones in.",
},
},
},
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this backup policy plan.",
},
"lifecycle_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of this backup policy plan.",
},
"remote_region_policy": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Backup policy plan cross region rule.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_over_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The maximum number of recent remote copies to keep in this region.",
},
"encryption_key": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN of the [Key Protect Root Key](https://cloud.ibm.com/docs/key-protect?topic=key-protect-getting-started-tutorial) or [Hyper Protect Crypto Services Root Key](https://cloud.ibm.com/docs/hs-crypto?topic=hs-crypto-get-started) for this resource.",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this region.",
},
},
},
},
"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.",
},
},
},
},
},
}
}
func dataSourceIBMIsBackupPolicyPlansRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
listBackupPolicyPlansOptions := &vpcv1.ListBackupPolicyPlansOptions{}
listBackupPolicyPlansOptions.SetBackupPolicyID(d.Get("backup_policy_id").(string))
backupPolicyPlanCollection, response, err := vpcClient.ListBackupPolicyPlansWithContext(context, listBackupPolicyPlansOptions)
if err != nil {
log.Printf("[DEBUG] ListBackupPolicyPlansWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] ListBackupPolicyPlansWithContext failed %s\n%s", err, response))
}
// Use the provided filter argument and construct a new list with only the requested resource(s)
var matchPlans []vpcv1.BackupPolicyPlan
var name string
var suppliedFilter bool
if v, ok := d.GetOk("name"); ok {
name = v.(string)
suppliedFilter = true
for _, data := range backupPolicyPlanCollection.Plans {
if *data.Name == name {
matchPlans = append(matchPlans, data)
}
}
backupPolicyPlanCollection.Plans = matchPlans
}
if suppliedFilter {
if len(backupPolicyPlanCollection.Plans) == 0 {
return diag.FromErr(fmt.Errorf("[ERROR] no plans found with name %s", name))
}
d.SetId(name)
} else {
d.SetId(dataSourceIBMIsBackupPolicyPlansID(d))
}
if backupPolicyPlanCollection.Plans != nil {
err = d.Set("plans", dataSourceBackupPolicyPlanCollectionFlattenPlans(backupPolicyPlanCollection.Plans))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting plans %s", err))
}
}
return nil
}
// dataSourceIBMIsBackupPolicyPlansID returns a reasonable ID for the list.
func dataSourceIBMIsBackupPolicyPlansID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceBackupPolicyPlanCollectionFlattenPlans(result []vpcv1.BackupPolicyPlan) (plans []map[string]interface{}) {
for _, plansItem := range result {
plans = append(plans, dataSourceBackupPolicyPlanCollectionPlansToMap(plansItem))
}
return plans
}
func dataSourceBackupPolicyPlanCollectionPlansToMap(plansItem vpcv1.BackupPolicyPlan) (plansMap map[string]interface{}) {
plansMap = map[string]interface{}{}
if plansItem.Active != nil {
plansMap["active"] = plansItem.Active
}
if plansItem.AttachUserTags != nil {
plansMap["attach_user_tags"] = plansItem.AttachUserTags
}
if plansItem.CopyUserTags != nil {
plansMap["copy_user_tags"] = plansItem.CopyUserTags
}
if plansItem.CreatedAt != nil {
plansMap["created_at"] = plansItem.CreatedAt.String()
}
if plansItem.CronSpec != nil {
plansMap["cron_spec"] = plansItem.CronSpec
}
if plansItem.DeletionTrigger != nil {
deletionTriggerList := []map[string]interface{}{}
deletionTriggerMap := dataSourceBackupPolicyPlanCollectionPlansDeletionTriggerToMap(*plansItem.DeletionTrigger)
deletionTriggerList = append(deletionTriggerList, deletionTriggerMap)
plansMap["deletion_trigger"] = deletionTriggerList
}
if plansItem.Href != nil {
plansMap["href"] = plansItem.Href
}
if plansItem.ID != nil {
plansMap["id"] = plansItem.ID
}
if plansItem.LifecycleState != nil {
plansMap["lifecycle_state"] = plansItem.LifecycleState
}
if plansItem.Name != nil {
plansMap["name"] = plansItem.Name
}
remoteRegionPolicies := []map[string]interface{}{}
if plansItem.RemoteRegionPolicies != nil {
for _, remoteCopyPolicy := range plansItem.RemoteRegionPolicies {
remoteRegionPoliciesMap, _ := dataSourceIBMIsVPCBackupPolicyPlanRemoteCopyPolicyItemToMap(&remoteCopyPolicy)
remoteRegionPolicies = append(remoteRegionPolicies, remoteRegionPoliciesMap)
}
plansMap["remote_region_policy"] = remoteRegionPolicies
}
if plansItem.ResourceType != nil {
plansMap["resource_type"] = plansItem.ResourceType
}
if plansItem.ClonePolicy != nil {
backupPolicyPlanClonePolicyMap := []map[string]interface{}{}
finalList := map[string]interface{}{}
if plansItem.ClonePolicy.MaxSnapshots != nil {
finalList["max_snapshots"] = flex.IntValue(plansItem.ClonePolicy.MaxSnapshots)
}
if plansItem.ClonePolicy.Zones != nil && len(plansItem.ClonePolicy.Zones) != 0 {
zoneList := []string{}
for i := 0; i < len(plansItem.ClonePolicy.Zones); i++ {
zoneList = append(zoneList, string(*(plansItem.ClonePolicy.Zones[i].Name)))
}
finalList["zones"] = flex.NewStringSet(schema.HashString, zoneList)
}
backupPolicyPlanClonePolicyMap = append(backupPolicyPlanClonePolicyMap, finalList)
plansMap["clone_policy"] = backupPolicyPlanClonePolicyMap
}
return plansMap
}
func dataSourceBackupPolicyPlanCollectionClonePolicyZonesToMap(zonesItem vpcv1.ZoneReference) (zonesMap map[string]interface{}) {
zonesMap = map[string]interface{}{}
if zonesItem.Href != nil {
zonesMap["href"] = zonesItem.Href
}
if zonesItem.Name != nil {
zonesMap["name"] = zonesItem.Name
}
return zonesMap
}
func dataSourceBackupPolicyPlanCollectionPlansDeletionTriggerToMap(deletionTriggerItem vpcv1.BackupPolicyPlanDeletionTrigger) (deletionTriggerMap map[string]interface{}) {
deletionTriggerMap = map[string]interface{}{}
if deletionTriggerItem.DeleteAfter != nil {
deletionTriggerMap["delete_after"] = deletionTriggerItem.DeleteAfter
}
if deletionTriggerItem.DeleteOverCount != nil {
deletionTriggerMap["delete_over_count"] = deletionTriggerItem.DeleteOverCount
}
return deletionTriggerMap
}