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

     1  package qemu
     2  
     3  import (
     4  	"github.com/emc-advanced-dev/pkg/errors"
     5  	"os"
     6  )
     7  
     8  func (p *QemuProvider) DeleteVolume(id string, force bool) error {
     9  
    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  			}
    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  	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  }