diff --git a/hnv/client.py b/hnv/client.py index c71d496..b57df91 100644 --- a/hnv/client.py +++ b/hnv/client.py @@ -346,7 +346,7 @@ class Resource(model.Model): def _load_models(self): models = globals().copy() - for _, model_cls in models.iteritems(): + for _, model_cls in models.items(): endpoint = getattr(model_cls, "_endpoint", None) if endpoint is not None: regexp = endpoint.format( @@ -365,7 +365,7 @@ class Resource(model.Model): """Return the associated resource.""" references = {"resource_id": None, "parent_id": None, "grandparent_id": None} - for model_cls, regexp in self._regexp.iteritems(): + for model_cls, regexp in self._regexp.items(): match = regexp.search(self.resource_ref) if match is not None: references.update(match.groupdict()) diff --git a/hnv/common/utils.py b/hnv/common/utils.py index ef23040..01d9ce5 100644 --- a/hnv/common/utils.py +++ b/hnv/common/utils.py @@ -208,3 +208,15 @@ def run_once(function, state={}, errors={}): def get_client(url, username, password, allow_insecure, ca_bundle): """Create a new client for the HNV REST API.""" return _HNVClient(url, username, password, allow_insecure, ca_bundle) + + +def get_as_string(value): + if value is None or isinstance(value, six.text_type): + return value + else: + try: + return value.decode() + except Exception: + # This is important, because None will be returned, + # but not that serious to raise an exception. + LOG.error("Couldn't decode: %r", value) diff --git a/hnv/tests/fake/fake_response.py b/hnv/tests/fake/fake_response.py index 37aba7f..4bd1780 100644 --- a/hnv/tests/fake/fake_response.py +++ b/hnv/tests/fake/fake_response.py @@ -17,6 +17,8 @@ import json import pkg_resources +from hnv.common import utils + class FakeResponse(object): @@ -29,9 +31,10 @@ class FakeResponse(object): def _load_resource(self, resource): """Load the json response for the required resource.""" if resource not in self._cache: - json_response = pkg_resources.resource_stream( + resource_stream = pkg_resources.resource_stream( self._resources, resource) - self._cache[resource] = json.load(json_response) + json_response = utils.get_as_string(resource_stream.read()) + self._cache[resource] = json.loads(json_response) return self._cache[resource] def logical_networks(self):