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

     1  package gcloud
     2  
     3  import (
     4  	"github.com/emc-advanced-dev/pkg/errors"
     5  )
     6  
     7  func (p *GcloudProvider) DeleteImage(id string, force bool) error {
     8  	image, err := p.GetImage(id)
     9  	if err != nil {
    10  		return errors.New("retrieving image", err)
    11  	}
    12  	instances, err := p.ListInstances()
    13  	if err != nil {
    14  		return errors.New("retrieving list of instances", err)
    15  	}
    16  	for _, instance := range instances {
    17  		if instance.ImageId == image.Id {
    18  			if !force {
    19  				return errors.New("instance "+instance.Id+" found which uses image "+image.Id+"; try again with force=true", nil)
    20  			} else {
    21  				err = p.DeleteInstance(instance.Id, true)
    22  				if err != nil {
    23  					return errors.New("failed to delete instance "+instance.Id+" which is using image "+image.Id, err)
    24  				}
    25  			}
    26  		}
    27  	}
    28  
    29  	if _, err := p.compute().Images.Delete(p.config.ProjectID, image.Name).Do(); err != nil {
    30  		return errors.New("deleting image from gcloud", err)
    31  	}
    32  	return p.state.RemoveImage(image)
    33  }