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 4# Ansible still belong to the author of the module, and may assign their 5# own license to the complete work. 6# 7# Copyright (C) 2017 Lenovo, Inc. 8# All rights reserved. 9# 10# Redistribution and use in source and binary forms, with or without 11# modification, are permitted provided that the following conditions are met: 12# * Redistributions of source code must retain the above copyright 13# notice, this list of conditions and the following disclaimer. 14# * Redistributions in binary form must reproduce the above copyright notice, 15# this list of conditions and the following disclaimer in the documentation 16# and/or other materials provided with the distribution. 17# 18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28# POSSIBILITY OF SUCH DAMAGE. 29# 30# Contains LXCA common class 31# Lenovo xClarity Administrator (LXCA) 32 33import traceback 34try: 35 from pylxca import connect, disconnect 36 HAS_PYLXCA = True 37except ImportError: 38 HAS_PYLXCA = False 39 40 41PYLXCA_REQUIRED = "Lenovo xClarity Administrator Python Client (Python package 'pylxca') is required for this module." 42 43 44def has_pylxca(module): 45 """ 46 Check pylxca is installed 47 :param module: 48 """ 49 if not HAS_PYLXCA: 50 module.fail_json(msg=PYLXCA_REQUIRED) 51 52 53LXCA_COMMON_ARGS = dict( 54 login_user=dict(required=True), 55 login_password=dict(required=True, no_log=True), 56 auth_url=dict(required=True), 57) 58 59 60class connection_object: 61 def __init__(self, module): 62 self.module = module 63 64 def __enter__(self): 65 return setup_conn(self.module) 66 67 def __exit__(self, type, value, traceback): 68 close_conn() 69 70 71def setup_conn(module): 72 """ 73 this function create connection to LXCA 74 :param module: 75 :return: lxca connection 76 """ 77 lxca_con = None 78 try: 79 lxca_con = connect(module.params['auth_url'], 80 module.params['login_user'], 81 module.params['login_password'], 82 "True") 83 except Exception as exception: 84 error_msg = '; '.join(exception.args) 85 module.fail_json(msg=error_msg, exception=traceback.format_exc()) 86 return lxca_con 87 88 89def close_conn(): 90 """ 91 this function close connection to LXCA 92 :param module: 93 :return: None 94 """ 95 disconnect() 96