Variables

Variables types

Armonic provides base classes for defining your own variables. They all inherit from armonic.variable.Variable. No dict like variable is provided since all variables have names.

class armonic.variable.VString(name, default=None, required=True, from_xpath=None, modifier='%s', **extra)[source]

Variable of type string

pattern = None

Validate the value again a regexp

pattern_error = None

Error message if the value doesn’t match the regexp

class armonic.variable.VInt(name, default=None, required=True, from_xpath=None, **extra)[source]

Variable of type int.

max_val = None

Maximum value

min_val = None

Minimum value

class armonic.variable.VFloat(name, default=None, required=True, from_xpath=None, **extra)[source]

Variable of type float.

class armonic.variable.VBool(name, default=None, required=True, from_xpath=None, **extra)[source]

Variable of type boolean.

class armonic.variable.VList(name, inner, default=None, required=True, from_xpath=None, **extra)[source]

VList provide a list container for Variable instances.

Running the validation on VList will recursively run the validation for all contained instances.

Parameters:
  • name (str) – variable name
  • inner (all instances of Variable) – the type of variable used in the list
  • default (list) – default value
  • required (bool) – required variable
  • **extra

    extra variable fields

Predefined variables

class armonic.variable.Host(name, default=None, required=True, from_xpath=None, modifier='%s', **extra)[source]

Variable for hosts.

Validate that the value is an IP or a hostname

class armonic.variable.Hostname(name, default=None, required=True, from_xpath=None, modifier='%s', **extra)[source]

Variable for hostnames.

Validate that the value is a hostname

class armonic.variable.Port(name, default=None, required=True, from_xpath=None, **extra)[source]

Variable for port numbers.

Validate that the value is between 0 and 65535

Custom validation

To specifiy a custom validation method subclass the variable type of your choice and implement a validate function. If the validation fails you must raise armonic.common.ValidationError with an error message:

from armonic.common import ValidationError
from armonic.variable import VString

class CustomVar(VString):

    def validate(self):
        if not self.value in ('foo', 'bar'):
            raise ValidationError('%s value should be foo or bar' % self.name)
        return True

Extra variable info

If you wish to provide additional context to the variable you can define extra arguments in the variable constructor:

>>> from armonic.variable import VString
>>> var = VString('username', label="Username", help="Type your username")
>>> print var.to_primitive()
{'default': None,
 'error': None,
 'extra': {'help': 'Type your username', 'label': 'Username'},
 'from_xpath': None,
 'name': 'username',
 'required': True,
 'type': 'str',
 'value': None,
 'xpath': None}

These extra infos can be used in clients that consume the armonic.lifecycle.Lifecycle API.