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

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