mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Modify job schedule to allow list of date and times instead of a single datetime (#51510)
* changes to clusteR * Revert "changes to clusteR" This reverts commit 33ee1b71e4bc8435fb315762a871f8c4cb6c5f80. * allow for more than 1 time to be set * move file to correct directory * Ansibot fixes
This commit is contained in:
parent
4876e9a48a
commit
a722133640
2 changed files with 103 additions and 59 deletions
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
# (c) 2018, NetApp, Inc
|
# (c) 2018-2019, NetApp, Inc
|
||||||
# GNU General Public License v3.0+
|
# GNU General Public License v3.0+
|
||||||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
@ -38,7 +38,8 @@ options:
|
||||||
-1 represents all minutes and is
|
-1 represents all minutes and is
|
||||||
only supported for cron schedule create and modify.
|
only supported for cron schedule create and modify.
|
||||||
Range is [-1..59]
|
Range is [-1..59]
|
||||||
job_hour:
|
type: list
|
||||||
|
job_hours:
|
||||||
version_added: '2.8'
|
version_added: '2.8'
|
||||||
description:
|
description:
|
||||||
- The hour(s) of the day when the job should be run.
|
- The hour(s) of the day when the job should be run.
|
||||||
|
@ -46,7 +47,8 @@ options:
|
||||||
-1 represents all hours and is
|
-1 represents all hours and is
|
||||||
only supported for cron schedule create and modify.
|
only supported for cron schedule create and modify.
|
||||||
Range is [-1..23]
|
Range is [-1..23]
|
||||||
job_month:
|
type: list
|
||||||
|
job_months:
|
||||||
version_added: '2.8'
|
version_added: '2.8'
|
||||||
description:
|
description:
|
||||||
- The month(s) when the job should be run.
|
- The month(s) when the job should be run.
|
||||||
|
@ -54,7 +56,8 @@ options:
|
||||||
-1 represents all months and is
|
-1 represents all months and is
|
||||||
only supported for cron schedule create and modify.
|
only supported for cron schedule create and modify.
|
||||||
Range is [-1..11]
|
Range is [-1..11]
|
||||||
job_day_of_month:
|
type: list
|
||||||
|
job_days_of_month:
|
||||||
version_added: '2.8'
|
version_added: '2.8'
|
||||||
description:
|
description:
|
||||||
- The day(s) of the month when the job should be run.
|
- The day(s) of the month when the job should be run.
|
||||||
|
@ -62,7 +65,8 @@ options:
|
||||||
-1 represents all days of a month from 1 to 31, and is
|
-1 represents all days of a month from 1 to 31, and is
|
||||||
only supported for cron schedule create and modify.
|
only supported for cron schedule create and modify.
|
||||||
Range is [-1..31]
|
Range is [-1..31]
|
||||||
job_day_of_week:
|
type: list
|
||||||
|
job_days_of_week:
|
||||||
version_added: '2.8'
|
version_added: '2.8'
|
||||||
description:
|
description:
|
||||||
- The day(s) in the week when the job should be run.
|
- The day(s) in the week when the job should be run.
|
||||||
|
@ -70,6 +74,7 @@ options:
|
||||||
Zero represents Sunday. -1 represents all days of a week and is
|
Zero represents Sunday. -1 represents all days of a week and is
|
||||||
only supported for cron schedule create and modify.
|
only supported for cron schedule create and modify.
|
||||||
Range is [-1..6]
|
Range is [-1..6]
|
||||||
|
type: list
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = """
|
EXAMPLES = """
|
||||||
|
@ -78,9 +83,9 @@ EXAMPLES = """
|
||||||
state: present
|
state: present
|
||||||
name: jobName
|
name: jobName
|
||||||
job_minutes: 30
|
job_minutes: 30
|
||||||
job_hour: 23
|
job_hours: 23
|
||||||
job_day_of_month: 10
|
job_days_of_month: 10
|
||||||
job_month: -1
|
job_months: -1
|
||||||
hostname: "{{ netapp_hostname }}"
|
hostname: "{{ netapp_hostname }}"
|
||||||
username: "{{ netapp_username }}"
|
username: "{{ netapp_username }}"
|
||||||
password: "{{ netapp_password }}"
|
password: "{{ netapp_password }}"
|
||||||
|
@ -117,11 +122,11 @@ class NetAppONTAPJob(object):
|
||||||
state=dict(required=False, choices=[
|
state=dict(required=False, choices=[
|
||||||
'present', 'absent'], default='present'),
|
'present', 'absent'], default='present'),
|
||||||
name=dict(required=True, type='str'),
|
name=dict(required=True, type='str'),
|
||||||
job_minutes=dict(required=False, type='int'),
|
job_minutes=dict(required=False, type='list'),
|
||||||
job_month=dict(required=False, type='int'),
|
job_months=dict(required=False, type='list'),
|
||||||
job_hour=dict(required=False, type='int'),
|
job_hours=dict(required=False, type='list'),
|
||||||
job_day_of_month=dict(required=False, type='int'),
|
job_days_of_month=dict(required=False, type='list'),
|
||||||
job_day_of_week=dict(required=False, type='int')
|
job_days_of_week=dict(required=False, type='list')
|
||||||
))
|
))
|
||||||
|
|
||||||
self.module = AnsibleModule(
|
self.module = AnsibleModule(
|
||||||
|
@ -131,6 +136,7 @@ class NetAppONTAPJob(object):
|
||||||
|
|
||||||
self.na_helper = NetAppModule()
|
self.na_helper = NetAppModule()
|
||||||
self.parameters = self.na_helper.set_parameters(self.module.params)
|
self.parameters = self.na_helper.set_parameters(self.module.params)
|
||||||
|
self.set_playbook_zapi_key_map()
|
||||||
|
|
||||||
if HAS_NETAPP_LIB is False:
|
if HAS_NETAPP_LIB is False:
|
||||||
self.module.fail_json(
|
self.module.fail_json(
|
||||||
|
@ -138,6 +144,18 @@ class NetAppONTAPJob(object):
|
||||||
else:
|
else:
|
||||||
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module)
|
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module)
|
||||||
|
|
||||||
|
def set_playbook_zapi_key_map(self):
|
||||||
|
self.na_helper.zapi_string_keys = {
|
||||||
|
'name': 'job-schedule-name',
|
||||||
|
}
|
||||||
|
self.na_helper.zapi_list_keys = {
|
||||||
|
'job_minutes': ('job-schedule-cron-minute', 'cron-minute'),
|
||||||
|
'job_months': ('job-schedule-cron-month', 'cron-month'),
|
||||||
|
'job_hours': ('job-schedule-cron-hour', 'cron-hour'),
|
||||||
|
'job_days_of_month': ('job-schedule-cron-day', 'cron-day-of-month'),
|
||||||
|
'job_days_of_week': ('job-schedule-cron-day-of-week', 'cron-day-of-week')
|
||||||
|
}
|
||||||
|
|
||||||
def get_job_schedule(self):
|
def get_job_schedule(self):
|
||||||
"""
|
"""
|
||||||
Return details about the job
|
Return details about the job
|
||||||
|
@ -158,25 +176,19 @@ class NetAppONTAPJob(object):
|
||||||
job_details = None
|
job_details = None
|
||||||
# check if job exists
|
# check if job exists
|
||||||
if result.get_child_by_name('num-records') and int(result['num-records']) >= 1:
|
if result.get_child_by_name('num-records') and int(result['num-records']) >= 1:
|
||||||
job_exists_info = result['attributes-list']['job-schedule-cron-info']
|
job_info = result['attributes-list']['job-schedule-cron-info']
|
||||||
job_details = {
|
job_details = dict()
|
||||||
'name': job_exists_info.get_child_content('job-schedule-name'),
|
for item_key, zapi_key in self.na_helper.zapi_string_keys.items():
|
||||||
'job_minutes': int(job_exists_info['job-schedule-cron-minute']['cron-minute']),
|
job_details[item_key] = job_info[zapi_key]
|
||||||
# set default values to other job attributes (by default, cron runs on all months if months is empty)
|
for item_key, zapi_key in self.na_helper.zapi_list_keys.items():
|
||||||
'job_hour': 0,
|
parent, dummy = zapi_key
|
||||||
'job_month': -1,
|
job_details[item_key] = self.na_helper.get_value_for_list(from_zapi=True,
|
||||||
'job_day_of_month': 0,
|
zapi_parent=job_info.get_child_by_name(parent)
|
||||||
'job_day_of_week': 0
|
)
|
||||||
}
|
# if any of the job_hours, job_minutes, job_months, job_days are empty:
|
||||||
if job_exists_info.get_child_by_name('job-schedule-cron-hour'):
|
# it means the value is -1 for ZAPI
|
||||||
job_details['job_hour'] = int(job_exists_info['job-schedule-cron-hour']['cron-hour'])
|
if not job_details[item_key]:
|
||||||
if job_exists_info.get_child_by_name('job-schedule-cron-month'):
|
job_details[item_key] = ['-1']
|
||||||
job_details['job_month'] = int(job_exists_info['job-schedule-cron-month']['cron-month'])
|
|
||||||
if job_exists_info.get_child_by_name('job-schedule-cron-day'):
|
|
||||||
job_details['job_day_of_month'] = int(job_exists_info['job-schedule-cron-day']['cron-day-of-month'])
|
|
||||||
if job_exists_info.get_child_by_name('job-schedule-cron-day-of-week'):
|
|
||||||
job_details['job_day_of_week'] = int(job_exists_info['job-schedule-cron-day-of-week']
|
|
||||||
['cron-day-of-week'])
|
|
||||||
return job_details
|
return job_details
|
||||||
|
|
||||||
def add_job_details(self, na_element_object, values):
|
def add_job_details(self, na_element_object, values):
|
||||||
|
@ -186,21 +198,16 @@ class NetAppONTAPJob(object):
|
||||||
:param values: dictionary of cron values to be added
|
:param values: dictionary of cron values to be added
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
if values.get('job_minutes'):
|
for item_key in values:
|
||||||
na_element_object.add_node_with_children(
|
if item_key in self.na_helper.zapi_string_keys:
|
||||||
'job-schedule-cron-minute', **{'cron-minute': str(values['job_minutes'])})
|
zapi_key = self.na_helper.zapi_string_keys.get(item_key)
|
||||||
if values.get('job_hour'):
|
na_element_object[zapi_key] = values[item_key]
|
||||||
na_element_object.add_node_with_children(
|
elif item_key in self.na_helper.zapi_list_keys:
|
||||||
'job-schedule-cron-hour', **{'cron-hour': str(values['job_hour'])})
|
parent_key, child_key = self.na_helper.zapi_list_keys.get(item_key)
|
||||||
if values.get('job_month'):
|
na_element_object.add_child_elem(self.na_helper.get_value_for_list(from_zapi=False,
|
||||||
na_element_object.add_node_with_children(
|
zapi_parent=parent_key,
|
||||||
'job-schedule-cron-month', **{'cron-month': str(values['job_month'])})
|
zapi_child=child_key,
|
||||||
if values.get('job_day_of_month'):
|
data=values.get(item_key)))
|
||||||
na_element_object.add_node_with_children(
|
|
||||||
'job-schedule-cron-day', **{'cron-day-of-month': str(values['job_day_of_month'])})
|
|
||||||
if values.get('job_day_of_week'):
|
|
||||||
na_element_object.add_node_with_children(
|
|
||||||
'job-schedule-cron-day-of-week', **{'cron-day-of-week': str(values['job_day_of_week'])})
|
|
||||||
|
|
||||||
def create_job_schedule(self):
|
def create_job_schedule(self):
|
||||||
"""
|
"""
|
||||||
|
@ -210,8 +217,7 @@ class NetAppONTAPJob(object):
|
||||||
if self.parameters.get('job_minutes') is None:
|
if self.parameters.get('job_minutes') is None:
|
||||||
self.module.fail_json(msg='Error: missing required parameter job_minutes for create')
|
self.module.fail_json(msg='Error: missing required parameter job_minutes for create')
|
||||||
|
|
||||||
job_schedule_create = netapp_utils.zapi.NaElement.create_node_with_children(
|
job_schedule_create = netapp_utils.zapi.NaElement('job-schedule-cron-create')
|
||||||
'job-schedule-cron-create', **{'job-schedule-name': self.parameters['name']})
|
|
||||||
self.add_job_details(job_schedule_create, self.parameters)
|
self.add_job_details(job_schedule_create, self.parameters)
|
||||||
try:
|
try:
|
||||||
self.server.invoke_successfully(job_schedule_create,
|
self.server.invoke_successfully(job_schedule_create,
|
||||||
|
@ -225,8 +231,8 @@ class NetAppONTAPJob(object):
|
||||||
"""
|
"""
|
||||||
Delete a job schedule
|
Delete a job schedule
|
||||||
"""
|
"""
|
||||||
job_schedule_delete = netapp_utils.zapi.NaElement.create_node_with_children(
|
job_schedule_delete = netapp_utils.zapi.NaElement('job-schedule-cron-destroy')
|
||||||
'job-schedule-cron-destroy', **{'job-schedule-name': self.parameters['name']})
|
self.add_job_details(job_schedule_delete, self.parameters)
|
||||||
try:
|
try:
|
||||||
self.server.invoke_successfully(job_schedule_delete,
|
self.server.invoke_successfully(job_schedule_delete,
|
||||||
enable_tunneling=True)
|
enable_tunneling=True)
|
||||||
|
@ -265,7 +271,9 @@ class NetAppONTAPJob(object):
|
||||||
self.autosupport_log()
|
self.autosupport_log()
|
||||||
current = self.get_job_schedule()
|
current = self.get_job_schedule()
|
||||||
action = self.na_helper.get_cd_action(current, self.parameters)
|
action = self.na_helper.get_cd_action(current, self.parameters)
|
||||||
|
if action is None and self.parameters['state'] == 'present':
|
||||||
modify = self.na_helper.get_modified_attributes(current, self.parameters)
|
modify = self.na_helper.get_modified_attributes(current, self.parameters)
|
||||||
|
|
||||||
if self.na_helper.changed:
|
if self.na_helper.changed:
|
||||||
if self.module.check_mode:
|
if self.module.check_mode:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -17,7 +17,7 @@ from ansible.modules.storage.netapp.na_ontap_job_schedule \
|
||||||
import NetAppONTAPJob as job_module # module under test
|
import NetAppONTAPJob as job_module # module under test
|
||||||
|
|
||||||
if not netapp_utils.has_netapp_lib():
|
if not netapp_utils.has_netapp_lib():
|
||||||
pytestmark = pytest.skip('skipping as missing required netapp_lib')
|
pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
|
||||||
|
|
||||||
|
|
||||||
def set_module_args(args):
|
def set_module_args(args):
|
||||||
|
@ -64,6 +64,8 @@ class MockONTAPConnection(object):
|
||||||
self.xml_in = xml
|
self.xml_in = xml
|
||||||
if self.kind == 'job':
|
if self.kind == 'job':
|
||||||
xml = self.build_job_schedule_cron_info(self.params)
|
xml = self.build_job_schedule_cron_info(self.params)
|
||||||
|
elif self.kind == 'job_multiple':
|
||||||
|
xml = self.build_job_schedule_multiple_cron_info(self.params)
|
||||||
# TODO: mock invoke_elem for autosupport calls
|
# TODO: mock invoke_elem for autosupport calls
|
||||||
elif self.kind == 'vserver':
|
elif self.kind == 'vserver':
|
||||||
xml = self.build_vserver_info()
|
xml = self.build_vserver_info()
|
||||||
|
@ -92,6 +94,30 @@ class MockONTAPConnection(object):
|
||||||
xml.translate_struct(attributes)
|
xml.translate_struct(attributes)
|
||||||
return xml
|
return xml
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def build_job_schedule_multiple_cron_info(job_details):
|
||||||
|
''' build xml data for vserser-info '''
|
||||||
|
print("CALLED MULTIPLE BUILD")
|
||||||
|
xml = netapp_utils.zapi.NaElement('xml')
|
||||||
|
attributes = {
|
||||||
|
'num-records': 1,
|
||||||
|
'attributes-list': {
|
||||||
|
'job-schedule-cron-info': {
|
||||||
|
'job-schedule-name': job_details['name'],
|
||||||
|
'job-schedule-cron-minute': [
|
||||||
|
{'cron-minute': '25'},
|
||||||
|
{'cron-minute': '35'}
|
||||||
|
],
|
||||||
|
'job-schedule-cron-month': [
|
||||||
|
{'cron-month': '5'},
|
||||||
|
{'cron-month': '10'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xml.translate_struct(attributes)
|
||||||
|
return xml
|
||||||
|
|
||||||
|
|
||||||
class TestMyModule(unittest.TestCase):
|
class TestMyModule(unittest.TestCase):
|
||||||
''' Unit tests for na_ontap_job_schedule '''
|
''' Unit tests for na_ontap_job_schedule '''
|
||||||
|
@ -104,13 +130,13 @@ class TestMyModule(unittest.TestCase):
|
||||||
self.addCleanup(self.mock_module_helper.stop)
|
self.addCleanup(self.mock_module_helper.stop)
|
||||||
self.mock_job = {
|
self.mock_job = {
|
||||||
'name': 'test_job',
|
'name': 'test_job',
|
||||||
'minutes': 25
|
'minutes': '25'
|
||||||
}
|
}
|
||||||
|
|
||||||
def mock_args(self):
|
def mock_args(self):
|
||||||
return {
|
return {
|
||||||
'name': self.mock_job['name'],
|
'name': self.mock_job['name'],
|
||||||
'job_minutes': self.mock_job['minutes'],
|
'job_minutes': [self.mock_job['minutes']],
|
||||||
'hostname': 'test',
|
'hostname': 'test',
|
||||||
'username': 'test_user',
|
'username': 'test_user',
|
||||||
'password': 'test_pass!'
|
'password': 'test_pass!'
|
||||||
|
@ -127,7 +153,7 @@ class TestMyModule(unittest.TestCase):
|
||||||
if kind is None:
|
if kind is None:
|
||||||
job_obj.server = MockONTAPConnection()
|
job_obj.server = MockONTAPConnection()
|
||||||
else:
|
else:
|
||||||
job_obj.server = MockONTAPConnection(kind='job', data=self.mock_job)
|
job_obj.server = MockONTAPConnection(kind=kind, data=self.mock_job)
|
||||||
return job_obj
|
return job_obj
|
||||||
|
|
||||||
def test_module_fail_when_required_args_missing(self):
|
def test_module_fail_when_required_args_missing(self):
|
||||||
|
@ -145,10 +171,20 @@ class TestMyModule(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_existing_job(self):
|
def test_get_existing_job(self):
|
||||||
''' Test if get_job_schedule retuns job details for existing job '''
|
''' Test if get_job_schedule retuns job details for existing job '''
|
||||||
set_module_args(self.mock_args())
|
data = self.mock_args()
|
||||||
|
set_module_args(data)
|
||||||
result = self.get_job_mock_object('job').get_job_schedule()
|
result = self.get_job_mock_object('job').get_job_schedule()
|
||||||
assert result['name'] == self.mock_job['name']
|
assert result['name'] == self.mock_job['name']
|
||||||
assert result['job_minutes'] == self.mock_job['minutes']
|
assert result['job_minutes'] == data['job_minutes']
|
||||||
|
|
||||||
|
def test_get_existing_job_multiple_minutes(self):
|
||||||
|
''' Test if get_job_schedule retuns job details for existing job '''
|
||||||
|
set_module_args(self.mock_args())
|
||||||
|
result = self.get_job_mock_object('job_multiple').get_job_schedule()
|
||||||
|
print(str(result))
|
||||||
|
assert result['name'] == self.mock_job['name']
|
||||||
|
assert result['job_minutes'] == ['25', '35']
|
||||||
|
assert result['job_months'] == ['5', '10']
|
||||||
|
|
||||||
def test_create_error_missing_param(self):
|
def test_create_error_missing_param(self):
|
||||||
''' Test if create throws an error if job_minutes is not specified'''
|
''' Test if create throws an error if job_minutes is not specified'''
|
||||||
|
@ -195,7 +231,7 @@ class TestMyModule(unittest.TestCase):
|
||||||
def test_successful_modify(self):
|
def test_successful_modify(self):
|
||||||
''' Test successful modify job_minutes '''
|
''' Test successful modify job_minutes '''
|
||||||
data = self.mock_args()
|
data = self.mock_args()
|
||||||
data['job_minutes'] = 20
|
data['job_minutes'] = ['20']
|
||||||
set_module_args(data)
|
set_module_args(data)
|
||||||
with pytest.raises(AnsibleExitJson) as exc:
|
with pytest.raises(AnsibleExitJson) as exc:
|
||||||
self.get_job_mock_object('job').apply()
|
self.get_job_mock_object('job').apply()
|
||||||
|
|
Loading…
Reference in a new issue