Skip to content

Commit e296f40

Browse files
authored
[zos_apf] Port shell commands to Python APIs (#2055)
* shell commands to api transition * changes for sanity test * changelog addition * incorporating review comments * removing unused import exceptions
1 parent 55d5cf4 commit e296f40

File tree

2 files changed

+15
-127
lines changed

2 files changed

+15
-127
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trivial:
2+
- zos_apf - Updated the module to use the API instead of shell commands.
3+
(https://github.com/ansible-collections/ibm_zos_core/pull/2055).

plugins/modules/zos_apf.py

Lines changed: 12 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,8 @@
319319

320320
try:
321321
from zoautil_py import zsystem
322-
from zoautil_py import ztypes
323322
except Exception:
324323
zsystem = ZOAUImportError(traceback.format_exc())
325-
ztypes = ZOAUImportError(traceback.format_exc())
326324

327325

328326
# supported data set types
@@ -390,119 +388,6 @@ def backupOper(module, src, backup, tmphlq=None):
390388
return backup_name
391389

392390

393-
def make_apf_command(library, opt, volume=None, sms=None, force_dynamic=None, persistent=None):
394-
"""Returns a string that can run an APF command in a shell.
395-
396-
Parameters
397-
----------
398-
library : str
399-
Name of the data set that will be operated on.
400-
opt : str
401-
APF operation (either add or del)
402-
volume : str
403-
Volume of library.
404-
sms : bool
405-
Whether library is managed by SMS.
406-
force_dynamic : bool
407-
Whether the APF list format should be dynamic.
408-
persistent : dict
409-
Options for persistent entries that should be modified by APF.
410-
411-
Returns
412-
-------
413-
str
414-
APF command.
415-
"""
416-
# -i is available in ZOAU version 1.3.4
417-
# before that all versions will not be able to use -i
418-
if zoau_version_checker.is_zoau_version_higher_than("1.3.4"):
419-
operation = "-i -A" if opt == "add" else "-i -D"
420-
421-
else:
422-
operation = "-A" if opt == "add" else "-D"
423-
424-
operation_args = library
425-
426-
if volume:
427-
operation_args = f"{operation_args},{volume}"
428-
elif sms:
429-
operation_args = f"{operation_args},SMS"
430-
431-
command = f"apfadm {operation} '{operation_args}'"
432-
433-
if force_dynamic:
434-
command = f"{command} -f"
435-
436-
if persistent:
437-
if opt == "add":
438-
persistent_args = f""" -P '{persistent.get("addDataset")}' """
439-
else:
440-
persistent_args = f""" -R '{persistent.get("delDataset")}' """
441-
442-
if persistent.get("marker"):
443-
persistent_args = f""" {persistent_args} -M '{persistent.get("marker")}' """
444-
445-
command = f"{command} {persistent_args}"
446-
447-
return command
448-
449-
450-
def make_apf_batch_command(batch, force_dynamic=None, persistent=None):
451-
"""Returns a string that can run an APF command for multiple operations
452-
in a shell.
453-
454-
Parameters
455-
----------
456-
batch : list
457-
List of dicts containing different APF add/del operations.
458-
force_dynamic : bool
459-
Whether the APF list format should be dynamic.
460-
persistent : dict
461-
Options for persistent entries that should be modified by APF.
462-
463-
Returns
464-
-------
465-
str
466-
APF command.
467-
"""
468-
command = "apfadm"
469-
470-
for item in batch:
471-
if zoau_version_checker.is_zoau_version_higher_than("1.3.4"):
472-
operation = "-i -A" if item["opt"] == "add" else "-i -D"
473-
474-
else:
475-
operation = "-A" if item["opt"] == "add" else "-D"
476-
477-
operation_args = item["dsname"]
478-
479-
volume = item.get("volume")
480-
sms = item.get("sms")
481-
482-
if volume:
483-
operation_args = f"{operation_args},{volume}"
484-
elif sms:
485-
operation_args = f"{operation_args},SMS"
486-
487-
command = f"{command} {operation} '{operation_args}'"
488-
489-
if force_dynamic:
490-
command = f"{command} -f"
491-
492-
if persistent:
493-
if persistent.get("addDataset"):
494-
persistent_args = f""" -P '{persistent.get("addDataset")}' """
495-
else:
496-
persistent_args = f""" -R '{persistent.get("delDataset")}' """
497-
498-
if persistent.get("marker"):
499-
persistent_args = f""" {persistent_args} -M '{persistent.get("marker")}' """
500-
501-
command = f"{command} {persistent_args}"
502-
503-
return command
504-
505-
506391
def main():
507392
"""Initialize the module.
508393
@@ -693,21 +578,21 @@ def main():
693578
item['opt'] = opt
694579
item['dsname'] = item.get('library')
695580
del item['library']
696-
# Commenting this line to implement a workaround for names with '$'. ZOAU should
697-
# release a fix soon so we can uncomment this Python API call.
698-
# ret = zsystem.apf(batch=batch, forceDynamic=force_dynamic, persistent=persistent)
699-
apf_command = make_apf_batch_command(batch, force_dynamic=force_dynamic, persistent=persistent)
700-
rc, out, err = module.run_command(apf_command)
701-
ret = ztypes.ZOAUResponse(rc, out, err, apf_command, 'utf-8')
581+
# ignore=true is added so that it's ignoring in case of addition if already present
582+
# ignore=true is added so that it's ignoring in case the file is not in apf list while deletion
583+
if zoau_version_checker.is_zoau_version_higher_than("1.3.4"):
584+
ret = zsystem.apf(batch=batch, forceDynamic=force_dynamic, persistent=persistent, ignore=True)
585+
else:
586+
ret = zsystem.apf(batch=batch, forceDynamic=force_dynamic, persistent=persistent)
702587
else:
703588
if not library:
704589
module.fail_json(msg='library is required')
705-
# Commenting this line to implement a workaround for names with '$'. ZOAU should
706-
# release a fix soon so we can uncomment this Python API call.
707-
# ret = zsystem.apf(opt=opt, dsname=library, volume=volume, sms=sms, forceDynamic=force_dynamic, persistent=persistent)
708-
apf_command = make_apf_command(library, opt, volume=volume, sms=sms, force_dynamic=force_dynamic, persistent=persistent)
709-
rc, out, err = module.run_command(apf_command)
710-
ret = ztypes.ZOAUResponse(rc, out, err, apf_command, 'utf-8')
590+
# ignore=true is added so that it's ignoring in case of addition if already present
591+
# ignore=true is added so that it's ignoring in case the file is not in apf list while deletion
592+
if zoau_version_checker.is_zoau_version_higher_than("1.3.4"):
593+
ret = zsystem.apf(opt=opt, dsname=library, volume=volume, sms=sms, forceDynamic=force_dynamic, persistent=persistent, ignore=True)
594+
else:
595+
ret = zsystem.apf(opt=opt, dsname=library, volume=volume, sms=sms, forceDynamic=force_dynamic, persistent=persistent)
711596

712597
operOut = ret.stdout_response
713598
operErr = ret.stderr_response

0 commit comments

Comments
 (0)