1717
1818import logging
1919
20- from keystoneauth1 import exceptions as ks_exc
20+ from openstack import exceptions as sdk_exc
2121from osc_lib .command import command
2222from osc_lib import exceptions
2323from osc_lib import utils
2929LOG = logging .getLogger (__name__ )
3030
3131
32+ def _format_group (group ):
33+ columns = (
34+ 'description' ,
35+ 'domain_id' ,
36+ 'id' ,
37+ 'name' ,
38+ )
39+ column_headers = (
40+ 'description' ,
41+ 'domain_id' ,
42+ 'id' ,
43+ 'name' ,
44+ )
45+ return (
46+ column_headers ,
47+ utils .get_item_properties (group , columns ),
48+ )
49+
50+
3251class AddUserToGroup (command .Command ):
3352 _description = _ ("Add user to group" )
3453
@@ -53,19 +72,19 @@ def get_parser(self, prog_name):
5372 return parser
5473
5574 def take_action (self , parsed_args ):
56- identity_client = self .app .client_manager .identity
75+ identity_client = self .app .client_manager .sdk_connection . identity
5776
58- group_id = common .find_group (
77+ group_id = common .find_group_id_sdk (
5978 identity_client , parsed_args .group , parsed_args .group_domain
60- ). id
79+ )
6180
6281 result = 0
6382 for i in parsed_args .user :
6483 try :
65- user_id = common .find_user (
84+ user_id = common .find_user_id_sdk (
6685 identity_client , i , parsed_args .user_domain
67- ). id
68- identity_client .users . add_to_group (user_id , group_id )
86+ )
87+ identity_client .add_user_to_group (user_id , group_id )
6988 except Exception as e :
7089 result += 1
7190 msg = _ ("%(user)s not added to group %(group)s: %(e)s" ) % {
@@ -109,32 +128,41 @@ def get_parser(self, prog_name):
109128 return parser
110129
111130 def take_action (self , parsed_args ):
112- identity_client = self .app .client_manager .identity
131+ identity_client = self .app .client_manager .sdk_connection . identity
113132
114- user_id = common .find_user (
115- identity_client , parsed_args .user , parsed_args .user_domain
116- ).id
117- group_id = common .find_group (
118- identity_client , parsed_args .group , parsed_args .group_domain
119- ).id
133+ user_id = common .find_user_id_sdk (
134+ identity_client ,
135+ parsed_args .user ,
136+ parsed_args .user_domain ,
137+ validate_actor_existence = False ,
138+ )
139+ group_id = common .find_group_id_sdk (
140+ identity_client ,
141+ parsed_args .group ,
142+ parsed_args .group_domain ,
143+ validate_actor_existence = False ,
144+ )
120145
146+ user_in_group = False
121147 try :
122- identity_client .users .check_in_group (user_id , group_id )
123- except ks_exc .http .HTTPClientError as e :
124- if e .http_status == 403 or e .http_status == 404 :
125- msg = _ ("%(user)s not in group %(group)s\n " ) % {
126- 'user' : parsed_args .user ,
127- 'group' : parsed_args .group ,
128- }
129- self .app .stderr .write (msg )
130- else :
131- raise e
132- else :
148+ user_in_group = identity_client .check_user_in_group (
149+ user_id , group_id
150+ )
151+ except sdk_exc .ForbiddenException :
152+ # Assume False if forbidden
153+ pass
154+ if user_in_group :
133155 msg = _ ("%(user)s in group %(group)s\n " ) % {
134156 'user' : parsed_args .user ,
135157 'group' : parsed_args .group ,
136158 }
137159 self .app .stdout .write (msg )
160+ else :
161+ msg = _ ("%(user)s not in group %(group)s\n " ) % {
162+ 'user' : parsed_args .user ,
163+ 'group' : parsed_args .group ,
164+ }
165+ self .app .stderr .write (msg )
138166
139167
140168class CreateGroup (command .ShowOne ):
@@ -165,29 +193,33 @@ def get_parser(self, prog_name):
165193 return parser
166194
167195 def take_action (self , parsed_args ):
168- identity_client = self .app .client_manager .identity
196+ identity_client = self .app .client_manager .sdk_connection . identity
169197
170- domain = None
198+ kwargs = {}
199+ if parsed_args .name :
200+ kwargs ['name' ] = parsed_args .name
201+ if parsed_args .description :
202+ kwargs ['description' ] = parsed_args .description
171203 if parsed_args .domain :
172- domain = common .find_domain (identity_client , parsed_args .domain ).id
204+ kwargs ['domain_id' ] = common .find_domain_id_sdk (
205+ identity_client , parsed_args .domain
206+ )
173207
174208 try :
175- group = identity_client .groups .create (
176- name = parsed_args .name ,
177- domain = domain ,
178- description = parsed_args .description ,
179- )
180- except ks_exc .Conflict :
209+ group = identity_client .create_group (** kwargs )
210+ except sdk_exc .ConflictException :
181211 if parsed_args .or_show :
182- group = utils .find_resource (
183- identity_client .groups , parsed_args .name , domain_id = domain
184- )
212+ if parsed_args .domain :
213+ group = identity_client .find_group (
214+ parsed_args .name , domain_id = parsed_args .domain
215+ )
216+ else :
217+ group = identity_client .find_group (parsed_args .name )
185218 LOG .info (_ ('Returning existing group %s' ), group .name )
186219 else :
187220 raise
188221
189- group ._info .pop ('links' )
190- return zip (* sorted (group ._info .items ()))
222+ return _format_group (group )
191223
192224
193225class DeleteGroup (command .Command ):
@@ -209,15 +241,15 @@ def get_parser(self, prog_name):
209241 return parser
210242
211243 def take_action (self , parsed_args ):
212- identity_client = self .app .client_manager .identity
244+ identity_client = self .app .client_manager .sdk_connection . identity
213245
214246 errors = 0
215247 for group in parsed_args .groups :
216248 try :
217- group_obj = common .find_group (
249+ group_id = common .find_group_id_sdk (
218250 identity_client , group , parsed_args .domain
219251 )
220- identity_client .groups . delete ( group_obj . id )
252+ identity_client .delete_group ( group_id )
221253 except Exception as e :
222254 errors += 1
223255 LOG .error (
@@ -262,29 +294,37 @@ def get_parser(self, prog_name):
262294 return parser
263295
264296 def take_action (self , parsed_args ):
265- identity_client = self .app .client_manager .identity
297+ identity_client = self .app .client_manager .sdk_connection . identity
266298
267299 domain = None
268300 if parsed_args .domain :
269- domain = common .find_domain (identity_client , parsed_args .domain ).id
301+ domain = common .find_domain_id_sdk (
302+ identity_client , parsed_args .domain
303+ )
270304
305+ data = []
271306 if parsed_args .user :
272- user = common .find_user (
307+ user = common .find_user_id_sdk (
273308 identity_client ,
274309 parsed_args .user ,
275310 parsed_args .user_domain ,
276- ).id
311+ )
312+ if domain :
313+ # NOTE(0weng): The API doesn't actually support filtering additionally by domain_id,
314+ # so this doesn't really do anything.
315+ data = identity_client .user_groups (user , domain_id = domain )
316+ else :
317+ data = identity_client .user_groups (user )
277318 else :
278- user = None
319+ if domain :
320+ data = identity_client .groups (domain_id = domain )
321+ else :
322+ data = identity_client .groups ()
279323
280324 # List groups
281325 columns : tuple [str , ...] = ('ID' , 'Name' )
282326 if parsed_args .long :
283327 columns += ('Domain ID' , 'Description' )
284- data = identity_client .groups .list (
285- domain = domain ,
286- user = user ,
287- )
288328
289329 return (
290330 columns ,
@@ -323,19 +363,19 @@ def get_parser(self, prog_name):
323363 return parser
324364
325365 def take_action (self , parsed_args ):
326- identity_client = self .app .client_manager .identity
366+ identity_client = self .app .client_manager .sdk_connection . identity
327367
328- group_id = common .find_group (
368+ group_id = common .find_group_id_sdk (
329369 identity_client , parsed_args .group , parsed_args .group_domain
330- ). id
370+ )
331371
332372 result = 0
333373 for i in parsed_args .user :
334374 try :
335- user_id = common .find_user (
375+ user_id = common .find_user_id_sdk (
336376 identity_client , i , parsed_args .user_domain
337- ). id
338- identity_client .users . remove_from_group (user_id , group_id )
377+ )
378+ identity_client .remove_user_from_group (user_id , group_id )
339379 except Exception as e :
340380 result += 1
341381 msg = _ ("%(user)s not removed from group %(group)s: %(e)s" ) % {
@@ -387,8 +427,8 @@ def get_parser(self, prog_name):
387427 return parser
388428
389429 def take_action (self , parsed_args ):
390- identity_client = self .app .client_manager .identity
391- group = common .find_group (
430+ identity_client = self .app .client_manager .sdk_connection . identity
431+ group = common .find_group_id_sdk (
392432 identity_client , parsed_args .group , parsed_args .domain
393433 )
394434 kwargs = {}
@@ -397,7 +437,7 @@ def take_action(self, parsed_args):
397437 if parsed_args .description :
398438 kwargs ['description' ] = parsed_args .description
399439
400- identity_client .groups . update (group . id , ** kwargs )
440+ identity_client .update_group (group , ** kwargs )
401441
402442
403443class ShowGroup (command .ShowOne ):
@@ -418,13 +458,18 @@ def get_parser(self, prog_name):
418458 return parser
419459
420460 def take_action (self , parsed_args ):
421- identity_client = self .app .client_manager .identity
461+ identity_client = self .app .client_manager .sdk_connection . identity
422462
423- group = common .find_group (
424- identity_client ,
425- parsed_args .group ,
426- domain_name_or_id = parsed_args .domain ,
427- )
463+ if parsed_args .domain :
464+ domain = common .find_domain_id_sdk (
465+ identity_client , parsed_args .domain
466+ )
467+ group = identity_client .find_group (
468+ parsed_args .group , domain_id = domain , ignore_missing = False
469+ )
470+ else :
471+ group = identity_client .find_group (
472+ parsed_args .group , ignore_missing = False
473+ )
428474
429- group ._info .pop ('links' )
430- return zip (* sorted (group ._info .items ()))
475+ return _format_group (group )
0 commit comments