From 7490f1dc3540f0f6628c4befa78059a965432c3b Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Sat, 14 Feb 2015 19:22:01 -0600 Subject: [PATCH] Add Resource.name property We've been able to set name_attribute for a while now, but it's not actually hooked up to anything. We've also been attempting to access it through Resource.find and getting lucky that resources typically do have a name property set. This change adds it so we can reliably access it and provide the fallback that we've sort of been providing all along. You can also set the name here, which sets the underlying attribute pointed to by name_attribute. Change-Id: I081816adebbebf26339b5b57e3f79fc7b10649f7 --- openstack/resource.py | 17 +++++++++++++++++ openstack/tests/test_resource.py | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/openstack/resource.py b/openstack/resource.py index 022eee0d..e157c418 100644 --- a/openstack/resource.py +++ b/openstack/resource.py @@ -331,6 +331,23 @@ class Resource(collections.MutableMapping): def id(self): del self._attrs[self.id_attribute] + @property + def name(self): + """The name associated with this resource. + + The true value of the ``name`` property comes from the + attribute set as :data:`name_attribute`. + """ + return self._attrs.get(self.name_attribute, None) + + @name.setter + def name(self, value): + self._attrs[self.name_attribute] = value + + @name.deleter + def name(self): + del self._attrs[self.name_attribute] + @property def is_dirty(self): """True if the resource needs to be updated to the remote.""" diff --git a/openstack/tests/test_resource.py b/openstack/tests/test_resource.py index 0c9aca3c..6aab9662 100644 --- a/openstack/tests/test_resource.py +++ b/openstack/tests/test_resource.py @@ -547,6 +547,23 @@ class TestFind(base.TestCase): faker.id_attribute = 'id' self.assertEqual(fake_id, faker.id) + def test_name_attribute(self): + class Person_ES(resource.Resource): + name_attribute = "nombre" + nombre = resource.prop('nombre') + + name = "Brian" + args = {'nombre': name} + + person = Person_ES(args) + self.assertEqual(person.nombre, name) + self.assertEqual(person.name, name) + + new_name = "Julien" + person.name = new_name + self.assertEqual(person.nombre, new_name) + self.assertEqual(person.name, new_name) + def test_boolstr_prop(self): faker = FakeResource(fake_data) self.assertEqual(True, faker.enabled)