-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #683 from HewlettPackard/Tasks_Patch
Support Patch endpoint for Task
- Loading branch information
Showing
6 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
### | ||
# Copyright (2021) Hewlett Packard Enterprise Development LP | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
### | ||
--- | ||
- hosts: all | ||
vars: | ||
- config: "{{ playbook_dir }}/oneview_config.json" | ||
contents: "{{lookup('file', config)}}" | ||
tasks: | ||
- name: Gather facts about the last 5 running tasks | ||
oneview_task_facts: | ||
config: "{{ config }}" | ||
params: | ||
count: 5 | ||
view: "tree" | ||
filter: ["taskState='Running'", "isCancellable=true"] | ||
delegate_to: localhost | ||
|
||
- debug: var=tasks | ||
|
||
- name: Sets the state of task to 'Cancelling' | ||
oneview_task: | ||
config: "{{ config }}" | ||
data: | ||
name: "{{ tasks[0]['name'] }}" | ||
uri: "{{ tasks[0]['uri'] }}" | ||
delegate_to: localhost | ||
when: contents.api_version >= 1200 and ( tasks | length > 0 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
### | ||
# Copyright (2021) Hewlett Packard Enterprise Development LP | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
### | ||
ANSIBLE_METADATA = {'status': ['stableinterface'], | ||
'supported_by': 'community', | ||
'metadata_version': '1.1'} | ||
|
||
DOCUMENTATION = ''' | ||
module: oneview_task_facts | ||
short_description: Retrieve facts about the OneView Tasks. | ||
description: | ||
- Retrieve facts about the OneView Tasks. | ||
version_added: "2.4" | ||
requirements: | ||
- "python >= 3.4.0" | ||
- "hpeOneView >= 6.1.0" | ||
author: "Yuvarani Chidambaram (@yuvirani)" | ||
options: | ||
state: | ||
required: True | ||
choices: update - Set the task state to cancelling | ||
type: dict | ||
data: | ||
required: True | ||
type: dict | ||
extends_documentation_fragment: | ||
- oneview | ||
''' | ||
|
||
EXAMPLES = ''' | ||
tasks: | ||
- name: Gather facts about the last 5 running tasks | ||
oneview_task_facts: | ||
config: "{{ config }}" | ||
params: | ||
count: 5 | ||
view: "tree" | ||
filter: ["taskState='Running'", "isCancellable=true"] | ||
delegate_to: localhost | ||
- debug: var=tasks | ||
- name: Sets the state of task to 'Cancelling' | ||
oneview_task: | ||
config: "{{ config }}" | ||
data: | ||
name: "{{ tasks[0]['name'] }}" | ||
uri: "{{ tasks[0]['uri'] }}" | ||
delegate_to: localhost | ||
when: contents.api_version >= 1200 and ( tasks | length > 0 ) | ||
''' | ||
|
||
RETURN = ''' | ||
tasks: | ||
description: The updated task. | ||
returned: Always, but can be null. | ||
type: dict | ||
''' | ||
|
||
from ansible.module_utils.oneview import OneViewModule, OneViewModuleException | ||
|
||
|
||
class TaskModule(OneViewModule): | ||
MSG_TASK_UPDATED = 'Task has been updated.' | ||
MSG_RESOURCE_NOT_FOUND = 'Task Resource not found.' | ||
|
||
def __init__(self): | ||
argument_spec = dict( | ||
data=dict(required=True, type='dict') | ||
) | ||
|
||
super(TaskModule, self).__init__(additional_arg_spec=argument_spec) | ||
|
||
self.set_resource_object(self.oneview_client.tasks) | ||
|
||
def execute_module(self): | ||
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, | ||
msg=self.MSG_TASK_UPDATED, | ||
ansible_facts=dict(tasks=self.current_resource.data)) | ||
|
||
|
||
def main(): | ||
TaskModule().run() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
### | ||
# Copyright (2021) Hewlett Packard Enterprise Development LP | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
### | ||
|
||
import copy | ||
import mock | ||
import pytest | ||
|
||
from hpe_test_utils import OneViewBaseTest | ||
from oneview_module_loader import TaskModule, OneViewModuleException | ||
|
||
ERROR_MSG = 'Fake message error' | ||
|
||
PARAMS_FOR_PATCH = dict( | ||
config='config.json', | ||
data=dict( | ||
associatedResource=dict( | ||
associationType='MANAGED_BY', | ||
resourceCategory='certificates'), | ||
name='Add', | ||
taskState='Running', | ||
taskType='User', | ||
owner='Administrator', | ||
isCancellable=True, | ||
uri="/rest/tasks/D2B856D2-5939-421B-BDCA-FBF7D8961A89") | ||
) | ||
|
||
|
||
@pytest.mark.resource(TestTaskModule='tasks') | ||
class TestTaskModule(OneViewBaseTest): | ||
def test_should_validate_patch(self): | ||
self.resource.get_by_name.return_value = self.resource | ||
|
||
resource_data = PARAMS_FOR_PATCH.copy() | ||
self.resource.data = resource_data | ||
|
||
patch_return = resource_data.copy() | ||
patch_return['taskState'] = 'Cancelling' | ||
patch_return_obj = self.resource.copy() | ||
patch_return_obj.data = patch_return | ||
self.resource.patch.return_value = patch_return_obj | ||
|
||
self.mock_ansible_module.params = copy.deepcopy(PARAMS_FOR_PATCH) | ||
|
||
TaskModule().run() | ||
|
||
self.mock_ansible_module.exit_json.assert_called_once_with( | ||
changed=True, | ||
msg=TaskModule.MSG_TASK_UPDATED, | ||
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__]) |