1717
1818import logging
1919
20+ from cliff import columns
2021import iso8601
21- from novaclient import api_versions
22+ from openstack import exceptions as sdk_exceptions
23+ from openstack import utils as sdk_utils
2224from osc_lib .command import command
2325from osc_lib import exceptions
2426from osc_lib import utils
3032LOG = logging .getLogger (__name__ )
3133
3234
35+ class ServerActionEventColumn (columns .FormattableColumn ):
36+ """Custom formatter for server action events.
37+
38+ Format the :class:`~openstack.compute.v2.server_action.ServerActionEvent`
39+ objects as we'd like.
40+ """
41+
42+ def _format_event (self , event ):
43+ column_map = {}
44+ hidden_columns = ['id' , 'name' , 'location' ]
45+ _ , columns = utils .get_osc_show_columns_for_sdk_resource (
46+ event ,
47+ column_map ,
48+ hidden_columns ,
49+ )
50+ data = utils .get_item_properties (
51+ event ,
52+ columns ,
53+ )
54+ return dict (zip (columns , data ))
55+
56+ def human_readable (self ):
57+ events = [self ._format_event (event ) for event in self ._value ]
58+ return utils .format_list_of_dicts (events )
59+
60+ def machine_readable (self ):
61+ events = [self ._format_event (event ) for event in self ._value ]
62+ return events
63+
64+
65+ def _get_server_event_columns (item , client ):
66+ column_map = {}
67+ hidden_columns = ['name' , 'server_id' , 'links' , 'location' ]
68+
69+ if not sdk_utils .supports_microversion (client , '2.58' ):
70+ # updated_at was introduced in 2.58
71+ hidden_columns .append ('updated_at' )
72+
73+ return utils .get_osc_show_columns_for_sdk_resource (
74+ item ,
75+ column_map ,
76+ hidden_columns ,
77+ )
78+
79+
3380class ListServerEvent (command .Lister ):
3481 """List recent events of a server.
3582
@@ -38,7 +85,7 @@ class ListServerEvent(command.Lister):
3885 """
3986
4087 def get_parser (self , prog_name ):
41- parser = super (ListServerEvent , self ).get_parser (prog_name )
88+ parser = super ().get_parser (prog_name )
4289 parser .add_argument (
4390 'server' ,
4491 metavar = '<server>' ,
@@ -90,30 +137,33 @@ def get_parser(self, prog_name):
90137 return parser
91138
92139 def take_action (self , parsed_args ):
93- compute_client = self .app .client_manager .compute
140+ compute_client = self .app .client_manager .sdk_connection . compute
94141
95142 kwargs = {}
96143
97144 if parsed_args .marker :
98- if compute_client . api_version < api_versions . APIVersion ( '2.58' ):
145+ if not sdk_utils . supports_microversion ( compute_client , '2.58' ):
99146 msg = _ (
100147 '--os-compute-api-version 2.58 or greater is required to '
101148 'support the --marker option'
102149 )
103150 raise exceptions .CommandError (msg )
151+
104152 kwargs ['marker' ] = parsed_args .marker
105153
106154 if parsed_args .limit :
107- if compute_client . api_version < api_versions . APIVersion ( '2.58' ):
155+ if not sdk_utils . supports_microversion ( compute_client , '2.58' ):
108156 msg = _ (
109157 '--os-compute-api-version 2.58 or greater is required to '
110158 'support the --limit option'
111159 )
112160 raise exceptions .CommandError (msg )
161+
113162 kwargs ['limit' ] = parsed_args .limit
163+ kwargs ['paginated' ] = False
114164
115165 if parsed_args .changes_since :
116- if compute_client . api_version < api_versions . APIVersion ( '2.58' ):
166+ if not sdk_utils . supports_microversion ( compute_client , '2.58' ):
117167 msg = _ (
118168 '--os-compute-api-version 2.58 or greater is required to '
119169 'support the --changes-since option'
@@ -129,7 +179,7 @@ def take_action(self, parsed_args):
129179 kwargs ['changes_since' ] = parsed_args .changes_since
130180
131181 if parsed_args .changes_before :
132- if compute_client . api_version < api_versions . APIVersion ( '2.66' ):
182+ if not sdk_utils . supports_microversion ( compute_client , '2.66' ):
133183 msg = _ (
134184 '--os-compute-api-version 2.66 or greater is required to '
135185 'support the --changes-before option'
@@ -145,10 +195,11 @@ def take_action(self, parsed_args):
145195 kwargs ['changes_before' ] = parsed_args .changes_before
146196
147197 try :
148- server_id = utils .find_resource (
149- compute_client .servers , parsed_args .server ,
198+ server_id = compute_client .find_server (
199+ parsed_args .server ,
200+ ignore_missing = False
150201 ).id
151- except exceptions . CommandError :
202+ except sdk_exceptions . ResourceNotFound :
152203 # If we fail to find the resource, it is possible the server is
153204 # deleted. Try once more using the <server> arg directly if it is a
154205 # UUID.
@@ -157,11 +208,11 @@ def take_action(self, parsed_args):
157208 else :
158209 raise
159210
160- data = compute_client .instance_action . list (server_id , ** kwargs )
211+ data = compute_client .server_actions (server_id , ** kwargs )
161212
162213 columns = (
163214 'request_id' ,
164- 'instance_uuid ' ,
215+ 'server_id ' ,
165216 'action' ,
166217 'start_time' ,
167218 )
@@ -200,7 +251,7 @@ class ShowServerEvent(command.ShowOne):
200251 """
201252
202253 def get_parser (self , prog_name ):
203- parser = super (ShowServerEvent , self ).get_parser (prog_name )
254+ parser = super ().get_parser (prog_name )
204255 parser .add_argument (
205256 'server' ,
206257 metavar = '<server>' ,
@@ -214,13 +265,14 @@ def get_parser(self, prog_name):
214265 return parser
215266
216267 def take_action (self , parsed_args ):
217- compute_client = self .app .client_manager .compute
268+ compute_client = self .app .client_manager .sdk_connection . compute
218269
219270 try :
220- server_id = utils .find_resource (
221- compute_client .servers , parsed_args .server ,
271+ server_id = compute_client .find_server (
272+ parsed_args .server ,
273+ ignore_missing = False ,
222274 ).id
223- except exceptions . CommandError :
275+ except sdk_exceptions . ResourceNotFound :
224276 # If we fail to find the resource, it is possible the server is
225277 # deleted. Try once more using the <server> arg directly if it is a
226278 # UUID.
@@ -229,8 +281,19 @@ def take_action(self, parsed_args):
229281 else :
230282 raise
231283
232- action_detail = compute_client .instance_action .get (
233- server_id , parsed_args .request_id
284+ server_action = compute_client .get_server_action (
285+ parsed_args .request_id , server_id ,
286+ )
287+
288+ column_headers , columns = _get_server_event_columns (
289+ server_action , compute_client ,
234290 )
235291
236- return zip (* sorted (action_detail .to_dict ().items ()))
292+ return (
293+ column_headers ,
294+ utils .get_item_properties (
295+ server_action ,
296+ columns ,
297+ formatters = {'events' : ServerActionEventColumn },
298+ ),
299+ )
0 commit comments