1010Copyright Contributors to the Zowe Project.
1111"""
1212
13- from typing import Optional , Any
13+ from datetime import datetime , timezone
14+ from typing import Any , Literal , Optional
1415
1516from zowe .core_for_zowe_sdk import SdkApi
1617
17- from .response import ConsoleResponse , IssueCommandResponse
18+ from .response import (
19+ ConsoleResponse ,
20+ GetLogMessagesResponse ,
21+ IssueCommandResponse ,
22+ UnsuccessfulGetLogMessagesResponse ,
23+ )
1824
1925
2026class Console (SdkApi ): # type: ignore
@@ -30,9 +36,14 @@ class Console(SdkApi): # type: ignore
3036 """
3137
3238 def __init__ (self , connection : dict [str , Any ], log : bool = True ):
33- super ().__init__ (connection , "/zosmf/restconsoles/consoles/defcn" , logger_name = __name__ , log = log )
34-
35- def issue_command (self , command : str , console : Optional [str ] = None ) -> IssueCommandResponse :
39+ super ().__init__ (connection , "/zosmf/restconsoles/" , logger_name = __name__ , log = log )
40+
41+ def issue_command (
42+ self ,
43+ command : str ,
44+ console : Optional [str ] = None ,
45+ system : Optional [str ] = None
46+ ) -> IssueCommandResponse :
3647 """Issues a command on z/OS Console.
3748
3849 Parameters
@@ -41,15 +52,20 @@ def issue_command(self, command: str, console: Optional[str] = None) -> IssueCom
4152 The z/OS command to be executed
4253 console : Optional[str]
4354 Name of the console that should be used to execute the command (default is None)
55+ system : Optional[str]
56+ Name of the system in the same sysplex that the command is routed to
57+ (default is None, that means the local system)
4458
4559 Returns
4660 -------
4761 IssueCommandResponse
4862 A JSON containing the response from the console command
4963 """
5064 custom_args = self ._create_custom_request_arguments ()
51- custom_args ["url" ] = self ._request_endpoint .replace ("defcn" , console or "defcn" )
52- request_body = {"cmd" : command }
65+ custom_args ["url" ] = "{}consoles/{}" .format (self ._request_endpoint , console or "defcn" )
66+ request_body = {"cmd" :command }
67+ if system is not None :
68+ request_body ["system" ] = system
5369 custom_args ["json" ] = request_body
5470 response_json = self .request_handler .perform_request ("PUT" , custom_args )
5571 return IssueCommandResponse (response_json )
@@ -71,7 +87,88 @@ def get_response(self, response_key: str, console: Optional[str] = None) -> Cons
7187 A JSON containing the response to the command
7288 """
7389 custom_args = self ._create_custom_request_arguments ()
74- request_url = "{}/ solmsgs/{}" .format (console or "defcn" , response_key )
75- custom_args ["url" ] = self . _request_endpoint . replace ( "defcn" , request_url )
90+ request_url = "{}consoles/{}/ solmsgs/{}" .format (self . _request_endpoint , console or "defcn" , response_key )
91+ custom_args ["url" ] = request_url
7692 response_json = self .request_handler .perform_request ("GET" , custom_args )
7793 return ConsoleResponse (response_json )
94+
95+ def get_log_messages (
96+ self ,
97+ time : Optional [datetime ] = None ,
98+ timestamp : Optional [int ] = None ,
99+ time_range : Optional [str ] = None ,
100+ hardcopy : Optional [Literal ["OPERLOG" , "SYSLOG" ]] = None ,
101+ sys_name : Optional [str ] = None ,
102+ direction : Optional [Literal ["backward" , "forward" ]] = None
103+ ) -> GetLogMessagesResponse | UnsuccessfulGetLogMessagesResponse :
104+ """
105+ Retrieve messages from hardcopy logs on the system.
106+
107+ The maximum return size of the log is 10000.
108+ If more than 10000 logs exist in the timeframe, the system returns the first 10000 logs.
109+
110+ See more at: `Get messages from a hardcopy log`_
111+ .. _Get messages from a hardcopy log:
112+ https://www.ibm.com/docs/en/zos/3.2.0?topic=services-get-messages-from-hardcopy-log
113+
114+ Parameters
115+ ----------
116+ time : Optional[datetime]
117+ Specifies when z/OSMF starts to retrieve messages.
118+ This value is used if the timestamp parameter is not specified.
119+ timestamp : Optional[int]
120+ Specifies the UNIX timestamp, which is the number of milliseconds since 1970-01-01 UTC.
121+ This parameter is specified, the "time" parameter is ignored.
122+ time_range : Optional[str]
123+ Specifies the time range for which the log is to be retrieved.
124+ Supported time units include s, m, and h for seconds, minutes, and hours.
125+ The format is nnnu, where nnn is a number 1-999 and u is one of the time units "s", "m", or "h".
126+ For example, 999s of 20m.
127+ The default is 10m.
128+ hardcopy : Optional[Literal['OPERLOG', 'SYSLOG']]
129+ Specify the source where the logs come from.
130+ If not specified, the API tries OPERLOG first.
131+ If the OPERLOG is not enabled on the system, the API returns the SYSLOG.
132+ sys_name : Optional[str]
133+ The name of the system on which the SYSLOG resides.
134+ direction : Optional[Literal['backward', 'forward']]
135+ Specifies the direction (from a specified time) in which messages are retrieved.
136+ The default is "backward", meaning that messages are retrieved backward from the specified time.
137+
138+ Returns
139+ -------
140+ GetLogMessagesResponse | UnsuccessfulGetLogMessagesResponse
141+ A response content for a successful/unsuccessful get messages request response
142+ """
143+ custom_args = self ._create_custom_request_arguments ()
144+ custom_args ["url" ] = "{}v1/log" .format (self ._request_endpoint )
145+ params = {}
146+ if time is not None and timestamp is not None :
147+ self .logger .warning (
148+ 'Both "time" and "timestamp" query parameters are provided. "time" parameter is ignored'
149+ )
150+ time = None
151+ if time is not None :
152+ if time .tzinfo is not None and time .tzinfo != timezone .utc :
153+ self .logger .warning (
154+ f'"time" query parameter is not in UTC timezone ({ time .tzinfo } ). Conversion to UTC will occur'
155+ )
156+ time = time .astimezone (timezone .utc )
157+ params ["time" ] = time
158+ if timestamp :
159+ params ["timestamp" ] = timestamp
160+ if time_range :
161+ params ["timeRange" ] = time_range
162+ if hardcopy :
163+ params ["hardcopy" ] = hardcopy
164+ if sys_name :
165+ params ["sysName" ] = sys_name
166+ if direction :
167+ params ["direction" ] = direction
168+ custom_args ["params" ] = params
169+ response_json = self .request_handler .perform_request ("GET" , custom_args , expected_code = [200 , 400 , 500 ])
170+ return (
171+ GetLogMessagesResponse (response_json )
172+ if "returnCode" not in response_json .keys ()
173+ else UnsuccessfulGetLogMessagesResponse (response_json )
174+ )
0 commit comments