Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Patch endpoint for Task #683

Merged
merged 3 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 )
104 changes: 104 additions & 0 deletions library/oneview_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/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


class TaskModule(OneViewModule):
MSG_TASK_UPDATED = 'Task has been updated.'
MSG_RESOURCE_NOT_FOUND = 'Task Resource not found.'
VenkateshRavula marked this conversation as resolved.
Show resolved Hide resolved

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):
self.current_resource.patch(self.data['uri'])
VenkateshRavula marked this conversation as resolved.
Show resolved Hide resolved

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
69 changes: 69 additions & 0 deletions test/test_oneview_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/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

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)
)


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