github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/openstack/image.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package openstack
     5  
     6  import (
     7  	"github.com/juju/juju/environs"
     8  	"github.com/juju/juju/environs/imagemetadata"
     9  	"github.com/juju/juju/environs/instances"
    10  	"github.com/juju/juju/environs/simplestreams"
    11  )
    12  
    13  // findInstanceSpec returns an image and instance type satisfying the constraint.
    14  // The instance type comes from querying the flavors supported by the deployment.
    15  func findInstanceSpec(e *environ, ic *instances.InstanceConstraint) (*instances.InstanceSpec, error) {
    16  	// first construct all available instance types from the supported flavors.
    17  	nova := e.nova()
    18  	flavors, err := nova.ListFlavorsDetail()
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	allInstanceTypes := []instances.InstanceType{}
    23  	for _, flavor := range flavors {
    24  		instanceType := instances.InstanceType{
    25  			Id:       flavor.Id,
    26  			Name:     flavor.Name,
    27  			Arches:   ic.Arches,
    28  			Mem:      uint64(flavor.RAM),
    29  			CpuCores: uint64(flavor.VCPUs),
    30  			RootDisk: uint64(flavor.Disk * 1024),
    31  			// tags not currently supported on openstack
    32  		}
    33  		allInstanceTypes = append(allInstanceTypes, instanceType)
    34  	}
    35  
    36  	imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{
    37  		CloudSpec: simplestreams.CloudSpec{ic.Region, e.ecfg().authURL()},
    38  		Series:    []string{ic.Series},
    39  		Arches:    ic.Arches,
    40  		Stream:    e.Config().ImageStream(),
    41  	})
    42  	sources, err := environs.ImageMetadataSources(e)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	// TODO (wallyworld): use an env parameter (default true) to mandate use of only signed image metadata.
    47  	matchingImages, _, err := imagemetadata.Fetch(sources, imageConstraint, false)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	images := instances.ImageMetadataToImages(matchingImages)
    52  	spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return spec, nil
    57  }