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

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