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

     1  package gcloud
     2  
     3  import (
     4  	"github.com/emc-advanced-dev/pkg/errors"
     5  	"github.com/solo-io/unik/pkg/types"
     6  )
     7  
     8  func (p *GcloudProvider) 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 && !force {
    14  		return errors.New("instance "+instance.Id+"is still running. try again with --force or power off instance first", err)
    15  	}
    16  	_, err = p.compute().Instances.Delete(p.config.ProjectID, p.config.Zone, instance.Name).Do()
    17  	if err != nil {
    18  		return errors.New("failed to terminate instance "+instance.Id, err)
    19  	}
    20  	return p.state.RemoveInstance(instance)
    21  }