david-caro / python-foreman

Small low level python wrapper around Foreman API
GNU General Public License v2.0
57 stars 37 forks source link

Using api_version=2 fails #77

Closed erezzarum closed 6 years ago

erezzarum commented 7 years ago
#!/usr/bin/env python
from getpass import getpass
from foreman.client import Foreman, ForemanException
FOREMAN_URL = "https://foreman.example.com"
FOREMAN_USER = "foreman_api"
foreman_api = Foreman(url=FOREMAN_URL, auth=(FOREMAN_USER, getpass()), api_version=2)
/usr/lib/python2.7/site-packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    foreman_api = Foreman(url=FOREMAN_URL, auth=(FOREMAN_USER, getpass()), api_version=2)
  File "/root/python-foreman-0.4.14/foreman/client.py", line 608, in __init__
    self._generate_api_defs(use_cache, strict_cache)
  File "/root/python-foreman-0.4.14/foreman/client.py", line 815, in _generate_api_defs
    res_dct,
  File "/root/python-foreman-0.4.14/foreman/client.py", line 399, in parse_resource_definition
    new_dict[api.name] = api.generate_func()
  File "/root/python-foreman-0.4.14/foreman/client.py", line 277, in generate_func
    six.exec_(code)
  File "/usr/lib/python2.7/site-packages/six.py", line 672, in exec_
    exec("""exec _code_ in _globs_, _locs_""")
  File "<string>", line 1, in <module>
  File "<string>", line 1
    def import(self, template, overwrite=None):

foreman-python 0.4.14 requests 2.6.0 six 1.9.0

This does not happen when api_version=1, it manages to fetch the json file for both v1 and v2, though the v2 version is very big, about 1.5MB.

david-caro commented 7 years ago

Hmmm, that traceback does not look complete, can you try to get the whole of it? That will help a lot.

Thanks!

erezzarum commented 7 years ago

Exactly, i tried to debug it but this is all i managed to get out :( Edit: same on python3.5.2

erezzarum commented 7 years ago

The problem looks to be related to foreman's API now introducing "import" via API. It tries to build a function definition named "import" which is illegal. I'm not sure how is it going to affect (so far it's working), but i did a very quick hack to get it working for me: At line 129 in client.py:

        self.name = self._get_name()
        if self.name == "import":
            self.name = "import_"
david-caro commented 7 years ago

Can you send a pull request? If you can with the foreman api definiton too (https://github.com/david-caro/python-foreman/tree/master/foreman/definitions), you can find the one for your foreman version in the cache dire (usually ~/.python-foreman).

Good catch!

david-caro commented 7 years ago

If you don't have time, pass me the api definition at least so I can add it ;)

david-caro commented 6 years ago

Fixed in #84 👏