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

     1  package xen
     2  
     3  import (
     4  	"github.com/emc-advanced-dev/pkg/errors"
     5  	"github.com/solo-io/unik/pkg/types"
     6  	"os"
     7  )
     8  
     9  func (p *XenProvider) DeleteInstance(id string, force bool) error {
    10  	instance, err := p.GetInstance(id)
    11  	if err != nil {
    12  		return errors.New("retrieving instance "+id, err)
    13  	}
    14  
    15  	if err := p.client.DestroyVm(instance.Name); err != nil {
    16  		return errors.New("xen api destroy call", err)
    17  	}
    18  
    19  	if err := os.RemoveAll(getInstanceDir(instance.Name)); err != nil {
    20  		return errors.New("deleting instance dir", err)
    21  	}
    22  
    23  	volumesToDetach := []*types.Volume{}
    24  	volumes, err := p.ListVolumes()
    25  	if err != nil {
    26  		return errors.New("getting volume list", err)
    27  	}
    28  	for _, volume := range volumes {
    29  		if volume.Attachment == instance.Id {
    30  			volumesToDetach = append(volumesToDetach, volume)
    31  		}
    32  	}
    33  	if err := p.state.ModifyInstances(func(instances map[string]*types.Instance) error {
    34  		delete(instances, instance.Id)
    35  		return nil
    36  	}); err != nil {
    37  		return errors.New("modifying image map in state", err)
    38  	}
    39  	return p.state.RemoveInstance(instance)
    40  }