def _validate_request(self, request): """ Check that an individual request in a batch is parseable and the commands exists. """ if 'method' not in request: raise errors.RequirementError(name='method') if 'params' not in request: raise errors.RequirementError(name='params') name = request['method'] if (name not in self.api.Command or isinstance(self.api.Command[name], Local)): raise errors.CommandError(name=name) # If params are not formated as a tuple(list, dict) # the following lines will raise an exception # that triggers an internal server error # Raise a ConversionError instead to report the issue # to the client try: a, kw = request['params'] newkw = dict((str(k), v) for k, v in kw.items()) api.Command[name].args_options_2_params(*a, **newkw) except (AttributeError, ValueError, TypeError): raise errors.ConversionError( name='params', error=_(u'must contain a tuple (list, dict)')) except Exception as e: raise errors.ConversionError(name='params', error=str(e))
def methodHelp(self, *params): """get method docstring for XML-RPC introspection""" method_name = self._get_method_name('system.methodHelp', *params) if method_name in self._system_commands: return u'' elif method_name in self.Command: return unicode(self.Command[method_name].__doc__ or '') else: raise errors.CommandError(name=method_name)
def methodSignature(self, *params): """get method signature for XML-RPC introspection""" method_name = self._get_method_name('system.methodSignature', *params) if method_name in self._system_commands: # TODO # for now let's not go out of our way to document standard XML-RPC return u'undef' elif method_name in self.Command: # All IPA commands return a dict (struct), # and take a params, options - list and dict (array, struct) return [[u'struct', u'array', u'struct']] else: raise errors.CommandError(name=method_name)
def _get_command(self, name): try: # assume version 1 for unversioned command calls command = self.api.Command[name, '1'] except KeyError: try: command = self.api.Command[name] except KeyError: command = None if command is None or isinstance(command, Local): raise errors.CommandError(name=name) return command
def execute(self, *args, **options): results = [] for arg in args[0]: params = dict() name = None try: if 'method' not in arg: raise errors.RequirementError(name='method') if 'params' not in arg: raise errors.RequirementError(name='params') name = arg['method'] if name not in self.Command: raise errors.CommandError(name=name) a, kw = arg['params'] newkw = dict((str(k), v) for k, v in kw.items()) params = api.Command[name].args_options_2_params(*a, **newkw) newkw.setdefault('version', options['version']) result = api.Command[name](*a, **newkw) self.info( '%s: batch: %s(%s): SUCCESS', context.principal, name, ', '.join(api.Command[name]._repr_iter(**params)) ) result['error']=None except Exception as e: if isinstance(e, errors.RequirementError) or \ isinstance(e, errors.CommandError): self.info( '%s: batch: %s', context.principal, # pylint: disable=no-member e.__class__.__name__ ) else: self.info( '%s: batch: %s(%s): %s', context.principal, name, # pylint: disable=no-member ', '.join(api.Command[name]._repr_iter(**params)), e.__class__.__name__ ) if isinstance(e, errors.PublicError): reported_error = e else: reported_error = errors.InternalError() result = dict( error=reported_error.strerror, error_code=reported_error.errno, error_name=unicode(type(reported_error).__name__), ) results.append(result) return dict(count=len(results) , results=results)
def execute(self, methods=None, **options): results = [] for arg in (methods or []): params = dict() name = None try: if 'method' not in arg: raise errors.RequirementError(name='method') if 'params' not in arg: raise errors.RequirementError(name='params') name = arg['method'] if (name not in self.api.Command or isinstance(self.api.Command[name], Local)): raise errors.CommandError(name=name) # If params are not formated as a tuple(list, dict) # the following lines will raise an exception # that triggers an internal server error # Raise a ConversionError instead to report the issue # to the client try: a, kw = arg['params'] newkw = dict((str(k), v) for k, v in kw.items()) params = api.Command[name].args_options_2_params( *a, **newkw) except (AttributeError, ValueError, TypeError): raise errors.ConversionError( name='params', error=_(u'must contain a tuple (list, dict)')) newkw.setdefault('version', options['version']) result = api.Command[name](*a, **newkw) self.info('%s: batch: %s(%s): SUCCESS', getattr(context, 'principal', 'UNKNOWN'), name, ', '.join(api.Command[name]._repr_iter(**params))) result['error'] = None except Exception as e: if isinstance(e, errors.RequirementError) or \ isinstance(e, errors.CommandError): self.info( '%s: batch: %s', context.principal, # pylint: disable=no-member e.__class__.__name__) else: self.info( '%s: batch: %s(%s): %s', context.principal, name, # pylint: disable=no-member ', '.join(api.Command[name]._repr_iter(**params)), e.__class__.__name__) if isinstance(e, errors.PublicError): reported_error = e else: reported_error = errors.InternalError() result = dict( error=reported_error.strerror, error_code=reported_error.errno, error_name=unicode(type(reported_error).__name__), error_kw=reported_error.kw, ) results.append(result) return dict(count=len(results), results=results)