2010-10-22 14:08:29 -05:00
|
|
|
# Authors:
|
|
|
|
# Adam Young <ayoung@redhat.com>
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Red Hat
|
|
|
|
# See file 'copying' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# 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.
|
2010-10-22 14:08:29 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
2010-12-09 06:59:11 -06:00
|
|
|
# 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.
|
2010-10-22 14:08:29 -05:00
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-10-22 14:08:29 -05:00
|
|
|
|
|
|
|
"""
|
|
|
|
Plugin to make multiple ipa calls via one remote procedure call
|
|
|
|
|
|
|
|
To run this code in the lite-server
|
|
|
|
|
2011-05-04 03:26:18 -05:00
|
|
|
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
|
2010-10-22 14:08:29 -05:00
|
|
|
|
2011-05-04 03:26:18 -05:00
|
|
|
where the contents of the file batch_request.json follow the below example
|
2010-10-22 14:08:29 -05:00
|
|
|
|
|
|
|
{"method":"batch","params":[[
|
|
|
|
{"method":"group_find","params":[[],{}]},
|
|
|
|
{"method":"user_find","params":[[],{"whoami":"true","all":"true"}]},
|
|
|
|
{"method":"user_show","params":[["admin"],{"all":true}]}
|
|
|
|
],{}],"id":1}
|
|
|
|
|
2011-01-13 13:29:16 -06:00
|
|
|
The format of the response is nested the same way. At the top you will see
|
2010-10-22 14:08:29 -05:00
|
|
|
"error": null,
|
|
|
|
"id": 1,
|
|
|
|
"result": {
|
|
|
|
"count": 3,
|
|
|
|
"results": [
|
|
|
|
|
|
|
|
|
|
|
|
And then a nested response for each IPA command method sent in the request
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
import six
|
|
|
|
|
2010-10-22 14:08:29 -05:00
|
|
|
from ipalib import api, errors
|
|
|
|
from ipalib import Command
|
2016-06-30 02:32:00 -05:00
|
|
|
from ipalib.frontend import Local
|
2016-06-03 00:31:38 -05:00
|
|
|
from ipalib.parameters import Str, Dict
|
2010-10-22 14:08:29 -05:00
|
|
|
from ipalib.output import Output
|
|
|
|
from ipalib.text import _
|
2011-08-19 15:20:01 -05:00
|
|
|
from ipalib.request import context
|
2014-06-10 10:27:51 -05:00
|
|
|
from ipalib.plugable import Registry
|
2011-01-13 13:29:16 -06:00
|
|
|
from ipapython.version import API_VERSION
|
2010-10-22 14:08:29 -05:00
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
if six.PY3:
|
|
|
|
unicode = str
|
|
|
|
|
2014-06-10 10:27:51 -05:00
|
|
|
register = Registry()
|
|
|
|
|
|
|
|
@register()
|
2010-10-22 14:08:29 -05:00
|
|
|
class batch(Command):
|
2011-01-20 14:07:43 -06:00
|
|
|
NO_CLI = True
|
2010-10-22 14:08:29 -05:00
|
|
|
|
|
|
|
takes_args = (
|
2016-06-03 00:31:38 -05:00
|
|
|
Dict('methods*',
|
2011-11-21 09:50:27 -06:00
|
|
|
doc=_('Nested Methods to execute'),
|
|
|
|
),
|
|
|
|
)
|
2010-10-22 14:08:29 -05:00
|
|
|
|
2011-01-13 13:29:16 -06:00
|
|
|
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,
|
2011-11-21 09:50:27 -06:00
|
|
|
),
|
2011-01-13 13:29:16 -06:00
|
|
|
)
|
|
|
|
|
2010-10-22 14:08:29 -05:00
|
|
|
has_output = (
|
2011-02-23 15:47:49 -06:00
|
|
|
Output('count', int, doc=''),
|
2012-03-12 04:58:44 -05:00
|
|
|
Output('results', (list, tuple), doc='')
|
2010-10-22 14:08:29 -05:00
|
|
|
)
|
|
|
|
|
2016-05-19 07:19:07 -05:00
|
|
|
def execute(self, methods=None, **options):
|
2012-03-12 04:58:44 -05:00
|
|
|
results = []
|
2016-05-19 07:19:07 -05:00
|
|
|
for arg in (methods or []):
|
2011-08-19 15:20:01 -05:00
|
|
|
params = dict()
|
|
|
|
name = None
|
2010-10-22 14:08:29 -05:00
|
|
|
try:
|
2011-08-19 15:20:01 -05:00
|
|
|
if 'method' not in arg:
|
|
|
|
raise errors.RequirementError(name='method')
|
|
|
|
if 'params' not in arg:
|
|
|
|
raise errors.RequirementError(name='params')
|
|
|
|
name = arg['method']
|
2016-06-30 02:32:00 -05:00
|
|
|
if (name not in self.api.Command or
|
|
|
|
isinstance(self.api.Command[name], Local)):
|
2011-08-19 15:20:01 -05:00
|
|
|
raise errors.CommandError(name=name)
|
2016-05-27 01:19:39 -05:00
|
|
|
|
|
|
|
# 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)'))
|
2012-06-21 07:20:26 -05:00
|
|
|
newkw.setdefault('version', options['version'])
|
2011-08-19 15:20:01 -05:00
|
|
|
|
|
|
|
result = api.Command[name](*a, **newkw)
|
|
|
|
self.info(
|
2016-04-22 03:54:44 -05:00
|
|
|
'%s: batch: %s(%s): SUCCESS',
|
|
|
|
getattr(context, 'principal', 'UNKNOWN'),
|
|
|
|
name,
|
|
|
|
', '.join(api.Command[name]._repr_iter(**params))
|
2011-08-19 15:20:01 -05:00
|
|
|
)
|
2010-10-22 14:08:29 -05:00
|
|
|
result['error']=None
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2011-08-19 15:20:01 -05:00
|
|
|
if isinstance(e, errors.RequirementError) or \
|
|
|
|
isinstance(e, errors.CommandError):
|
|
|
|
self.info(
|
2016-02-25 06:46:33 -06:00
|
|
|
'%s: batch: %s',
|
|
|
|
context.principal, # pylint: disable=no-member
|
|
|
|
e.__class__.__name__
|
2011-08-19 15:20:01 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.info(
|
2016-02-25 06:46:33 -06:00
|
|
|
'%s: batch: %s(%s): %s',
|
|
|
|
context.principal, name, # pylint: disable=no-member
|
|
|
|
', '.join(api.Command[name]._repr_iter(**params)),
|
|
|
|
e.__class__.__name__
|
2011-08-19 15:20:01 -05:00
|
|
|
)
|
2012-07-04 07:39:21 -05:00
|
|
|
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__),
|
2016-05-18 02:42:56 -05:00
|
|
|
error_kw=reported_error.kw,
|
2012-07-04 07:39:21 -05:00
|
|
|
)
|
2010-10-22 14:08:29 -05:00
|
|
|
results.append(result)
|
|
|
|
return dict(count=len(results) , results=results)
|