def set_argument_value(namespace, arg, parts): existing_values = getattr(namespace, arg.name, None) if existing_values is None: existing_values = IterateValue() existing_values.append(parts[arg.type.settings['id_part']]) else: if isinstance(existing_values, str): if not getattr(arg.type, 'configured_default_applied', None): logger.warning( "Property '%s=%s' being overriden by value '%s' from IDs parameter.", arg.name, existing_values, parts[arg.type.settings['id_part']] ) existing_values = IterateValue() existing_values.append(parts[arg.type.settings['id_part']]) setattr(namespace, arg.name, existing_values)
def set_argument_value(namespace, arg, parts): existing_values = getattr(namespace, arg.name, None) if existing_values is None: existing_values = IterateValue() existing_values.append(parts.get(arg.type.settings['id_part'], None)) else: if isinstance(existing_values, str): if not getattr(arg.type, 'configured_default_applied', None): logger.warning( "Property '%s=%s' being overriden by value '%s' from IDs parameter.", arg.name, existing_values, parts[arg.type.settings['id_part']] ) existing_values = IterateValue() existing_values.append(parts.get(arg.type.settings['id_part'])) setattr(namespace, arg.name, existing_values)
def parse_ids_arguments(_, command, args): namespace = args cmd = namespace._cmd # pylint: disable=protected-access # some commands have custom IDs and parsing. This will not work for that. if not ids_metadata.get(command, None): return ids = getattr(namespace, 'ids', getattr(namespace, '_ids', None)) required_args = [ cmd.arguments[x] for x in ids_metadata[command]['required'] ] optional_args = [ cmd.arguments[x] for x in ids_metadata[command]['optional'] ] combined_args = required_args + optional_args if not ids: # ensure the required parameters are provided if --ids is not errors = [ arg for arg in required_args if getattr(namespace, arg.name, None) is None ] if errors: missing_required = ' '.join( (arg.options_list[0] for arg in errors)) raise CLIError('({} | {}) are required'.format( missing_required, '--ids')) return # show warning if names are used in conjunction with --ids other_values = { arg.name: { 'arg': arg, 'value': getattr(namespace, arg.name, None) } for arg in combined_args } for _, data in other_values.items(): if data['value'] and not getattr(data['value'], 'is_default', None): logger.warning( "option '%s' will be ignored due to use of '--ids'.", data['arg'].type.settings['options_list'][0]) # create the empty lists, overwriting any values that may already be there for arg in combined_args: setattr(namespace, arg.name, IterateValue()) def assemble_json(ids): lcount = 0 lind = None for i, line in enumerate(ids): if line == '[': if lcount == 0: lind = i lcount += 1 elif line == ']': lcount -= 1 # final closed set of matching brackets if lcount == 0: left = lind right = i + 1 l_comp = ids[:left] m_comp = [''.join(ids[left:right])] r_comp = ids[right:] ids = l_comp + m_comp + r_comp return assemble_json(ids) # base case--no more merging required return ids # reassemble JSON strings from bash ids = assemble_json(ids) # expand the IDs into the relevant fields full_id_list = [] for val in ids: try: # support piping values from JSON. Does not require use of --query json_vals = json.loads(val) if not isinstance(json_vals, list): json_vals = [json_vals] for json_val in json_vals: if isinstance(json_val, dict) and 'id' in json_val: full_id_list += [json_val['id']] except ValueError: # supports piping of --ids to the command when using TSV. Requires use of --query full_id_list = full_id_list + val.splitlines() if full_id_list: setattr(namespace, '_ids', full_id_list) from azure.mgmt.core.tools import parse_resource_id, is_valid_resource_id for val in full_id_list: if not is_valid_resource_id(val): raise CLIError('invalid resource ID: {}'.format(val)) # place the ID parts into the correct property lists parts = parse_resource_id(val) for arg in combined_args: id_part = arg.type.settings.get('id_part') id_value = parts.get(id_part, None) if id_value is None: argument_name = arg.type.settings.get('options_list')[0] raise CLIError( "Argument {arg_name} cannot be derived from ID {id}. " "Please provide a complete resource ID " "containing all information of '{group_name}' " "arguments. ".format(id=val, arg_name=argument_name, group_name=arg.arg_group)) getattr(namespace, arg.name).append(id_value) # support deprecating --ids deprecate_info = cmd.arguments['ids'].type.settings.get( 'deprecate_info') if deprecate_info: if not hasattr(namespace, '_argument_deprecations'): setattr(namespace, '_argument_deprecations', [deprecate_info]) else: namespace._argument_deprecations.append(deprecate_info) # pylint: disable=protected-access
def parse_ids_arguments(_, command, args): namespace = args cmd = namespace._cmd # pylint: disable=protected-access # some commands have custom IDs and parsing. This will not work for that. if not ids_metadata.get(command, None): return ids = getattr(namespace, 'ids', getattr(namespace, '_ids', None)) required_args = [ cmd.arguments[x] for x in ids_metadata[command]['required'] ] optional_args = [ cmd.arguments[x] for x in ids_metadata[command]['optional'] ] combined_args = required_args + optional_args if not ids: # ensure the required parameters are provided if --ids is not errors = [ arg for arg in required_args if getattr(namespace, arg.name, None) is None ] if errors: missing_required = ' '.join( (arg.options_list[0] for arg in errors)) raise ValueError('({} | {}) are required'.format( missing_required, '--ids')) return # show warning if names are used in conjunction with --ids other_values = { arg.name: { 'arg': arg, 'value': getattr(namespace, arg.name, None) } for arg in combined_args } for _, data in other_values.items(): if data['value'] and not getattr(data['value'], 'is_default', None): logger.warning( "option '%s' will be ignored due to use of '--ids'.", data['arg'].type.settings['options_list'][0]) # create the empty lists, overwriting any values that may already be there for arg in combined_args: setattr(namespace, arg.name, IterateValue()) # expand the IDs into the relevant fields full_id_list = [] for val in ids: try: # support piping values from JSON. Does not require use of --query json_vals = json.loads(val) if not isinstance(json_vals, list): json_vals = [json_vals] for json_val in json_vals: if 'id' in json_val: full_id_list += [json_val['id']] except ValueError: # supports piping of --ids to the command when using TSV. Requires use of --query full_id_list = full_id_list + val.splitlines() for val in full_id_list: if not is_valid_resource_id(val): raise CLIError('invalid resource ID: {}'.format(val)) # place the ID parts into the correct property lists parts = parse_resource_id(val) for arg in combined_args: getattr(namespace, arg.name).append(parts[arg.type.settings['id_part']]) # support deprecating --ids deprecate_info = cmd.arguments['ids'].type.settings.get( 'deprecate_info') if deprecate_info: if not hasattr(namespace, '_argument_deprecations'): setattr(namespace, '_argument_deprecations', [deprecate_info]) else: namespace._argument_deprecations.append(deprecate_info) # pylint: disable=protected-access