-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_volume.go
265 lines (231 loc) · 6.98 KB
/
data_source_ibm_is_volume.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
// 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 DataSourceIBMISVolume() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISVolumeRead,
Schema: map[string]*schema.Schema{
isVolumeName: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_subnet", isVolumeName),
Description: "Volume name",
},
isVolumeZone: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Zone name",
},
isVolumeBandwidth: {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum bandwidth (in megabits per second) for the volume",
},
isVolumeResourceGroup: {
Type: schema.TypeString,
Computed: true,
Description: "Resource group name",
},
isVolumeProfileName: {
Type: schema.TypeString,
Computed: true,
Description: "Volume profile name",
},
isVolumeEncryptionKey: {
Type: schema.TypeString,
Computed: true,
Description: "Volume encryption key info",
},
isVolumeEncryptionType: {
Type: schema.TypeString,
Computed: true,
Description: "Volume encryption type info",
},
isVolumeCapacity: {
Type: schema.TypeInt,
Computed: true,
Description: "Vloume capacity value",
},
isVolumeIops: {
Type: schema.TypeInt,
Computed: true,
Description: "IOPS value for the Volume",
},
isVolumeCrn: {
Type: schema.TypeString,
Computed: true,
Description: "CRN value for the volume instance",
},
isVolumeStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Volume status",
},
isVolumeStatusReasons: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVolumeStatusReasonsCode: {
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the status reason",
},
isVolumeStatusReasonsMessage: {
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the status reason",
},
isVolumeStatusReasonsMoreInfo: {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about this status reason",
},
},
},
},
isVolumeTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "Tags for the volume instance",
},
isVolumeSourceSnapshot: {
Type: schema.TypeString,
Computed: true,
Description: "Identifier of the snapshot from which this volume was cloned",
},
flex.ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about this instance",
},
flex.ResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the resource",
},
flex.ResourceCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
flex.ResourceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the resource",
},
flex.ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
},
}
}
func DataSourceIBMISVolumeValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isVolumeName,
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
ibmISVoulmeDataSourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_volume", Schema: validateSchema}
return &ibmISVoulmeDataSourceValidator
}
func dataSourceIBMISVolumeRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get(isVolumeName).(string)
err := volumeGet(d, meta, name)
if err != nil {
return err
}
return nil
}
func volumeGet(d *schema.ResourceData, meta interface{}, name string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
zone := ""
if zname, ok := d.GetOk(isVolumeZone); ok {
zone = zname.(string)
}
listVolumesOptions := &vpcv1.ListVolumesOptions{
Name: &name,
}
if zone != "" {
listVolumesOptions.ZoneName = &zone
}
listVolumesOptions.Name = &name
vols, response, err := sess.ListVolumes(listVolumesOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching volumes %s\n%s", err, response)
}
allrecs := vols.Volumes
if len(allrecs) == 0 {
return fmt.Errorf("[ERROR] No Volume found with name %s", name)
}
vol := allrecs[0]
d.SetId(*vol.ID)
d.Set(isVolumeBandwidth, int(*vol.Bandwidth))
d.Set(isVolumeName, *vol.Name)
d.Set(isVolumeProfileName, *vol.Profile.Name)
d.Set(isVolumeZone, *vol.Zone.Name)
if vol.EncryptionKey != nil {
d.Set(isVolumeEncryptionKey, vol.EncryptionKey.CRN)
}
if vol.Encryption != nil {
d.Set(isVolumeEncryptionType, vol.Encryption)
}
if vol.SourceSnapshot != nil {
d.Set(isVolumeSourceSnapshot, *vol.SourceSnapshot.ID)
}
d.Set(isVolumeIops, *vol.Iops)
d.Set(isVolumeCapacity, *vol.Capacity)
d.Set(isVolumeCrn, *vol.CRN)
d.Set(isVolumeStatus, *vol.Status)
if vol.StatusReasons != nil {
statusReasonsList := make([]map[string]interface{}, 0)
for _, sr := range vol.StatusReasons {
currentSR := map[string]interface{}{}
if sr.Code != nil && sr.Message != nil {
currentSR[isVolumeStatusReasonsCode] = *sr.Code
currentSR[isVolumeStatusReasonsMessage] = *sr.Message
if sr.MoreInfo != nil {
currentSR[isVolumeStatusReasonsMoreInfo] = *sr.Message
}
statusReasonsList = append(statusReasonsList, currentSR)
}
}
d.Set(isVolumeStatusReasons, statusReasonsList)
}
tags, err := flex.GetTagsUsingCRN(meta, *vol.CRN)
if err != nil {
log.Printf(
"Error on get of resource vpc volume (%s) tags: %s", d.Id(), err)
}
d.Set(isVolumeTags, tags)
controller, err := flex.GetBaseController(meta)
if err != nil {
return err
}
d.Set(flex.ResourceControllerURL, controller+"/vpc-ext/storage/storageVolumes")
d.Set(flex.ResourceName, *vol.Name)
d.Set(flex.ResourceCRN, *vol.CRN)
d.Set(flex.ResourceStatus, *vol.Status)
if vol.ResourceGroup != nil {
d.Set(flex.ResourceGroupName, vol.ResourceGroup.Name)
d.Set(isVolumeResourceGroup, *vol.ResourceGroup.ID)
}
return nil
}