Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from Rainforest/default_values
Browse files Browse the repository at this point in the history
Changes resource_client to manage default values related to API version
  • Loading branch information
Bruno Souza authored and GitHub Enterprise committed Oct 5, 2016
2 parents e036e83 + 9c05f10 commit af9f3d1
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 26 deletions.
2 changes: 1 addition & 1 deletion hpOneView/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, applianceIp, api_version=200):
self._session = None
self._host = applianceIp
self._cred = None
self._apiVersion = api_version
self._apiVersion = int(api_version)
self._headers = {
'X-API-Version': self._apiVersion,
'Accept': 'application/json',
Expand Down
17 changes: 14 additions & 3 deletions hpOneView/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ class ResourceClient(object):
This class implements common functions for HpOneView API rest
"""

def __init__(self, con, uri):
def __init__(self, con, uri, default_values={}):
self._connection = con
self._uri = uri
self._task_monitor = TaskMonitor(con)
self._default_values = default_values

def get_all(self, start=0, count=-1, filter='', query='', sort='', view='', fields='', uri=None):
"""
Expand Down Expand Up @@ -290,7 +291,9 @@ def update(self, resource, uri=None, force=False, timeout=-1, custom_headers=Non
if force:
uri += '?force=True'

return self.__do_put(uri, resource, timeout, custom_headers)
data = self.__merge_default_values(resource)

return self.__do_put(uri, data, timeout, custom_headers)

def create_with_zero_body(self, uri=None, timeout=-1, custom_headers=None):
"""
Expand Down Expand Up @@ -341,7 +344,9 @@ def create(self, resource, uri=None, timeout=-1, custom_headers=None):
logger.debug('Create (uri = %s, resource = %s)' %
(uri, str(resource)))

return self.__do_post(uri, resource, timeout, custom_headers)
data = self.__merge_default_values(resource)

return self.__do_post(uri, data, timeout, custom_headers)

def patch(self, id_or_uri, operation, path, value, timeout=-1, custom_headers=None):
"""
Expand Down Expand Up @@ -600,3 +605,9 @@ def __get_next_page(self, response, items, requested_count):
return None

return response.get('nextPageUri') if has_next_page else None

def __merge_default_values(self, resource):
api_version = str(self._connection._apiVersion)
data = self._default_values.get(api_version, {}).copy()
data.update(resource)
return data
19 changes: 9 additions & 10 deletions hpOneView/resources/servers/server_profile_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@


class ServerProfileTemplate(object):

URI = '/rest/server-profile-templates'

DEFAULT_VALUES = {
'200': {'type': 'ServerProfileTemplateV1'},
'300': {'type': 'ServerProfileTemplateV2'}
}

def __init__(self, con):
self._connection = con
self._client = ResourceClient(con, self.URI)
self.__default_values = {
'type': 'ServerProfileTemplateV1'
}
self._client = ResourceClient(con, self.URI, self.DEFAULT_VALUES)

def get_all(self, start=0, count=-1, filter='', sort=''):
"""
Expand Down Expand Up @@ -132,9 +135,7 @@ def create(self, resource, timeout=-1):
dict: Created resource.
"""
data = self.__default_values.copy()
data.update(resource)
return self._client.create(resource=data, timeout=timeout)
return self._client.create(resource=resource, timeout=timeout)

def update(self, resource, id_or_uri):
"""
Expand All @@ -148,9 +149,7 @@ def update(self, resource, id_or_uri):
Returns:
dict: The server profile template resource.
"""
data = self.__default_values.copy()
data.update(resource)
return self._client.update(resource=data, uri=id_or_uri)
return self._client.update(resource=resource, uri=id_or_uri)

def delete(self, resource, timeout=-1):
"""
Expand Down
10 changes: 2 additions & 8 deletions tests/unit/resources/servers/test_server_profile_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,16 @@ def test_get_by_name(self, mock_get_by_name):
def test_create(self, mock_create):
template = dict(name="BL460c Gen8 1")

expected_template = template.copy()
expected_template["type"] = "ServerProfileTemplateV1"

self._resource.create(resource=template, timeout=TIMEOUT)
mock_create.assert_called_once_with(resource=expected_template, timeout=TIMEOUT)
mock_create.assert_called_once_with(resource=template, timeout=TIMEOUT)

@mock.patch.object(ResourceClient, 'update')
def test_update(self, mock_update):
uri = "/rest/server-profile-templates/4ff2327f-7638-4b66-ad9d-283d4940a4ae"
template = dict(name="BL460c Gen8 1", macType="Virtual")

expected_template = template.copy()
expected_template["type"] = "ServerProfileTemplateV1"

self._resource.update(resource=template, id_or_uri=uri)
mock_update.assert_called_once_with(resource=expected_template, uri=uri)
mock_update.assert_called_once_with(resource=template, uri=uri)

@mock.patch.object(ResourceClient, 'delete')
def test_delete(self, mock_delete):
Expand Down
76 changes: 72 additions & 4 deletions tests/unit/resources/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ def get_fake(self, uri):
class ResourceTest(unittest.TestCase):
URI = "/rest/testuri"

DEFAULT_VALUES = {
'200': {'type': 'TypeV1'},
'300': {'type': 'TypeV2'}
}

def setUp(self):
super(ResourceTest, self).setUp()
self.host = '127.0.0.1'
self.connection = connection(self.host)
self.resource_client = ResourceClient(self.connection, self.URI)
self.resource_client = ResourceClient(self.connection, self.URI, default_values=self.DEFAULT_VALUES)
self.task = {"task": "task"}
self.response_body = {"body": "body"}
self.custom_headers = {'Accept-Language': 'en_US'}
Expand Down Expand Up @@ -408,8 +413,11 @@ def test_update_with_uri_called_once(self, mock_put):

response = self.resource_client.update(dict_to_update, uri=uri)

expected_dict = self.DEFAULT_VALUES['200'].copy()
expected_dict.update(dict_to_update)

self.assertEqual(self.response_body, response)
mock_put.assert_called_once_with(uri, dict_to_update, custom_headers=None)
mock_put.assert_called_once_with(uri, expected_dict, custom_headers=None)

@mock.patch.object(connection, 'put')
def test_update_with_custom_headers(self, mock_put):
Expand All @@ -428,8 +436,39 @@ def test_update_with_force(self, mock_put):

self.resource_client.update(dict_to_update, uri=uri, force=True)

expected_dict = self.DEFAULT_VALUES['200'].copy()
expected_dict.update(dict_to_update)

expected_uri = "/rest/resource/test?force=True"
mock_put.assert_called_once_with(expected_uri, expected_dict, custom_headers=None)

@mock.patch.object(connection, 'put')
def test_update_with_api_version_300(self, mock_put):
dict_to_update = {"name": "test"}
uri = "/rest/resource/test"

mock_put.return_value = None, self.response_body
self.connection._apiVersion = 300

self.resource_client.update(dict_to_update, uri=uri, force=True)

expected_dict = self.DEFAULT_VALUES['300'].copy()
expected_dict.update(dict_to_update)

expected_uri = "/rest/resource/test?force=True"
mock_put.assert_called_once_with(expected_uri, dict_to_update, custom_headers=None)
mock_put.assert_called_once_with(expected_uri, expected_dict, custom_headers=None)

@mock.patch.object(connection, 'put')
def test_update_without_default_values(self, mock_put):
dict_to_update = {"name": "test"}
uri = "/rest/resource/test"

mock_put.return_value = None, self.response_body

resource_client = ResourceClient(self.connection, self.URI)
resource_client.update(dict_to_update, uri=uri)

mock_put.assert_called_once_with(uri, dict_to_update, custom_headers=None)

@mock.patch.object(connection, 'put')
@mock.patch.object(TaskMonitor, 'wait_for_task')
Expand All @@ -441,8 +480,11 @@ def test_update_uri(self, mock_wait4task, mock_update):
mock_wait4task.return_value = self.task
update_task = self.resource_client.update(dict_to_update, False)

expected_dict = self.DEFAULT_VALUES['200'].copy()
expected_dict.update(dict_to_update)

self.assertEqual(self.task, update_task)
mock_update.assert_called_once_with("a_uri", dict_to_update, custom_headers=None)
mock_update.assert_called_once_with("a_uri", expected_dict, custom_headers=None)

@mock.patch.object(connection, 'put')
@mock.patch.object(TaskMonitor, 'wait_for_task')
Expand Down Expand Up @@ -517,6 +559,32 @@ def test_create_uri(self, mock_post):

self.resource_client.create(dict_to_create, timeout=-1)

expected_dict = self.DEFAULT_VALUES['200'].copy()
expected_dict.update(dict_to_create)

mock_post.assert_called_once_with(self.URI, expected_dict, custom_headers=None)

@mock.patch.object(connection, 'post')
def test_create_with_api_version_300(self, mock_post):
dict_to_create = {"resource_name": "a name"}
mock_post.return_value = {}, {}
self.connection._apiVersion = 300

self.resource_client.create(dict_to_create, timeout=-1)

expected_dict = self.DEFAULT_VALUES['300'].copy()
expected_dict.update(dict_to_create)

mock_post.assert_called_once_with(self.URI, expected_dict, custom_headers=None)

@mock.patch.object(connection, 'post')
def test_create_without_default_values(self, mock_post):
dict_to_create = {"resource_name": "a name"}
mock_post.return_value = {}, {}

resource_client = ResourceClient(self.connection, self.URI)
resource_client.create(dict_to_create, timeout=-1)

mock_post.assert_called_once_with(self.URI, dict_to_create, custom_headers=None)

@mock.patch.object(connection, 'post')
Expand Down

0 comments on commit af9f3d1

Please sign in to comment.