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 <abokovoy@redhat.com>
Reviewed-By: Thomas Woerner <twoerner@redhat.com>
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
Reviewed-By: Rafael Guterres Jeffman <rjeffman@redhat.com>
This commit is contained in:
Alexander Bokovoy
2024-05-17 14:18:45 +03:00
committed by Antonio Torres
parent 6cc0a0b9a8
commit 9e861693fc
6 changed files with 39 additions and 16 deletions

View File

@@ -451,8 +451,9 @@ output: Entry('result')
output: Output('summary', type=[<type 'unicode'>, <type 'NoneType'>])
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=[<type 'int'>])
output: Output('results', type=[<type 'list'>, <type 'tuple'>])

View File

@@ -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

View File

@@ -8,6 +8,7 @@ Make multiple ipa calls via one remote procedure call
|methods|:ref:`Dict<Dict>`|False
### Options
* keeponly : :ref:`Str<Str>`
* version : :ref:`Str<Str>`
### Output

View File

@@ -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

View File

@@ -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

View File

@@ -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(