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