eole-galaxy/cadoles/eole/plugins/modules/zephir_variante.py

147 lines
4.6 KiB
Python

#!/usr/bin/python
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = r'''
---
module: zephir_variante
short_description: This is a module to automate edition of zephir configuration
# If this is part of a collection, you need to use semantic versioning,
# i.e. the version is of the form "2.5.0" and not "2.4".
version_added: "1.0.0"
description: This is my longer description explaining my test module.
options:
zephir_user:
description: zephir user authorized to perform action
required: true
type: str
zephir_user_password:
description: zephir user password
required: true
type: str
module:
description: module the variant is related to
required: true
type: str
name:
description: variant name
required: true
type: str
copy_id:
description: variant id this variant is copied from
required: false
type: int
variant_password:
description: password protecting variant edition
required: false
type: str
# Specify this value according to your collection
# in format of namespace.collection.doc_fragment_name
extends_documentation_fragment:
- cadoles.eole.zephir_variante
author:
- Cadoles
'''
EXAMPLES = r'''
# Pass in a message
- name: Test with a message
cadoles.eole.zephir_variante:
module: eolebase-2.7.2
module: dhcp
'''
RETURN = r'''
# These are examples of possible return values, and in general should use other names for return values.
id:
description: id the variant is associated with.
type: int
returned: always
sample: 1
module:
description: module the variant is associated with.
type: str
returned: always
sample: eolebase-2.7.2
'''
from ansible.module_utils.basic import AnsibleModule
from zephir.eolerpclib import EoleProxy
from zephir.web import config
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
zephir_user=dict(type='str', required=True),
zephir_user_password=dict(type='str', required=True),
module=dict(type='str', required=True),
name=dict(type='str', required=True),
copied_id=dict(type='int', required=False),
variant_password=dict(type='str', required=False),
)
# seed the result dict in the object
# we primarily care about changed and state
# changed is if this module effectively modified the target
# state will include any data that you want your module to pass back
# for consumption, for example, in a subsequent task
result = dict(
changed=False,
id=None,
msg='',
)
# the AnsibleModule object will be our abstraction working with Ansible
# this includes instantiation, a couple of common attr would be the
# args/params passed to the execution, as well as if the module
# supports check mode
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)
if module.check_mode:
module.exit_json(**result)
#module.fail_json(**result)
#module.params['module']
port_zephir = str(int(config.PORT_ZEPHIR) + 1)
proxy_addr = "http://{0}:{1}@localhost:{2}/".format(module.params['zephir_user'], module.params['zephir_user_password'], port_zephir)
proxy = EoleProxy(proxy_addr)
modules = {m['libelle']: m['id'] for m in proxy.modules.get_module()[1]}
module_id = modules.get(module.params['module'], None)
if not module_id:
result['msg'] = 'Unknown module {}'.format(module.params['module'])
module.fail_json(**result)
return_code, proxy_msg = proxy.modules.add_variante(module_id, module.params['name'])
if return_code:
result['changed'] = True
result['id'] = proxy_msg
result['msg'] = 'Variant {} of module {} created with id {}'.format(module.params['name'], module.params['module'], result['id'])
else:
result['msg'] = 'Variant {} already exists'.format(module.params['name'])
# during the execution of the module, if there is an exception or a
# conditional state that effectively causes a failure, run
# AnsibleModule.fail_json() to pass in the message and the result
# in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()