[Freeipa-devel] Converting plugin output
Christian Heimes
cheimes at redhat.com
Fri Mar 18 14:26:48 UTC 2016
Hi,
I'd like to use FreeIPA's RPC interface from Ansible directly. But the
output of plugins is rather unfriendly and unpythonic:
>>> print(api.Command.dnsconfig_show())
{u'result': {u'dn': u'cn=dns,dc=ipa,dc=example', u'idnsallowsyncptr':
(u'FALSE',)}, u'value': None, u'summary': None}
Please notice (u'FALSE',) instead of False.
I have written a simple function that uses the parameter definitions to
convert most values automatically:
def converter(plugin, *args, **kwargs):
response = plugin(*args, **kwargs)
params = {p.name: p for p in plugin.obj.takes_params}
if hasattr(plugin, 'output_params'):
params.update({p.name: p for p in plugin.output_params()})
results = response['result']
if isinstance(results, dict):
results = [results]
for result in results:
for key, value in result.iteritems():
param = params.get(key)
if param is None:
continue
if (value and not param.multivalue and
isinstance(value, (list, tuple))):
if len(value) > 1:
raise ValueError(key, value)
value = value[0]
result[key] = param.convert(value)
return response
It works like a charm for several plugins:
>>> print(converter(api.Command.dnsconfig_show))
{u'result': {u'dn': u'cn=dns,dc=ipa,dc=example', u'idnsallowsyncptr':
False}, u'value': None, u'summary': None}
But it is failing for some plugins like user_find(). The plugin returns
u'memberof_group': (u'admins', u'trust admins'). However
global_output_params defines the value as an optional and single valued
string:
Str('memberof_group?', label=_('Member of groups')).
I think the definition is wrong. memberof_group and some other fields
should be defined as optional and multivalued fields insteads. Even the
field's label uses a plural form.
What do you think?
Christian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/freeipa-devel/attachments/20160318/4a2d4294/attachment.sig>
More information about the Freeipa-devel
mailing list