constraints

package
v0.0.0-...-0db00f2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2024 License: AGPL-3.0 Imports: 13 Imported by: 62

Documentation

Index

Constants

View Source
const (
	Arch      = "arch"
	Container = "container"

	Cores            = "cores"
	CpuPower         = "cpu-power"
	Mem              = "mem"
	RootDisk         = "root-disk"
	RootDiskSource   = "root-disk-source"
	Tags             = "tags"
	InstanceRole     = "instance-role"
	InstanceType     = "instance-type"
	Spaces           = "spaces"
	VirtType         = "virt-type"
	Zones            = "zones"
	AllocatePublicIP = "allocate-public-ip"
	ImageID          = "image-id"
)

The following constants list the supported constraint attribute names, as defined by the fields in the Value struct.

Variables

This section is empty.

Functions

func ArchOrDefault

func ArchOrDefault(cons Value, defaultCons *Value) string

ArchOrDefault returns the arch for the constraint if there is one, else it returns the default arch.

func IsEmpty

func IsEmpty(v *Value) bool

IsEmpty returns if the given constraints value has no constraints set

Types

type ConflictResolver

type ConflictResolver func(attrValues map[string]interface{}) error

type ConstraintsValue

type ConstraintsValue struct {
	Target *Value
}

Constraints implements gnuflag.Value for a Constraints.

func (ConstraintsValue) Set

func (v ConstraintsValue) Set(s string) error

func (ConstraintsValue) String

func (v ConstraintsValue) String() string

type InvalidVocabValueError

type InvalidVocabValueError struct {
	// contains filtered or unexported fields
}

InvalidVocabValueError represents an error that occurs when a validation constraint is violated. It provides details for the invalid inputs, closest valid values and all possible valid values.

func (*InvalidVocabValueError) Error

func (ve *InvalidVocabValueError) Error() string

type Validator

type Validator interface {

	// RegisterConflicts is used to define cross-constraint override behaviour.
	// The red and blue attribute lists contain attribute names which conflict
	// with those in the other list.
	// When two constraints conflict:
	//  it is an error to set both constraints in the same constraints Value.
	//  when a constraints Value overrides another which specifies a conflicting
	//   attribute, the attribute in the overridden Value is cleared.
	RegisterConflicts(reds, blues []string)

	// RegisterConflictResolver defines a resolver between two conflicting constraints.
	// When there is a registered conflict between two contraints, it can be resolved by
	// calling the resolver, if it returns a nil error, the conflict is considered resolved.
	RegisterConflictResolver(red, blue string, resolver ConflictResolver)

	// RegisterUnsupported records attributes which are not supported by a constraints Value.
	RegisterUnsupported(unsupported []string)

	// RegisterVocabulary records allowed values for the specified constraint attribute.
	// allowedValues is expected to be a slice/array but is declared as interface{} so
	// that vocabs of different types can be passed in.
	RegisterVocabulary(attributeName string, allowedValues interface{})

	// Validate returns an error if the given constraints are not valid, and also
	// any unsupported attributes.
	Validate(cons Value) ([]string, error)

	// Merge merges cons into consFallback, with any conflicting attributes from cons
	// overriding those from consFallback.
	Merge(consFallback, cons Value) (Value, error)

	// UpdateVocabulary merges new attribute values with existing values.
	// This method does not overwrite or delete values, i.e.
	//     if existing values are {a, b}
	//     and new values are {c, d},
	//     then the merge result would be {a, b, c, d}.
	UpdateVocabulary(attributeName string, newValues interface{})
}

Validator defines operations on constraints attributes which are used to ensure a constraints value is valid, as well as being able to handle overridden attributes.

func NewValidator

func NewValidator() Validator

NewValidator returns a new constraints Validator instance.

type Value

type Value struct {

	// Arch, if not nil or empty, indicates that a machine must run the named
	// architecture.
	Arch *string `json:"arch,omitempty" yaml:"arch,omitempty"`

	// Container, if not nil, indicates that a machine must be the specified container type.
	Container *instance.ContainerType `json:"container,omitempty" yaml:"container,omitempty"`

	// CpuCores, if not nil, indicates that a machine must have at least that
	// number of effective cores available.
	CpuCores *uint64 `json:"cores,omitempty" yaml:"cores,omitempty"`

	// CpuPower, if not nil, indicates that a machine must have at least that
	// amount of CPU power available, where 100 CpuPower is considered to be
	// equivalent to 1 Amazon ECU (or, roughly, a single 2007-era Xeon).
	CpuPower *uint64 `json:"cpu-power,omitempty" yaml:"cpu-power,omitempty"`

	// Mem, if not nil, indicates that a machine must have at least that many
	// megabytes of RAM.
	Mem *uint64 `json:"mem,omitempty" yaml:"mem,omitempty"`

	// RootDisk, if not nil, indicates that a machine must have at least
	// that many megabytes of disk space available in the root disk. In
	// providers where the root disk is configurable at instance startup
	// time, an instance with the specified amount of disk space in the OS
	// disk might be requested.
	RootDisk *uint64 `json:"root-disk,omitempty" yaml:"root-disk,omitempty"`

	// RootDiskSource, if specified, determines what storage the root
	// disk should be allocated from. This will be provider specific -
	// in the case of vSphere it identifies the datastore the root
	// disk file should be created in.
	RootDiskSource *string `json:"root-disk-source,omitempty" yaml:"root-disk-source,omitempty"`

	// Tags, if not nil, indicates tags that the machine must have applied to it.
	// An empty list is treated the same as a nil (unspecified) list, except an
	// empty list will override any default tags, where a nil list will not.
	Tags *[]string `json:"tags,omitempty" yaml:"tags,omitempty"`

	// InstanceRole, if not nil, indicates that the specified role/profile for
	// the given cloud should be used. Only valid for clouds which support
	// instance roles. Currently only for AWS with instance-profiles
	InstanceRole *string `json:"instance-role,omitempty" yaml:"instance-role,omitempty"`

	// InstanceType, if not nil, indicates that the specified cloud instance type
	// be used. Only valid for clouds which support instance types.
	InstanceType *string `json:"instance-type,omitempty" yaml:"instance-type,omitempty"`

	// Spaces, if not nil, holds a list of juju network spaces that
	// should be available (or not) on the machine. Positive and
	// negative values are accepted, and the difference is the latter
	// have a "^" prefix to the name.
	Spaces *[]string `json:"spaces,omitempty" yaml:"spaces,omitempty"`

	// VirtType, if not nil or empty, indicates that a machine must run the named
	// virtual type. Only valid for clouds with multi-hypervisor support.
	VirtType *string `json:"virt-type,omitempty" yaml:"virt-type,omitempty"`

	// Zones, if not nil, holds a list of availability zones limiting where
	// the machine can be located.
	Zones *[]string `json:"zones,omitempty" yaml:"zones,omitempty"`

	// AllocatePublicIP, if nil or true, signals that machines should be
	// created with a public IP address instead of a cloud local one.
	// The default behaviour if the value is not specified is to allocate
	// a public IP so that public cloud behaviour works out of the box.
	AllocatePublicIP *bool `json:"allocate-public-ip,omitempty" yaml:"allocate-public-ip,omitempty"`

	// ImageID, if not nil, indicates that a machine must use the specified
	// image. This is provider specific, and for the moment is only
	// implemented on MAAS clouds.
	ImageID *string `json:"image-id,omitempty" yaml:"image-id,omitempty"`
}

Value describes a user's requirements of the hardware on which units of an application will run. Constraints are used to choose an existing machine onto which a unit will be deployed, or to provision a new machine if no existing one satisfies the requirements.

func Merge

func Merge(values ...Value) (Value, error)

Merge returns the effective constraints after merging any given existing values.

func MustParse

func MustParse(args ...string) Value

MustParse constructs a constraints.Value from the supplied arguments, as Parse, but panics on failure.

func Parse

func Parse(args ...string) (Value, error)

Parse constructs a constraints.Value from the supplied arguments, each of which must contain only spaces and name=value pairs. If any name is specified more than once, an error is returned.

func ParseWithAliases

func ParseWithAliases(args ...string) (cons Value, aliases map[string]string, err error)

ParseWithAliases constructs a constraints.Value from the supplied arguments, each of which must contain only spaces and name=value pairs. If any name is specified more than once, an error is returned. The aliases map returned contains a map of aliases used, and their canonical values.

func (*Value) ExcludeSpaces

func (v *Value) ExcludeSpaces() []string

ExcludeSpaces returns a list of space IDs to exclude when starting a machine, if specified. They are given in the spaces constraint with a "^" prefix to the id, which is stripped before returning.

func (Value) GoString

func (v Value) GoString() string

GoString allows printing a constraints.Value nicely with the fmt package, especially when nested inside other types.

func (*Value) HasAllocatePublicIP

func (v *Value) HasAllocatePublicIP() bool

HasAllocatePublicIP returns whether the allocate-public-ip constraint was specified.

func (*Value) HasArch

func (v *Value) HasArch() bool

HasArch returns true if the constraints.Value specifies an architecture.

func (*Value) HasContainer

func (v *Value) HasContainer() bool

HasContainer returns true if the constraints.Value specifies a container.

func (*Value) HasCpuCores

func (v *Value) HasCpuCores() bool

HasCpuCores returns true if the constraints.Value specifies a minimum number of CPU cores.

func (*Value) HasCpuPower

func (v *Value) HasCpuPower() bool

HasCpuPower returns true if the constraints.Value specifies a minimum amount of CPU power.

func (*Value) HasImageID

func (v *Value) HasImageID() bool

HasImageID returns true if the constraints.Value specifies an image-id.

func (*Value) HasInstanceRole

func (v *Value) HasInstanceRole() bool

HasInstanceRole returns true if the constraints.Value specifies an instance role.

func (*Value) HasInstanceType

func (v *Value) HasInstanceType() bool

HasInstanceType returns true if the constraints.Value specifies an instance type.

func (*Value) HasMem

func (v *Value) HasMem() bool

HasMem returns true if the constraints.Value specifies a minimum amount of memory.

func (*Value) HasRootDisk

func (v *Value) HasRootDisk() bool

HasRootDisk returns true if the constraints.Value specifies a RootDisk size.

func (*Value) HasRootDiskSource

func (v *Value) HasRootDiskSource() bool

HasRootDiskSource returns true if the constraints.Value specifies a source for its root disk.

func (*Value) HasSpaces

func (v *Value) HasSpaces() bool

HasSpaces returns whether any spaces constraints were specified.

func (*Value) HasVirtType

func (v *Value) HasVirtType() bool

HasVirtType returns true if the constraints.Value specifies an virtual type.

func (*Value) HasZones

func (v *Value) HasZones() bool

HasZones returns whether any zone constraints were specified.

func (*Value) IncludeSpaces

func (v *Value) IncludeSpaces() []string

IncludeSpaces returns a list of space IDs to include when starting a machine, if specified.

func (Value) String

func (v Value) String() string

String expresses a constraints.Value in the language in which it was specified.

func (*Value) UnmarshalYAML

func (v *Value) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is required to unmarshal a constraints.Value object to ensure the container attribute is correctly handled when it is empty. Because ContainerType is an alias for string, Go's reflect logic used in the YAML decode determines that *string and *ContainerType are not assignable so the container value of "" in the YAML is ignored.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL