| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# (c) 2016 Red Hat Inc. |
| 8 |
# (c) 2019 Cisco Systems Inc. |
| 9 |
# |
| 10 |
# Redistribution and use in source and binary forms, with or without modification, |
| 11 |
# are permitted provided that the following conditions are met: |
| 12 |
# |
| 13 |
# * Redistributions of source code must retain the above copyright |
| 14 |
# notice, this list of conditions and the following disclaimer. |
| 15 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
# this list of conditions and the following disclaimer in the documentation |
| 17 |
# and/or other materials provided with the distribution. |
| 18 |
# |
| 19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 |
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 27 |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
# |
| 29 |
|
| 30 |
from __future__ import (absolute_import, division, print_function) |
| 31 |
__metaclass__ = type |
| 32 |
import traceback |
| 33 |
|
| 34 |
from ansible.module_utils.basic import missing_required_lib |
| 35 |
|
| 36 |
UCSMSDK_IMP_ERR = None |
| 37 |
try: |
| 38 |
# import done here to provide common import check for all modules |
| 39 |
# pylint: disable=unused-import |
| 40 |
import ucsmsdk |
| 41 |
HAS_UCSMSDK = True |
| 42 |
except Exception: |
| 43 |
UCSMSDK_IMP_ERR = traceback.format_exc() |
| 44 |
HAS_UCSMSDK = False |
| 45 |
|
| 46 |
ucs_argument_spec = dict( |
| 47 |
hostname=dict(type='str', required=True), |
| 48 |
username=dict(type='str', default='admin'), |
| 49 |
password=dict(type='str', required=True, no_log=True), |
| 50 |
port=dict(type='int', default=None), |
| 51 |
use_ssl=dict(type='bool', default=True), |
| 52 |
use_proxy=dict(type='bool', default=True), |
| 53 |
proxy=dict(type='str', default=None), |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
class UCSModule(): |
| 58 |
|
| 59 |
def __init__(self, module): |
| 60 |
self.module = module |
| 61 |
self.result = {} |
| 62 |
if not HAS_UCSMSDK: |
| 63 |
self.module.fail_json(msg=missing_required_lib('ucsmsdk'), exception=UCSMSDK_IMP_ERR) |
| 64 |
self.login() |
| 65 |
|
| 66 |
def __del__(self): |
| 67 |
self.logout() |
| 68 |
|
| 69 |
def login(self): |
| 70 |
from ucsmsdk.ucshandle import UcsHandle |
| 71 |
|
| 72 |
# use_proxy=yes (default) and proxy=None (default) should be using the system defined proxy |
| 73 |
# use_proxy=yes (default) and proxy=value should use the provided proxy |
| 74 |
# use_proxy=no (user) should not be using a proxy |
| 75 |
if self.module.params['use_proxy']: |
| 76 |
proxy = self.module.params['proxy'] |
| 77 |
else: |
| 78 |
# force no proxy to be used. Note that proxy=None in UcsHandle will |
| 79 |
# use the system proxy so we must set to something else |
| 80 |
proxy = {} |
| 81 |
|
| 82 |
try: |
| 83 |
handle = UcsHandle( |
| 84 |
ip=self.module.params['hostname'], |
| 85 |
username=self.module.params['username'], |
| 86 |
password=self.module.params['password'], |
| 87 |
port=self.module.params['port'], |
| 88 |
secure=self.module.params['use_ssl'], |
| 89 |
proxy=proxy |
| 90 |
) |
| 91 |
handle.login() |
| 92 |
except Exception as e: |
| 93 |
self.result['msg'] = str(e) |
| 94 |
self.module.fail_json(**self.result) |
| 95 |
self.login_handle = handle |
| 96 |
|
| 97 |
def logout(self): |
| 98 |
if hasattr(self, 'login_handle'): |
| 99 |
self.login_handle.logout() |
| 100 |
return True |
| 101 |
return False |