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

    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?


Attachment: signature.asc
Description: OpenPGP digital signature

Manage your subscription for the Freeipa-devel mailing list:
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to