Simplify check for already detached volume before destroying

This commit is contained in:
Jonathan McCall 2016-09-30 14:09:35 -04:00
parent f3deaab878
commit dc4a7b7d25
1 changed files with 6 additions and 15 deletions

View File

@ -136,7 +136,7 @@ func resourceAwsVolumeAttachmentRead(d *schema.ResourceData, meta interface{}) e
}, },
} }
_, err := conn.DescribeVolumes(request) vols, err := conn.DescribeVolumes(request)
if err != nil { if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVolume.NotFound" { if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVolume.NotFound" {
d.SetId("") d.SetId("")
@ -144,6 +144,11 @@ func resourceAwsVolumeAttachmentRead(d *schema.ResourceData, meta interface{}) e
} }
return fmt.Errorf("Error reading EC2 volume %s for instance: %s: %#v", d.Get("volume_id").(string), d.Get("instance_id").(string), err) return fmt.Errorf("Error reading EC2 volume %s for instance: %s: %#v", d.Get("volume_id").(string), d.Get("instance_id").(string), err)
} }
if len(vols.Volumes) == 0 || *vols.Volumes[0].State == "available" {
d.SetId("")
}
return nil return nil
} }
@ -153,20 +158,6 @@ func resourceAwsVolumeAttachmentDelete(d *schema.ResourceData, meta interface{})
vID := d.Get("volume_id").(string) vID := d.Get("volume_id").(string)
iID := d.Get("instance_id").(string) iID := d.Get("instance_id").(string)
desc_opts := &ec2.DescribeVolumesInput{
VolumeIds: []*string{aws.String(vID)},
}
attr, desc_err := conn.DescribeVolumes(desc_opts)
if desc_err != nil {
return fmt.Errorf("Error reading EC2 volume %s: %s", vID, desc_err)
}
if len(attr.Volumes) == 0 || *attr.Volumes[0].State == "available" {
d.SetId("")
return nil
}
opts := &ec2.DetachVolumeInput{ opts := &ec2.DetachVolumeInput{
Device: aws.String(d.Get("device_name").(string)), Device: aws.String(d.Get("device_name").(string)),
InstanceId: aws.String(iID), InstanceId: aws.String(iID),