Skip to content

Commit

Permalink
Update review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvirani committed Apr 26, 2021
1 parent 21b1a85 commit b8370f2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ In this release, we have added support for IDPools, Tasks and Label resources.
- oneview_storage_volume_attachment_facts
- oneview_storage_volume_template
- oneview_storage_volume_template_facts
- oneview_task
- oneview_task_facts
- oneview_uplink_set
- oneview_uplink_set_facts
Expand Down
12 changes: 10 additions & 2 deletions library/oneview_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
type: dict
'''

from ansible.module_utils.oneview import OneViewModule
from ansible.module_utils.oneview import OneViewModule, OneViewModuleException


class TaskModule(OneViewModule):
Expand All @@ -88,7 +88,15 @@ def __init__(self):
self.set_resource_object(self.oneview_client.tasks)

def execute_module(self):
self.current_resource.patch(self.data['uri'])
if not self.current_resource:
return dict(failed=True,
msg=self.MSG_RESOURCE_NOT_FOUND)

try:
self.current_resource.patch(self.data['uri'])
except OneViewModuleException as exception:
error_msg = '; '.join(str(e) for e in exception.args)
raise OneViewModuleException(error_msg)

return dict(
changed=True,
Expand Down
23 changes: 22 additions & 1 deletion test/test_oneview_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import pytest

from hpe_test_utils import OneViewBaseTest
from oneview_module_loader import TaskModule
from oneview_module_loader import TaskModule, OneViewModuleException

ERROR_MSG = 'Fake message error'

Expand Down Expand Up @@ -64,6 +64,27 @@ def test_should_validate_patch(self):
ansible_facts=dict(tasks=self.resource.data)
)

def test_should_fail_when_resource_not_found(self):
self.resource.get_by_name.return_value = None
self.mock_ansible_module.params = copy.deepcopy(PARAMS_FOR_PATCH)

TaskModule().run()

self.mock_ansible_module.exit_json.assert_called_once_with(
failed=True,
changed=False,
msg=TaskModule.MSG_RESOURCE_NOT_FOUND
)

def test_should_return_error_when_expected_state_not_found(self):
self.resource.data = PARAMS_FOR_PATCH.copy()
self.resource.patch.side_effect = OneViewModuleException(ERROR_MSG)
self.mock_ansible_module.params = copy.deepcopy(PARAMS_FOR_PATCH)

TaskModule().run()

self.mock_ansible_module.fail_json.assert_called_once_with(exception=mock.ANY, msg=ERROR_MSG)


if __name__ == '__main__':
pytest.main([__file__])

0 comments on commit b8370f2

Please sign in to comment.