Skip to content

Commit b329aa9

Browse files
committed
r/aws_volume_attachment: Move finder functions.
1 parent 1f7e7c2 commit b329aa9

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

internal/service/ec2/ebs_volume_attachment.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import (
1313

1414
"github.com/aws/aws-sdk-go-v2/aws"
1515
"github.com/aws/aws-sdk-go-v2/service/ec2"
16-
awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types"
1716
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
1817
"github.com/hashicorp/terraform-plugin-log/tflog"
1918
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
20-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2119
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2220
"github.com/hashicorp/terraform-provider-aws/internal/conns"
2321
"github.com/hashicorp/terraform-provider-aws/internal/create"
@@ -206,48 +204,6 @@ func volumeAttachmentID(name, volumeID, instanceID string) string {
206204
return fmt.Sprintf("vai-%d", create.StringHashcode(buf.String()))
207205
}
208206

209-
func findVolumeAttachment(ctx context.Context, conn *ec2.Client, volumeID, instanceID, deviceName string) (*awstypes.VolumeAttachment, error) {
210-
input := ec2.DescribeVolumesInput{
211-
Filters: newAttributeFilterList(map[string]string{
212-
"attachment.device": deviceName,
213-
"attachment.instance-id": instanceID,
214-
}),
215-
VolumeIds: []string{volumeID},
216-
}
217-
218-
output, err := findEBSVolume(ctx, conn, &input)
219-
220-
if err != nil {
221-
return nil, err
222-
}
223-
224-
if state := output.State; state == awstypes.VolumeStateAvailable || state == awstypes.VolumeStateDeleted {
225-
return nil, &retry.NotFoundError{
226-
Message: string(state),
227-
LastRequest: input,
228-
}
229-
}
230-
231-
// Eventual consistency check.
232-
if aws.ToString(output.VolumeId) != volumeID {
233-
return nil, &retry.NotFoundError{
234-
LastRequest: input,
235-
}
236-
}
237-
238-
for _, v := range output.Attachments {
239-
if v.State == awstypes.VolumeAttachmentStateDetached {
240-
continue
241-
}
242-
243-
if aws.ToString(v.Device) == deviceName && aws.ToString(v.InstanceId) == instanceID {
244-
return &v, nil
245-
}
246-
}
247-
248-
return nil, &retry.NotFoundError{}
249-
}
250-
251207
func stopVolumeAttachmentInstance(ctx context.Context, conn *ec2.Client, id string, force bool, timeout time.Duration) error {
252208
tflog.Info(ctx, "Stopping EC2 Instance", map[string]any{
253209
"ec2_instance_id": id,

internal/service/ec2/find.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,48 @@ func findPublicIPv4PoolByID(ctx context.Context, conn *ec2.Client, id string) (*
11161116
return output, nil
11171117
}
11181118

1119+
func findVolumeAttachment(ctx context.Context, conn *ec2.Client, volumeID, instanceID, deviceName string) (*awstypes.VolumeAttachment, error) {
1120+
input := ec2.DescribeVolumesInput{
1121+
Filters: newAttributeFilterList(map[string]string{
1122+
"attachment.device": deviceName,
1123+
"attachment.instance-id": instanceID,
1124+
}),
1125+
VolumeIds: []string{volumeID},
1126+
}
1127+
1128+
output, err := findEBSVolume(ctx, conn, &input)
1129+
1130+
if err != nil {
1131+
return nil, err
1132+
}
1133+
1134+
if state := output.State; state == awstypes.VolumeStateAvailable || state == awstypes.VolumeStateDeleted {
1135+
return nil, &retry.NotFoundError{
1136+
Message: string(state),
1137+
LastRequest: input,
1138+
}
1139+
}
1140+
1141+
// Eventual consistency check.
1142+
if aws.ToString(output.VolumeId) != volumeID {
1143+
return nil, &retry.NotFoundError{
1144+
LastRequest: input,
1145+
}
1146+
}
1147+
1148+
for _, v := range output.Attachments {
1149+
if v.State == awstypes.VolumeAttachmentStateDetached {
1150+
continue
1151+
}
1152+
1153+
if aws.ToString(v.Device) == deviceName && aws.ToString(v.InstanceId) == instanceID {
1154+
return &v, nil
1155+
}
1156+
}
1157+
1158+
return nil, &retry.NotFoundError{}
1159+
}
1160+
11191161
func findVolumeAttachmentInstanceByID(ctx context.Context, conn *ec2.Client, id string) (*awstypes.Instance, error) {
11201162
input := ec2.DescribeInstancesInput{
11211163
InstanceIds: []string{id},

0 commit comments

Comments
 (0)