def keyvault_command_handler(command_args): from azure.cli.core.commands.client_factory import resolve_client_arg_name from azure.cli.core.profiles import ResourceType from azure.cli.core.util import get_arg_list, poller_classes from msrest.paging import Paged op = get_op_handler() op_args = get_arg_list(op) command_type = merged_kwargs.get('command_type', None) client_factory = command_type.settings.get('client_factory', None) if command_type \ else merged_kwargs.get('client_factory', None) client_arg_name = resolve_client_arg_name( operations_tmpl.format(method_name), kwargs) if client_arg_name in op_args: client = client_factory(self.command_loader.cli_ctx, command_args) command_args[client_arg_name] = client if 'cmd' not in op_args: command_args.pop('cmd') try: if command_type.settings.get( 'resource_type' ) == ResourceType.DATA_KEYVAULT_ADMINISTRATION_BACKUP: abandoned_args = ['identifier', 'vault_base_url'] for arg in abandoned_args: if arg in command_args: command_args.pop(arg) result = op(**command_args) # apply results transform if specified transform_result = merged_kwargs.get('transform', None) if transform_result: return _encode_hex(transform_result( result, **command_args)) # otherwise handle based on return type of results if isinstance(result, poller_classes()): return _encode_hex( LongRunningOperation( self.command_loader.cli_ctx, 'Starting {}'.format(name))(result)) if isinstance(result, Paged): try: return _encode_hex(list(result)) except TypeError: # TODO: Workaround for an issue in either KeyVault server-side or msrest # See https://github.com/Azure/autorest/issues/1309 return [] return _encode_hex(result) except Exception as ex: # pylint: disable=broad-except if name == 'show': # show_exception_handler needs to be called before the keyvault_exception_handler from azure.cli.core.commands.arm import show_exception_handler try: show_exception_handler(ex) except Exception: # pylint: disable=broad-except pass return keyvault_exception_handler(self.command_loader, ex)
def handler(args): from azure.cli.core.commands.client_factory import resolve_client_arg_name getter_args = dict(extract_args_from_signature(context.get_op_handler(getter_op), excluded_params=EXCLUDED_NON_CLIENT_PARAMS)) cmd = args.get('cmd') if 'cmd' in getter_args else args.pop('cmd') operations_tmpl = _get_operations_tmpl(cmd, custom_command=custom_command) client_arg_name = resolve_client_arg_name(operations_tmpl, kwargs) try: client = factory(context.cli_ctx) if factory else None except TypeError: client = factory(context.cli_ctx, args) if factory else None if client and (client_arg_name in getter_args): args[client_arg_name] = client getter = context.get_op_handler(getter_op) try: return getter(**args) except Exception as ex: # pylint: disable=broad-except if getattr(getattr(ex, 'response', ex), 'status_code', None) == 404: logger.error(getattr(ex, 'message', ex)) import sys sys.exit(3) raise
def handler(args): from azure.cli.core.commands.client_factory import resolve_client_arg_name getter_args = dict( extract_args_from_signature( context.get_op_handler(getter_op), excluded_params=EXCLUDED_NON_CLIENT_PARAMS)) cmd = args.get('cmd') if 'cmd' in getter_args else args.pop('cmd') operations_tmpl = _get_operations_tmpl(cmd, custom_command=custom_command) client_arg_name = resolve_client_arg_name(operations_tmpl, kwargs) try: client = factory(context.cli_ctx) if factory else None except TypeError: client = factory(context.cli_ctx, args) if factory else None if client and (client_arg_name in getter_args): args[client_arg_name] = client getter = context.get_op_handler(getter_op) try: return getter(**args) except Exception as ex: # pylint: disable=broad-except if getattr(getattr(ex, 'response', ex), 'status_code', None) == 404: logger.error(getattr(ex, 'message', ex)) import sys sys.exit(3) raise
def default_command_handler(command_args): from azure.cli.core.util import get_arg_list from azure.cli.core.commands.client_factory import resolve_client_arg_name op = handler or self.get_op_handler(operation) op_args = get_arg_list(op) client = client_factory(self.cli_ctx, command_args) if client_factory else None if client: client_arg_name = resolve_client_arg_name(operation, kwargs) if client_arg_name in op_args: command_args[client_arg_name] = client result = op(**command_args) return result
def _extract_handler_and_args(args, commmand_kwargs, op, context): from azure.cli.core.commands.client_factory import resolve_client_arg_name factory = _get_client_factory(name, **commmand_kwargs) client = None if factory: try: client = factory(context.cli_ctx) except TypeError: client = factory(context.cli_ctx, args) client_arg_name = resolve_client_arg_name(op, kwargs) op_handler = context.get_op_handler(op, operation_group=kwargs.get('operation_group')) raw_args = dict(extract_args_from_signature(op_handler, excluded_params=EXCLUDED_NON_CLIENT_PARAMS)) op_args = {key: val for key, val in args.items() if key in raw_args} if client_arg_name in raw_args: op_args[client_arg_name] = client return op_handler, op_args
def keyvault_command_handler(command_args): from azure.cli.core.util import get_arg_list from azure.cli.core.commands.client_factory import resolve_client_arg_name from msrest.paging import Paged from azure.cli.core.util import poller_classes op = get_op_handler() op_args = get_arg_list(op) command_type = merged_kwargs.get('command_type', None) client_factory = command_type.settings.get('client_factory', None) if command_type \ else merged_kwargs.get('client_factory', None) client_arg_name = resolve_client_arg_name(operations_tmpl.format(method_name), kwargs) if client_arg_name in op_args: client = client_factory(self.command_loader.cli_ctx, command_args) command_args[client_arg_name] = client if 'cmd' not in op_args: command_args.pop('cmd') try: result = op(**command_args) # apply results transform if specified transform_result = merged_kwargs.get('transform', None) if transform_result: return _encode_hex(transform_result(result)) # otherwise handle based on return type of results if isinstance(result, poller_classes()): return _encode_hex( LongRunningOperation(self.command_loader.cli_ctx, 'Starting {}'.format(name))(result)) if isinstance(result, Paged): try: return _encode_hex(list(result)) except TypeError: # TODO: Workaround for an issue in either KeyVault server-side or msrest # See https://github.com/Azure/autorest/issues/1309 return [] return _encode_hex(result) except Exception as ex: # pylint: disable=broad-except if name == 'show': # show_exception_handler needs to be called before the keyvault_exception_handler from azure.cli.core.commands.arm import show_exception_handler try: show_exception_handler(ex) except Exception: # pylint: disable=broad-except pass return keyvault_exception_handler(self.command_loader, ex)
def default_command_handler(command_args): from azure.cli.core.util import get_arg_list, augment_no_wait_handler_args from azure.cli.core.commands.client_factory import resolve_client_arg_name op = handler or self.get_op_handler(operation) op_args = get_arg_list(op) cmd = command_args.get('cmd') if 'cmd' in op_args else command_args.pop('cmd') client = client_factory(cmd.cli_ctx, command_args) if client_factory else None supports_no_wait = kwargs.get('supports_no_wait', None) if supports_no_wait: no_wait_enabled = command_args.pop('no_wait', False) augment_no_wait_handler_args(no_wait_enabled, op, command_args) if client: client_arg_name = resolve_client_arg_name(operation, kwargs) if client_arg_name in op_args: command_args[client_arg_name] = client return op(**command_args)
def _extract_handler_and_args(args, commmand_kwargs, op): from azure.cli.core.commands.client_factory import resolve_client_arg_name factory = _get_client_factory(name, commmand_kwargs) client = None if factory: try: client = factory(context.cli_ctx) except TypeError: client = factory(context.cli_ctx, None) client_arg_name = resolve_client_arg_name(op, kwargs) op_handler = context.get_op_handler(op) exclude = list(set(EXCLUDED_PARAMS) - set(['self', 'client'])) raw_args = dict(extract_args_from_signature(op_handler, excluded_params=exclude)) op_args = {key: val for key, val in args.items() if key in raw_args} if client_arg_name in raw_args: op_args[client_arg_name] = client return op_handler, op_args
def default_command_handler(command_args): from azure.cli.core.util import get_arg_list, augment_no_wait_handler_args from azure.cli.core.commands.client_factory import resolve_client_arg_name op = handler or self.get_op_handler(operation) op_args = get_arg_list(op) cmd = command_args.get('cmd') if 'cmd' in op_args else command_args.pop('cmd') client = client_factory(cmd.cli_ctx, command_args) if client_factory else None supports_no_wait = kwargs.get('supports_no_wait', None) if supports_no_wait: no_wait_enabled = command_args.pop('no_wait', False) augment_no_wait_handler_args(no_wait_enabled, op, command_args) if client: client_arg_name = resolve_client_arg_name(operation, kwargs) if client_arg_name in op_args: command_args[client_arg_name] = client return op(**command_args)
def _extract_handler_and_args(args, commmand_kwargs, op): from azure.cli.core.commands.client_factory import resolve_client_arg_name factory = _get_client_factory(name, commmand_kwargs) client = None if factory: try: client = factory(context.cli_ctx) except TypeError: client = factory(context.cli_ctx, None) client_arg_name = resolve_client_arg_name(op, kwargs) op_handler = context.get_op_handler(op) exclude = list(set(EXCLUDED_PARAMS) - set(['self', 'client'])) raw_args = dict(extract_args_from_signature(op_handler, excluded_params=exclude)) op_args = {key: val for key, val in args.items() if key in raw_args} if client_arg_name in raw_args: op_args[client_arg_name] = client return op_handler, op_args
def keyvault_command_handler(command_args): from azure.cli.core.util import get_arg_list from azure.cli.core.commands.client_factory import resolve_client_arg_name from msrest.paging import Paged from msrestazure.azure_operation import AzureOperationPoller op = get_op_handler() op_args = get_arg_list(op) command_type = merged_kwargs.get('command_type', None) client_factory = command_type.settings.get('client_factory', None) if command_type \ else merged_kwargs.get('client_factory', None) client_arg_name = resolve_client_arg_name( operations_tmpl.format(method_name), kwargs) if client_arg_name in op_args: client = client_factory(self.command_loader.cli_ctx, command_args) command_args[client_arg_name] = client try: result = op(**command_args) # apply results transform if specified transform_result = merged_kwargs.get('transform', None) if transform_result: return _encode_hex(transform_result(result)) # otherwise handle based on return type of results if isinstance(result, AzureOperationPoller): return _encode_hex( LongRunningOperation( self.command_loader.cli_ctx, 'Starting {}'.format(name))(result)) elif isinstance(result, Paged): try: return _encode_hex(list(result)) except TypeError: # TODO: Workaround for an issue in either KeyVault server-side or msrest # See https://github.com/Azure/autorest/issues/1309 return [] else: return _encode_hex(result) except Exception as ex: # pylint: disable=broad-except return keyvault_exception_handler(ex)
def handler(args): from azure.cli.core.commands.client_factory import resolve_client_arg_name context_copy = copy.copy(context) getter_args = dict(extract_args_from_signature(context_copy.get_op_handler(getter_op), excluded_params=EXCLUDED_NON_CLIENT_PARAMS)) cmd = args.get('cmd') if 'cmd' in getter_args else args.pop('cmd') context_copy.cli_ctx = cmd.cli_ctx operations_tmpl = _get_operations_tmpl(cmd, custom_command=custom_command) client_arg_name = resolve_client_arg_name(operations_tmpl, kwargs) try: client = factory(context_copy.cli_ctx) if factory else None except TypeError: client = factory(context_copy.cli_ctx, args) if factory else None if client and (client_arg_name in getter_args): args[client_arg_name] = client getter = context_copy.get_op_handler(getter_op) try: return getter(**args) except Exception as ex: # pylint: disable=broad-except show_exception_handler(ex)
def handler(args): from azure.cli.core.commands.client_factory import resolve_client_arg_name context_copy = copy.copy(context) getter_args = dict(extract_args_from_signature( context_copy.get_op_handler(getter_op, operation_group=kwargs.get('operation_group')), excluded_params=EXCLUDED_NON_CLIENT_PARAMS)) cmd = args.get('cmd') if 'cmd' in getter_args else args.pop('cmd') context_copy.cli_ctx = cmd.cli_ctx operations_tmpl = _get_operations_tmpl(cmd, custom_command=custom_command) client_arg_name = resolve_client_arg_name(operations_tmpl, kwargs) try: client = factory(context_copy.cli_ctx) if factory else None except TypeError: client = factory(context_copy.cli_ctx, args) if factory else None if client and (client_arg_name in getter_args): args[client_arg_name] = client getter = context_copy.get_op_handler(getter_op, operation_group=kwargs.get('operation_group')) try: return getter(**args) except Exception as ex: # pylint: disable=broad-except show_exception_handler(ex)
def handler(args): from azure.cli.core.commands.client_factory import resolve_client_arg_name from msrest.exceptions import ClientException import time cmd = args.get('cmd') operations_tmpl = _get_operations_tmpl(cmd) getter_args = dict( extract_args_from_signature(context.get_op_handler(getter_op), excluded_params=EXCLUDED_PARAMS)) client_arg_name = resolve_client_arg_name(operations_tmpl, kwargs) try: client = factory(context.cli_ctx) if factory else None except TypeError: client = factory(context.cli_ctx, args) if factory else None if client and (client_arg_name in getter_args or client_arg_name == 'self'): args[client_arg_name] = client getter = context.get_op_handler(getter_op) timeout = args.pop('timeout') interval = args.pop('interval') wait_for_created = args.pop('created') wait_for_deleted = args.pop('deleted') wait_for_updated = args.pop('updated') wait_for_exists = args.pop('exists') custom_condition = args.pop('custom') if not any([ wait_for_created, wait_for_updated, wait_for_deleted, wait_for_exists, custom_condition ]): raise CLIError( "incorrect usage: --created | --updated | --deleted | --exists | --custom JMESPATH" ) progress_indicator = context.cli_ctx.get_progress_controller() progress_indicator.begin() for _ in range(0, timeout, interval): try: progress_indicator.add(message='Waiting') instance = getter(**args) if wait_for_exists: progress_indicator.end() return None provisioning_state = get_provisioning_state(instance) # until we have any needs to wait for 'Failed', let us bail out on this if provisioning_state == 'Failed': progress_indicator.stop() raise CLIError('The operation failed') if ((wait_for_created or wait_for_updated) and provisioning_state == 'Succeeded') or \ custom_condition and bool(verify_property(instance, custom_condition)): progress_indicator.end() return None except ClientException as ex: progress_indicator.stop() if getattr(ex, 'status_code', None) == 404: if wait_for_deleted: return None if not any( [wait_for_created, wait_for_exists, custom_condition]): raise else: raise except Exception as ex: # pylint: disable=broad-except progress_indicator.stop() raise time.sleep(interval) progress_indicator.end() return CLIError( 'Wait operation timed-out after {} seconds'.format(timeout))
def handler(args): from azure.cli.core.commands.client_factory import resolve_client_arg_name from msrest.exceptions import ClientException import time cmd = args.get('cmd') operations_tmpl = _get_operations_tmpl(cmd) getter_args = dict(extract_args_from_signature(context.get_op_handler(getter_op), excluded_params=EXCLUDED_PARAMS)) client_arg_name = resolve_client_arg_name(operations_tmpl, kwargs) try: client = factory(context.cli_ctx) if factory else None except TypeError: client = factory(context.cli_ctx, None) if factory else None if client and (client_arg_name in getter_args or client_arg_name == 'self'): args[client_arg_name] = client getter = context.get_op_handler(getter_op) timeout = args.pop('timeout') interval = args.pop('interval') wait_for_created = args.pop('created') wait_for_deleted = args.pop('deleted') wait_for_updated = args.pop('updated') wait_for_exists = args.pop('exists') custom_condition = args.pop('custom') if not any([wait_for_created, wait_for_updated, wait_for_deleted, wait_for_exists, custom_condition]): raise CLIError( "incorrect usage: --created | --updated | --deleted | --exists | --custom JMESPATH") progress_indicator = context.cli_ctx.get_progress_controller() progress_indicator.begin() for _ in range(0, timeout, interval): try: progress_indicator.add(message='Waiting') instance = getter(**args) if wait_for_exists: progress_indicator.end() return None provisioning_state = get_provisioning_state(instance) # until we have any needs to wait for 'Failed', let us bail out on this if provisioning_state == 'Failed': progress_indicator.stop() raise CLIError('The operation failed') if ((wait_for_created or wait_for_updated) and provisioning_state == 'Succeeded') or \ custom_condition and bool(verify_property(instance, custom_condition)): progress_indicator.end() return None except ClientException as ex: progress_indicator.stop() if getattr(ex, 'status_code', None) == 404: if wait_for_deleted: return None if not any([wait_for_created, wait_for_exists, custom_condition]): raise else: raise except Exception as ex: # pylint: disable=broad-except progress_indicator.stop() raise time.sleep(interval) progress_indicator.end() return CLIError('Wait operation timed-out after {} seconds'.format(timeout))
def resolve_client_arg_name(self, op_path): from azure.cli.core.commands.client_factory import resolve_client_arg_name return resolve_client_arg_name(op_path, self.merged_kwargs)