Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--ids parameter does not work correctly with --query #5591

Closed
tjprescott opened this issue Feb 14, 2018 · 4 comments · Fixed by #6105
Closed

--ids parameter does not work correctly with --query #5591

tjprescott opened this issue Feb 14, 2018 · 4 comments · Fixed by #6105
Assignees
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Infrastructure

Comments

@tjprescott
Copy link
Member

tjprescott commented Feb 14, 2018

If I run the following (admitted contrived) example, in which I want to get the names of my VNETs:

az network vnet show --ids $(az network vnet list -g {rg} --query [].id -o tsv) --query [].name

I get the following:

[
  "null",
  {
    "addressSpace": {
      "addressPrefixes": [
        "10.0.0.0/16"
      ]
    },
    "dhcpOptions": {
      "dnsServers": []
    },
    "enableDdosProtection": false,
    "enableVmProtection": false,
    "etag": "W/\"57083102-bd57-42fa-b474-3bffaa6d3ed0\"",
    "id": "/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/tjp-vnet/providers/Microsoft.Network/virtualNetworks/vnet2",
    "location": "westus",
    "name": "vnet2",
    "provisioningState": "Succeeded",
    "resourceGroup": "tjp-vnet",
    "resourceGuid": "a4073a2e-65b8-42b8-bdb9-7f7f875d8d1b",
    "subnets": [],
    "tags": {},
    "type": "Microsoft.Network/virtualNetworks",
    "virtualNetworkPeerings": []
  }
]

The query is only applied to the first item. Because --ids actually runs the command in a loop, I would actually need to use the following syntax:

az vnet show --ids $(az vnet list -g {rg} --query [].id -o tsv) --query name

This syntax is not intuitive to the user who expects a list and thus would expect list-like query syntax. However, even then my results are still screwed up:

[
  "vnet1",
  {
    "addressSpace": {
      "addressPrefixes": [
        "10.0.0.0/16"
      ]
    },
    "dhcpOptions": {
      "dnsServers": []
    },
    "enableDdosProtection": false,
    "enableVmProtection": false,
    "etag": "W/\"57083102-bd57-42fa-b474-3bffaa6d3ed0\"",
    "id": "/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/tjp-vnet/providers/Microsoft.Network/virtualNetworks/vnet2",
    "location": "westus",
    "name": "vnet2",
    "provisioningState": "Succeeded",
    "resourceGroup": "tjp-vnet",
    "resourceGuid": "a4073a2e-65b8-42b8-bdb9-7f7f875d8d1b",
    "subnets": [],
    "tags": {},
    "type": "Microsoft.Network/virtualNetworks",
    "virtualNetworkPeerings": []
  }
]

Because the query isn't applied in subsequent loops.


Environment summary

Install Method (e.g. pip, interactive script, apt-get, Docker, MSI, edge build) / CLI version (az --version) / OS version / Shell Type (e.g. bash, cmd.exe, Bash on Windows)

azure-cli (2.0.27)

acr (2.0.21)
acs (2.0.26)
advisor (0.1.2)
appservice (0.1.26)
backup (1.0.6)
batch (3.1.10)
batchai (0.1.5)
billing (0.1.7)
cdn (0.0.13)
cloud (2.0.12)
cognitiveservices (0.1.10)
configure (2.0.14)
consumption (0.2.1)
container (0.1.18)
core (2.0.27)
cosmosdb (0.1.19)
dev-tools (0.1.1)
dla (0.0.18)
dls (0.0.19)
eventgrid (0.1.10)
extension (0.0.9)
feedback (2.1.0)
find (0.2.8)
interactive (0.3.16)
iot (0.1.17)
keyvault (2.0.18)
lab (0.0.17)
monitor (0.1.2)
network (2.0.23)
nspkg (3.0.1)
profile (2.0.19)
rdbms (0.0.12)
redis (0.2.11)
reservations (0.1.1)
resource (2.0.23)
role (2.0.19)
servicefabric (0.0.10)
sql (2.0.21)
storage (2.0.25)
testsdk (0.1.0)
vm (2.0.26)

Extensions:
subscription (0.1.0)

Python location 'C:\Users\trpresco\Documents\github\azure-cli\env\Scripts\python.exe'
Extensions directory 'C:\Users\trpresco\.azure\cliextensions'

Python (Windows) 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]
@tjprescott tjprescott added bug This issue requires a change to an existing behavior in the product in order to be resolved. Infrastructure labels Feb 14, 2018
@dzeyelid
Copy link

Hello, nice to meet you.

I also got a similar problem with yours with az vm list-ip-addresses --ids $(az vm list -g {rg} --query "[].id" -o tsv) --query "[].virtualMachine.network.publicIpAddresses[].ipAddress". Only first item of that result is applied query, but after the second item is not applied query. (my environment is on portal and az version is same with yours.)

BTW, I guess that you mean az network vnet show instead of above az vnet show, don't you?

@tjprescott
Copy link
Member Author

Yes and yes. :)

@richeney
Copy link

richeney commented Feb 28, 2018

I am seeing the same thing with all of the commands using --ids, including az vm show and az vm get-instance-view. If there is more than one id provided then an array is returned, but JMESPATH queries are not handled correctly. E.g.

az vm get-instance-view --id $vmIds --output json --query "{VM:name, RG:resourceGroup, FD:instanceView.platformFaultDomain, UD:instanceView.platformUpdateDomain}"

This will work for a single id, or for the first element in an array (or loop as tjprescott discussed. The rest of the JSON comes through unqueried. Outputting to a table looks even worse.

And actually this should not work at all. If the query was applied to the whole array then the query should look like:

--query "[].{VM:name, RG:resourceGroup, FD:instanceView.platformFaultDomain, UD:instanceView.platformUpdateDomain}"

If you were looking to output an array of filtered objects.

Arguably the --ids switch should always return an array, even if it has only one element - at least then scripting around it would be consistent.

@tjprescott
Copy link
Member Author

@richeney yes, the issue is with the --ids infrastructure in the CLI, so it will affect any all commands that use it.

@tjprescott tjprescott modified the milestones: Backlog, Sprint 36 (Build) Mar 29, 2018
@tjprescott tjprescott self-assigned this Apr 9, 2018
@tjprescott tjprescott mentioned this issue Apr 12, 2018
2 tasks
tjprescott added a commit that referenced this issue Apr 17, 2018
* Fix #5591.

* More --ids improvements.

* Code review feedback.
@haroldrandom haroldrandom added bug This issue requires a change to an existing behavior in the product in order to be resolved. Infrastructure labels Oct 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Infrastructure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants