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

     1  package photon
     2  
     3  import (
     4  	"github.com/emc-advanced-dev/pkg/errors"
     5  	"github.com/solo-io/unik/pkg/types"
     6  )
     7  
     8  func (p *PhotonProvider) DeleteInstance(id string, force bool) error {
     9  	instance, err := p.GetInstance(id)
    10  	if err != nil {
    11  		return errors.New("retrieving instance "+id, err)
    12  	}
    13  	if instance.State == types.InstanceState_Running {
    14  		if !force {
    15  			return errors.New("instance "+instance.Id+"is still running. try again with --force or power off instance first", err)
    16  		} else {
    17  			p.StopInstance(instance.Id)
    18  		}
    19  	}
    20  
    21  	task, err := p.client.VMs.Delete(instance.Id)
    22  	if err != nil {
    23  		return errors.New("Delete vm", err)
    24  	}
    25  
    26  	task, err = p.waitForTaskSuccess(task)
    27  	if err != nil {
    28  		return errors.New("Delete vm", err)
    29  	}
    30  	return p.state.RemoveInstance(instance)
    31  }