Skip to content

Commit

Permalink
refactor volume attachment to not retry attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
catsby committed May 27, 2015
1 parent c523401 commit 3af25c1
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions builtin/providers/aws/resource_aws_volume_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,20 @@ func resourceAwsVolumeAttachmentCreate(d *schema.ResourceData, meta interface{})
InstanceID: aws.String(iID),
VolumeID: aws.String(vID),
}
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: "attaching",
Refresh: attachVolumeFunc(conn, opts),
Timeout: 1 * time.Minute,
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err := stateConf.WaitForState()
_, err := conn.AttachVolume(opts)
if err != nil {
return fmt.Errorf("Error attaching volume %s to instance %s: %s", vID, iID, err)
if awsErr, ok := err.(awserr.Error); ok {
return fmt.Errorf("[WARN] Error attaching volume (%s) to instance (%s), message: \"%s\", code: \"%s\"",
vID, iID, awsErr.Message(), awsErr.Code())
}
return err
}

d.SetId(volumeAttachmentID(name, vID, iID))
return resourceAwsVolumeAttachmentRead(d, meta)
}

func attachVolumeFunc(conn *ec2.EC2, opts *ec2.AttachVolumeInput) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
va, err := conn.AttachVolume(opts)
if err != nil {
awsErr, ok := err.(awserr.Error)
if ok && awsErr.Code() == "VolumeInUse" && *va.InstanceID == *opts.InstanceID {
return nil, "attaching", nil
}
return nil, "error", err
}
return va, *va.State, nil
}
}

func resourceAwsVolumeAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

Expand Down Expand Up @@ -127,7 +109,7 @@ func resourceAwsVolumeAttachmentDelete(d *schema.ResourceData, meta interface{})
Force: aws.Boolean(d.Get("force_detach").(bool)),
}

return resource.Retry(1*time.Minute, func() error {
return resource.Retry(3*time.Minute, func() error {
resp, err := conn.DetachVolume(opts)
if err != nil {
awsErr, ok := err.(awserr.Error)
Expand Down

0 comments on commit 3af25c1

Please sign in to comment.