-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
ebs_volume_attachment.go
199 lines (164 loc) · 6.39 KB
/
ebs_volume_attachment.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package ec2
import (
"bytes"
"context"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)
// @SDKResource("aws_volume_attachment")
func ResourceVolumeAttachment() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceVolumeAttachmentCreate,
ReadWithoutTimeout: resourceVolumeAttachmentRead,
UpdateWithoutTimeout: schema.NoopContext,
DeleteWithoutTimeout: resourceVolumeAttachmentDelete,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), ":")
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected DEVICE_NAME:VOLUME_ID:INSTANCE_ID", d.Id())
}
deviceName := idParts[0]
volumeID := idParts[1]
instanceID := idParts[2]
d.SetId(volumeAttachmentID(deviceName, volumeID, instanceID))
d.Set("device_name", deviceName)
d.Set("instance_id", instanceID)
d.Set("volume_id", volumeID)
return []*schema.ResourceData{d}, nil
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"device_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"force_detach": {
Type: schema.TypeBool,
Optional: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"skip_destroy": {
Type: schema.TypeBool,
Optional: true,
},
"stop_instance_before_detaching": {
Type: schema.TypeBool,
Optional: true,
},
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceVolumeAttachmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EC2Conn(ctx)
deviceName := d.Get("device_name").(string)
instanceID := d.Get("instance_id").(string)
volumeID := d.Get("volume_id").(string)
_, err := FindEBSVolumeAttachment(ctx, conn, volumeID, instanceID, deviceName)
if tfresource.NotFound(err) {
// This handles the situation where the instance is created by
// a spot request and whilst the request has been fulfilled the
// instance is not running yet.
if _, err := WaitInstanceReady(ctx, conn, instanceID, InstanceReadyTimeout); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EC2 Instance (%s) to be ready: %s", instanceID, err)
}
input := &ec2.AttachVolumeInput{
Device: aws.String(deviceName),
InstanceId: aws.String(instanceID),
VolumeId: aws.String(volumeID),
}
log.Printf("[DEBUG] Create EBS Volume Attachment: %s", input)
_, err := conn.AttachVolumeWithContext(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "attaching EBS Volume (%s) to EC2 Instance (%s): %s", volumeID, instanceID, err)
}
} else if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EBS Volume (%s) Attachment (%s): %s", volumeID, instanceID, err)
}
if _, err := WaitVolumeAttachmentCreated(ctx, conn, volumeID, instanceID, deviceName, d.Timeout(schema.TimeoutCreate)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EBS Volume (%s) Attachment (%s) create: %s", volumeID, instanceID, err)
}
d.SetId(volumeAttachmentID(deviceName, volumeID, instanceID))
return append(diags, resourceVolumeAttachmentRead(ctx, d, meta)...)
}
func resourceVolumeAttachmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EC2Conn(ctx)
deviceName := d.Get("device_name").(string)
instanceID := d.Get("instance_id").(string)
volumeID := d.Get("volume_id").(string)
_, err := FindEBSVolumeAttachment(ctx, conn, volumeID, instanceID, deviceName)
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EBS Volume Attachment %s not found, removing from state", d.Id())
d.SetId("")
return diags
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EBS Volume (%s) Attachment (%s): %s", volumeID, instanceID, err)
}
return diags
}
func resourceVolumeAttachmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EC2Conn(ctx)
if _, ok := d.GetOk("skip_destroy"); ok {
return diags
}
deviceName := d.Get("device_name").(string)
instanceID := d.Get("instance_id").(string)
volumeID := d.Get("volume_id").(string)
if _, ok := d.GetOk("stop_instance_before_detaching"); ok {
if err := StopInstance(ctx, conn, instanceID, InstanceStopTimeout); err != nil {
return sdkdiag.AppendErrorf(diags, "deleting EBS Volume (%s) Attachment (%s): %s", volumeID, instanceID, err)
}
}
input := &ec2.DetachVolumeInput{
Device: aws.String(deviceName),
Force: aws.Bool(d.Get("force_detach").(bool)),
InstanceId: aws.String(instanceID),
VolumeId: aws.String(volumeID),
}
log.Printf("[DEBUG] Deleting EBS Volume Attachment: %s", d.Id())
_, err := conn.DetachVolumeWithContext(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting EBS Volume (%s) Attachment (%s): %s", volumeID, instanceID, err)
}
if _, err := WaitVolumeAttachmentDeleted(ctx, conn, volumeID, instanceID, deviceName, d.Timeout(schema.TimeoutDelete)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EBS Volume (%s) Attachment (%s) delete: %s", volumeID, instanceID, err)
}
return diags
}
func volumeAttachmentID(name, volumeID, instanceID string) string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("%s-", name))
buf.WriteString(fmt.Sprintf("%s-", instanceID))
buf.WriteString(fmt.Sprintf("%s-", volumeID))
return fmt.Sprintf("vai-%d", create.StringHashcode(buf.String()))
}