@@ -187,6 +187,52 @@ def resource_exists(cli_ctx, subscription, resource_group, name, namespace, type
187187 return existing
188188
189189
190+ def register_global_policy_argument (cli_ctx ):
191+
192+ def add_global_policy_argument (_ , ** kwargs ):
193+ command_table = kwargs .get ('commands_loader' ).command_table
194+
195+ if not command_table :
196+ return
197+
198+ class ChangeReferenceAction (argparse .Action ): # pylint:disable=too-few-public-methods
199+
200+ def __call__ (self , parser , namespace , value , option_string = None ):
201+ # save change reference to CLI context
202+ cmd = getattr (namespace , 'cmd' , None ) or getattr (namespace , '_cmd' , None )
203+ cmd .cli_ctx .data ['_change_reference' ] = value
204+
205+ class AcquirePolicyTokenAction (argparse .Action ): # pylint:disable=too-few-public-methods
206+
207+ def __call__ (self , parser , namespace , value , option_string = None ):
208+ # save change reference to CLI context
209+ cmd = getattr (namespace , 'cmd' , None ) or getattr (namespace , '_cmd' , None )
210+ cmd .cli_ctx .data ['_acquire_policy_token' ] = True
211+
212+ for command in command_table .values ():
213+ if command .name .split ()[- 1 ] in ['list' , 'show' ]:
214+ continue
215+
216+ change_reference_kwargs = {
217+ 'help' : 'The related change reference ID for this resource operation' ,
218+ 'arg_group' : 'Global Policy' ,
219+ 'action' : ChangeReferenceAction ,
220+ }
221+ acquire_policy_token_kwargs = {
222+ 'help' : 'Acquiring an Azure Policy token automatically for this resource operation' ,
223+ 'arg_group' : 'Global Policy' ,
224+ 'nargs' : 0 ,
225+ 'action' : AcquirePolicyTokenAction ,
226+ }
227+ command .add_argument ('_change_reference' , '--change-reference' , ** change_reference_kwargs )
228+ command .add_argument ('_acquire_policy_token' , '--acquire-policy-token' , ** acquire_policy_token_kwargs )
229+
230+ policy_token_feature_enabled = cli_ctx .config .getboolean ('core' , 'enable_policy_token' , False )
231+ if policy_token_feature_enabled :
232+ from knack import events
233+ cli_ctx .register_event (events .EVENT_INVOKER_POST_CMD_TBL_CREATE , add_global_policy_argument )
234+
235+
190236# pylint: disable=too-many-statements
191237def register_ids_argument (cli_ctx ):
192238
0 commit comments