This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource_openstack_blockstorage_volume_attach_v2.go
397 lines (323 loc) · 9.81 KB
/
resource_openstack_blockstorage_volume_attach_v2.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
package openstack
import (
"fmt"
"log"
"time"
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceBlockStorageVolumeAttachV2() *schema.Resource {
return &schema.Resource{
Create: resourceBlockStorageVolumeAttachV2Create,
Read: resourceBlockStorageVolumeAttachV2Read,
Delete: resourceBlockStorageVolumeAttachV2Delete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Removed: "instance_id is no longer used in this resource",
},
"host_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"device": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"attach_mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"ro", "rw",
}, false),
},
"initiator": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ip_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"multipath": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"os_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"platform": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"wwpn": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"wwnn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
// Volume attachment information
"data": {
Type: schema.TypeMap,
Computed: true,
Sensitive: true,
},
"driver_volume_type": {
Type: schema.TypeString,
Computed: true,
},
"mount_point_base": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceBlockStorageVolumeAttachV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.blockStorageV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
}
// initialize the connection
volumeId := d.Get("volume_id").(string)
connOpts := &volumeactions.InitializeConnectionOpts{}
if v, ok := d.GetOk("host_name"); ok {
connOpts.Host = v.(string)
}
if v, ok := d.GetOk("multipath"); ok {
multipath := v.(bool)
connOpts.Multipath = &multipath
}
if v, ok := d.GetOk("ip_address"); ok {
connOpts.IP = v.(string)
}
if v, ok := d.GetOk("initiator"); ok {
connOpts.Initiator = v.(string)
}
if v, ok := d.GetOk("os_type"); ok {
connOpts.OSType = v.(string)
}
if v, ok := d.GetOk("platform"); ok {
connOpts.Platform = v.(string)
}
if v, ok := d.GetOk("wwnns"); ok {
connOpts.Wwnns = v.(string)
}
if v, ok := d.GetOk("wwpns"); ok {
var wwpns []string
for _, i := range v.([]string) {
wwpns = append(wwpns, i)
}
connOpts.Wwpns = wwpns
}
connInfo, err := volumeactions.InitializeConnection(client, volumeId, connOpts).Extract()
if err != nil {
return fmt.Errorf(
"Unable to initialize connection for openstack_blockstorage_volume_attach_v2: %s", err)
}
// Only uncomment this when debugging since connInfo contains sensitive information.
// log.Printf("[DEBUG] Volume Connection for %s: %#v", volumeId, connInfo)
// Because this information is only returned upon creation,
// it must be set in Create.
if v, ok := connInfo["data"]; ok {
data := make(map[string]string)
for key, value := range v.(map[string]interface{}) {
if v, ok := value.(string); ok {
data[key] = v
}
}
d.Set("data", data)
}
if v, ok := connInfo["driver_volume_type"]; ok {
d.Set("driver_volume_type", v)
}
if v, ok := connInfo["mount_point_base"]; ok {
d.Set("mount_point_base", v)
}
// Once the connection has been made, tell Cinder to mark the volume as attached.
attachMode, err := expandBlockStorageV2AttachMode(d.Get("attach_mode").(string))
if err != nil {
return nil
}
attachOpts := &volumeactions.AttachOpts{
HostName: d.Get("host_name").(string),
MountPoint: d.Get("device").(string),
Mode: attachMode,
}
log.Printf("[DEBUG] openstack_blockstorage_volume_attach_v2 attach options: %#v", attachOpts)
if err := volumeactions.Attach(client, volumeId, attachOpts).ExtractErr(); err != nil {
return fmt.Errorf(
"Error attaching openstack_blockstorage_volume_attach_v2 for volume %s: %s", volumeId, err)
}
// Wait for the volume to become available.
log.Printf(
"[DEBUG] Waiting for openstack_blockstorage_volume_attach_v2 volume %s to become available", volumeId)
stateConf := &resource.StateChangeConf{
Pending: []string{"available", "attaching"},
Target: []string{"in-use"},
Refresh: blockStorageVolumeV2StateRefreshFunc(client, volumeId),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for openstack_blockstorage_volume_attach_v2 volume %s to become in-use: %s", volumeId, err)
}
// Once the volume has been marked as attached,
// retrieve a fresh copy of it with all information now available.
volume, err := volumes.Get(client, volumeId).Extract()
if err != nil {
return fmt.Errorf(
"Unable to retrieve openstack_blockstorage_volume_attach_v2 volume %s: %s", volumeId, err)
}
// Search for the attachmentId
var attachmentId string
hostName := d.Get("host_name").(string)
for _, attachment := range volume.Attachments {
if hostName != "" && hostName == attachment.HostName {
attachmentId = attachment.AttachmentID
}
}
if attachmentId == "" {
return fmt.Errorf(
"Unable to determine attachment ID for openstack_blockstorage_volume_attach_v2 volume %s.", volumeId)
}
// The ID must be a combination of the volume and attachment ID
// since a volume ID is required to retrieve an attachment ID.
id := fmt.Sprintf("%s/%s", volumeId, attachmentId)
d.SetId(id)
return resourceBlockStorageVolumeAttachV2Read(d, meta)
}
func resourceBlockStorageVolumeAttachV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.blockStorageV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
}
volumeId, attachmentId, err := blockStorageVolumeAttachV2ParseID(d.Id())
if err != nil {
return err
}
volume, err := volumes.Get(client, volumeId).Extract()
if err != nil {
return fmt.Errorf(
"Unable to retrieve openstack_blockstorage_volume_attach_v2 volume %s: %s", volumeId, err)
}
log.Printf("[DEBUG] Retrieved openstack_blockstorage_volume_attach_v2 volume %s: %#v", volumeId, volume)
var attachment volumes.Attachment
for _, v := range volume.Attachments {
if attachmentId == v.AttachmentID {
attachment = v
}
}
log.Printf(
"[DEBUG] Retrieved openstack_blockstorage_volume_attach_v2 attachment %s: %#v", d.Id(), attachment)
return nil
}
func resourceBlockStorageVolumeAttachV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.blockStorageV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
}
volumeId, attachmentId, err := blockStorageVolumeAttachV2ParseID(d.Id())
if err != nil {
return err
}
// Terminate the connection
termOpts := &volumeactions.TerminateConnectionOpts{}
if v, ok := d.GetOk("host_name"); ok {
termOpts.Host = v.(string)
}
if v, ok := d.GetOk("multipath"); ok {
multipath := v.(bool)
termOpts.Multipath = &multipath
}
if v, ok := d.GetOk("ip_address"); ok {
termOpts.IP = v.(string)
}
if v, ok := d.GetOk("initiator"); ok {
termOpts.Initiator = v.(string)
}
if v, ok := d.GetOk("os_type"); ok {
termOpts.OSType = v.(string)
}
if v, ok := d.GetOk("platform"); ok {
termOpts.Platform = v.(string)
}
if v, ok := d.GetOk("wwnns"); ok {
termOpts.Wwnns = v.(string)
}
if v, ok := d.GetOk("wwpns"); ok {
var wwpns []string
for _, i := range v.([]string) {
wwpns = append(wwpns, i)
}
termOpts.Wwpns = wwpns
}
err = volumeactions.TerminateConnection(client, volumeId, termOpts).ExtractErr()
if err != nil {
return fmt.Errorf(
"Error terminating openstack_blockstorage_volume_attach_v2 connection %s: %s", d.Id(), err)
}
// Detach the volume
detachOpts := volumeactions.DetachOpts{
AttachmentID: attachmentId,
}
log.Printf(
"[DEBUG] openstack_blockstorage_volume_attach_v2 detachment options %s: %#v", d.Id(), detachOpts)
if err := volumeactions.Detach(client, volumeId, detachOpts).ExtractErr(); err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{"in-use", "attaching", "detaching"},
Target: []string{"available"},
Refresh: blockStorageVolumeV2StateRefreshFunc(client, volumeId),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for openstack_blockstorage_volume_attach_v2 volume %s to become available: %s", volumeId, err)
}
return nil
}