1818import logging
1919from typing import Any
2020
21+ from openstack import utils as sdk_utils
2122from osc_lib .cli import format_columns
2223from osc_lib import exceptions
2324from osc_lib import utils
3031LOG = logging .getLogger (__name__ )
3132
3233
34+ def _format_identity_provider (idp ):
35+ columns = (
36+ 'authorization_ttl' ,
37+ 'description' ,
38+ 'domain_id' ,
39+ 'is_enabled' ,
40+ 'name' ,
41+ 'remote_ids' ,
42+ )
43+ column_headers = (
44+ 'authorization_ttl' ,
45+ 'description' ,
46+ 'domain_id' ,
47+ 'enabled' ,
48+ 'id' ,
49+ 'remote_ids' ,
50+ )
51+ return (
52+ column_headers ,
53+ utils .get_item_properties (
54+ idp , columns , formatters = {'remote_ids' : format_columns .ListColumn }
55+ ),
56+ )
57+
58+
3359class CreateIdentityProvider (command .ShowOne ):
3460 _description = _ ("Create new identity provider" )
3561
@@ -104,25 +130,31 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
104130 def take_action (
105131 self , parsed_args : argparse .Namespace
106132 ) -> tuple [Sequence [str ], Iterable [Any ]]:
107- identity_client = self .app .client_manager .identity
108- remote_ids : list [str ] | None = None
133+ identity_client = sdk_utils .ensure_service_version (
134+ self .app .client_manager .sdk_connection .identity , '3'
135+ )
136+ kwargs = {'is_enabled' : parsed_args .enabled }
137+ if parsed_args .identity_provider_id :
138+ kwargs ['id' ] = parsed_args .identity_provider_id
139+ if parsed_args .description :
140+ kwargs ['description' ] = parsed_args .description
141+
109142 if parsed_args .remote_id_file :
110143 file_content = utils .read_blob_file_contents (
111144 parsed_args .remote_id_file
112145 )
113146 remote_ids = file_content .splitlines ()
114- remote_ids = list (map (str .strip , remote_ids ))
147+ kwargs [ ' remote_ids' ] = list (map (str .strip , remote_ids ))
115148 elif parsed_args .remote_ids :
116- remote_ids = parsed_args .remote_ids
149+ kwargs [ ' remote_ids' ] = parsed_args .remote_ids
117150
118- domain_id = None
119151 if parsed_args .domain :
120- domain_id = common .find_domain (
121- identity_client , parsed_args .domain
122- ).id
152+ kwargs ['domain_id' ] = common .find_domain_id_sdk (
153+ identity_client ,
154+ parsed_args .domain ,
155+ validate_actor_existence = False ,
156+ )
123157
124- # TODO(pas-ha) actually check for 3.14 microversion
125- kwargs = {}
126158 auth_ttl = parsed_args .authorization_ttl
127159 if auth_ttl is not None :
128160 if auth_ttl < 0 :
@@ -132,21 +164,9 @@ def take_action(
132164 raise exceptions .CommandError (msg )
133165 kwargs ['authorization_ttl' ] = auth_ttl
134166
135- idp = identity_client .federation .identity_providers .create (
136- id = parsed_args .identity_provider_id ,
137- remote_ids = remote_ids ,
138- description = parsed_args .description ,
139- domain_id = domain_id ,
140- enabled = parsed_args .enabled ,
141- ** kwargs ,
142- )
167+ idp = identity_client .create_identity_provider (** kwargs )
143168
144- idp ._info .pop ('links' , None )
145- idp ._info ['remote_ids' ] = format_columns .ListColumn (
146- idp ._info .pop ('remote_ids' , [])
147- )
148- col_headers , col_data = zip (* sorted (idp ._info .items ()))
149- return col_headers , col_data
169+ return _format_identity_provider (idp )
150170
151171
152172class DeleteIdentityProvider (command .Command ):
@@ -163,16 +183,18 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
163183 return parser
164184
165185 def take_action (self , parsed_args : argparse .Namespace ) -> None :
166- identity_client = self .app .client_manager .identity
186+ identity_client = sdk_utils .ensure_service_version (
187+ self .app .client_manager .sdk_connection .identity , '3'
188+ )
167189 result = 0
168190 for i in parsed_args .identity_provider :
169191 try :
170- identity_client .federation . identity_providers . delete (i )
192+ identity_client .delete_identity_provider (i )
171193 except Exception as e :
172194 result += 1
173195 LOG .error (
174196 _ (
175- "Failed to delete identity providers with "
197+ "Failed to delete identity provider with "
176198 "name or ID '%(provider)s': %(e)s"
177199 ),
178200 {'provider' : i , 'e' : e },
@@ -207,18 +229,21 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
207229 def take_action (
208230 self , parsed_args : argparse .Namespace
209231 ) -> tuple [tuple [str , ...], Iterable [tuple [Any , ...]]]:
210- columns = ('ID' , 'Enabled' , 'Domain ID' , 'Description' )
211- identity_client = self .app .client_manager .identity
232+ columns = ('id' , 'is_enabled' , 'domain_id' , 'description' )
233+ column_headers = ('ID' , 'Enabled' , 'Domain ID' , 'Description' )
234+ identity_client = sdk_utils .ensure_service_version (
235+ self .app .client_manager .sdk_connection .identity , '3'
236+ )
212237
213238 kwargs = {}
214239 if parsed_args .id :
215240 kwargs ['id' ] = parsed_args .id
216241 if parsed_args .enabled :
217- kwargs ['enabled ' ] = True
242+ kwargs ['is_enabled ' ] = True
218243
219- data = identity_client .federation . identity_providers . list (** kwargs )
244+ data = identity_client .identity_providers (** kwargs )
220245 return (
221- columns ,
246+ column_headers ,
222247 (
223248 utils .get_item_properties (
224249 s ,
@@ -288,7 +313,9 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
288313 return parser
289314
290315 def take_action (self , parsed_args : argparse .Namespace ) -> None :
291- federation_client = self .app .client_manager .identity .federation
316+ identity_client = sdk_utils .ensure_service_version (
317+ self .app .client_manager .sdk_connection .identity , '3'
318+ )
292319
293320 # Always set remote_ids if either is passed in
294321 if parsed_args .remote_id_file :
@@ -305,13 +332,14 @@ def take_action(self, parsed_args: argparse.Namespace) -> None:
305332 if parsed_args .description :
306333 kwargs ['description' ] = parsed_args .description
307334 if parsed_args .enable :
308- kwargs ['enabled ' ] = True
335+ kwargs ['is_enabled ' ] = True
309336 if parsed_args .disable :
310- kwargs ['enabled ' ] = False
337+ kwargs ['is_enabled ' ] = False
311338 if parsed_args .remote_id_file or parsed_args .remote_ids :
312339 kwargs ['remote_ids' ] = remote_ids
313340
314- # TODO(pas-ha) actually check for 3.14 microversion
341+ # NOTE(0weng): This is now possible in SDK! An option should be added.
342+ # Original comment:
315343 # TODO(pas-ha) make it possible to reset authorization_ttl
316344 # back to None value.
317345 # Currently not possible as filter_kwargs decorator in
@@ -326,7 +354,7 @@ def take_action(self, parsed_args: argparse.Namespace) -> None:
326354 raise exceptions .CommandError (msg )
327355 kwargs ['authorization_ttl' ] = auth_ttl
328356
329- federation_client . identity_providers . update (
357+ identity_client . update_identity_provider (
330358 parsed_args .identity_provider , ** kwargs
331359 )
332360
@@ -346,15 +374,11 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
346374 def take_action (
347375 self , parsed_args : argparse .Namespace
348376 ) -> tuple [Sequence [str ], Iterable [Any ]]:
349- identity_client = self . app . client_manager . identity
350- idp = utils . find_resource (
351- identity_client . federation . identity_providers ,
352- parsed_args . identity_provider ,
353- id = parsed_args .identity_provider ,
377+ identity_client = sdk_utils . ensure_service_version (
378+ self . app . client_manager . sdk_connection . identity , '3'
379+ )
380+ idp = identity_client . get_identity_provider (
381+ parsed_args .identity_provider
354382 )
355383
356- idp ._info .pop ('links' , None )
357- remote_ids = format_columns .ListColumn (idp ._info .pop ('remote_ids' , []))
358- idp ._info ['remote_ids' ] = remote_ids
359- col_headers , col_data = zip (* sorted (idp ._info .items ()))
360- return col_headers , col_data
384+ return _format_identity_provider (idp )
0 commit comments