From 9e861693fcb79d256af6d0cfe26f27c7f7ff8e13 Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Fri, 17 May 2024 14:18:45 +0300 Subject: [PATCH] batch: add keeponly option batch(methods=Dict(), keeponly=list) will allow to execute batch of commands and remove from the output everything but the attributes which names were passed in the keeponly list. This can be useful if you are only interested in getting names and assigned random passwords, for example. Fix batch API test in test_integration/test_idm_api.py and use it to validate keeponly option. Fixes: https://pagure.io/freeipa/issue/9583 Signed-off-by: Alexander Bokovoy Reviewed-By: Thomas Woerner Reviewed-By: Rob Crittenden Reviewed-By: Rafael Guterres Jeffman --- API.txt | 3 ++- VERSION.m4 | 4 ++-- doc/api/batch.md | 1 + ipaserver/plugins/batch.py | 21 +++++++++++---------- ipatests/prci_definitions/gating.yaml | 12 ++++++++++++ ipatests/test_integration/test_idm_api.py | 14 +++++++++++--- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/API.txt b/API.txt index 5ed1f5327..61e8e463a 100644 --- a/API.txt +++ b/API.txt @@ -451,8 +451,9 @@ output: Entry('result') output: Output('summary', type=[, ]) output: PrimaryKey('value') command: batch/1 -args: 1,1,2 +args: 1,2,2 arg: Dict('methods*') +option: Str('keeponly*') option: Str('version?') output: Output('count', type=[]) output: Output('results', type=[, ]) diff --git a/VERSION.m4 b/VERSION.m4 index 4ca4a32b9..ab8a7961f 100644 --- a/VERSION.m4 +++ b/VERSION.m4 @@ -86,8 +86,8 @@ define(IPA_DATA_VERSION, 20100614120000) # # ######################################################## define(IPA_API_VERSION_MAJOR, 2) -# Last change: add passkey support -define(IPA_API_VERSION_MINOR, 253) +# Last change: add keeponly option to batch command +define(IPA_API_VERSION_MINOR, 254) ######################################################## # Following values are auto-generated from values above diff --git a/doc/api/batch.md b/doc/api/batch.md index 582ef57b4..4c5beda50 100644 --- a/doc/api/batch.md +++ b/doc/api/batch.md @@ -8,6 +8,7 @@ Make multiple ipa calls via one remote procedure call |methods|:ref:`Dict`|False ### Options +* keeponly : :ref:`Str` * version : :ref:`Str` ### Output diff --git a/ipaserver/plugins/batch.py b/ipaserver/plugins/batch.py index 13ff1f153..6fa867279 100644 --- a/ipaserver/plugins/batch.py +++ b/ipaserver/plugins/batch.py @@ -30,7 +30,6 @@ 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 __doc__ = _(""" Plugin to make multiple ipa calls via one remote procedure call @@ -77,14 +76,10 @@ class batch(Command): ), ) - 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, + takes_options = ( + Str('keeponly*', + doc=_('Keep specified attributes in the output, ' + 'remove everything else.'), ), ) @@ -160,6 +155,7 @@ class batch(Command): def execute(self, methods=None, **options): results = [] op_account = getattr(context, 'principal', '[autobind]') + keeponly = options.get("keeponly", None) for arg in (methods or []): params = dict() name = None @@ -179,7 +175,12 @@ class batch(Command): name, ', '.join(api.Command[name]._repr_iter(**params)) ) - result['error']=None + result['error'] = None + res = result.get('result', None) + if keeponly is not None and isinstance(res, dict): + result["result"] = dict( + filter(lambda x: x[0] in keeponly, res.items()) + ) except Exception as e: if (isinstance(e, errors.RequirementError) or isinstance(e, errors.CommandError) or diff --git a/ipatests/prci_definitions/gating.yaml b/ipatests/prci_definitions/gating.yaml index 5921c8c3f..bb5af57fc 100644 --- a/ipatests/prci_definitions/gating.yaml +++ b/ipatests/prci_definitions/gating.yaml @@ -143,6 +143,18 @@ jobs: timeout: 5400 topology: *master_1repl_1client + fedora-latest/test_idm_api: + requires: [fedora-latest/build] + priority: 50 + job: + class: RunPytest + args: + build_url: '{fedora-latest/build_url}' + test_suite: test_integration/test_idm_api.py + template: *ci-master-latest + timeout: 3600 + topology: *master_1repl_1client + fedora-latest/test_kerberos_flags: requires: [fedora-latest/build] priority: 100 diff --git a/ipatests/test_integration/test_idm_api.py b/ipatests/test_integration/test_idm_api.py index eafef5dd8..1be0b34ba 100644 --- a/ipatests/test_integration/test_idm_api.py +++ b/ipatests/test_integration/test_idm_api.py @@ -524,9 +524,17 @@ class TestAPIScenario(IntegrationTest): for i in range(5): user_id = "user%i" % i args = [user_id] - kw = {{'givenname' : user_id, 'sn' : user_id}} - batch_args.append({{'method' : 'user_add', 'params' : [args, kw]}}) - api.Command["batch"](*batch_args) + kw = dict(givenname=user_id, sn=user_id, random=True) + batch_args.append(dict(method='user_add', params=[args, kw])) + + batch_args.append(dict(method='ping', params=[(), dict()])) + keeponly=('dn', 'uid', 'randompassword') + batch = api.Command["batch"](methods=batch_args, keeponly=keeponly) + # Make sure only the attributes from keeponly returned in result dict + # The ping() test above will have no attributes returned + for r in batch['results']: + if r.get('result', None): + assert set(keeponly) >= set(r['result'].keys()) """ ) self.create_and_run_script(