Skip to content

Commit

Permalink
Add sys_uuid property to Adapter
Browse files Browse the repository at this point in the history
This adds a sys_uuid to the Adapter class. The ManagedSystem uuid
should never change, so the Adapter class is given a sys_uuid property
and caches the value. This will allow us to use the adapter property
as the parent uuid when addressing CHILD objects.

Change-Id: Iee9933c8f6afe0c9ab13d72f7327676d6f8dd47a
  • Loading branch information
esberglu committed Nov 13, 2017
1 parent 708dc86 commit 651a01b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pypowervm/adapter.py
Expand Up @@ -596,6 +596,7 @@ def __init__(self, session=None, use_cache=False, helpers=None):

self.session = session if session else Session()
self._helpers = self._standardize_helper_list(helpers)
self._sys_uuid = None

@staticmethod
def _standardize_helper_list(helpers):
Expand All @@ -609,6 +610,14 @@ def helpers(self):
"""Returns a copy of the list of helpers for the adapter."""
return list(self._helpers) if self._helpers else []

@property
def sys_uuid(self):
if self._sys_uuid is None:
# Can't use the wrapper here without a local import
resp = self.read('ManagedSystem')
self._sys_uuid = resp.feed.entries[0].uuid
return self._sys_uuid

@property
def traits(self):
return self.session.traits
Expand Down
18 changes: 18 additions & 0 deletions pypowervm/tests/test_adapter.py
Expand Up @@ -842,6 +842,24 @@ def test_extract_atom(self, mock_fromstring, mock_unm_ent, mock_unm_feed):
mock_unm_feed.assert_not_called()
mock_unm_ent.assert_not_called()

@mock.patch('pypowervm.adapter.Adapter.read')
def test_sys_uuid(self, mock_read):

# Set and return the sys_uuid if not yet defined
adapter = adp.Adapter(self.sess)
mock_resp = mock.MagicMock()
mock_resp.feed.entries[0].uuid = 'uuid'
mock_read.return_value = mock_resp
sys_uuid = adapter.sys_uuid
mock_read.assert_called_once_with('ManagedSystem')
self.assertEqual('uuid', sys_uuid)
self.assertEqual('uuid', adapter._sys_uuid)

# Return sys_uuid if defined already
mock_read.reset_mock()
sys_uuid = adapter.sys_uuid
mock_read.assert_not_called()


class TestElement(testtools.TestCase):
def setUp(self):
Expand Down

0 comments on commit 651a01b

Please sign in to comment.