Browse Source

Getting OneView resources by their uuid

Until now, the python-oneviewclient didn't have any function
to get Server Hardwares and Server Profile Templates by
it's uuids. It will make it easy to fix the bug 1541861

Change-Id: I7988e85df0c7bc2aee722b6bd8cd7c2f2d21dd73
Partial-Bug: 1541861
changes/69/276269/8
Caio Oliveira 6 years ago committed by Thiago Paiva
parent
commit
2b6b15a15a
  1. 26
      oneview_client/client.py
  2. 82
      oneview_client/tests/test_oneview_client.py

26
oneview_client/client.py

@ -39,6 +39,9 @@ DELETE_REQUEST_TYPE = 'DELETE'
MOMENTARY_PRESS = 'MomentaryPress'
PRESS_AND_HOLD = 'PressAndHold'
SERVER_HARDWARE_PREFIX_URI = '/rest/server-hardware/'
SERVER_PROFILE_TEMPLATE_PREFIX_URI = '/rest/server-profile-template/'
class Client(object):
@ -176,13 +179,21 @@ class Client(object):
# --- ManagementDriver ---
def get_server_hardware(self, node_info):
server_hardware_uri = node_info.get('server_hardware_uri')
INDEX_BEGIN_UUID = len(SERVER_HARDWARE_PREFIX_URI) - 1
server_hardware_uri = node_info['server_hardware_uri']
uuid = server_hardware_uri[INDEX_BEGIN_UUID:]
return self.get_server_hardware_by_uuid(uuid)
def get_server_hardware_by_uuid(self, uuid):
server_hardware_uri = SERVER_HARDWARE_PREFIX_URI + str(uuid)
server_hardware_json = self._prepare_and_do_request(
uri=server_hardware_uri
)
if server_hardware_json.get("uri") is None:
message = "OneView Server Hardware resource not found."
raise exceptions.OneViewResourceNotFoundError(message)
return ServerHardware.from_json(server_hardware_json)
def get_server_profile_from_hardware(self, node_info):
@ -208,9 +219,16 @@ class Client(object):
return ServerProfile.from_json(server_profile_json)
def get_server_profile_template(self, node_info):
server_profile_template_uri = (
node_info.get('server_profile_template_uri')
)
INDEX_BEGIN_UUID = len(SERVER_PROFILE_TEMPLATE_PREFIX_URI) - 1
server_hardware_uri = node_info['server_profile_template_uri']
uuid = server_hardware_uri[INDEX_BEGIN_UUID:]
return self.get_server_profile_template_by_uuid(uuid)
def get_server_profile_template_by_uuid(self, uuid):
server_profile_template_uri = SERVER_PROFILE_TEMPLATE_PREFIX_URI \
+ str(uuid)
spt_json = self._prepare_and_do_request(
uri=server_profile_template_uri
)

82
oneview_client/tests/test_oneview_client.py

@ -378,10 +378,12 @@ class OneViewClientTestCase(unittest.TestCase):
oneview_client.set_node_power_state, driver_info, target_state
)
@mock.patch.object(client.Client, '_prepare_and_do_request', autospec=True)
def test_get_server_hardware_nonexistent(self, mock__prepare_do_request,
@mock.patch.object(requests, 'get')
def test_get_server_hardware_nonexistent(self, mock_get,
mock__authenticate):
mock__prepare_do_request.return_value = {"error": "resource not found"}
response = mock_get.return_value
response.status_code = http_client.NOT_FOUND
mock_get.return_value = response
driver_info = {"server_hardware_uri": ""}
oneview_client = client.Client(self.manager_url,
@ -394,6 +396,39 @@ class OneViewClientTestCase(unittest.TestCase):
driver_info
)
@mock.patch.object(requests, 'get')
def test_get_server_hardware_nonexistent_by_uuid(
self, mock_get, mock__authenticate
):
response = mock_get.return_value
response.status_code = http_client.NOT_FOUND
mock_get.return_value = response
uuid = 0
oneview_client = client.Client(self.manager_url,
self.username,
self.password)
self.assertRaises(
exceptions.OneViewResourceNotFoundError,
oneview_client.get_server_hardware_by_uuid,
uuid
)
@mock.patch.object(client.Client, '_prepare_and_do_request', autospec=True)
def test_get_server_hardware_by_uuid(
self, mock__prepare_do_request, mock__authenticate
):
mock__prepare_do_request.return_value = {
"uri": "/rest/server-hardware/555"
}
uuid = 555
oneview_client = client.Client(self.manager_url,
self.username,
self.password)
oneview_client.get_server_hardware_by_uuid(uuid)
mock__prepare_do_request.assert_called_once_with(
oneview_client, uri="/rest/server-hardware/555"
)
@mock.patch.object(client.Client, '_prepare_and_do_request', autospec=True)
@mock.patch.object(client.Client, 'get_server_profile_from_hardware',
autospec=True)
@ -477,6 +512,39 @@ class OneViewClientTestCase(unittest.TestCase):
new_first_boot_device
)
@mock.patch.object(requests, 'get')
def test_get_server_profile_template_nonexistent_by_uuid(
self, mock_get, mock__authenticate
):
response = mock_get.return_value
response.status_code = http_client.NOT_FOUND
mock_get.return_value = response
uuid = 0
oneview_client = client.Client(self.manager_url,
self.username,
self.password)
self.assertRaises(
exceptions.OneViewResourceNotFoundError,
oneview_client.get_server_profile_template_by_uuid,
uuid
)
@mock.patch.object(client.Client, '_prepare_and_do_request', autospec=True)
def test_get_server_profile_template_by_uuid(
self, mock__prepare_do_request, mock__authenticate
):
mock__prepare_do_request.return_value = {
"uri": "/rest/server-profile-template/123"
}
uuid = 123
oneview_client = client.Client(self.manager_url,
self.username,
self.password)
oneview_client.get_server_profile_template_by_uuid(uuid)
mock__prepare_do_request.assert_called_once_with(
oneview_client, uri="/rest/server-profile-template/123"
)
@mock.patch.object(client.Client, '_prepare_and_do_request', autospec=True)
def test__wait_for_task_to_complete(self, mock__prepare_do_request,
mock__authenticate):
@ -524,17 +592,18 @@ class OneViewClientTestCase(unittest.TestCase):
oneview_client = client.Client(self.manager_url,
self.username,
self.password,
max_polling_attempts=3)
max_polling_attempts=2)
response = mock_get.return_value
response.status_code = http_client.REQUEST_TIMEOUT
mock_get.return_value = response
self.assertRaises(
retrying.RetryError,
oneview_client._wait_for_task_to_complete,
task,
)
self.assertEqual(mock_get.call_count, 3)
self.assertEqual(mock_get.call_count, 2)
@mock.patch.object(client.Client, 'get_server_hardware', autospec=True)
def test_validate_node_server_hardware_inconsistent_memorymb_value(
@ -956,7 +1025,8 @@ class OneViewClientTestCase(unittest.TestCase):
@mock.patch.object(client.Client, 'get_server_profile_from_hardware')
def test_is_mac_compatible_with_server_profile(
self, mock_get_server_profile_from_hardware, mock__authenticate):
self, mock_get_server_profile_from_hardware, mock__authenticate
):
oneview_client = client.Client(self.manager_url,
self.username,
self.password)

Loading…
Cancel
Save