Skip to content

Commit 98a7f5c

Browse files
committed
[Enhancement] [zos_stat] Add support for aliases (#2048)
1 parent 85a8668 commit 98a7f5c

File tree

5 files changed

+321
-61
lines changed

5 files changed

+321
-61
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
minor_changes:
2+
- zos_stat - Adds support to query data sets using their aliases.
3+
(https://github.com/ansible-collections/ibm_zos_core/pull/2048)

docs/source/modules/zos_stat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Notes
196196
-----
197197

198198
.. note::
199-
When querying data sets, the module will create a temporary data set that requires around 4 kilobytes of available space on the managed node. This data set will be removed before the module finishes execution.
199+
When querying data sets, the module will create two temporary data sets. One requires around 4 kilobytes of available space on the managed node. The second one, around 1 kilobyte of available space. Both data sets will be removed before the module finishes execution.
200200

201201
Sometimes, the system could be unable to properly determine the organization or record format of the data set or the space units used to represent its allocation. When this happens, the values for these fields will be null.
202202

plugins/module_utils/data_set.py

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import re
1717
import tempfile
1818
import traceback
19-
from os import path, walk
19+
from os import path, walk, environ
2020
from random import sample
2121
from string import ascii_uppercase, digits
2222

@@ -35,12 +35,13 @@
3535
vtoc = MissingImport("vtoc")
3636

3737
try:
38-
from zoautil_py import datasets, exceptions, gdgs
38+
from zoautil_py import datasets, exceptions, gdgs, mvscmd, ztypes
3939
except ImportError:
4040
datasets = ZOAUImportError(traceback.format_exc())
4141
exceptions = ZOAUImportError(traceback.format_exc())
4242
gdgs = ZOAUImportError(traceback.format_exc())
43-
Dataset = ZOAUImportError(traceback.format_exc())
43+
mvscmd = ZOAUImportError(traceback.format_exc())
44+
ztypes = ZOAUImportError(traceback.format_exc())
4445

4546

4647
class DataSet(object):
@@ -2092,6 +2093,119 @@ def _build_volume_string_iehprogm(volumes):
20922093
volume_string += single_volume_string + ")\n"
20932094
return volume_string
20942095

2096+
@staticmethod
2097+
def get_name_if_data_set_is_alias(name, tmp_hlq=None):
2098+
"""Checks the catalog to see if 'name' corresponds to a data set
2099+
alias and returns the original data set name in case it is.
2100+
Creates a temp data set to hold the IDCAMS command.
2101+
2102+
Parameters
2103+
----------
2104+
name : str
2105+
Name of a data set or alias.
2106+
2107+
Keyword Parameters
2108+
------------------
2109+
tmp_hlq : str
2110+
Temp HLQ to use with mvscmdauth.
2111+
2112+
Returns
2113+
-------
2114+
tuple(bool, str)
2115+
A tuple containing whether name corresponds to a data
2116+
set alias and the name of the data set that the alias
2117+
points to.
2118+
"""
2119+
# We need to unescape because this call to the system can handle
2120+
# special characters just fine.
2121+
name = name.upper().replace("\\", '')
2122+
idcams_cmd = f" LISTCAT ENTRIES('{name}') ALL"
2123+
response = DataSet._execute_idcams_cmd(idcams_cmd, tmp_hlq=tmp_hlq)
2124+
2125+
if response.rc > 0 or response.stderr_response != '':
2126+
raise MVSCmdExecError(
2127+
rc=response.rc,
2128+
stdout=response.stdout_response,
2129+
stderr=response.stderr_response
2130+
)
2131+
2132+
if re.search(r'(ALIAS -+)(1)', response.stdout_response):
2133+
base_name = re.search(
2134+
r'(ASSOCIATIONS\s*\n\s*[0-9a-zA-Z]+-+)([0-9a-zA-Z\.@\$#-]+)',
2135+
response.stdout_response
2136+
).group(2)
2137+
return True, base_name
2138+
else:
2139+
return False, name
2140+
2141+
@staticmethod
2142+
def _execute_idcams_cmd(
2143+
cmd,
2144+
tmp_hlq=None,
2145+
space_primary=1,
2146+
space_type='k',
2147+
record_format='fb',
2148+
record_length=120
2149+
):
2150+
"""Runs an IDCAMS command using mvscmdauth's Python API.
2151+
2152+
Parameters
2153+
----------
2154+
cmd : str
2155+
IDCAMS command to run.
2156+
2157+
Keyword Parameters
2158+
------------------
2159+
tmp_hlq : str
2160+
Temp HLQ to use with mvscmdauth.
2161+
space_primary : int
2162+
Units of primary space for the input data set for IDCAMS.
2163+
space_type : str
2164+
Unit of data set space.
2165+
record_format : str
2166+
Record format for the input data set.
2167+
record_length : int
2168+
Record length for the input data set.
2169+
2170+
Returns
2171+
-------
2172+
ztypes.ZOAUResponse
2173+
Response object returned by mvscmd.execute_authorized.
2174+
"""
2175+
temp_dd_location = None
2176+
2177+
try:
2178+
temp_dd_location = DataSet.create_temp(
2179+
hlq=tmp_hlq,
2180+
type='SEQ',
2181+
record_format=record_format,
2182+
space_primary=space_primary,
2183+
space_secondary=0,
2184+
space_type=space_type,
2185+
record_length=record_length
2186+
)
2187+
2188+
datasets.write(temp_dd_location, cmd)
2189+
cmd_dd = ztypes.DatasetDefinition(temp_dd_location, disposition='SHR')
2190+
2191+
dds = [
2192+
ztypes.DDStatement('SYSPRINT', '*'),
2193+
ztypes.DDStatement('SYSIN', cmd_dd)
2194+
]
2195+
2196+
if tmp_hlq:
2197+
environ['TMPHLQ'] = tmp_hlq
2198+
2199+
response = mvscmd.execute_authorized('IDCAMS', dds=dds)
2200+
2201+
if tmp_hlq:
2202+
del environ['TMPHLQ']
2203+
2204+
return response
2205+
finally:
2206+
if temp_dd_location:
2207+
datasets.delete(temp_dd_location)
2208+
20952209

20962210
class DataSetUtils(object):
20972211
def __init__(self, data_set, tmphlq=None):

0 commit comments

Comments
 (0)