github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/gce/environ_policy.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package gce
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/constraints"
    10  	"github.com/juju/juju/environs/imagemetadata"
    11  	"github.com/juju/juju/environs/simplestreams"
    12  	"github.com/juju/juju/network"
    13  	"github.com/juju/juju/provider/common"
    14  )
    15  
    16  // PrecheckInstance verifies that the provided series and constraints
    17  // are valid for use in creating an instance in this environment.
    18  func (env *environ) PrecheckInstance(series string, cons constraints.Value, placement string) error {
    19  	if _, err := env.parsePlacement(placement); err != nil {
    20  		return errors.Trace(err)
    21  	}
    22  
    23  	if cons.HasInstanceType() {
    24  		if !checkInstanceType(cons) {
    25  			return errors.Errorf("invalid GCE instance type %q", *cons.InstanceType)
    26  		}
    27  	}
    28  
    29  	return nil
    30  }
    31  
    32  // SupportedArchitectures returns the image architectures which can
    33  // be hosted by this environment.
    34  func (env *environ) SupportedArchitectures() ([]string, error) {
    35  	env.archLock.Lock()
    36  	defer env.archLock.Unlock()
    37  
    38  	if env.supportedArchitectures != nil {
    39  		return env.supportedArchitectures, nil
    40  	}
    41  
    42  	archList, err := env.lookupArchitectures()
    43  	if err != nil {
    44  		return nil, errors.Trace(err)
    45  	}
    46  	env.supportedArchitectures = archList
    47  	return archList, nil
    48  }
    49  
    50  var supportedArchitectures = common.SupportedArchitectures
    51  
    52  func (env *environ) lookupArchitectures() ([]string, error) {
    53  	// Create a filter to get all images from our region and for the
    54  	// correct stream.
    55  	cloudSpec, err := env.Region()
    56  	if err != nil {
    57  		return nil, errors.Trace(err)
    58  	}
    59  	imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{
    60  		CloudSpec: cloudSpec,
    61  		Stream:    env.Config().ImageStream(),
    62  	})
    63  	archList, err := supportedArchitectures(env, imageConstraint)
    64  	return archList, errors.Trace(err)
    65  }
    66  
    67  var unsupportedConstraints = []string{
    68  	constraints.Tags,
    69  	constraints.VirtType,
    70  }
    71  
    72  // instanceTypeConstraints defines the fields defined on each of the
    73  // instance types.  See instancetypes.go.
    74  var instanceTypeConstraints = []string{
    75  	constraints.Arch, // Arches
    76  	constraints.CpuCores,
    77  	constraints.CpuPower,
    78  	constraints.Mem,
    79  	constraints.Container, // VirtType
    80  }
    81  
    82  // ConstraintsValidator returns a Validator value which is used to
    83  // validate and merge constraints.
    84  func (env *environ) ConstraintsValidator() (constraints.Validator, error) {
    85  	validator := constraints.NewValidator()
    86  
    87  	// conflicts
    88  
    89  	// TODO(ericsnow) Are these correct?
    90  	validator.RegisterConflicts(
    91  		[]string{constraints.InstanceType},
    92  		instanceTypeConstraints,
    93  	)
    94  
    95  	// unsupported
    96  
    97  	validator.RegisterUnsupported(unsupportedConstraints)
    98  
    99  	// vocab
   100  
   101  	supportedArches, err := env.SupportedArchitectures()
   102  	if err != nil {
   103  		return nil, errors.Trace(err)
   104  	}
   105  	validator.RegisterVocabulary(constraints.Arch, supportedArches)
   106  
   107  	instTypeNames := make([]string, len(allInstanceTypes))
   108  	for i, itype := range allInstanceTypes {
   109  		instTypeNames[i] = itype.Name
   110  	}
   111  	validator.RegisterVocabulary(constraints.InstanceType, instTypeNames)
   112  
   113  	validator.RegisterVocabulary(constraints.Container, []string{vtype})
   114  
   115  	return validator, nil
   116  }
   117  
   118  // environ provides SupportsUnitPlacement (a method of the
   119  // state.EnvironCapatability interface) by embedding
   120  // common.SupportsUnitPlacementPolicy.
   121  
   122  // SupportNetworks returns whether the environment has support to
   123  // specify networks for services and machines.
   124  func (env *environ) SupportNetworks() bool {
   125  	return false
   126  }
   127  
   128  // SupportAddressAllocation takes a network.Id and returns a bool
   129  // and an error. The bool indicates whether that network supports
   130  // static ip address allocation.
   131  func (env *environ) SupportAddressAllocation(netID network.Id) (bool, error) {
   132  	return false, nil
   133  }