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

     1  package virtualbox
     2  
     3  import (
     4  	"github.com/emc-advanced-dev/pkg/errors"
     5  	"os"
     6  )
     7  
     8  func (p *VirtualboxProvider) DeleteVolume(id string, force bool) error {
     9  	volume, err := p.GetVolume(id)
    10  	if err != nil {
    11  		return errors.New("retrieving volume "+id, err)
    12  	}
    13  	if volume.Attachment != "" {
    14  		if force {
    15  			if err := p.DetachVolume(volume.Id); err != nil {
    16  				return errors.New("detaching volume for deletion", err)
    17  			} else {
    18  				return errors.New("volume "+volume.Id+" is attached to instance."+volume.Attachment+", try again with --force or detach volume first", err)
    19  			}
    20  		}
    21  	}
    22  	volumePath := getVolumePath(volume.Name)
    23  	err = os.Remove(volumePath)
    24  	if err != nil {
    25  		return errors.New("could not delete volume at path "+volumePath, err)
    26  	}
    27  	return p.state.RemoveVolume(volume)
    28  }