-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_snapshot_consistency_groups.go
369 lines (348 loc) · 14 KB
/
data_source_ibm_is_snapshot_consistency_groups.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
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"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/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMIsSnapshotConsistencyGroups() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsSnapshotConsistencyGroupsRead,
Schema: map[string]*schema.Schema{
"resource_group": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Filters the collection to resources with a `resource_group.id` property matching the specified identifier.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Filters the collection to resources with a `name` property matching the exact specified name.",
},
"backup_policy_plan": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Filters the collection to backup policy jobs with a `backup_policy_plan.id` property matching the specified identifier.",
},
"snapshot_consistency_groups": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of snapshot consistency groups.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"backup_policy_plan": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, the backup policy plan which created this snapshot consistency group.",
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 name for this backup policy plan. The name is unique across all plans in the backup policy.",
},
"remote": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource is remote to this region,and identifies the native region.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this region.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this region.",
},
},
},
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this snapshot consistency group was created.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN of this snapshot consistency group.",
},
"delete_snapshots_on_delete": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether deleting the snapshot consistency group will also delete the snapshots in the group.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this snapshot consistency group.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this snapshot consistency group.",
},
"lifecycle_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of this snapshot consistency group.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name for this snapshot consistency group. The name is unique across all snapshot consistency groups in the region.",
},
"resource_group": {
Type: schema.TypeString,
Computed: true,
Description: "Resource group info",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
"service_tags": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The [service tags](https://cloud.ibm.com/apidocs/tagging#types-of-tags)[`is.instance:` prefix](https://cloud.ibm.com/docs/vpc?topic=vpc-snapshots-vpc-faqs) associated with this snapshot consistency group.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"snapshots": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The member snapshots that are data-consistent with respect to captured time. (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 of 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 name for this snapshot. The name is unique across all snapshots in the region.",
},
"remote": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource is remote to this region,and identifies the native region.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this region.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this region.",
},
},
},
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
"tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "User Tags for the snapshot consistency group",
},
"access_tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access tags",
},
},
},
},
},
}
}
func dataSourceIBMIsSnapshotConsistencyGroupsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
start := ""
allrecs := []vpcv1.SnapshotConsistencyGroup{}
for {
listSnapshotConsistencyGroupsOptions := &vpcv1.ListSnapshotConsistencyGroupsOptions{}
if start != "" {
listSnapshotConsistencyGroupsOptions.Start = &start
}
if rgFilterOk, ok := d.GetOk("resource_group"); ok {
rgFilter := rgFilterOk.(string)
listSnapshotConsistencyGroupsOptions.ResourceGroupID = &rgFilter
}
if nameFilterOk, ok := d.GetOk("name"); ok {
nameFilter := nameFilterOk.(string)
listSnapshotConsistencyGroupsOptions.Name = &nameFilter
}
if backupPolicyPlanIdFilterOk, ok := d.GetOk("backup_policy_plan"); ok {
backupPolicyPlanIdFilter := backupPolicyPlanIdFilterOk.(string)
listSnapshotConsistencyGroupsOptions.BackupPolicyPlanID = &backupPolicyPlanIdFilter
}
snapshotConsistencyGroup, response, err := vpcClient.ListSnapshotConsistencyGroups(listSnapshotConsistencyGroupsOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching snapshots %s\n%s", err, response))
}
start = flex.GetNext(snapshotConsistencyGroup.Next)
allrecs = append(allrecs, snapshotConsistencyGroup.SnapshotConsistencyGroups...)
if start == "" {
break
}
}
snapshotConsistencyGroupsInfo := make([]map[string]interface{}, 0)
for _, snapshotConsistencyGroup := range allrecs {
l := map[string]interface{}{
"id": *snapshotConsistencyGroup.ID,
"name": *snapshotConsistencyGroup.Name,
"href": *snapshotConsistencyGroup.Href,
"crn": *snapshotConsistencyGroup.CRN,
"delete_snapshots_on_delete": *snapshotConsistencyGroup.DeleteSnapshotsOnDelete,
"lifecycle_state": *snapshotConsistencyGroup.LifecycleState,
"resource_type": *snapshotConsistencyGroup.ResourceType,
"created_at": (*snapshotConsistencyGroup.CreatedAt).String(),
}
//backup policy plan
backupPolicyPlanList := []map[string]interface{}{}
if snapshotConsistencyGroup.BackupPolicyPlan != nil {
backupPolicyPlan := map[string]interface{}{}
if snapshotConsistencyGroup.BackupPolicyPlan.Deleted != nil {
snapshotConsistencyGroupBackupPolicyPlanDeletedMap := map[string]interface{}{}
snapshotConsistencyGroupBackupPolicyPlanDeletedMap["more_info"] = snapshotConsistencyGroup.BackupPolicyPlan.Deleted.MoreInfo
backupPolicyPlan["deleted"] = []map[string]interface{}{snapshotConsistencyGroupBackupPolicyPlanDeletedMap}
}
backupPolicyPlan["href"] = snapshotConsistencyGroup.BackupPolicyPlan.Href
backupPolicyPlan["id"] = snapshotConsistencyGroup.BackupPolicyPlan.ID
backupPolicyPlan["name"] = snapshotConsistencyGroup.BackupPolicyPlan.Name
backupPolicyPlan["resource_type"] = snapshotConsistencyGroup.BackupPolicyPlan.ResourceType
backupPolicyPlanList = append(backupPolicyPlanList, backupPolicyPlan)
}
l[isSnapshotBackupPolicyPlan] = backupPolicyPlanList
// source snapshot
if !core.IsNil(snapshotConsistencyGroup.Snapshots) {
snapshots := []map[string]interface{}{}
for _, snapshotsItem := range snapshotConsistencyGroup.Snapshots {
snapshotsItemMap, err := resourceIBMIsSnapshotConsistencyGroupSnapshotConsistencyGroupSnapshotsItemToMap(&snapshotsItem)
if err != nil {
return diag.FromErr(err)
}
snapshots = append(snapshots, snapshotsItemMap)
}
l["snapshots"] = snapshots
} else {
l["snapshots"] = []map[string]interface{}{}
}
if snapshotConsistencyGroup.ResourceGroup != nil && snapshotConsistencyGroup.ResourceGroup.ID != nil {
l["resource_group"] = *snapshotConsistencyGroup.ResourceGroup.ID
}
if snapshotConsistencyGroup.ServiceTags != nil {
l["service_tags"] = snapshotConsistencyGroup.ServiceTags
}
tags, err := flex.GetGlobalTagsUsingCRN(meta, *snapshotConsistencyGroup.CRN, "", isUserTagType)
if err != nil {
log.Printf(
"Error on get of resource vpc snapshot consistency group (%s) tags: %s", d.Id(), err)
}
l["tags"] = tags
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *snapshotConsistencyGroup.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"[ERROR] Error on get of resource snapshot (%s) access tags: %s", d.Id(), err)
}
l["access_tags"] = accesstags
snapshotConsistencyGroupsInfo = append(snapshotConsistencyGroupsInfo, l)
}
d.SetId(dataSourceIBMIsSnapshotConsistencyGroupsID(d))
if err = d.Set("snapshot_consistency_groups", snapshotConsistencyGroupsInfo); err != nil {
return diag.FromErr(fmt.Errorf("Error setting snapshot_consistency_groups %s", err))
}
return nil
}
// dataSourceIBMIsSnapshotConsistencyGroupsID returns a reasonable ID for the list.
func dataSourceIBMIsSnapshotConsistencyGroupsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}