github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/core/constraints" 10 "github.com/juju/juju/environs" 11 "github.com/juju/juju/environs/context" 12 ) 13 14 // PrecheckInstance verifies that the provided series and constraints 15 // are valid for use in creating an instance in this environment. 16 func (env *environ) PrecheckInstance(ctx context.ProviderCallContext, args environs.PrecheckInstanceParams) error { 17 volumeAttachmentsZone, err := volumeAttachmentsZone(args.VolumeAttachments) 18 if err != nil { 19 return errors.Trace(err) 20 } 21 if _, err := env.instancePlacementZone(ctx, args.Placement, volumeAttachmentsZone); err != nil { 22 return errors.Trace(err) 23 } 24 25 if args.Constraints.HasInstanceType() { 26 if !env.checkInstanceType(ctx, args.Constraints) { 27 return errors.Errorf("invalid GCE instance type %q", *args.Constraints.InstanceType) 28 } 29 } 30 31 return nil 32 } 33 34 var unsupportedConstraints = []string{ 35 constraints.Tags, 36 constraints.VirtType, 37 constraints.ImageID, 38 } 39 40 // instanceTypeConstraints defines the fields defined on each of the 41 // instance types. See instancetypes.go. 42 var instanceTypeConstraints = []string{ 43 // TODO: move to a dynamic conflict for arch when gce supports more than amd64 44 //constraints.Arch, // Arches 45 constraints.Cores, 46 constraints.CpuPower, 47 constraints.Mem, 48 constraints.Container, // VirtType 49 } 50 51 // ConstraintsValidator returns a Validator value which is used to 52 // validate and merge constraints. 53 func (env *environ) ConstraintsValidator(ctx context.ProviderCallContext) (constraints.Validator, error) { 54 validator := constraints.NewValidator() 55 56 validator.RegisterConflicts( 57 []string{constraints.InstanceType}, 58 instanceTypeConstraints, 59 ) 60 61 validator.RegisterUnsupported(unsupportedConstraints) 62 63 // vocab 64 65 instTypesAndCosts, err := env.InstanceTypes(ctx, constraints.Value{}) 66 if err != nil { 67 return nil, errors.Trace(err) 68 } 69 instTypeNames := make([]string, len(instTypesAndCosts.InstanceTypes)) 70 for i, itype := range instTypesAndCosts.InstanceTypes { 71 instTypeNames[i] = itype.Name 72 } 73 validator.RegisterVocabulary(constraints.InstanceType, instTypeNames) 74 validator.RegisterVocabulary(constraints.Container, []string{virtType}) 75 76 return validator, nil 77 } 78 79 // SupportNetworks returns whether the environment has support to 80 // specify networks for applications and machines. 81 func (env *environ) SupportNetworks(ctx context.ProviderCallContext) bool { 82 return false 83 }