Azure / azure-cli

Azure Command-Line Interface
MIT License
3.97k stars 2.95k forks source link

SubsciptionId missing when querying `az account list` in table format #28388

Open daviewales opened 6 months ago

daviewales commented 6 months ago

Describe the bug

I am trying to query the names and IDs of my Azure subscriptions. The following commands do exactly what I want with JSON or TSV output:

$ az account list --query "[].{name: name, id: id}"           # works
[
  {
    "id": "abc123",
    "name": "Example"
  }
]
$ az account list --query "[].{name: name, id: id}" -o tsv    # works
Example       abc123

However, if I switch to table output, I only get the name field:

$ az account list --query "[].{name: name, id: id}" -o table  # does not show subscription id
Name
-----------------
Example

I thought that perhaps the table output doesn't support the subscription ID field, so I tried the same as above, but without the --query. However, this does include the subscription ID:

$ az account list -o table
Name               CloudName    SubscriptionId                        TenantId                              State    IsDefault
-----------------  -----------  ------------------------------------  ------------------------------------  -------  -----------
Example            AzureCloud   abc123                                xyz987                                Enabled  True

It would be nice to be able to use -o table output with --query, and get the same data that is included in the other output formats.

Related command

az account list

Errors

NA

Issue script & Debug output

❯ az account list --debug --query "[].{name: name, id: id}" -o table
cli.knack.cli: Command arguments: ['account', 'list', '--debug', '--query', '[].{name: name, id: id}', '-o', 'table']
cli.knack.cli: __init__ debug log:
Enable color in terminal.
cli.knack.cli: Event: Cli.PreExecute []
cli.knack.cli: Event: CommandParser.OnGlobalArgumentsCreate [<function CLILogging.on_global_arguments at 0x7f85f958c040>, <function OutputProducer.on_global_arguments at 0x7f85f9536160>, <function CLIQuery.on_global_arguments at 0x7f85f956bc40>]
cli.knack.cli: Event: CommandInvoker.OnPreCommandTableCreate []
cli.azure.cli.core: Modules found from index for 'account': ['azure.cli.command_modules.profile', 'azure.cli.command_modules.resource', 'azext_account']
cli.azure.cli.core: Loading command modules:
cli.azure.cli.core: Name                  Load Time    Groups  Commands
cli.azure.cli.core: profile                   0.001         2         8
cli.azure.cli.core: resource                  0.067        51       228
cli.azure.cli.core: Total (2)                 0.068        53       236
cli.azure.cli.core: These extensions are not installed and will be skipped: ['azext_ai_examples', 'azext_next']
cli.azure.cli.core: Loading extensions:
cli.azure.cli.core: Name                  Load Time    Groups  Commands  Directory
cli.azure.cli.core: account                   0.002         4        13  /home/example/.azure/cliextensions/account
cli.azure.cli.core: Total (1)                 0.002         4        13
cli.azure.cli.core: Loaded 55 groups, 249 commands.
cli.azure.cli.core: Found a match in the command table.
cli.azure.cli.core: Raw command  : account list
cli.azure.cli.core: Command table: account list
cli.knack.cli: Event: CommandInvoker.OnPreCommandTableTruncate [<function AzCliLogging.init_command_file_logging at 0x7f85f845d8a0>]
cli.azure.cli.core.azlogging: metadata file logging enabled - writing logs to '/home/example/.azure/commands/2024-02-19.16-20-09.account_list.2742.log'.
az_command_data_logger: command args: account list --debug --query {} -o {}
cli.knack.cli: Event: CommandInvoker.OnPreArgumentLoad [<function register_global_subscription_argument.<locals>.add_subscription_parameter at 0x7f85f846be20>]
cli.knack.cli: Event: CommandInvoker.OnPostArgumentLoad []
cli.knack.cli: Event: CommandInvoker.OnPostCommandTableCreate [<function register_ids_argument.<locals>.add_ids_arguments at 0x7f85f84cdbc0>, <function register_cache_arguments.<locals>.add_cache_arguments at 0x7f85f84cdd00>]
cli.knack.cli: Event: CommandInvoker.OnCommandTableLoaded []
cli.knack.cli: Event: CommandInvoker.OnPreParseArgs []
cli.knack.cli: Event: CommandInvoker.OnPostParseArgs [<function OutputProducer.handle_output_argument at 0x7f85f9536200>, <function CLIQuery.handle_query_parameter at 0x7f85f956bce0>, <function register_ids_argument.<locals>.parse_ids_arguments at 0x7f85f84cdc60>]
cli.knack.cli: Event: CommandInvoker.OnTransformResult [<function _resource_group_transform at 0x7f85f84a6de0>, <function _x509_from_base64_to_hex_transform at 0x7f85f84a6e80>]
cli.knack.cli: Event: CommandInvoker.OnFilterResult [<function CLIQuery.handle_query_parameter.<locals>.filter_output at 0x7f85f7cebf60>]
Name
-----------------
Example
cli.knack.cli: Event: Cli.SuccessfulExecute []
cli.knack.cli: Event: Cli.PostExecute [<function AzCliLogging.deinit_cmd_metadata_logging at 0x7f85f845db20>]
az_command_data_logger: exit code: 0
cli.__main__: Command ran in 0.195 seconds (init: 0.112, invoke: 0.082)
cli.azure.cli.core.decorators: Suppress exception:
Traceback (most recent call last):
  File "/opt/az/lib/python3.11/site-packages/azure/cli/__main__.py", line 62, in <module>
    raise ex
  File "/opt/az/lib/python3.11/site-packages/azure/cli/__main__.py", line 55, in <module>
    sys.exit(exit_code)
SystemExit: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/az/lib/python3.11/site-packages/azure/cli/core/decorators.py", line 79, in _wrapped_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/az/lib/python3.11/site-packages/azure/cli/core/telemetry.py", line 532, in _get_secrets_warning_config
    show_secrets_warning = _get_config().getboolean('clients', 'show_secrets_warning', fallback=None)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/az/lib/python3.11/site-packages/knack/config.py", line 147, in getboolean
    raise ValueError('Not a boolean: {}'.format(val))
ValueError: Not a boolean: None

telemetry.main: Begin splitting cli events and extra events, total events: 1
telemetry.client: Accumulated 0 events. Flush the clients.
telemetry.main: Finish splitting cli events and extra events, cli events: 1
telemetry.save: Save telemetry record of length 3412 in cache
telemetry.main: Begin creating telemetry upload process.
telemetry.process: Creating upload process: "/opt/az/bin/python3 /opt/az/lib/python3.11/site-packages/azure/cli/telemetry/__init__.py /home/example/.azure"
telemetry.process: Return from creating process
telemetry.main: Finish creating telemetry upload process.

Expected behavior

I expected the -o table output to include the selected fields, the same as -o tsv.

Environment Summary

❯ az --version
azure-cli                         2.57.0

core                              2.57.0
telemetry                          1.1.0

Extensions:
account                            0.2.5
ssh                                2.0.2

Dependencies:
msal                              1.26.0
azure-mgmt-resource             23.1.0b2

Python location '/opt/az/bin/python3'
Extensions directory '/home/example/.azure/cliextensions'

Python (Linux) 3.11.7 (main, Jan 31 2024, 05:29:49) [GCC 11.4.0]

Legal docs and information: aka.ms/AzureCliLegal

Your CLI is up-to-date.

Additional context

No response

yonzhan commented 6 months ago

Thank you for opening this issue, we will look into it.

zhoxing-ms commented 6 months ago

Please note that the attribute names obtained using -o json and -o table are inconsistent. image

Please try using az account list --query "[].{Name: name, SubscriptionId: id}" -o table instead.

(env) PS C:\Users\zhoxing.FAREAST\Desktop\project> az account list --query "[].{Name: name, SubscriptionId: id}" -o table
Name                                              SubscriptionId
------------------------------------------------  ------------------------------------
.....
daviewales commented 6 months ago

That works, but it seems a bit strange that the names would be different. The query is a JMESPath query, which is defined in terms of JSON, so it's confusing that you can't use the names from the JSON output.