github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/vsphere/environ_policy.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build !gccgo
     5  
     6  package vsphere
     7  
     8  import (
     9  	"github.com/juju/errors"
    10  	"github.com/juju/utils/set"
    11  
    12  	"github.com/juju/juju/constraints"
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/imagemetadata"
    15  	"github.com/juju/juju/environs/simplestreams"
    16  	"github.com/juju/juju/network"
    17  )
    18  
    19  // PrecheckInstance verifies that the provided series and constraints
    20  // are valid for use in creating an instance in this environment.
    21  func (env *environ) PrecheckInstance(series string, cons constraints.Value, placement string) error {
    22  	if placement != "" {
    23  		if _, err := env.parsePlacement(placement); err != nil {
    24  			return err
    25  		}
    26  	}
    27  	return nil
    28  }
    29  
    30  // SupportedArchitectures returns the image architectures which can
    31  // be hosted by this environment.
    32  func (env *environ) SupportedArchitectures() ([]string, error) {
    33  	env.archLock.Lock()
    34  	defer env.archLock.Unlock()
    35  
    36  	if env.supportedArchitectures != nil {
    37  		return env.supportedArchitectures, nil
    38  	}
    39  
    40  	archList, err := env.lookupArchitectures()
    41  	if err != nil {
    42  		return nil, errors.Trace(err)
    43  	}
    44  	env.supportedArchitectures = archList
    45  	return archList, nil
    46  }
    47  
    48  func (env *environ) lookupArchitectures() ([]string, error) {
    49  	// Create a filter to get all images for the
    50  	// correct stream.
    51  	imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{
    52  		Stream: env.Config().ImageStream(),
    53  	})
    54  	sources, err := environs.ImageMetadataSources(env)
    55  	if err != nil {
    56  		return nil, errors.Trace(err)
    57  	}
    58  	matchingImages, err := imageMetadataFetch(sources, imageConstraint)
    59  	if err != nil {
    60  		return nil, errors.Trace(err)
    61  	}
    62  
    63  	var arches = set.NewStrings()
    64  	for _, im := range matchingImages {
    65  		arches.Add(im.Arch)
    66  	}
    67  
    68  	return arches.Values(), nil
    69  }
    70  
    71  var unsupportedConstraints = []string{
    72  	constraints.Tags,
    73  	constraints.Networks,
    74  }
    75  
    76  // instanceTypeConstraints defines the fields defined on each of the
    77  // instance types.  See instancetypes.go.
    78  var instanceConstraints = []string{
    79  	constraints.Arch, // Arches
    80  	constraints.CpuCores,
    81  	constraints.CpuPower,
    82  	constraints.Mem,
    83  }
    84  
    85  // ConstraintsValidator returns a Validator value which is used to
    86  // validate and merge constraints.
    87  func (env *environ) ConstraintsValidator() (constraints.Validator, error) {
    88  	validator := constraints.NewValidator()
    89  
    90  	// unsupported
    91  
    92  	validator.RegisterUnsupported(unsupportedConstraints)
    93  
    94  	// vocab
    95  
    96  	supportedArches, err := env.SupportedArchitectures()
    97  	if err != nil {
    98  		return nil, errors.Trace(err)
    99  	}
   100  	validator.RegisterVocabulary(constraints.Arch, supportedArches)
   101  
   102  	return validator, nil
   103  }
   104  
   105  // SupportsUnitPlacement implement via common.SupportsUnitPlacementPolicy
   106  
   107  // SupportNetworks returns whether the environment has support to
   108  // specify networks for services and machines.
   109  func (env *environ) SupportNetworks() bool {
   110  	return false
   111  }
   112  
   113  // SupportAddressAllocation takes a network.Id and returns a bool
   114  // and an error. The bool indicates whether that network supports
   115  // static ip address allocation.
   116  func (env *environ) SupportAddressAllocation(netID network.Id) (bool, error) {
   117  	return false, nil
   118  }