github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  )
    17  
    18  // PrecheckInstance verifies that the provided series and constraints
    19  // are valid for use in creating an instance in this environment.
    20  func (env *environ) PrecheckInstance(series string, cons constraints.Value, placement string) error {
    21  	if placement != "" {
    22  		if _, err := env.parsePlacement(placement); err != nil {
    23  			return err
    24  		}
    25  	}
    26  	return nil
    27  }
    28  
    29  // supportedArchitectures returns the image architectures which can
    30  // be hosted by this environment.
    31  func (env *environ) allSupportedArchitectures() ([]string, error) {
    32  	env.archLock.Lock()
    33  	defer env.archLock.Unlock()
    34  
    35  	if env.supportedArchitectures != nil {
    36  		return env.supportedArchitectures, nil
    37  	}
    38  
    39  	archList, err := env.lookupArchitectures()
    40  	if err != nil {
    41  		return nil, errors.Trace(err)
    42  	}
    43  	env.supportedArchitectures = archList
    44  	return archList, nil
    45  }
    46  
    47  func (env *environ) lookupArchitectures() ([]string, error) {
    48  	// Create a filter to get all images for the
    49  	// correct stream.
    50  	imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{
    51  		Stream: env.Config().ImageStream(),
    52  	})
    53  	sources, err := environs.ImageMetadataSources(env)
    54  	if err != nil {
    55  		return nil, errors.Trace(err)
    56  	}
    57  	matchingImages, err := imageMetadataFetch(sources, imageConstraint)
    58  	if err != nil {
    59  		return nil, errors.Trace(err)
    60  	}
    61  
    62  	var arches = set.NewStrings()
    63  	for _, im := range matchingImages {
    64  		arches.Add(im.Arch)
    65  	}
    66  
    67  	return arches.Values(), nil
    68  }
    69  
    70  var unsupportedConstraints = []string{
    71  	constraints.Tags,
    72  	constraints.VirtType,
    73  }
    74  
    75  // ConstraintsValidator returns a Validator value which is used to
    76  // validate and merge constraints.
    77  func (env *environ) ConstraintsValidator() (constraints.Validator, error) {
    78  	validator := constraints.NewValidator()
    79  	validator.RegisterUnsupported(unsupportedConstraints)
    80  
    81  	supportedArches, err := env.allSupportedArchitectures()
    82  	if err != nil {
    83  		return nil, errors.Trace(err)
    84  	}
    85  	validator.RegisterVocabulary(constraints.Arch, supportedArches)
    86  	return validator, nil
    87  }
    88  
    89  // SupportNetworks returns whether the environment has support to
    90  // specify networks for applications and machines.
    91  func (env *environ) SupportNetworks() bool {
    92  	return false
    93  }