github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 !checkInstanceType(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  }
    38  
    39  // instanceTypeConstraints defines the fields defined on each of the
    40  // instance types.  See instancetypes.go.
    41  var instanceTypeConstraints = []string{
    42  	constraints.Arch, // Arches
    43  	constraints.Cores,
    44  	constraints.CpuPower,
    45  	constraints.Mem,
    46  	constraints.Container, // VirtType
    47  }
    48  
    49  // ConstraintsValidator returns a Validator value which is used to
    50  // validate and merge constraints.
    51  func (env *environ) ConstraintsValidator(ctx context.ProviderCallContext) (constraints.Validator, error) {
    52  	validator := constraints.NewValidator()
    53  
    54  	// conflicts
    55  
    56  	// TODO(ericsnow) Are these correct?
    57  	validator.RegisterConflicts(
    58  		[]string{constraints.InstanceType},
    59  		instanceTypeConstraints,
    60  	)
    61  
    62  	// unsupported
    63  
    64  	validator.RegisterUnsupported(unsupportedConstraints)
    65  
    66  	// vocab
    67  
    68  	instTypeNames := make([]string, len(allInstanceTypes))
    69  	for i, itype := range allInstanceTypes {
    70  		instTypeNames[i] = itype.Name
    71  	}
    72  	validator.RegisterVocabulary(constraints.InstanceType, instTypeNames)
    73  
    74  	validator.RegisterVocabulary(constraints.Container, []string{vtype})
    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  }