def _create_key_vault_command(name, operation, transform_result, table_transformer): def _execute_command(kwargs): try: def get_token(server, resource, scope): # pylint: disable=unused-argument return Profile().get_login_credentials(resource)[0]._token_retriever() # pylint: disable=protected-access # since the convenience client can be inconvenient, we have to check and create the # correct client version if 'generated' in operation.__module__: client = BaseKeyVaultClient(KeyVaultAuthentication(get_token)) else: client = KeyVaultClient(KeyVaultAuthentication(get_token)) # pylint: disable=redefined-variable-type result = operation(client, **kwargs) # apply results transform if specified 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('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 (ValidationError, KeyVaultErrorException) as ex: try: raise CLIError(ex.inner_exception.error.message) except AttributeError: raise CLIError(ex) name = ' '.join(name.split()) cmd = CliCommand(name, _execute_command, table_transformer=table_transformer) cmd.description = extract_full_summary_from_signature(operation) cmd.arguments.update(extract_args_from_signature(operation)) return cmd
def expand(self, argument_name, model_type, group_name=None, patches=None): # TODO: # two privates symbols are imported here. they should be made public or this utility class # should be moved into azure.cli.core from azure.cli.core.commands import _cli_extra_argument_registry from azure.cli.core.commands._introspection import \ (extract_args_from_signature, _option_descriptions) from azure.cli.core.sdk.validators import get_complex_argument_processor if not patches: patches = dict() self.ignore(argument_name) # fetch the documentation for model parameters first. for models, which are the classes # derive from msrest.serialization.Model and used in the SDK API to carry parameters, the # document of their properties are attached to the classes instead of constructors. parameter_docs = _option_descriptions(model_type) expanded_arguments = [] for name, arg in extract_args_from_signature(model_type.__init__): if name in parameter_docs: arg.type.settings['help'] = parameter_docs[name] if group_name: arg.type.settings['arg_group'] = group_name if name in patches: patches[name](arg) _cli_extra_argument_registry[self._commmand][name] = arg expanded_arguments.append(name) self.argument(argument_name, arg_type=ignore_type, validator=get_complex_argument_processor(expanded_arguments, argument_name, model_type))
def function_arguments_loader(): return dict(extract_args_from_signature(get_op_handler(custom_function_op))) \ if custom_function_op else {}
def set_arguments_loader(): return dict(extract_args_from_signature(get_op_handler(setter_op), no_wait_param=no_wait_param))
def arguments_loader(): return extract_args_from_signature(get_op_handler(operation))
def get_arguments_loader(): return dict(extract_args_from_signature(get_op_handler(getter_op)))
def _load_transformed_arguments(self, handler): """Load all the command line arguments from the request parameters. :param func handler: The operation function. """ from azure.cli.core.commands.parameters import file_type from argcomplete.completers import FilesCompleter, DirectoriesCompleter self.parser = BatchArgumentTree(self.validator, self.silent) self._load_options_model(handler) for arg in extract_args_from_signature(handler): arg_type = find_param_type(handler, arg[0]) if arg[0] == self._options_param: for option_arg in self._process_options(): yield option_arg elif arg_type.startswith("str or"): docstring = find_param_help(handler, arg[0]) choices = [] values_index = docstring.find(' Possible values include') if values_index >= 0: choices = docstring[values_index + 25:].split(', ') choices = [c for c in choices if c != "'unmapped'"] docstring = docstring[0:values_index] yield (arg[0], CliCommandArgument(arg[0], options_list=[arg_name(arg[0])], required=False, default=None, choices=choices, help=docstring)) elif arg_type.startswith(":class:"): # TODO: could add handling for enums param_type = class_name(arg_type) self.parser.set_request_param(arg[0], param_type) param_model = _load_model(param_type) self._flatten_object(arg[0], param_model) for flattened_arg in self.parser.compile_args(): yield flattened_arg param = 'json_file' docstring = "A file containing the {} specification in JSON format. " \ "If this parameter is specified, all '{} Arguments'" \ " are ignored.".format(arg[0].replace('_', ' '), group_title(arg[0])) yield (param, CliCommandArgument(param, options_list=[arg_name(param)], required=False, default=None, type=file_type, completer=FilesCompleter(), help=docstring)) elif arg[0] not in self.ignore: yield arg if find_return_type(handler) == 'Generator': param = 'destination' docstring = "The path to the destination file or directory." yield (param, CliCommandArgument(param, options_list=[arg_name(param)], required=True, default=None, completer=DirectoriesCompleter(), type=file_type, validator=validators.validate_file_destination, help=docstring)) if self.confirmation: param = CONFIRM_PARAM_NAME docstring = 'Do not prompt for confirmation.' yield (param, CliCommandArgument(param, options_list=['--yes', '-y'], required=False, action='store_true', help=docstring))
def cli_generic_wait_command(module_name, name, getter_op, factory=None): if not isinstance(getter_op, string_types): raise ValueError("Getter operation must be a string. Got '{}'".format(type(getter_op))) get_arguments_loader = lambda: dict(extract_args_from_signature(get_op_handler(getter_op))) def arguments_loader(): arguments = {} arguments.update(get_arguments_loader()) return arguments def get_provisioning_state(instance): provisioning_state = getattr(instance, 'provisioning_state', None) if not provisioning_state: #some SDK, like resource-group, has 'provisioning_state' under 'properties' properties = getattr(instance, 'properties', None) if properties: provisioning_state = getattr(properties, 'provisioning_state', None) return provisioning_state def handler(args): from msrest.exceptions import ClientException import time try: client = factory() if factory else None except TypeError: client = factory(None) if factory else None getterargs = {key: val for key, val in args.items() if key in get_arguments_loader()} getter = 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")#pylint: disable=line-too-long for _ in range(0, timeout, interval): try: instance = getter(client, **getterargs) if client else getter(**getterargs) if wait_for_exists: return 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': raise CLIError('The operation failed') if wait_for_created or wait_for_updated: if provisioning_state == 'Succeeded': return if custom_condition and bool(verify_property(instance, custom_condition)): return except ClientException as ex: if getattr(ex, 'status_code', None) == 404: if wait_for_deleted: return if not any([wait_for_created, wait_for_exists, custom_condition]): raise else: raise time.sleep(interval) return CLIError('Wait operation timed-out after {} seconds'.format(timeout)) cmd = CliCommand(name, handler, arguments_loader=arguments_loader) group_name = 'Wait Condition' cmd.add_argument('timeout', '--timeout', default=3600, arg_group=group_name, type=int, help='maximum wait in seconds') cmd.add_argument('interval', '--interval', default=30, arg_group=group_name, type=int, help='polling interval in seconds') cmd.add_argument('deleted', '--deleted', action='store_true', arg_group=group_name, help='wait till deleted') cmd.add_argument('created', '--created', action='store_true', arg_group=group_name, help="wait till created with 'provisioningState' at 'Succeeded'") cmd.add_argument('updated', '--updated', action='store_true', arg_group=group_name, help="wait till updated with provisioningState at 'Succeeded'") cmd.add_argument('exists', '--exists', action='store_true', arg_group=group_name, help="wait till the resource exists") cmd.add_argument('custom', '--custom', arg_group=group_name, help=("Wait until the condition satisfies a custom JMESPath query. E.g. " "provisioningState!='InProgress', " "instanceView.statuses[?code=='PowerState/running']")) main_command_table[name] = cmd main_command_module_map[name] = module_name
def cli_generic_update_command(module_name, name, getter_op, setter_op, factory=None, setter_arg_name='parameters', # pylint: disable=too-many-arguments, line-too-long table_transformer=None, child_collection_prop_name=None, child_collection_key='name', child_arg_name='item_name', custom_function_op=None, no_wait_param=None, transform=None): if not isinstance(getter_op, string_types): raise ValueError("Getter operation must be a string. Got '{}'".format(getter_op)) if not isinstance(setter_op, string_types): raise ValueError("Setter operation must be a string. Got '{}'".format(setter_op)) if custom_function_op and not isinstance(custom_function_op, string_types): raise ValueError("Custom function operation must be a string. Got '{}'".format(custom_function_op)) #pylint: disable=line-too-long get_arguments_loader = lambda: dict(extract_args_from_signature(get_op_handler(getter_op))) set_arguments_loader = lambda: dict(extract_args_from_signature(get_op_handler(setter_op), no_wait_param=no_wait_param)) #pylint: disable=line-too-long function_arguments_loader = lambda: dict(extract_args_from_signature(get_op_handler(custom_function_op))) if custom_function_op else {} #pylint: disable=line-too-long def arguments_loader(): arguments = {} arguments.update(set_arguments_loader()) arguments.update(get_arguments_loader()) arguments.update(function_arguments_loader()) arguments.pop('instance', None) # inherited from custom_function(instance, ...) arguments.pop('parent', None) arguments.pop('expand', None) # possibly inherited from the getter arguments.pop(setter_arg_name, None) return arguments def handler(args):#pylint: disable=too-many-branches,too-many-statements from msrestazure.azure_operation import AzureOperationPoller ordered_arguments = args.pop('ordered_arguments') if 'ordered_arguments' in args else [] try: client = factory() if factory else None except TypeError: client = factory(None) if factory else None getterargs = {key: val for key, val in args.items() if key in get_arguments_loader()} getter = get_op_handler(getter_op) if child_collection_prop_name: parent = getter(client, **getterargs) if client else getter(**getterargs) instance = _get_child( parent, child_collection_prop_name, args.get(child_arg_name), child_collection_key ) else: parent = None instance = getter(client, **getterargs) if client else getter(**getterargs) # pass instance to the custom_function, if provided if custom_function_op: custom_function = get_op_handler(custom_function_op) custom_func_args = {k: v for k, v in args.items() if k in function_arguments_loader()} if child_collection_prop_name: parent = custom_function(instance, parent, **custom_func_args) else: instance = custom_function(instance, **custom_func_args) # apply generic updates after custom updates setterargs = set_arguments_loader() for k in args.copy().keys(): if k in get_arguments_loader() or k in setterargs \ or k in ('properties_to_add', 'properties_to_remove', 'properties_to_set'): args.pop(k) for key, val in args.items(): ordered_arguments.append((key, val)) for arg in ordered_arguments: arg_type, arg_values = arg if arg_type == '--set': try: for expression in arg_values: set_properties(instance, expression) except ValueError: raise CLIError('invalid syntax: {}'.format(set_usage)) elif arg_type == '--add': try: add_properties(instance, arg_values) except ValueError: raise CLIError('invalid syntax: {}'.format(add_usage)) elif arg_type == '--remove': try: remove_properties(instance, arg_values) except ValueError: raise CLIError('invalid syntax: {}'.format(remove_usage)) # Done... update the instance! getterargs[setter_arg_name] = parent if child_collection_prop_name else instance setter = get_op_handler(setter_op) no_wait = no_wait_param and setterargs.get(no_wait_param, None) if no_wait: getterargs[no_wait_param] = True opres = setter(client, **getterargs) if client else setter(**getterargs) if no_wait: return None result = opres.result() if isinstance(opres, AzureOperationPoller) else opres if child_collection_prop_name: result = _get_child( result, child_collection_prop_name, args.get(child_arg_name), child_collection_key ) # apply results transform if specified if transform: return transform(result) return result class OrderedArgsAction(argparse.Action): #pylint:disable=too-few-public-methods def __call__(self, parser, namespace, values, option_string=None): if not getattr(namespace, 'ordered_arguments', None): setattr(namespace, 'ordered_arguments', []) namespace.ordered_arguments.append((option_string, values)) cmd = CliCommand(name, handler, table_transformer=table_transformer, arguments_loader=arguments_loader) group_name = 'Generic Update' cmd.add_argument('properties_to_set', '--set', nargs='+', action=OrderedArgsAction, default=[], help='Update an object by specifying a property path and value to set.' ' Example: {}'.format(set_usage), metavar='KEY=VALUE', arg_group=group_name) cmd.add_argument('properties_to_add', '--add', nargs='+', action=OrderedArgsAction, default=[], help='Add an object to a list of objects by specifying a path and key' ' value pairs. Example: {}'.format(add_usage), metavar='LIST KEY=VALUE', arg_group=group_name) cmd.add_argument('properties_to_remove', '--remove', nargs='+', action=OrderedArgsAction, default=[], help='Remove a property or an element from a list. Example: ' '{}'.format(remove_usage), metavar='LIST INDEX', arg_group=group_name) main_command_table[name] = cmd main_command_module_map[name] = module_name
def _create_key_vault_command(module_name, name, operation, transform_result, table_transformer): if not isinstance(operation, string_types): raise ValueError( "Operation must be a string. Got '{}'".format(operation)) def _execute_command(kwargs): from msrest.paging import Paged from msrest.exceptions import ValidationError, ClientRequestError from msrestazure.azure_operation import AzureOperationPoller from azure.cli.core._profile import Profile from azure.cli.command_modules.keyvault.keyvaultclient import \ (KeyVaultClient, KeyVaultAuthentication) from azure.cli.command_modules.keyvault.keyvaultclient.generated import \ (KeyVaultClient as BaseKeyVaultClient) from azure.cli.command_modules.keyvault.keyvaultclient.generated.models import \ (KeyVaultErrorException) try: def get_token(server, resource, scope): # pylint: disable=unused-argument return Profile().get_login_credentials( resource)[0]._token_retriever() # pylint: disable=protected-access op = get_op_handler(operation) # since the convenience client can be inconvenient, we have to check and create the # correct client version if 'generated' in op.__module__: client = BaseKeyVaultClient(KeyVaultAuthentication(get_token)) else: client = KeyVaultClient(KeyVaultAuthentication(get_token)) # pylint: disable=redefined-variable-type result = op(client, **kwargs) # apply results transform if specified 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('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 (ValidationError, KeyVaultErrorException) as ex: try: raise CLIError(ex.inner_exception.error.message) except AttributeError: raise CLIError(ex) except ClientRequestError as ex: if 'Failed to establish a new connection' in str( ex.inner_exception): raise CLIError( 'Max retries exceeded attempting to connect to vault. ' 'The vault may not exist or you may need to flush your DNS cache ' 'and try again later.') raise CLIError(ex) command_module_map[name] = module_name name = ' '.join(name.split()) arguments_loader = lambda: extract_args_from_signature( get_op_handler(operation)) description_loader = lambda: extract_full_summary_from_signature( get_op_handler(operation)) cmd = CliCommand(name, _execute_command, table_transformer=table_transformer, arguments_loader=arguments_loader, description_loader=description_loader) return cmd
def cli_generic_update_command(name, getter, setter, factory=None, setter_arg_name='parameters', # pylint: disable=too-many-arguments table_transformer=None, child_collection_prop_name=None, child_collection_key='name', child_arg_name='item_name', custom_function=None): get_arguments = dict(extract_args_from_signature(getter)) set_arguments = dict(extract_args_from_signature(setter)) function_arguments = dict(extract_args_from_signature(custom_function)) \ if custom_function else None def handler(args): ordered_arguments = args.pop('ordered_arguments') if 'ordered_arguments' in args else [] try: client = factory() if factory else None except TypeError: client = factory(None) if factory else None getterargs = {key: val for key, val in args.items() if key in get_arguments} if child_collection_prop_name: parent = getter(client, **getterargs) if client else getter(**getterargs) instance = _get_child( parent, child_collection_prop_name, args.get(child_arg_name), child_collection_key ) else: parent = None instance = getter(client, **getterargs) if client else getter(**getterargs) # pass instance to the custom_function, if provided if custom_function: custom_func_args = {k: v for k, v in args.items() if k in function_arguments} if child_collection_prop_name: parent = custom_function(instance, parent, **custom_func_args) else: instance = custom_function(instance, **custom_func_args) # apply generic updates after custom updates for k in args.copy().keys(): if k in get_arguments or k in set_arguments \ or k in ('properties_to_add', 'properties_to_remove', 'properties_to_set'): args.pop(k) for key, val in args.items(): ordered_arguments.append((key, val)) for arg in ordered_arguments: arg_type, arg_values = arg if arg_type == '--set': try: for expression in arg_values: set_properties(instance, expression) except ValueError: raise CLIError('invalid syntax: {}'.format(set_usage)) elif arg_type == '--add': try: add_properties(instance, arg_values) except ValueError: raise CLIError('invalid syntax: {}'.format(add_usage)) elif arg_type == '--remove': try: remove_properties(instance, arg_values) except ValueError: raise CLIError('invalid syntax: {}'.format(remove_usage)) # Done... update the instance! getterargs[setter_arg_name] = parent if child_collection_prop_name else instance opres = setter(client, **getterargs) if client else setter(**getterargs) result = opres.result() if isinstance(opres, AzureOperationPoller) else opres if child_collection_prop_name: return _get_child( result, child_collection_prop_name, args.get(child_arg_name), child_collection_key ) else: return result class OrderedArgsAction(argparse.Action): #pylint:disable=too-few-public-methods def __call__(self, parser, namespace, values, option_string=None): if not getattr(namespace, 'ordered_arguments', None): setattr(namespace, 'ordered_arguments', []) namespace.ordered_arguments.append((option_string, values)) cmd = CliCommand(name, handler, table_transformer=table_transformer) cmd.arguments.update(set_arguments) cmd.arguments.update(get_arguments) if function_arguments: cmd.arguments.update(function_arguments) cmd.arguments.pop('instance', None) # inherited from custom_function(instance, ...) cmd.arguments.pop('parent', None) cmd.arguments.pop('expand', None) # possibly inherited from the getter cmd.arguments.pop(setter_arg_name, None) group_name = 'Generic Update' cmd.add_argument('properties_to_set', '--set', nargs='+', action=OrderedArgsAction, default=[], help='Update an object by specifying a property path and value to set.' ' Example: {}'.format(set_usage), metavar='KEY=VALUE', arg_group=group_name) cmd.add_argument('properties_to_add', '--add', nargs='+', action=OrderedArgsAction, default=[], help='Add an object to a list of objects by specifying a path and key' ' value pairs. Example: {}'.format(add_usage), metavar='LIST KEY=VALUE', arg_group=group_name) cmd.add_argument('properties_to_remove', '--remove', nargs='+', action=OrderedArgsAction, default=[], help='Remove a property or an element from a list. Example: ' '{}'.format(remove_usage), metavar='LIST INDEX', arg_group=group_name) main_command_table[name] = cmd
def _create_key_vault_command(module_name, name, operation, transform_result, table_transformer): if not isinstance(operation, string_types): raise ValueError("Operation must be a string. Got '{}'".format(operation)) def _execute_command(kwargs): from msrest.paging import Paged from msrest.exceptions import ValidationError, ClientRequestError from msrestazure.azure_operation import AzureOperationPoller from azure.cli.core._profile import Profile from azure.keyvault import KeyVaultClient, KeyVaultAuthentication from azure.keyvault.generated import KeyVaultClient as BaseKeyVaultClient from azure.keyvault.generated.models import KeyVaultErrorException try: def get_token(server, resource, scope): # pylint: disable=unused-argument try: return ( Profile().get_login_credentials(resource)[0]._token_retriever() ) # pylint: disable=protected-access except adal.AdalError as err: # pylint: disable=no-member if ( hasattr(err, "error_response") and ("error_description" in err.error_response) and ("AADSTS70008:" in err.error_response["error_description"]) ): raise CLIError("Credentials have expired due to inactivity. Please run 'az login'") raise CLIError(err) op = get_op_handler(operation) # since the convenience client can be inconvenient, we have to check and create the # correct client version if "generated" in op.__module__: client = BaseKeyVaultClient(KeyVaultAuthentication(get_token)) else: client = KeyVaultClient(KeyVaultAuthentication(get_token)) # pylint: disable=redefined-variable-type result = op(client, **kwargs) # apply results transform if specified 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("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 (ValidationError, KeyVaultErrorException) as ex: try: raise CLIError(ex.inner_exception.error.message) except AttributeError: raise CLIError(ex) except ClientRequestError as ex: if "Failed to establish a new connection" in str(ex.inner_exception): raise CLIError( "Max retries exceeded attempting to connect to vault. " "The vault may not exist or you may need to flush your DNS cache " "and try again later." ) raise CLIError(ex) command_module_map[name] = module_name name = " ".join(name.split()) arguments_loader = lambda: extract_args_from_signature(get_op_handler(operation)) description_loader = lambda: extract_full_summary_from_signature(get_op_handler(operation)) cmd = CliCommand( name, _execute_command, table_transformer=table_transformer, arguments_loader=arguments_loader, description_loader=description_loader, ) return cmd
def _load_transformed_arguments(self, handler): """Load all the command line arguments from the request parameters. :param func handler: The operation function. """ from azure.cli.core.commands.parameters import file_type from argcomplete.completers import FilesCompleter, DirectoriesCompleter self.parser = BatchArgumentTree(self.validator, self.silent) self._load_options_model(handler) for arg in extract_args_from_signature(handler): arg_type = find_param_type(handler, arg[0]) if arg[0] == self._options_param: for option_arg in self._process_options(): yield option_arg elif arg_type.startswith("str or"): docstring = find_param_help(handler, arg[0]) choices = [] values_index = docstring.find(' Possible values include') if values_index >= 0: choices = docstring[values_index + 25:].split(', ') choices = [c for c in choices if c != "'unmapped'"] docstring = docstring[0:values_index] yield (arg[0], CliCommandArgument(arg[0], options_list=[arg_name(arg[0])], required=False, default=None, choices=choices, help=docstring)) elif arg_type.startswith( ":class:"): # TODO: could add handling for enums param_type = class_name(arg_type) self.parser.set_request_param(arg[0], param_type) param_model = _load_model(param_type) self._flatten_object(arg[0], param_model) for flattened_arg in self.parser.compile_args(): yield flattened_arg param = 'json_file' docstring = "A file containing the {} specification in JSON format. " \ "If this parameter is specified, all '{} Arguments'" \ " are ignored.".format(arg[0].replace('_', ' '), group_title(arg[0])) yield (param, CliCommandArgument(param, options_list=[arg_name(param)], required=False, default=None, type=file_type, completer=FilesCompleter(), help=docstring)) elif arg[0] not in self.ignore: yield arg return_type = find_return_type(handler) if return_type == 'Generator': param = 'destination' docstring = "The path to the destination file or directory." yield (param, CliCommandArgument( param, options_list=[arg_name(param)], required=True, default=None, completer=DirectoriesCompleter(), type=file_type, validator=validators.validate_file_destination, help=docstring)) if return_type == 'None' and handler.__name__.startswith('get'): self.head_cmd = True if self.confirmation: param = CONFIRM_PARAM_NAME docstring = 'Do not prompt for confirmation.' yield (param, CliCommandArgument(param, options_list=['--yes', '-y'], required=False, action='store_true', help=docstring))
def set_arguments_loader(): return dict( extract_args_from_signature(get_op_handler(setter_op), no_wait_param=no_wait_param))