1# -*- coding: utf-8 -*-
2# Copyright (C) 2018 IBM CORPORATION
3# Author(s): Tzur Eliyahu <tzure@il.ibm.com>
4#
5# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
6
7from __future__ import absolute_import, division, print_function
8__metaclass__ = type
9
10import traceback
11
12from functools import wraps
13from ansible.module_utils.common.text.converters import to_native
14from ansible.module_utils.basic import missing_required_lib
15
16PYXCLI_INSTALLED = True
17PYXCLI_IMP_ERR = None
18try:
19    from pyxcli import client, errors
20except ImportError:
21    PYXCLI_IMP_ERR = traceback.format_exc()
22    PYXCLI_INSTALLED = False
23
24AVAILABLE_PYXCLI_FIELDS = ['pool', 'size', 'snapshot_size',
25                           'domain', 'perf_class', 'vol',
26                           'iscsi_chap_name', 'iscsi_chap_secret',
27                           'cluster', 'host', 'lun', 'override',
28                           'fcaddress', 'iscsi_name', 'max_dms',
29                           'max_cgs', 'ldap_id', 'max_mirrors',
30                           'max_pools', 'max_volumes', 'hard_capacity',
31                           'soft_capacity']
32
33
34def xcli_wrapper(func):
35    """ Catch xcli errors and return a proper message"""
36    @wraps(func)
37    def wrapper(module, *args, **kwargs):
38        try:
39            return func(module, *args, **kwargs)
40        except errors.CommandExecutionError as e:
41            module.fail_json(msg=to_native(e))
42    return wrapper
43
44
45@xcli_wrapper
46def connect_ssl(module):
47    endpoints = module.params['endpoints']
48    username = module.params['username']
49    password = module.params['password']
50    if not (username and password and endpoints):
51        module.fail_json(
52            msg="Username, password or endpoints arguments "
53            "are missing from the module arguments")
54
55    try:
56        return client.XCLIClient.connect_multiendpoint_ssl(username,
57                                                           password,
58                                                           endpoints)
59    except errors.CommandFailedConnectionError as e:
60        module.fail_json(
61            msg="Connection with Spectrum Accelerate system has "
62            "failed: {[0]}.".format(to_native(e)))
63
64
65def spectrum_accelerate_spec():
66    """ Return arguments spec for AnsibleModule """
67    return dict(
68        endpoints=dict(required=True),
69        username=dict(required=True),
70        password=dict(no_log=True, required=True),
71    )
72
73
74@xcli_wrapper
75def execute_pyxcli_command(module, xcli_command, xcli_client):
76    pyxcli_args = build_pyxcli_command(module.params)
77    getattr(xcli_client.cmd, xcli_command)(**(pyxcli_args))
78    return True
79
80
81def build_pyxcli_command(fields):
82    """ Builds the args for pyxcli using the exact args from ansible"""
83    pyxcli_args = {}
84    for field in fields:
85        if not fields[field]:
86            continue
87        if field in AVAILABLE_PYXCLI_FIELDS and fields[field] != '':
88            pyxcli_args[field] = fields[field]
89    return pyxcli_args
90
91
92def is_pyxcli_installed(module):
93    if not PYXCLI_INSTALLED:
94        module.fail_json(msg=missing_required_lib('pyxcli'),
95                         exception=PYXCLI_IMP_ERR)
96