Foreman Inventory Plugin fails

It looks like unicode shouldn’t matter:

$ python2
>>> {'comment': True, u'comment': False}
{'comment': False}
$ python3
>>> {'comment': True, u'comment': False}
{'comment': False}

What I do find odd is this part:

It returns element 0 but according to API documentation the structure is:

{
  "all_parameters": [
    {
      "priority": null,
      "created_at": "2018-11-15 19:01:27 UTC",
      "updated_at": "2018-11-15 19:01:27 UTC",
      "id": 513706444,
      "name": "loc_param",
      "value": "abc"
    },
    {
      "priority": null,
      "created_at": "2018-11-15 19:01:27 UTC",
      "updated_at": "2018-11-15 19:01:27 UTC",
      "id": 32400255,
      "name": "org_param",
      "value": "xyz"
    },
    {
      "priority": null,
      "created_at": "2018-11-15 19:01:27 UTC",
      "updated_at": "2018-11-15 19:01:27 UTC",
      "id": 636252244,
      "name": "test",
      "value": "myvalue"
    }
  ]
}

To me that suggests that it’s getting the very first parameter and iterating over the properties of that parameter. Looks like something like this is needed:

def _get_all_params_by_id(self, hid):
    url = "%s/api/v2/hosts/%s" % (self.foreman_url, hid)
    ret = self._get_json(url, [404])
    if not ret or not isinstance(ret, MutableMapping) or not ret.get('all_parameters', []):
        ret = {'all_parameters': []} 
    return {parameter['name']: parameter['value'] for parameter in ret['all_parameters']}

Disclaimer: I only read documentation and source code. None of this has been tested.

Another note: I don’t understand why this script isn’t getting /hosts lists with include=all_parameters and avoid n API calls where n is the number of hosts. Probably a massive performance boost. Looks like this parameter was added in Foreman 1.14.