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

202 lines
7.1 KiB
Python
Raw Normal View History

2021-09-29 10:45:20 +02:00
#!/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
etab:
description: etab the server is related to
required: true
type: str
module:
description: module the server is an instance of
required: true
type: str
variante:
description: module variant the server is an instance of
required: false
type: int
# 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
import time
def serveur_id_from_libelle(proxy, libelle):
return_code, servers = proxy.serveurs.get_serveur()
servers = {s['libelle']: s['id'] for s in servers}
return servers.get(libelle, None)
def etab_id_from_libelle(proxy, libelle):
return_code, etabs = proxy.etabs.get_etab()
etabs = {e['libelle']: e['rne'] for e in etabs}
return etabs.get(libelle, None)
def module_id_from_libelle(proxy, libelle):
return_code, modules = proxy.modules.get_module()
modules = {m['libelle']: m['id'] for m in modules}
return modules.get(libelle, None)
def variante_id_from_libelle(proxy, libelle):
return_code, variantes = proxy.modules.get_variante()
variantes = {v['libelle']: v['id'] for v in variantes}
return variantes.get(libelle, None)
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),
rne=dict(type='str', required=True),
libelle=dict(type='str', required=True),
materiel=dict(type='str', required=False, default=''),
processeur=dict(type='str', required=False, default=''),
disque_dur=dict(type='str', required=False, default=''),
date_install=dict(type='str', required=False, default=time.strftime('%Y-%m-%d', time.localtime())),
installateur=dict(type='str', required=False, default=''),
tel=dict(type='str', required=False, default=''),
remarques=dict(type='str', required=False, default=''),
module_initial=dict(type='str', required=False),
module_actuel=dict(type='str', required=True),
timeout=dict(type='int', required=False, default=60),
variante=dict(type='str', required=True),
cle_rsa1=dict(type='str', required=False, default=''),
id_groupe=dict(type='int', required=False, default=-1),
)
# 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)
module.params['module_actuel'] = module_id_from_libelle(proxy, module.params['module_actuel'])
module.params['variante'] = variante_id_from_libelle(proxy, module.params['variante'])
module.params['rne'] = etab_id_from_libelle(proxy, module.params['rne'])
if not module.params.get('module_initial', None):
module.params['module_initial'] = module.params['module_actuel']
else:
module.params['module_initial'] = module_id_from_libelle(proxy, module.params['module_initial'])
return_code, module_descr = proxy.modules.get_module(module.params['module_actuel'])
if not return_code:
result['msg'] = 'Unknown module {}'.format(module.params['module_actuel'])
module.fail_json(**result)
return_code, proxy_msg = proxy.serveurs.add_serveur(
module.params['rne'],
module.params['libelle'],
module.params['materiel'],
module.params['processeur'],
module.params['disque_dur'],
module.params['date_install'],
module.params['installateur'],
module.params['tel'],
module.params['remarques'],
module.params['module_initial'],
module.params['module_actuel'],
module.params['timeout'],
module.params['variante'],
module.params['cle_rsa1'],
module.params['id_groupe'],
)
if return_code:
result['changed'] = True
result['id'] = proxy_msg
result['msg'] = 'Server {} created in etab {} with id {}'.format(module.params['libelle'], module.params['rne'], result['id'])
else:
result['msg'] = 'Server {} already exists {}'.format(module.params['libelle'], proxy_msg)
# 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()