Hi,

On 18.3.2016 15:26, Christian Heimes wrote:
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.

This is how the framework does things - there is no internal consistency and no singular place where coding of values is handled, lot of the output is generated by ad-hoc code somewhere in post_callbacks. Unfortunately this is not easily fixable.



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?

Yes, the definition is wrong, but I don't think it's worth fixing, since you can't rely on a single-value param having a single value in the output for any other command and param anyway.

Honza

--
Jan Cholasta

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

Reply via email to