github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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 // TODO(dimitern: Replace Networks with Spaces in a follow-up. 70 constraints.Networks, 71 } 72 73 // instanceTypeConstraints defines the fields defined on each of the 74 // instance types. See instancetypes.go. 75 var instanceTypeConstraints = []string{ 76 constraints.Arch, // Arches 77 constraints.CpuCores, 78 constraints.CpuPower, 79 constraints.Mem, 80 constraints.Container, // VirtType 81 } 82 83 // ConstraintsValidator returns a Validator value which is used to 84 // validate and merge constraints. 85 func (env *environ) ConstraintsValidator() (constraints.Validator, error) { 86 validator := constraints.NewValidator() 87 88 // conflicts 89 90 // TODO(ericsnow) Are these correct? 91 validator.RegisterConflicts( 92 []string{constraints.InstanceType}, 93 instanceTypeConstraints, 94 ) 95 96 // unsupported 97 98 validator.RegisterUnsupported(unsupportedConstraints) 99 100 // vocab 101 102 supportedArches, err := env.SupportedArchitectures() 103 if err != nil { 104 return nil, errors.Trace(err) 105 } 106 validator.RegisterVocabulary(constraints.Arch, supportedArches) 107 108 instTypeNames := make([]string, len(allInstanceTypes)) 109 for i, itype := range allInstanceTypes { 110 instTypeNames[i] = itype.Name 111 } 112 validator.RegisterVocabulary(constraints.InstanceType, instTypeNames) 113 114 validator.RegisterVocabulary(constraints.Container, []string{vtype}) 115 116 return validator, nil 117 } 118 119 // environ provides SupportsUnitPlacement (a method of the 120 // state.EnvironCapatability interface) by embedding 121 // common.SupportsUnitPlacementPolicy. 122 123 // SupportNetworks returns whether the environment has support to 124 // specify networks for services and machines. 125 func (env *environ) SupportNetworks() bool { 126 return false 127 } 128 129 // SupportAddressAllocation takes a network.Id and returns a bool 130 // and an error. The bool indicates whether that network supports 131 // static ip address allocation. 132 func (env *environ) SupportAddressAllocation(netID network.Id) (bool, error) { 133 return false, nil 134 }