Skip to content

Commit

Permalink
--ids fixes (#6105)
Browse files Browse the repository at this point in the history
* Fix #5591.

* More --ids improvements.

* Code review feedback.
  • Loading branch information
tjprescott committed Apr 17, 2018
1 parent 24f6df3 commit e03b8b7
Show file tree
Hide file tree
Showing 5 changed files with 762 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/azure-cli-core/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Release History
2.0.32
++++++
* Added limited support for positional arguments.
* Fix issue where `--query` could not be used with `--ids`. [#5591](https://github.com/Azure/azure-cli/issues/5591)
* Improves piping scenarios from commands when using `--ids`. Supports `-o tsv` with a query specified or `-o json`
without specifying a query.

2.0.31
++++++
Expand Down
11 changes: 6 additions & 5 deletions src/azure-cli-core/azure/cli/core/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import datetime
import json
import logging as logs
import os
import sys
import time
from importlib import import_module
Expand Down Expand Up @@ -62,10 +63,9 @@ def _load_file(path):
if path == '-':
content = sys.stdin.read()
else:
import os
content = read_file_content(os.path.expanduser(path), allow_binary=True)

return content[0:-1] if content and content[-1] == '\n' else content
return content.rstrip(os.linesep)

def _maybe_load_file(arg):
ix = arg.find('@')
Expand Down Expand Up @@ -309,7 +309,6 @@ def execute(self, args):
elif cmd.no_wait_param and getattr(expanded_arg, cmd.no_wait_param, False):
result = None

# TODO: Not sure how to make this actually work with the TRANSFORM event...
transform_op = cmd.command_kwargs.get('transform', None)
if transform_op:
result = transform_op(result)
Expand All @@ -322,7 +321,6 @@ def execute(self, args):
result = todict(result)
event_data = {'result': result}
self.cli_ctx.raise_event(EVENT_INVOKER_TRANSFORM_RESULT, event_data=event_data)
self.cli_ctx.raise_event(EVENT_INVOKER_FILTER_RESULT, event_data=event_data)
result = event_data['result']
results.append(result)

Expand All @@ -336,8 +334,11 @@ def execute(self, args):
if results and len(results) == 1:
results = results[0]

event_data = {'result': results}
self.cli_ctx.raise_event(EVENT_INVOKER_FILTER_RESULT, event_data=event_data)

return CommandResultItem(
results,
event_data['result'],
table_transformer=self.commands_loader.command_table[parsed_args.command].table_transformer,
is_query_active=self.data['query_active'])

Expand Down
19 changes: 17 additions & 2 deletions src/azure-cli-core/azure/cli/core/commands/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,22 @@ def __call__(self, parser, namespace, values, option_string=None):
(dest) fields will also be of type `IterateValue`
'''
from msrestazure.tools import parse_resource_id
import os
if isinstance(values, str):
values = [values]
expanded_values = []
for val in values:
try:
# support piping values from JSON. Does not require use of --query
json_vals = json.loads(val)
for json_val in json_vals:
if 'id' in json_val:
expanded_values += [json_val['id']]
except ValueError:
# supports piping of --ids to the command when using TSV. Requires use of --query
expanded_values = expanded_values + val.split(os.linesep)
try:
for value in [values] if isinstance(values, str) else values:
for value in expanded_values:
parts = parse_resource_id(value)
for arg in [arg for arg in arguments.values() if arg.type.settings.get('id_part')]:
self.set_argument_value(namespace, arg, parts)
Expand All @@ -188,6 +202,7 @@ def __call__(self, parser, namespace, values, option_string=None):

@staticmethod
def set_argument_value(namespace, arg, parts):

existing_values = getattr(namespace, arg.name, None)
if existing_values is None:
existing_values = IterateValue()
Expand Down Expand Up @@ -226,6 +241,7 @@ def command_loaded_handler(command):
arg.required = False

def required_values_validator(namespace):

errors = [arg for arg in required_arguments
if getattr(namespace, arg.name, None) is None]

Expand All @@ -246,7 +262,6 @@ def required_values_validator(namespace):
"no other 'Resource Id' arguments should be specified.",
action=split_action(command.arguments),
nargs='+',
type=ResourceId,
validator=required_values_validator,
arg_group=group_name)

Expand Down

0 comments on commit e03b8b7

Please sign in to comment.