1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/lib/ansible/modules/cloud/ovirt
maorlipchuk 81caa837a4 Use data center UUID/name in storage domains module (#28659)
The ansible action ovirt_storage_domains obligates a data center
name of the attached storage domain as part of its action's arguments,
so it will get the attached_sd_service as part of the functionality
of changing the storage domain status (to maintenance for example).

On the other hand, ovirt_storage_domains_facts retrieves a storage
domain entity with information about the data center which the storage
domain is attached to as a UUID identifier (without name).
So for the user to use that storage domain, fetched from the facts
module, one will have to fetch the DC entity to get the name.

We could use the search which is used today using:
   service.list(search=...)
but that type of search does not support search by Guid.
Therefor this patch provides the ability to use ovirt_storage_domains
action with state change using also a DC UUID instead of a DC name.
2017-08-29 09:50:38 -04:00
..
__init__.py Relocating extras into lib/ansible/modules/ after merge 2016-12-08 11:35:18 -05:00
_ovirt_affinity_groups.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_affinity_labels.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_affinity_labels_facts.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_clusters.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_clusters_facts.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_datacenters.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_datacenters_facts.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_disks.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_external_providers.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_external_providers_facts.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_groups.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
_ovirt_groups_facts.py Rename modules from plural to singular (#27185) 2017-08-14 16:35:15 -04:00
ovirt_affinity_group.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_affinity_label.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_affinity_label_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_auth.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_cluster.py cloud/ovirt: set mac pools via ovirt_clusters (#28120) 2017-08-23 08:45:25 -04:00
ovirt_cluster_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_datacenter.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_datacenter_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_disk.py ovirt_disks: added option to export disk to glance (#27959) 2017-08-23 08:46:51 -04:00
ovirt_external_provider.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_external_provider_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_group.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_group_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_host_networks.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_host_pm.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_host_storage_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_hosts.py ovirt_hosts: Add upgrade_check action (#28437) 2017-08-28 11:15:05 -04:00
ovirt_hosts_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_mac_pools.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_networks.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_networks_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_nics.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_nics_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_permissions.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_permissions_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_quotas.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_quotas_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_scheduling_policies_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_snapshots.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_snapshots_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_storage_connections.py Fix trailing space in ec2_vol example, fix 'the the' typos (#28440) 2017-08-19 23:00:51 +02:00
ovirt_storage_domains.py Use data center UUID/name in storage domains module (#28659) 2017-08-29 09:50:38 -04:00
ovirt_storage_domains_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_storage_templates_facts.py [oVirt] Update/Add functionalities for import storage domain (#26568) 2017-08-23 08:44:02 -04:00
ovirt_storage_vms_facts.py [oVirt] Update/Add functionalities for import storage domain (#26568) 2017-08-23 08:44:02 -04:00
ovirt_tags.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_tags_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_templates.py [oVirt] Update/Add functionalities for import storage domain (#26568) 2017-08-23 08:44:02 -04:00
ovirt_templates_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_users.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_users_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_vmpools.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_vmpools_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
ovirt_vms.py cloud: ovirt: present state shouldn't change state if not needed (#28214) 2017-08-28 08:20:55 -04:00
ovirt_vms_facts.py Update metadata to 1.1 2017-08-15 23:12:08 -07:00
README.rst cloud: ovirt: add developer README.rst (#22685) 2017-08-28 20:58:30 -04:00

oVirt Ansible Modules
=====================

This is a set of modules for interacting with oVirt/RHV. This document
serves as developer coding guidelines for creating oVIRT/RHV modules.

Naming
------

-  All modules should start with an ``ovirt_`` prefix.
-  All modules should be named after the resource it manages in singular
   form.
-  All modules that gather facts should have a ``_facts``
   suffix.

Interface
---------

-  Every module should return the ID of the resource it manages.
-  Every module should return the dictionary of the resource it manages.
-  Never change the name of the parameter, as we guarantee backward
   compatibility. Use aliases instead.
-  If a parameter can't achieve idempotency for any reason, please
   document it.

Interoperability
----------------

-  All modules should work against all minor versions of
   version 4 of the API. Version 3 of the API is not supported.

Libraries
---------

-  All modules should use ``ovirt_full_argument_spec`` or
   ``ovirt_facts_full_argument_spec`` to pick up the standard input (such
   as auth and ``fetch_nested``).
-  All modules should use ``extends_documentation_fragment``: ovirt to go
   along with ``ovirt_full_argument_spec``.
-  All facts modules should use ``extends_documentation_fragment``:
   ``ovirt_facts`` to go along with ``ovirt_facts_full_argument_spec``.
-  Functions that are common to all modules should be implemeneted in the 
   ``module_utils/ovirt.py`` file, so they can be reused.
-  Python SDK version 4 must be used.

New module development
----------------------

Please read 
`link <http://docs.ansible.com/ansible/dev_guide/developing_modules.html#how-to-develop-a-module>`__,
first to know what common properties, functions and features every module must
have.

In order to achieve idempotency of oVirt entity attributes, a helper class
was created. The first thing you need to do is to extend this class and override a few
methods:

.. code:: python

    try:
        import ovirtsdk4.types as otypes
    except ImportError:
        pass

    from ansible.module_utils.ovirt import (
        BaseModule,
        equal
    )

    class ClustersModule(BaseModule):

        # The build method builds the entity we want to create.
        # Always be sure to build only the parameters the user specified
        # in his yaml file, so we don't change the values which we shouldn't
        # change. If you set the parameter to None, nothing will be changed.
        def build_entity(self):
            return otypes.Cluster(
                name=self.param('name'),
                comment=self.param('comment'),
                description=self.param('description'),
            )

        # The update_check method checks if the update is needed to be done on
        # the entity. The equal method doesn't check the values which are None,
        # which means it doesn't check the values which user didn't set in yaml.
        # All other values are checked and if there is found some mismatch,
        # the update method is run on the entity, the entity is build by
        # 'build_entity' method. You don't have to care about calling the update,
        # it's called behind the scene by the 'BaseModule' class.
        def update_check(self, entity):
            return (
                equal(self.param('comment'), entity.comment)
                and equal(self.param('description'), entity.description)
            )

The code above handle the check if the entity should be updated, so we
don't update the entity if not needed and also it construct the needed
entity of the SDK.

.. code:: python

    from ansible.module_utils.basic import AnsibleModule
    from ansible.module_utils.ovirt import (
        check_sdk,
        create_connection,
        ovirt_full_argument_spec,
    )

    # This module will support two states of the cluster,
    # either it will be present or absent. The user can
    # specify three parameters: name, comment and description,
    # The 'ovirt_full_argument_spec' function, will merge the
    # parameters created here with some common one like 'auth':
    argument_spec = ovirt_full_argument_spec(
        state=dict(
            choices=['present', 'absent'],
            default='present',
        ),
        name=dict(default=None, required=True),
        description=dict(default=None),
        comment=dict(default=None),
    )

    # Create the Ansible module, please always implement the
    # feautre called 'check_mode', for 'create', 'update' and
    # 'delete' operations it's implemented by default in BaseModule:
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    # Check if the user has Python SDK installed:
    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        
        # Create the connection to the oVirt engine:
        connection = create_connection(auth)

        # Create the service which manages the entity:
        clusters_service = connection.system_service().clusters_service()

        # Create the module which will handle create, update and delete flow:
        clusters_module = ClustersModule(
            connection=connection,
            module=module,
            service=clusters_service,
        )

        # Check the state and call the appropriate method:
        state = module.params['state']
        if state == 'present':
            ret = clusters_module.create()
        elif state == 'absent':
            ret = clusters_module.remove()

        # The return value of the 'create' and 'remove' method is dictionary
        # with the 'id' of the entity we manage and the type of the entity
        # with filled in attributes of the entity. The 'change' status is
        # also returned by those methods:
        module.exit_json(**ret)
    except Exception as e:
        # Modules can't raises exception, it always must exit with
        # 'module.fail_json' in case of exception. Always use
        # 'exception=traceback.format_exc' for debugging purposes:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        # Logout only in case the user passed the 'token' in 'auth'
        # parameter:
        connection.close(logout=auth.get('token') is None)

If your module must support action handling (for example, 
virtual machine start) you must ensure that you handle the states of the
virtual machine correctly, and document the behavior of the
module:

.. code:: python

        if state == 'running':
            ret = vms_module.action(
                action='start',
                post_action=vms_module._post_start_action,
                action_condition=lambda vm: (
                    vm.status not in [
                        otypes.VmStatus.MIGRATING,
                        otypes.VmStatus.POWERING_UP,
                        otypes.VmStatus.REBOOT_IN_PROGRESS,
                        otypes.VmStatus.WAIT_FOR_LAUNCH,
                        otypes.VmStatus.UP,
                        otypes.VmStatus.RESTORING_STATE,
                    ]
                ),
                wait_condition=lambda vm: vm.status == otypes.VmStatus.UP,
                # Start action kwargs:
                use_cloud_init=use_cloud_init,
                use_sysprep=use_sysprep,
                # ...
            )

As you can see from the preceding example, the ``action`` method accepts the ``action_condition`` and
``wait_condition``, which are methods which accept the virtual machine
object as a parameter, so you can check whether the virtual
machine is in a proper state before the action. The rest of the
parameters are for the ``start`` action. You may also handle pre-
or post- action tasks by defining ``pre_action`` and ``post_action``
parameters.

Testing
-------

-  Integration testing is currently done in oVirt's CI system
   `here <http://jenkins.ovirt.org/job/ovirt_master-ansible-system-tests/>`__
   and
   `here <https://github.com/oVirt/ovirt-system-tests/tree/master/ansible-suite-master/>`__.
-  Please consider using these integrationtests if you create a new module or add a new feature to an existing
   module.