Skip to content

Commit a90d153

Browse files
Merge pull request #2061 from ansible-collections/enhancement/1988/alias-support-zos_stat-dev
[Enhancement] [zos_stat] Add support for aliases
2 parents 2e70099 + cba5792 commit a90d153

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

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):
@@ -2105,6 +2106,119 @@ def _build_volume_string_iehprogm(volumes):
21052106
volume_string += single_volume_string + ")\n"
21062107
return volume_string
21072108

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

21092223
class DataSetUtils(object):
21102224
def __init__(self, data_set, tmphlq=None):

0 commit comments

Comments
 (0)