david-caro / python-foreman

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

Instantiating foreman.client.Foreman object throws exception with python-foreman 0.3 #42

Closed tmclaugh closed 7 years ago

tmclaugh commented 9 years ago

python-foreman: 0.3 Foreman: 1.7.4

The following works in 0.2.2 but broke after creating new virtualenv with 0.3

In [1]: from foreman.client import Foreman

In [2]: foreman_client = Foreman('http://foreman.example.com', ('user', 'password'), api_version=2)
/Users/tom/.virtualenvs/central-booking/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/Users/tom/.virtualenvs/central-booking/lib/python2.7/site-packages/requests/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)
/Users/tom/.virtualenvs/central-booking/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/Users/tom/.virtualenvs/central-booking/lib/python2.7/site-packages/requests/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)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-2-6d24fd5ae9c0> in <module>()
----> 1 foreman_client = Foreman('http://foreman.jana.io', ('tom', 'Camel1Merit!F'), api_version=2)

/Users/tom/.virtualenvs/central-booking/lib/python2.7/site-packages/foreman/client.pyc in __init__(self, url, auth, version, api_version, use_cache, strict_cache, timeout, timeout_post, timeout_delete, timeout_put, verify)
    573             self.version = self.get_foreman_version()
    574
--> 575         self._generate_api_defs(use_cache, strict_cache)
    576         # Instantiate plugins
    577         self.plugins = self._plugins_resources(self)

/Users/tom/.virtualenvs/central-booking/lib/python2.7/site-packages/foreman/client.pyc in _generate_api_defs(self, use_cache, strict_cache)
    743             definitions file as valid
    744         """
--> 745         data = self._get_defs(use_cache, strict_cache=strict_cache)
    746
    747         resource_defs = {}

/Users/tom/.virtualenvs/central-booking/lib/python2.7/site-packages/foreman/client.pyc in _get_defs(self, use_cache, strict_cache)
    728             except ForemanVersionException as exc:
    729                 logging.debug(exc)
--> 730         if not data:
    731             logging.debug("Checking remote server for definitions")
    732             data = self._get_remote_defs()

UnboundLocalError: local variable 'data' referenced before assignment
david-caro commented 9 years ago

can you try now? Sorry for the issue, the next release will focus on adding tests to avoid these mistakes

jonapich commented 7 years ago

Sounds like this fix might have not completely worked.

In client.py, ln788: data = self._get_defs(use_cache, strict_cache=strict_cache) returns None. Then it breaks trying to iterate data['docs']['resources'].

It sounds like _get_remote_defs() tries to create a directory, and if it cannot do so it will return. Shouldn't it return 'data'?

I can run it fine locally, but once in AWS Lambda it breaks; and of course you don't have access to my home directory (talking about which; why would any python package try to create any sort of static data in my home folder!?).

jonapich commented 7 years ago

I was about to file in a pull request but it looks like this bug was already taken care of. I will use a local, fixed copy for now, are we going to see a new release soon for pypi?

david-caro commented 7 years ago

I can try to make a release this weekend, let me check what's new and retest some stuff.

david-caro commented 7 years ago

Released the version 0.4.12, let me know if you have issues with it, cheers!