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

232 lines
7.9 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
rne:
description: Unique identifier
required: true
type: str
libelle:
description: etab libelle
required: true
type: str
ville:
description: city where etab belongs
required: true
type: str
code_postal:
description: postal code related to city
required: true
type: str
etab_type:
description: integer identifying etab type
required: true
type: int
adresse:
description: etab postal address
required: false
type: str
tel:
description: telephone number used to contact the one in charge
required: false
type: str
fax:
description: fax number used to transfer data to the one in charge
required: false
type: str
mail:
description: mail address used to contact the one in charge
required: false
type: str
responsable: identity of the one in charge
description:
required: false
type: str
remarque: other information
description:
required: false
type: str
state:
description: wether data have to be added or deleted from the database
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
- libelle: 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
key_mapping = {'rne': 'rne',
'libelle': 'libelle',
'ville': 'ville',
'cp': 'code_postal',
'type': 'etab_type',
'adresse': 'adresse',
'tel': 'tel',
'fax': 'fax',
'mail': 'mail',
'responsable': 'responsable',
'remarques': 'remarques'
}
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),
ville=dict(type='str', required=True),
code_postal=dict(type='str', required=True),
etab_type=dict(type='int', required=True),
adresse=dict(type='str', required=False, default=''),
tel=dict(type='str', required=False, default=''),
fax=dict(type='str', required=False, default=''),
mail=dict(type='str', required=False, default=''),
responsable=dict(type='str', required=False, default=''),
remarques=dict(type='str', required=False, default=''),
state=dict(type='str', required=False, default='present'),
)
# 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,
rne=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 = 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)
return_code, etabs = proxy.etabs.get_etab()
if return_code:
etabs = {m['rne']:m for m in etabs}
if module.params['rne'] in etabs:
if module.params['state'] == 'absent':
return_code, proxy_msg = proxy.etabs.del_etab(module.params['rne'])
result['changed'] = True
result['rne'] = proxy_msg
result['msg'] = 'Etab {}'.format(module.params['rne'])
module.exit_json(**result)
result['msg'] = 'Etab {} already exists'.format(module.params['rne'])
data_change = {}
for key, value in etabs[module.params['rne']].items():
if module.params[key_mapping[key]] != value:
data_change[key] = module.params[key_mapping[key]]
if not data_change:
result['changed'] = False
result['rne'] = proxy_msg
result['msg'] = 'Etab {}'.format(module.params['rne'])
module.exit_json(**result)
return_code, proxy_msg = proxy.etabs.edit_etab(module.params['rne'], data_change)
else:
if module.params['state'] == 'absent':
result['changed'] = False
result['rne'] = proxy_msg
result['msg'] = 'Etab {}'.format(module.params['rne'])
module.exit_json(**result)
return_code, proxy_msg = proxy.etabs.add_etab(module.params['rne'],
module.params['libelle'],
module.params['adresse'],
module.params['ville'],
module.params['code_postal'],
module.params['tel'],
module.params['fax'],
module.params['mail'],
module.params['responsable'],
module.params['remarques'],
module.params['etab_type'])
result['changed'] = True
result['rne'] = proxy_msg
result['msg'] = 'Etab {}'.format(module.params['rne'])
module.exit_json(**result)
if return_code:
result['changed'] = True
result['rne'] = proxy_msg
result['msg'] = 'Etab {}'.format(module.params['rne'])
module.exit_json(**result)
else:
result['msg'] = 'Etab {} not created: {}'.format(module.params['libelle'], proxy_msg)
module.fail_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()