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

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