From 1e37be448441b11aaf72160b8bd824b2369507ad Mon Sep 17 00:00:00 2001 From: Ruby Loo Date: Tue, 28 Jun 2016 22:13:44 -0400 Subject: [PATCH] Add invalid attribute names to exception When creating a resource via CreateManager.create(), an InvalidAttribute could be raised for invalid attributes (attributes that are not needed to create the resource). The raised exception had no message. This changes the exception to include a list of all the invalid attributes as well as indicate what the problem is. Change-Id: Iaea056ea6946a6f813f8081c26f62dcfe63d1bbd --- ironicclient/common/base.py | 9 ++++++++- ironicclient/tests/unit/common/test_base.py | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ironicclient/common/base.py b/ironicclient/common/base.py index 5925f73d4..61050c7ea 100644 --- a/ironicclient/common/base.py +++ b/ironicclient/common/base.py @@ -201,11 +201,18 @@ class CreateManager(Manager): """ new = {} + invalid = [] for (key, value) in kwargs.items(): if key in self._creation_attributes: new[key] = value else: - raise exc.InvalidAttribute() + invalid.append(key) + if invalid: + raise exc.InvalidAttribute( + 'The attribute(s) "%(attrs)s" are invalid; they are not ' + 'needed to create %(resource)s.' % + {'resource': self._resource_name, + 'attrs': '","'.join(invalid)}) url = self._path() resp, body = self.api.json_request('POST', url, body=new) if body: diff --git a/ironicclient/tests/unit/common/test_base.py b/ironicclient/tests/unit/common/test_base.py index 28d84f476..927f71216 100644 --- a/ironicclient/tests/unit/common/test_base.py +++ b/ironicclient/tests/unit/common/test_base.py @@ -117,8 +117,9 @@ class ManagerTestCase(testtools.TestCase): self.assertIsInstance(resource, TestableResource) def test_create_with_invalid_attribute(self): - self.assertRaises(exc.InvalidAttribute, self.manager.create, - **INVALID_ATTRIBUTE_TESTABLE_RESOURCE) + self.assertRaisesRegex(exc.InvalidAttribute, "non-existent-attribute", + self.manager.create, + **INVALID_ATTRIBUTE_TESTABLE_RESOURCE) def test_get(self): resource = self.manager.get(TESTABLE_RESOURCE['uuid'])