github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  )
    10  
    11  // findInstanceSpec returns an image and instance type satisfying the constraint.
    12  // The instance type comes from querying the flavors supported by the deployment.
    13  func findInstanceSpec(
    14  	e *Environ,
    15  	ic *instances.InstanceConstraint,
    16  	imageMetadata []*imagemetadata.ImageMetadata,
    17  ) (*instances.InstanceSpec, error) {
    18  	// First construct all available instance types from the supported flavors.
    19  	nova := e.nova()
    20  	flavors, err := nova.ListFlavorsDetail()
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	// Not all needed information is available in flavors,
    25  	// for e.g. architectures or virtualisation types.
    26  	// For these properties, we assume that all instance types support
    27  	// all values.
    28  	allInstanceTypes := []instances.InstanceType{}
    29  	for _, flavor := range flavors {
    30  		instanceType := instances.InstanceType{
    31  			Id:       flavor.Id,
    32  			Name:     flavor.Name,
    33  			Arches:   ic.Arches,
    34  			Mem:      uint64(flavor.RAM),
    35  			CpuCores: uint64(flavor.VCPUs),
    36  			RootDisk: uint64(flavor.Disk * 1024),
    37  			// tags not currently supported on openstack
    38  		}
    39  		if ic.Constraints.HasVirtType() {
    40  			// Instance Type virtual type depends on the virtual type of the selected image, i.e.
    41  			// picking an image with a virt type gives a machine with this virt type.
    42  			instanceType.VirtType = ic.Constraints.VirtType
    43  		}
    44  		allInstanceTypes = append(allInstanceTypes, instanceType)
    45  	}
    46  
    47  	images := instances.ImageMetadataToImages(imageMetadata)
    48  	spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	// If instance constraints did not have a virtualisation type,
    54  	// but image metadata did, we will have an instance type
    55  	// with virtualisation type of an image.
    56  	if !ic.Constraints.HasVirtType() && spec.Image.VirtType != "" {
    57  		spec.InstanceType.VirtType = &spec.Image.VirtType
    58  	}
    59  	return spec, nil
    60  }