2020import logging
2121from typing import Any
2222
23+ from openstack .identity .v3 import policy as _policy
24+ from openstack import utils as sdk_utils
2325from osc_lib import exceptions
2426from osc_lib import utils
2527
3032LOG = logging .getLogger (__name__ )
3133
3234
35+ def _format_policy (
36+ policy : _policy .Policy ,
37+ ) -> tuple [tuple [str , ...], tuple [str , ...]]:
38+ columns = ('id' , 'blob' , 'type' )
39+ column_headers = ('id' , 'rules' , 'type' )
40+ return (column_headers , utils .get_item_properties (policy , columns ))
41+
42+
3343class CreatePolicy (command .ShowOne ):
3444 _description = _ ("Create new policy" )
3545
@@ -56,15 +66,14 @@ def take_action(
5666 ) -> tuple [Sequence [str ], Iterable [Any ]]:
5767 blob = utils .read_blob_file_contents (parsed_args .rules )
5868
59- identity_client = self .app .client_manager .identity
60- policy = identity_client .policies .create (
69+ identity_client = sdk_utils .ensure_service_version (
70+ self .app .client_manager .sdk_connection .identity , '3'
71+ )
72+ policy = identity_client .create_policy (
6173 blob = blob , type = parsed_args .type
6274 )
6375
64- policy ._info .pop ('links' )
65- policy ._info .update ({'rules' : policy ._info .pop ('blob' )})
66- col_headers , col_data = zip (* sorted (policy ._info .items ()))
67- return col_headers , col_data
76+ return _format_policy (policy )
6877
6978
7079class DeletePolicy (command .Command ):
@@ -81,11 +90,13 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
8190 return parser
8291
8392 def take_action (self , parsed_args : argparse .Namespace ) -> None :
84- identity_client = self .app .client_manager .identity
93+ identity_client = sdk_utils .ensure_service_version (
94+ self .app .client_manager .sdk_connection .identity , '3'
95+ )
8596 result = 0
8697 for i in parsed_args .policy :
8798 try :
88- identity_client .policies . delete ( i )
99+ identity_client .delete_policy ( i , ignore_missing = False )
89100 except Exception as e :
90101 result += 1
91102 LOG .error (
@@ -126,17 +137,20 @@ def take_action(
126137 if parsed_args .long :
127138 columns += ('Blob' ,)
128139 column_headers += ('Rules' ,)
129- data = self .app .client_manager .identity .policies .list ()
140+ identity_client = sdk_utils .ensure_service_version (
141+ self .app .client_manager .sdk_connection .identity , '3'
142+ )
143+ data = identity_client .policies ()
130144 return (
131145 column_headers ,
132- (
146+ [
133147 utils .get_item_properties (
134148 s ,
135149 columns ,
136150 formatters = {},
137151 )
138152 for s in data
139- ) ,
153+ ] ,
140154 )
141155
142156
@@ -163,19 +177,19 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
163177 return parser
164178
165179 def take_action (self , parsed_args : argparse .Namespace ) -> None :
166- identity_client = self .app .client_manager .identity
167- blob = None
180+ identity_client = sdk_utils .ensure_service_version (
181+ self .app .client_manager .sdk_connection .identity , '3'
182+ )
183+
184+ kwargs = {}
168185
169186 if parsed_args .rules :
170- blob = utils .read_blob_file_contents (parsed_args .rules )
187+ kwargs [ ' blob' ] = utils .read_blob_file_contents (parsed_args .rules )
171188
172- kwargs = {}
173- if blob :
174- kwargs ['blob' ] = blob
175189 if parsed_args .type :
176190 kwargs ['type' ] = parsed_args .type
177191
178- identity_client .policies . update (parsed_args .policy , ** kwargs )
192+ identity_client .update_policy (parsed_args .policy , ** kwargs )
179193
180194
181195class ShowPolicy (command .ShowOne ):
@@ -193,12 +207,9 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
193207 def take_action (
194208 self , parsed_args : argparse .Namespace
195209 ) -> tuple [Sequence [str ], Iterable [Any ]]:
196- identity_client = self .app .client_manager .identity
197- policy = utils .find_resource (
198- identity_client .policies , parsed_args .policy
210+ identity_client = sdk_utils .ensure_service_version (
211+ self .app .client_manager .sdk_connection .identity , '3'
199212 )
213+ policy = identity_client .get_policy (parsed_args .policy )
200214
201- policy ._info .pop ('links' )
202- policy ._info .update ({'rules' : policy ._info .pop ('blob' )})
203- col_headers , col_data = zip (* sorted (policy ._info .items ()))
204- return col_headers , col_data
215+ return _format_policy (policy )
0 commit comments