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)