github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/aws/delete_volume.go (about)

     1  package aws
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/service/ec2"
     6  	"github.com/emc-advanced-dev/pkg/errors"
     7  )
     8  
     9  func (p *AwsProvider) DeleteVolume(id string, force bool) error {
    10  	volume, err := p.GetVolume(id)
    11  	if err != nil {
    12  		return errors.New("retrieving volume "+id, err)
    13  	}
    14  	if volume.Attachment != "" {
    15  		if force {
    16  			if err := p.DetachVolume(volume.Id); err != nil {
    17  				return errors.New("detaching volume for deletion", err)
    18  			} else {
    19  				return errors.New("volume "+volume.Id+" is attached to instance."+volume.Attachment+", try again with --force or detach volume first", err)
    20  			}
    21  		}
    22  	}
    23  	param := &ec2.DeleteVolumeInput{
    24  		VolumeId: aws.String(volume.Id),
    25  	}
    26  	_, err = p.newEC2().DeleteVolume(param)
    27  	if err != nil {
    28  		return errors.New("failed to terminate volume "+volume.Id, err)
    29  	}
    30  	return p.state.RemoveVolume(volume)
    31  }