-
Notifications
You must be signed in to change notification settings - Fork 670
/
data_source_ibm_is_snapshot.go
345 lines (322 loc) · 11.7 KB
/
data_source_ibm_is_snapshot.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceSnapshot() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISSnapshotRead,
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{isSnapshotName, "identifier"},
Description: "Snapshot identifier",
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_snapshot", "identifier"),
},
isSnapshotName: {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{isSnapshotName, "identifier"},
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_snapshot", isSnapshotName),
Description: "Snapshot name",
},
isSnapshotResourceGroup: {
Type: schema.TypeString,
Computed: true,
Description: "Resource group info",
},
isSnapshotSourceVolume: {
Type: schema.TypeString,
Computed: true,
Description: "Snapshot source volume id",
},
isSnapshotSourceImage: {
Type: schema.TypeString,
Computed: true,
Description: "If present, the image id from which the data on this volume was most directly provisioned.",
},
isSnapshotAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access tags",
},
isSnapshotOperatingSystem: {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for the operating system included in this image",
},
isSnapshotBootable: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates if a boot volume attachment can be created with a volume created from this snapshot",
},
isSnapshotLCState: {
Type: schema.TypeString,
Computed: true,
Description: "Snapshot lifecycle state",
},
isSnapshotCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
isSnapshotEncryption: {
Type: schema.TypeString,
Computed: true,
Description: "Encryption type of the snapshot",
},
isSnapshotHref: {
Type: schema.TypeString,
Computed: true,
Description: "URL for the snapshot",
},
isSnapshotMinCapacity: {
Type: schema.TypeInt,
Computed: true,
Description: "Minimum capacity of the snapshot",
},
isSnapshotResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type of the snapshot",
},
isSnapshotSize: {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the snapshot",
},
isSnapshotCapturedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this snapshot was created",
},
isSnapshotUserTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "User Tags for the snapshot",
},
isSnapshotBackupPolicyPlan: {
Type: schema.TypeList,
Computed: true,
Description: "If present, the backup policy plan which created this snapshot.",
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 provides some 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",
},
},
},
},
},
}
}
func DataSourceIBMISSnapshotValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "identifier",
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isSnapshotName,
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
ibmISSnapshotDataSourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_snapshot", Schema: validateSchema}
return &ibmISSnapshotDataSourceValidator
}
func dataSourceIBMISSnapshotRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get(isSnapshotName).(string)
id := d.Get("identifier").(string)
err := snapshotGetByNameOrID(d, meta, name, id)
if err != nil {
return err
}
return nil
}
func snapshotGetByNameOrID(d *schema.ResourceData, meta interface{}, name, id string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
if name != "" {
start := ""
allrecs := []vpcv1.Snapshot{}
for {
listSnapshotOptions := &vpcv1.ListSnapshotsOptions{
Name: &name,
}
if start != "" {
listSnapshotOptions.Start = &start
}
snapshots, response, err := sess.ListSnapshots(listSnapshotOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching snapshots %s\n%s", err, response)
}
start = flex.GetNext(snapshots.Next)
allrecs = append(allrecs, snapshots.Snapshots...)
if start == "" {
break
}
}
for _, snapshot := range allrecs {
if *snapshot.Name == name || *snapshot.ID == id {
d.SetId(*snapshot.ID)
d.Set(isSnapshotName, *snapshot.Name)
d.Set(isSnapshotHref, *snapshot.Href)
d.Set(isSnapshotCRN, *snapshot.CRN)
d.Set(isSnapshotMinCapacity, *snapshot.MinimumCapacity)
d.Set(isSnapshotSize, *snapshot.Size)
d.Set(isSnapshotEncryption, *snapshot.Encryption)
d.Set(isSnapshotLCState, *snapshot.LifecycleState)
d.Set(isSnapshotResourceType, *snapshot.ResourceType)
d.Set(isSnapshotBootable, *snapshot.Bootable)
if snapshot.UserTags != nil {
if err = d.Set(isSnapshotUserTags, snapshot.UserTags); err != nil {
return fmt.Errorf("Error setting user tags: %s", err)
}
}
if snapshot.ResourceGroup != nil && snapshot.ResourceGroup.ID != nil {
d.Set(isSnapshotResourceGroup, *snapshot.ResourceGroup.ID)
}
if snapshot.SourceVolume != nil && snapshot.SourceVolume.ID != nil {
d.Set(isSnapshotSourceVolume, *snapshot.SourceVolume.ID)
}
if snapshot.SourceImage != nil && snapshot.SourceImage.ID != nil {
d.Set(isSnapshotSourceImage, *snapshot.SourceImage.ID)
}
if snapshot.OperatingSystem != nil && snapshot.OperatingSystem.Name != nil {
d.Set(isSnapshotOperatingSystem, *snapshot.OperatingSystem.Name)
}
backupPolicyPlanList := []map[string]interface{}{}
if snapshot.BackupPolicyPlan != nil {
backupPolicyPlan := map[string]interface{}{}
if snapshot.BackupPolicyPlan.Deleted != nil {
snapshotBackupPolicyPlanDeletedMap := map[string]interface{}{}
snapshotBackupPolicyPlanDeletedMap["more_info"] = snapshot.BackupPolicyPlan.Deleted.MoreInfo
backupPolicyPlan["deleted"] = []map[string]interface{}{snapshotBackupPolicyPlanDeletedMap}
}
backupPolicyPlan["href"] = snapshot.BackupPolicyPlan.Href
backupPolicyPlan["id"] = snapshot.BackupPolicyPlan.ID
backupPolicyPlan["name"] = snapshot.BackupPolicyPlan.Name
backupPolicyPlan["resource_type"] = snapshot.BackupPolicyPlan.ResourceType
backupPolicyPlanList = append(backupPolicyPlanList, backupPolicyPlan)
}
d.Set(isSnapshotBackupPolicyPlan, backupPolicyPlanList)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *snapshot.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource snapshot (%s) access tags: %s", d.Id(), err)
}
d.Set(isSnapshotAccessTags, accesstags)
return nil
}
}
return fmt.Errorf("[ERROR] No snapshot found with name %s", name)
} else {
getSnapshotOptions := &vpcv1.GetSnapshotOptions{
ID: &id,
}
snapshot, response, err := sess.GetSnapshot(getSnapshotOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching snapshot %s\n%s", err, response)
}
if (response != nil && response.StatusCode == 404) || snapshot == nil {
return fmt.Errorf("[ERROR] No snapshot found with id %s", id)
}
d.SetId(*snapshot.ID)
d.Set(isSnapshotName, *snapshot.Name)
d.Set(isSnapshotHref, *snapshot.Href)
d.Set(isSnapshotCRN, *snapshot.CRN)
d.Set(isSnapshotMinCapacity, *snapshot.MinimumCapacity)
d.Set(isSnapshotSize, *snapshot.Size)
d.Set(isSnapshotEncryption, *snapshot.Encryption)
d.Set(isSnapshotLCState, *snapshot.LifecycleState)
d.Set(isSnapshotResourceType, *snapshot.ResourceType)
d.Set(isSnapshotBootable, *snapshot.Bootable)
if snapshot.CapturedAt != nil {
d.Set(isSnapshotCapturedAt, (*snapshot.CapturedAt).String())
}
if snapshot.UserTags != nil {
if err = d.Set(isSnapshotUserTags, snapshot.UserTags); err != nil {
return fmt.Errorf("Error setting user tags: %s", err)
}
}
if snapshot.ResourceGroup != nil && snapshot.ResourceGroup.ID != nil {
d.Set(isSnapshotResourceGroup, *snapshot.ResourceGroup.ID)
}
if snapshot.SourceVolume != nil && snapshot.SourceVolume.ID != nil {
d.Set(isSnapshotSourceVolume, *snapshot.SourceVolume.ID)
}
if snapshot.SourceImage != nil && snapshot.SourceImage.ID != nil {
d.Set(isSnapshotSourceImage, *snapshot.SourceImage.ID)
}
if snapshot.OperatingSystem != nil && snapshot.OperatingSystem.Name != nil {
d.Set(isSnapshotOperatingSystem, *snapshot.OperatingSystem.Name)
}
backupPolicyPlanList := []map[string]interface{}{}
if snapshot.BackupPolicyPlan != nil {
backupPolicyPlan := map[string]interface{}{}
if snapshot.BackupPolicyPlan.Deleted != nil {
snapshotBackupPolicyPlanDeletedMap := map[string]interface{}{}
snapshotBackupPolicyPlanDeletedMap["more_info"] = snapshot.BackupPolicyPlan.Deleted.MoreInfo
backupPolicyPlan["deleted"] = []map[string]interface{}{snapshotBackupPolicyPlanDeletedMap}
}
backupPolicyPlan["href"] = snapshot.BackupPolicyPlan.Href
backupPolicyPlan["id"] = snapshot.BackupPolicyPlan.ID
backupPolicyPlan["name"] = snapshot.BackupPolicyPlan.Name
backupPolicyPlan["resource_type"] = snapshot.BackupPolicyPlan.ResourceType
backupPolicyPlanList = append(backupPolicyPlanList, backupPolicyPlan)
}
d.Set(isSnapshotBackupPolicyPlan, backupPolicyPlanList)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *snapshot.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource snapshot (%s) access tags: %s", d.Id(), err)
}
d.Set(isSnapshotAccessTags, accesstags)
return nil
}
}