-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_hcs_compute_volume_attach.go
237 lines (202 loc) · 7.15 KB
/
resource_hcs_compute_volume_attach.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
package ecs
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/common"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/config"
golangsdk "github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/ecs/v1/block_devices"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/ecs/v1/jobs"
)
func ResourceComputeVolumeAttach() *schema.Resource {
return &schema.Resource{
CreateContext: resourceComputeVolumeAttachCreate,
ReadContext: resourceComputeVolumeAttachRead,
DeleteContext: resourceComputeVolumeAttachDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
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,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"device": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"pci_address": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceComputeVolumeAttachCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := config.GetHcsConfig(meta)
region := conf.GetRegion(d)
computeClient, err := conf.ComputeV1Client(region)
if err != nil {
return diag.Errorf("Error creating compute v1 client: %s", err)
}
// The ECS instances do not support mounting multiple volumes at the same time.
instanceId := d.Get("instance_id").(string)
config.MutexKV.Lock(instanceId)
defer config.MutexKV.Unlock(instanceId)
// The EVS volumes also do not support being mounted to multiple instances at the same time.
volumeId := d.Get("volume_id").(string)
config.MutexKV.Lock(volumeId)
defer config.MutexKV.Unlock(volumeId)
device := d.Get("device").(string)
attachOpts := block_devices.AttachOpts{
Device: device,
VolumeId: volumeId,
ServerId: instanceId,
}
log.Printf("[DEBUG] Creating volume attachment: %#v", attachOpts)
job, err := block_devices.Attach(computeClient, attachOpts)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] The response of volume attachment request is: %#v", job)
stateConf := &resource.StateChangeConf{
Pending: []string{"INIT", "RUNNING"},
Target: []string{"SUCCESS"},
Refresh: AttachmentJobRefreshFunc(computeClient, job.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 10 * time.Second,
PollInterval: 10 * time.Second,
// Sometime, the status on the EVS side is not complete yet, but the job status shows as "SUCCESS".
ContinuousTargetOccurence: 2,
}
if _, err = stateConf.WaitForStateContext(ctx); err != nil {
return diag.Errorf("Error attaching volume: %s", err)
}
id := fmt.Sprintf("%s/%s", instanceId, volumeId)
d.SetId(id)
return resourceComputeVolumeAttachRead(ctx, d, meta)
}
func resourceComputeVolumeAttachRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := config.GetHcsConfig(meta)
region := conf.GetRegion(d)
computeClient, err := conf.ComputeV1Client(region)
if err != nil {
return diag.Errorf("Error creating compute V1 client: %s", err)
}
instanceId, volumeId, err := parseComputeVolumeAttachmentId(d.Id())
if err != nil {
return diag.FromErr(err)
}
attachment, err := block_devices.Get(computeClient, instanceId, volumeId).Extract()
if err != nil {
return common.CheckDeletedDiag(d, parseRequestError(err), "error fetching compute_volume_attach")
}
log.Printf("[DEBUG] Retrieved volume attachment: %#v", attachment)
mErr := multierror.Append(nil,
d.Set("instance_id", attachment.ServerId),
d.Set("volume_id", attachment.VolumeId),
d.Set("device", attachment.Device),
d.Set("region", region),
d.Set("pci_address", attachment.PciAddress),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func resourceComputeVolumeAttachDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := config.GetHcsConfig(meta)
region := conf.GetRegion(d)
computeClient, err := conf.ComputeV1Client(region)
if err != nil {
return diag.Errorf("Error creating compute V1 client: %s", err)
}
// The ECS instances do not support unmounting multiple volumes at the same time.
instanceId := d.Get("instance_id").(string)
config.MutexKV.Lock(instanceId)
defer config.MutexKV.Unlock(instanceId)
// The EVS volumes also do not support being unmounted from multiple instances at the same time.
volumeId := d.Get("volume_id").(string)
config.MutexKV.Lock(volumeId)
defer config.MutexKV.Unlock(volumeId)
opts := block_devices.DetachOpts{
ServerId: instanceId,
}
job, err := block_devices.Detach(computeClient, volumeId, opts)
if err != nil {
return common.CheckDeletedDiag(d, parseRequestError(err), "error detaching volume")
}
stateConf := &resource.StateChangeConf{
Pending: []string{"RUNNING"},
Target: []string{"SUCCESS", "NOTFOUND"},
Refresh: AttachmentJobRefreshFunc(computeClient, job.ID),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
PollInterval: 10 * time.Second,
// Sometime, the status on the EVS side is not complete yet, but the job status shows as "SUCCESS".
ContinuousTargetOccurence: 2,
}
if _, err = stateConf.WaitForStateContext(ctx); err != nil {
return diag.Errorf("Error detaching volume: %s", err)
}
return nil
}
func AttachmentJobRefreshFunc(c *golangsdk.ServiceClient, jobId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := jobs.Get(c, jobId)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return resp, "NOTFOUND", nil
}
return resp, "ERROR", err
}
return resp, resp.Status, nil
}
}
func parseRequestError(respErr error) error {
var apiErr block_devices.ErrorResponse
if errCode, ok := respErr.(golangsdk.ErrDefault400); ok && errCode.Body != nil {
pErr := json.Unmarshal(errCode.Body, &apiErr)
if pErr == nil && apiErr.Error.Code == "Ecs.1000" && strings.Contains(apiErr.Error.Message, "itemNotFound") {
return golangsdk.ErrDefault404{
ErrUnexpectedResponseCode: golangsdk.ErrUnexpectedResponseCode{
Body: []byte("the volume has been deleted"),
},
}
}
}
return respErr
}
func parseComputeVolumeAttachmentId(id string) (instanceID, volumeID string, err error) {
idParts := strings.Split(id, "/")
if len(idParts) < 2 {
err = fmt.Errorf("unable to determine volume attachment ID")
return
}
instanceID = idParts[0]
volumeID = idParts[1]
return
}