00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 import traceback
00018 from lsm import (LsmError, ErrorNumber, error)
00019 from pywbem import (CIMError, CIMInstanceName)
00020 import pywbem
00021 import json
00022
00023
00024 def merge_list(list_a, list_b):
00025 return list(set(list_a + list_b))
00026
00027
00028 def handle_cim_errors(method):
00029 def cim_wrapper(*args, **kwargs):
00030 try:
00031 return method(*args, **kwargs)
00032 except LsmError as lsm:
00033 raise
00034 except CIMError as ce:
00035 error_code, desc = ce
00036
00037 if error_code == 0:
00038 if 'Socket error' in desc:
00039 if 'Errno 111' in desc:
00040 raise LsmError(ErrorNumber.NETWORK_CONNREFUSED,
00041 'Connection refused')
00042 if 'Errno 113' in desc:
00043 raise LsmError(ErrorNumber.NETWORK_HOSTDOWN,
00044 'Host is down')
00045 elif 'SSL error' in desc:
00046 raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION,
00047 desc)
00048 elif 'The web server returned a bad status line':
00049 raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION,
00050 desc)
00051 elif 'HTTP error' in desc:
00052 raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION,
00053 desc)
00054 raise LsmError(ErrorNumber.PLUGIN_BUG, desc)
00055 except pywbem.cim_http.AuthError as ae:
00056 raise LsmError(ErrorNumber.PLUGIN_AUTH_FAILED, "Unauthorized user")
00057 except pywbem.cim_http.Error as te:
00058 raise LsmError(ErrorNumber.NETWORK_ERROR, str(te))
00059 except Exception as e:
00060 error("Unexpected exception:\n" + traceback.format_exc())
00061 raise LsmError(ErrorNumber.PLUGIN_BUG, str(e),
00062 traceback.format_exc())
00063 return cim_wrapper
00064
00065
00066 def hex_string_format(hex_str, length, every):
00067 hex_str = hex_str.lower()
00068 return ':'.join(hex_str[i:i + every] for i in range(0, length, every))
00069
00070
00071 def cim_path_to_path_str(cim_path):
00072 """
00073 Convert CIMInstanceName to a string which could save in plugin_data
00074 """
00075 return json.dumps({
00076 'classname': cim_path.classname,
00077 'keybindings': dict(cim_path.keybindings),
00078 'host': cim_path.host,
00079 'namespace': cim_path.namespace,
00080 })
00081
00082
00083 def path_str_to_cim_path(path_str):
00084 """
00085 Convert a string into CIMInstanceName.
00086 """
00087 path_dict = json.loads(path_str)
00088 return CIMInstanceName(**path_dict)