github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/aws/detach_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  	"github.com/solo-io/unik/pkg/types"
     8  )
     9  
    10  func (p *AwsProvider) DetachVolume(id string) error {
    11  	volume, err := p.GetVolume(id)
    12  	if err != nil {
    13  		return errors.New("retrieving volume "+id, err)
    14  	}
    15  	if volume.Attachment == "" {
    16  		return errors.New("volume has no attachment", nil)
    17  	}
    18  	param := &ec2.DetachVolumeInput{
    19  		VolumeId: aws.String(volume.Id),
    20  		Force:    aws.Bool(true),
    21  	}
    22  	if _, err := p.newEC2().DetachVolume(param); err != nil {
    23  		return errors.New("failed to detach volume "+volume.Id, err)
    24  	}
    25  	if err := p.state.ModifyVolumes(func(volumes map[string]*types.Volume) error {
    26  		volume, ok := volumes[volume.Id]
    27  		if !ok {
    28  			return errors.New("no record of "+volume.Id+" in the state", nil)
    29  		}
    30  		volume.Attachment = ""
    31  		return nil
    32  	}); err != nil {
    33  		return errors.New("modifying volume map in state", err)
    34  	}
    35  	return nil
    36  }