Skip to content

Commit

Permalink
Merge pull request #683 from HewlettPackard/Tasks_Patch
Browse files Browse the repository at this point in the history
Support Patch endpoint for Task
  • Loading branch information
yuvirani authored Apr 26, 2021
2 parents d0dfcad + b8370f2 commit fcd80af
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 0 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
1 change: 1 addition & 0 deletions endpoints-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
|<sub>/rest/tasks/{id}</sub> | GET{Tree View} | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
|<sub>/rest/tasks/{id}</sub> | GET{Aggregated Task list} | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
|<sub>/rest/tasks/{id}</sub> | GET{Flat Array} | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
|<sub>/rest/tasks/{id}</sub> | PATCH | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| **Uplink Sets** |
|<sub>/rest/uplink-sets</sub> | GET | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
|<sub>/rest/uplink-sets</sub> | POST | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
Expand Down
40 changes: 40 additions & 0 deletions examples/oneview_task.yml
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 )
112 changes: 112 additions & 0 deletions library/oneview_task.py
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()
1 change: 1 addition & 0 deletions test/oneview_module_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
from oneview_switch import SwitchModule
from oneview_switch_facts import SwitchFactsModule
from oneview_switch_type_facts import SwitchTypeFactsModule
from oneview_task import TaskModule
from oneview_task_facts import TaskFactsModule
from oneview_unmanaged_device import UnmanagedDeviceModule
from oneview_unmanaged_device_facts import UnmanagedDeviceFactsModule
Expand Down
90 changes: 90 additions & 0 deletions test/test_oneview_task.py
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__])

0 comments on commit fcd80af

Please sign in to comment.