From d8a621015259eaafd8d1fee99db3ca663cd4be5c Mon Sep 17 00:00:00 2001 From: Michael Dovgal Date: Mon, 26 Dec 2016 11:16:53 +0000 Subject: [PATCH] Fix adding non-ascii attrs to Resource objects error Due to these lines of code [0] we don't have an opportunity to add attributes with non-ascii symbols to Resource objects, but information about it will be in _info dict. Example of side effect - quota_show command. Because we don't have such an attr, here [1] it will be added None value instead of real value [2]. This patch fixes this problem. [0] - https://github.com/openstack/python-cinderclient/blob/f8c93ed03b388612ca28b8055debf915ce631cec/cinderclient/apiclient/base.py#L498-L499 [1] - https://github.com/openstack/python-cinderclient/blob/f8c93ed03b388612ca28b8055debf915ce631cec/cinderclient/shell_utils.py#L179 [2] - http://paste.openstack.org/show/593358/ Change-Id: I0493845dafc5dad836e899b9c22d563023c1dab0 Closes-Bug: #1652605 --- cinderclient/apiclient/base.py | 3 ++- cinderclient/tests/unit/test_base.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cinderclient/apiclient/base.py b/cinderclient/apiclient/base.py index 3ae5dff7b..e1f611cf7 100644 --- a/cinderclient/apiclient/base.py +++ b/cinderclient/apiclient/base.py @@ -31,6 +31,7 @@ import six from six.moves.urllib import parse from cinderclient.apiclient import exceptions +from oslo_utils import encodeutils from oslo_utils import strutils @@ -496,7 +497,7 @@ class Resource(RequestIdMixin): # In this case we already defined the attribute on the class continue except UnicodeEncodeError: - pass + setattr(self, encodeutils.safe_encode(k), v) self._info[k] = v def __getattr__(self, k): diff --git a/cinderclient/tests/unit/test_base.py b/cinderclient/tests/unit/test_base.py index 91f96baf3..ce8d9e40b 100644 --- a/cinderclient/tests/unit/test_base.py +++ b/cinderclient/tests/unit/test_base.py @@ -48,6 +48,16 @@ class BaseTest(utils.TestCase): self.assertEqual("", repr(r)) self.assertNotIn("x_openstack_request_ids", repr(r)) + def test_add_non_ascii_attr_to_resource(self): + info = {'gigabytes_тест': -1, + 'volumes_тест': -1, + 'id': 'admin'} + + res = base.Resource(None, info) + + for key, value in info.items(): + self.assertEqual(value, getattr(res, key, None)) + def test_getid(self): self.assertEqual(4, base.getid(4))