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

     1  package openstack
     2  
     3  import (
     4  	"github.com/solo-io/unik/pkg/types"
     5  	"github.com/rackspace/gophercloud"
     6  	"github.com/rackspace/gophercloud/openstack/imageservice/v2/images"
     7  	"github.com/rackspace/gophercloud/pagination"
     8  )
     9  
    10  func (p *OpenstackProvider) ListImages() ([]*types.Image, error) {
    11  	// Return immediately if no image is managed by unik.
    12  	managedImages := p.state.GetImages()
    13  	if len(managedImages) < 1 {
    14  		return []*types.Image{}, nil
    15  	}
    16  
    17  	clientGlance, err := p.newClientGlance()
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	return fetchImages(clientGlance, managedImages)
    23  }
    24  
    25  func fetchImages(clientGlance *gophercloud.ServiceClient, managedImages map[string]*types.Image) ([]*types.Image, error) {
    26  	result := []*types.Image{}
    27  
    28  	pager := images.List(clientGlance, nil)
    29  	pager.EachPage(func(page pagination.Page) (bool, error) {
    30  		imageList, err := images.ExtractImages(page)
    31  		if err != nil {
    32  			return false, err
    33  		}
    34  
    35  		for _, i := range imageList {
    36  			// Filter out images that unik is not aware of.
    37  			image, ok := managedImages[i.ID]
    38  			if !ok {
    39  				continue
    40  			}
    41  			result = append(result, image)
    42  		}
    43  
    44  		return true, nil
    45  	})
    46  
    47  	return result, nil
    48  }