summaryrefslogtreecommitdiffstats
path: root/ipaserver/plugins/batch.py
diff options
context:
space:
mode:
authorJan Cholasta <jcholast@redhat.com>2016-04-28 10:30:05 +0200
committerJan Cholasta <jcholast@redhat.com>2016-06-03 09:00:34 +0200
commit6e44557b601f769d23ee74555a72e8b5cc62c0c9 (patch)
treeeedd3e054b0709341b9f58c190ea54f999f7d13a /ipaserver/plugins/batch.py
parentec841e5d7ab29d08de294b3fa863a631cd50e30a (diff)
downloadfreeipa-6e44557b601f769d23ee74555a72e8b5cc62c0c9.tar.gz
freeipa-6e44557b601f769d23ee74555a72e8b5cc62c0c9.tar.xz
freeipa-6e44557b601f769d23ee74555a72e8b5cc62c0c9.zip
ipalib: move server-side plugins to ipaserver
Move the remaining plugin code from ipalib.plugins to ipaserver.plugins. Remove the now unused ipalib.plugins package. https://fedorahosted.org/freeipa/ticket/4739 Reviewed-By: David Kupka <dkupka@redhat.com>
Diffstat (limited to 'ipaserver/plugins/batch.py')
-rw-r--r--ipaserver/plugins/batch.py143
1 files changed, 143 insertions, 0 deletions
diff --git a/ipaserver/plugins/batch.py b/ipaserver/plugins/batch.py
new file mode 100644
index 000000000..84a650575
--- /dev/null
+++ b/ipaserver/plugins/batch.py
@@ -0,0 +1,143 @@
+# Authors:
+# Adam Young <ayoung@redhat.com>
+# Rob Crittenden <rcritten@redhat.com>
+#
+# Copyright (c) 2010 Red Hat
+# See file 'copying' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+"""
+Plugin to make multiple ipa calls via one remote procedure call
+
+To run this code in the lite-server
+
+curl -H "Content-Type:application/json" -H "Accept:application/json" -H "Accept-Language:en" --negotiate -u : --cacert /etc/ipa/ca.crt -d @batch_request.json -X POST http://localhost:8888/ipa/json
+
+where the contents of the file batch_request.json follow the below example
+
+{"method":"batch","params":[[
+ {"method":"group_find","params":[[],{}]},
+ {"method":"user_find","params":[[],{"whoami":"true","all":"true"}]},
+ {"method":"user_show","params":[["admin"],{"all":true}]}
+ ],{}],"id":1}
+
+The format of the response is nested the same way. At the top you will see
+ "error": null,
+ "id": 1,
+ "result": {
+ "count": 3,
+ "results": [
+
+
+And then a nested response for each IPA command method sent in the request
+
+"""
+
+import six
+
+from ipalib import api, errors
+from ipalib import Command
+from ipalib.parameters import Str, Any
+from ipalib.output import Output
+from ipalib.text import _
+from ipalib.request import context
+from ipalib.plugable import Registry
+from ipapython.version import API_VERSION
+
+if six.PY3:
+ unicode = str
+
+register = Registry()
+
+@register()
+class batch(Command):
+ NO_CLI = True
+
+ takes_args = (
+ Any('methods*',
+ doc=_('Nested Methods to execute'),
+ ),
+ )
+
+ take_options = (
+ Str('version',
+ cli_name='version',
+ doc=_('Client version. Used to determine if server will accept request.'),
+ exclude='webui',
+ flags=['no_option', 'no_output'],
+ default=API_VERSION,
+ autofill=True,
+ ),
+ )
+
+ has_output = (
+ Output('count', int, doc=''),
+ Output('results', (list, tuple), doc='')
+ )
+
+ 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.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',
+ 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)
+