def _check_session(self): """Checks if the session is active.""" if (self._session is None or self._session not in _db_content['session']): LOG.debug(_("Session is faulty")) raise error_util.VimFaultException([error_util.NOT_AUTHENTICATED], _("Session Invalid"))
def __getattr__(self, attr_name): """Makes the API calls and gets the result.""" try: return object.__getattr__(self, attr_name) except AttributeError: def vim_request_handler(managed_object, **kwargs): """ Builds the SOAP message and parses the response for fault checking and other errors. managed_object : Managed Object Reference or Managed Object Name **kwargs : Keyword arguments of the call """ # Dynamic handler for VI SDK Calls try: request_mo = \ self._request_managed_object_builder(managed_object) request = getattr(self.client.service, attr_name) response = request(request_mo, **kwargs) # To check for the faults that are part of the message body # and not returned as Fault object response from the ESX # SOAP server if hasattr(error_util.FaultCheckers, attr_name.lower() + "_fault_checker"): fault_checker = getattr( error_util.FaultCheckers, attr_name.lower() + "_fault_checker") fault_checker(response) return response # Catch the VimFaultException that is raised by the fault # check of the SOAP response except error_util.VimFaultException, excep: raise except suds.WebFault, excep: doc = excep.document detail = doc.childAtPath("/Envelope/Body/Fault/detail") fault_list = [] for child in detail.getChildren(): fault_list.append(child.get("type")) raise error_util.VimFaultException(fault_list, excep)
def vim_request_handler(managed_object, **kwargs): """ Builds the SOAP message and parses the response for fault checking and other errors. managed_object : Managed Object Reference or Managed Object Name **kwargs : Keyword arguments of the call """ # Dynamic handler for VI SDK Calls try: request_mo = self._request_managed_object_builder( managed_object) request = getattr(self.client.service, attr_name) response = request(request_mo, **kwargs) # To check for the faults that are part of the message body # and not returned as Fault object response from the ESX # SOAP server if hasattr(error_util.FaultCheckers, attr_name.lower() + "_fault_checker"): fault_checker = getattr( error_util.FaultCheckers, attr_name.lower() + "_fault_checker") fault_checker(response) return response # Catch the VimFaultException that is raised by the fault # check of the SOAP response except error_util.VimFaultException as excep: raise except suds.WebFault as excep: doc = excep.document detail = doc.childAtPath("/Envelope/Body/Fault/detail") fault_list = [] for child in detail.getChildren(): fault_list.append(child.get("type")) raise error_util.VimFaultException(fault_list, excep) except AttributeError as excep: raise error_util.VimAttributeError( _("No such SOAP method " "'%s' provided by VI SDK") % (attr_name), excep) except (httplib.CannotSendRequest, httplib.ResponseNotReady, httplib.CannotSendHeader) as excep: raise error_util.SessionOverLoadException( _("httplib " "error in %s: ") % (attr_name), excep) except Exception as excep: # Socket errors which need special handling for they # might be caused by ESX API call overload if (str(excep).find(ADDRESS_IN_USE_ERROR) != -1 or str(excep).find(CONN_ABORT_ERROR)) != -1: raise error_util.SessionOverLoadException( _("Socket " "error in %s: ") % (attr_name), excep) # Type error that needs special handling for it might be # caused by ESX host API call overload elif str(excep).find(RESP_NOT_XML_ERROR) != -1: raise error_util.SessionOverLoadException( _("Type " "error in %s: ") % (attr_name), excep) else: raise error_util.VimException( _("Exception in %s ") % (attr_name), excep)
def fake_session_file_exception(): fault_list = [error_util.FILE_ALREADY_EXISTS] raise error_util.VimFaultException(fault_list, Exception('fake'))
def fake_temp_method_exception(): raise error_util.VimFaultException([error_util.NOT_AUTHENTICATED], "Session Empty/Not Authenticated")
def test_vim_fault_exception(self): vfe = error_util.VimFaultException([ValueError("example")], "cause") string = str(vfe) self.assertEqual("cause", string)
def fake_session_permission_exception(): fault_list = [error_util.NO_PERMISSION] fault_string = 'Permission to perform this operation was denied.' details = {'privilegeId': 'Resource.AssignVMToPool', 'object': 'domain-c7'} raise error_util.VimFaultException(fault_list, fault_string, details)