validations_libs.validation module

class validations_libs.validation.Validation(validation_path)[source]

Bases: object

An object for encapsulating a validation

Each validation is an Ansible playbook. Each playbook have some metadata. Here is what a minimal validation would look like:

- hosts: webserver
  vars:
    metadata:
      name: Hello World
      description: This validation prints Hello World!
  roles:
  - hello_world

As shown here, the validation playbook requires three top-level directives:

hosts, vars -> metadata and roles

hosts specify which nodes to run the validation on.

The vars section serves for storing variables that are going to be available to the Ansible playbook. The validations API uses the metadata section to read validation’s name and description. These values are then reported by the API.

The validations can be grouped together by specifying a groups, a categories and a products metadata. groups are the deployment stage the validations should run on, categories are the technical classification for the validations and products are the specific validations which should be executed against a specific product.

Groups, Categories and Products function similar to tags and a validation can thus be part of many groups and many categories.

Here is an example:

- hosts: webserver
  vars:
    metadata:
      name: Hello World
      description: This validation prints Hello World!
      groups:
        - pre-deployment
        - hardware
      categories:
        - os
        - networking
        - storage
        - security
      products:
        - product1
        - product2
  roles:
  - hello_world
property categories

Get the validation list of categories

Returns:

A list of categories for the validation

Return type:

list or None if no metadata has been found

Raise:

A NameError exception if no metadata has been found in the playbook

Example:

>>> pl = '/foo/bar/val.yaml'
>>> val = Validation(pl)
>>> print(val.categories)
['category1', 'category2']
property get_data

Get the full contents of a validation playbook

Returns:

The full content of the playbook

Return type:

dict

Example:

>>> pl = '/foo/bar/val.yaml'
>>> val = Validation(pl)
>>> print(val.get_data)
{'gather_facts': True,
 'hosts': 'all',
 'roles': ['val_role'],
 'vars': {'metadata': {'description': 'description of val ',
                       'groups': ['group1', 'group2'],
                       'categories': ['category1', 'category2'],
                       'products': ['product1', 'product2'],
                       'name': 'validation one'},
                       'var_name1': 'value1'}}
property get_formated_data

Get basic information from a validation for output display

Returns:

Basic information of a validation including the Description, the list of ‘Categories’, the list of Groups, the ID and the Name.

Return type:

dict

Raise:

A NameError exception if no metadata has been found in the playbook

Example:

>>> pl = '/foo/bar/val.yaml'
>>> val = Validation(pl)
>>> print(val.get_formated_data)
{'Categories': ['category1', 'category2'],
 'Products': ['product1', 'product2'],
 'Description': 'description of val',
 'Groups': ['group1', 'group2'],
 'ID': 'val',
 'Name': 'validation one',
 'path': '/tmp/foo/'}
property get_id

Get the validation id

Returns:

The validation id

Return type:

string

Example:

>>> pl = '/foo/bar/check-cpu.yaml'
>>> val = Validation(pl)
>>> print(val.id)
'check-cpu'
property get_metadata

Get the metadata of a validation

Returns:

The validation metadata

Return type:

dict or None if no metadata has been found

Raise:

A NameError exception if no metadata has been found in the playbook

Example:

>>> pl = '/foo/bar/val1.yaml'
>>> val = Validation(pl)
>>> print(val.get_metadata)
{'description': 'Val1 desc.',
 'groups': ['group1', 'group2'],
 'categories': ['category1', 'category2'],
 'products': ['product1', 'product2'],
 'id': 'val1',
 'name': 'The validation val1's name',
 'path': '/tmp/foo/'}
property get_ordered_dict

Get the full ordered content of a validation

Returns:

An OrderedDict with the full data of a validation

Return type:

OrderedDict

property get_vars

Get only the variables of a validation

Returns:

All the variables belonging to a validation

Return type:

dict or None if no metadata has been found

Raise:

A NameError exception if no metadata has been found in the playbook

Example:

>>> pl = '/foo/bar/val.yaml'
>>> val = Validation(pl)
>>> print(val.get_vars)
{'var_name1': 'value1',
 'var_name2': 'value2'}
property groups

Get the validation list of groups

Returns:

A list of groups for the validation

Return type:

list or None if no metadata has been found

Raise:

A NameError exception if no metadata has been found in the playbook

Example:

>>> pl = '/foo/bar/val.yaml'
>>> val = Validation(pl)
>>> print(val.groups)
['group1', 'group2']
property has_metadata_dict

Check the presence of the metadata dictionary

- hosts: webserver
  vars:
    metadata:             <====
      name: hello world
      description: this validation prints hello world!
      groups:
        - pre-deployment
        - hardware
      categories:
        - os
        - networking
        - storage
        - security
      products:
        - product1
        - product2
  roles:
  - hello_world
Returns:

true if vars and metadata are found, false if not.

Return type:

boolean

property has_vars_dict

Check the presence of the vars dictionary

- hosts: webserver
  vars:                    <====
    metadata:
      name: hello world
      description: this validation prints hello world!
      groups:
        - pre-deployment
        - hardware
      categories:
        - os
        - networking
        - storage
        - security
      products:
        - product1
        - product2
  roles:
  - hello_world
Returns:

true if vars is found, false if not.

Return type:

boolean

property products

Get the validation list of products

Returns:

A list of products for the validation

Return type:

list or None if no metadata has been found

Raise:

A NameError exception if no metadata has been found in the playbook

Example:

>>> pl = '/foo/bar/val.yaml'
>>> val = Validation(pl)
>>> print(val.products)
['product1', 'product2']