Provide better fallback when finding id values

At least in the case of object_store's Account resource, it
intentionally has no id or name value due to the way the REST API is
structured and how it maps to the concepts we're using. However, when
looking for the id value of an Account, we end up going through the
_alternate_id code and don't find one of those, and thusly end up trying
to look up an empty string as the alternate ID.

If we don't have an id or an alternate id, the id value should just be
None, so intercept any potential KeyError and just return None.

Change-Id: I60dde9f8f3c9a0eebaeadc8b2953136ad66c4ed4
This commit is contained in:
Brian Curtin 2016-09-29 14:11:28 -04:00
parent 809f2fac73
commit 1bca393a6a
2 changed files with 29 additions and 1 deletions

View File

@ -290,7 +290,10 @@ class Resource(object):
if name in self._body:
return self._body[name]
else:
return self._body[self._alternate_id()]
try:
return self._body[self._alternate_id()]
except KeyError:
return None
else:
return object.__getattribute__(self, name)

View File

@ -569,6 +569,31 @@ class TestResource(base.TestCase):
self.assertIn("y", Test._uri_mapping())
self.assertIn("z", Test._uri_mapping())
def test__getattribute__id_in_body(self):
id = "lol"
sot = resource2.Resource(id=id)
result = getattr(sot, "id")
self.assertEqual(result, id)
def test__getattribute__id_with_alternate(self):
id = "lol"
class Test(resource2.Resource):
blah = resource2.Body("blah", alternate_id=True)
sot = Test(blah=id)
result = getattr(sot, "id")
self.assertEqual(result, id)
def test__getattribute__id_without_alternate(self):
class Test(resource2.Resource):
id = None
sot = Test()
self.assertIsNone(sot.id)
def test__alternate_id_None(self):
self.assertEqual("", resource2.Resource._alternate_id())