# -*- coding: utf-8 -*- # This code is part of Ansible, but is an independent component. # This particular file snippet, and this file snippet only, is BSD licensed. # Modules you write using this snippet, which is embedded dynamically by Ansible # still belong to the author of the module, and may assign their own license # to the complete work. # # Copyright (c), Gregory Shulov ,2016 # # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) from __future__ import (absolute_import, division, print_function) __metaclass__ = type HAS_INFINISDK = True try: from infinisdk import InfiniBox, core except ImportError: HAS_INFINISDK = False from functools import wraps from os import environ from os import path def api_wrapper(func): """ Catch API Errors Decorator""" @wraps(func) def __wrapper(*args, **kwargs): module = args[0] try: return func(*args, **kwargs) except core.exceptions.APICommandException as e: module.fail_json(msg=e.message) except core.exceptions.SystemNotFoundException as e: module.fail_json(msg=e.message) except Exception: raise return __wrapper @api_wrapper def get_system(module): """Return System Object or Fail""" box = module.params['system'] user = module.params.get('user', None) password = module.params.get('password', None) if user and password: system = InfiniBox(box, auth=(user, password)) elif environ.get('INFINIBOX_USER') and environ.get('INFINIBOX_PASSWORD'): system = InfiniBox(box, auth=(environ.get('INFINIBOX_USER'), environ.get('INFINIBOX_PASSWORD'))) elif path.isfile(path.expanduser('~') + '/.infinidat/infinisdk.ini'): system = InfiniBox(box) else: module.fail_json(msg="You must set INFINIBOX_USER and INFINIBOX_PASSWORD environment variables or set username/password module arguments") try: system.login() except Exception: module.fail_json(msg="Infinibox authentication failed. Check your credentials") return system def infinibox_argument_spec(): """Return standard base dictionary used for the argument_spec argument in AnsibleModule""" return dict( system=dict(required=True), user=dict(), password=dict(no_log=True), ) def infinibox_required_together(): """Return the default list used for the required_together argument to AnsibleModule""" return [['user', 'password']]