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
|
||||
|
||||
# (c) 2018, NetApp, Inc
|
||||
# (c) 2018-2019, NetApp, Inc
|
||||
# GNU General Public License v3.0+
|
||||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
@ -38,7 +38,8 @@ options:
|
|||
-1 represents all minutes and is
|
||||
only supported for cron schedule create and modify.
|
||||
Range is [-1..59]
|
||||
job_hour:
|
||||
type: list
|
||||
job_hours:
|
||||
version_added: '2.8'
|
||||
description:
|
||||
- The hour(s) of the day when the job should be run.
|
||||
|
@ -46,7 +47,8 @@ options:
|
|||
-1 represents all hours and is
|
||||
only supported for cron schedule create and modify.
|
||||
Range is [-1..23]
|
||||
job_month:
|
||||
type: list
|
||||
job_months:
|
||||
version_added: '2.8'
|
||||
description:
|
||||
- The month(s) when the job should be run.
|
||||
|
@ -54,7 +56,8 @@ options:
|
|||
-1 represents all months and is
|
||||
only supported for cron schedule create and modify.
|
||||
Range is [-1..11]
|
||||
job_day_of_month:
|
||||
type: list
|
||||
job_days_of_month:
|
||||
version_added: '2.8'
|
||||
description:
|
||||
- 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
|
||||
only supported for cron schedule create and modify.
|
||||
Range is [-1..31]
|
||||
job_day_of_week:
|
||||
type: list
|
||||
job_days_of_week:
|
||||
version_added: '2.8'
|
||||
description:
|
||||
- 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
|
||||
only supported for cron schedule create and modify.
|
||||
Range is [-1..6]
|
||||
type: list
|
||||
'''
|
||||
|
||||
EXAMPLES = """
|
||||
|
@ -78,9 +83,9 @@ EXAMPLES = """
|
|||
state: present
|
||||
name: jobName
|
||||
job_minutes: 30
|
||||
job_hour: 23
|
||||
job_day_of_month: 10
|
||||
job_month: -1
|
||||
job_hours: 23
|
||||
job_days_of_month: 10
|
||||
job_months: -1
|
||||
hostname: "{{ netapp_hostname }}"
|
||||
username: "{{ netapp_username }}"
|
||||
password: "{{ netapp_password }}"
|
||||
|
@ -117,11 +122,11 @@ class NetAppONTAPJob(object):
|
|||
state=dict(required=False, choices=[
|
||||
'present', 'absent'], default='present'),
|
||||
name=dict(required=True, type='str'),
|
||||
job_minutes=dict(required=False, type='int'),
|
||||
job_month=dict(required=False, type='int'),
|
||||
job_hour=dict(required=False, type='int'),
|
||||
job_day_of_month=dict(required=False, type='int'),
|
||||
job_day_of_week=dict(required=False, type='int')
|
||||
job_minutes=dict(required=False, type='list'),
|
||||
job_months=dict(required=False, type='list'),
|
||||
job_hours=dict(required=False, type='list'),
|
||||
job_days_of_month=dict(required=False, type='list'),
|
||||
job_days_of_week=dict(required=False, type='list')
|
||||
))
|
||||
|
||||
self.module = AnsibleModule(
|
||||
|
@ -131,6 +136,7 @@ class NetAppONTAPJob(object):
|
|||
|
||||
self.na_helper = NetAppModule()
|
||||
self.parameters = self.na_helper.set_parameters(self.module.params)
|
||||
self.set_playbook_zapi_key_map()
|
||||
|
||||
if HAS_NETAPP_LIB is False:
|
||||
self.module.fail_json(
|
||||
|
@ -138,6 +144,18 @@ class NetAppONTAPJob(object):
|
|||
else:
|
||||
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):
|
||||
"""
|
||||
Return details about the job
|
||||
|
@ -158,25 +176,19 @@ class NetAppONTAPJob(object):
|
|||
job_details = None
|
||||
# check if job exists
|
||||
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_details = {
|
||||
'name': job_exists_info.get_child_content('job-schedule-name'),
|
||||
'job_minutes': int(job_exists_info['job-schedule-cron-minute']['cron-minute']),
|
||||
# set default values to other job attributes (by default, cron runs on all months if months is empty)
|
||||
'job_hour': 0,
|
||||
'job_month': -1,
|
||||
'job_day_of_month': 0,
|
||||
'job_day_of_week': 0
|
||||
}
|
||||
if job_exists_info.get_child_by_name('job-schedule-cron-hour'):
|
||||
job_details['job_hour'] = int(job_exists_info['job-schedule-cron-hour']['cron-hour'])
|
||||
if job_exists_info.get_child_by_name('job-schedule-cron-month'):
|
||||
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'])
|
||||
job_info = result['attributes-list']['job-schedule-cron-info']
|
||||
job_details = dict()
|
||||
for item_key, zapi_key in self.na_helper.zapi_string_keys.items():
|
||||
job_details[item_key] = job_info[zapi_key]
|
||||
for item_key, zapi_key in self.na_helper.zapi_list_keys.items():
|
||||
parent, dummy = zapi_key
|
||||
job_details[item_key] = self.na_helper.get_value_for_list(from_zapi=True,
|
||||
zapi_parent=job_info.get_child_by_name(parent)
|
||||
)
|
||||
# if any of the job_hours, job_minutes, job_months, job_days are empty:
|
||||
# it means the value is -1 for ZAPI
|
||||
if not job_details[item_key]:
|
||||
job_details[item_key] = ['-1']
|
||||
return job_details
|
||||
|
||||
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
|
||||
:return: None
|
||||
"""
|
||||
if values.get('job_minutes'):
|
||||
na_element_object.add_node_with_children(
|
||||
'job-schedule-cron-minute', **{'cron-minute': str(values['job_minutes'])})
|
||||
if values.get('job_hour'):
|
||||
na_element_object.add_node_with_children(
|
||||
'job-schedule-cron-hour', **{'cron-hour': str(values['job_hour'])})
|
||||
if values.get('job_month'):
|
||||
na_element_object.add_node_with_children(
|
||||
'job-schedule-cron-month', **{'cron-month': str(values['job_month'])})
|
||||
if values.get('job_day_of_month'):
|
||||
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'])})
|
||||
for item_key in values:
|
||||
if item_key in self.na_helper.zapi_string_keys:
|
||||
zapi_key = self.na_helper.zapi_string_keys.get(item_key)
|
||||
na_element_object[zapi_key] = values[item_key]
|
||||
elif item_key in self.na_helper.zapi_list_keys:
|
||||
parent_key, child_key = self.na_helper.zapi_list_keys.get(item_key)
|
||||
na_element_object.add_child_elem(self.na_helper.get_value_for_list(from_zapi=False,
|
||||
zapi_parent=parent_key,
|
||||
zapi_child=child_key,
|
||||
data=values.get(item_key)))
|
||||
|
||||
def create_job_schedule(self):
|
||||
"""
|
||||
|
@ -210,8 +217,7 @@ class NetAppONTAPJob(object):
|
|||
if self.parameters.get('job_minutes') is None:
|
||||
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-cron-create', **{'job-schedule-name': self.parameters['name']})
|
||||
job_schedule_create = netapp_utils.zapi.NaElement('job-schedule-cron-create')
|
||||
self.add_job_details(job_schedule_create, self.parameters)
|
||||
try:
|
||||
self.server.invoke_successfully(job_schedule_create,
|
||||
|
@ -225,8 +231,8 @@ class NetAppONTAPJob(object):
|
|||
"""
|
||||
Delete a job schedule
|
||||
"""
|
||||
job_schedule_delete = netapp_utils.zapi.NaElement.create_node_with_children(
|
||||
'job-schedule-cron-destroy', **{'job-schedule-name': self.parameters['name']})
|
||||
job_schedule_delete = netapp_utils.zapi.NaElement('job-schedule-cron-destroy')
|
||||
self.add_job_details(job_schedule_delete, self.parameters)
|
||||
try:
|
||||
self.server.invoke_successfully(job_schedule_delete,
|
||||
enable_tunneling=True)
|
||||
|
@ -265,7 +271,9 @@ class NetAppONTAPJob(object):
|
|||
self.autosupport_log()
|
||||
current = self.get_job_schedule()
|
||||
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)
|
||||
|
||||
if self.na_helper.changed:
|
||||
if self.module.check_mode:
|
||||
pass
|
||||
|
|
|
@ -17,7 +17,7 @@ from ansible.modules.storage.netapp.na_ontap_job_schedule \
|
|||
import NetAppONTAPJob as job_module # module under test
|
||||
|
||||
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):
|
||||
|
@ -64,6 +64,8 @@ class MockONTAPConnection(object):
|
|||
self.xml_in = xml
|
||||
if self.kind == 'job':
|
||||
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
|
||||
elif self.kind == 'vserver':
|
||||
xml = self.build_vserver_info()
|
||||
|
@ -92,6 +94,30 @@ class MockONTAPConnection(object):
|
|||
xml.translate_struct(attributes)
|
||||
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):
|
||||
''' Unit tests for na_ontap_job_schedule '''
|
||||
|
@ -104,13 +130,13 @@ class TestMyModule(unittest.TestCase):
|
|||
self.addCleanup(self.mock_module_helper.stop)
|
||||
self.mock_job = {
|
||||
'name': 'test_job',
|
||||
'minutes': 25
|
||||
'minutes': '25'
|
||||
}
|
||||
|
||||
def mock_args(self):
|
||||
return {
|
||||
'name': self.mock_job['name'],
|
||||
'job_minutes': self.mock_job['minutes'],
|
||||
'job_minutes': [self.mock_job['minutes']],
|
||||
'hostname': 'test',
|
||||
'username': 'test_user',
|
||||
'password': 'test_pass!'
|
||||
|
@ -127,7 +153,7 @@ class TestMyModule(unittest.TestCase):
|
|||
if kind is None:
|
||||
job_obj.server = MockONTAPConnection()
|
||||
else:
|
||||
job_obj.server = MockONTAPConnection(kind='job', data=self.mock_job)
|
||||
job_obj.server = MockONTAPConnection(kind=kind, data=self.mock_job)
|
||||
return job_obj
|
||||
|
||||
def test_module_fail_when_required_args_missing(self):
|
||||
|
@ -145,10 +171,20 @@ class TestMyModule(unittest.TestCase):
|
|||
|
||||
def test_get_existing_job(self):
|
||||
''' 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()
|
||||
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):
|
||||
''' 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):
|
||||
''' Test successful modify job_minutes '''
|
||||
data = self.mock_args()
|
||||
data['job_minutes'] = 20
|
||||
data['job_minutes'] = ['20']
|
||||
set_module_args(data)
|
||||
with pytest.raises(AnsibleExitJson) as exc:
|
||||
self.get_job_mock_object('job').apply()
|
||||
|
|
Loading…
Reference in a new issue