I am trying to query a response from the following az command:
az provider operation show --namespace Microsoft.KeyVault
I want to query all operations that relate to secrets to be able to build a custom role for a RBAC enabled key vault. This is a short excerpt from the entire response:
{
"displayName": "Microsoft Key Vault",
"id": "/providers/Microsoft.Authorization/providerOperations/Microsoft.KeyVault",
"name": "Microsoft.KeyVault",
"operations": [
{
"description": "Registers a subscription",
"displayName": "Register Subscription",
"isDataAction": false,
"name": "Microsoft.KeyVault/register/action",
"origin": null,
"properties": null
},
{
"description": "Unregisters a subscription",
"displayName": "Unregister Subscription",
"isDataAction": false,
"name": "Microsoft.KeyVault/unregister/action",
"origin": null,
"properties": null
}
],
"resourceTypes": [
{
"displayName": "Secret",
"name": "vaults/secrets",
"operations": [
{
"description": "View the properties of a secret, but not its value.",
"displayName": "Read Secret Properties",
"isDataAction": false,
"name": "Microsoft.KeyVault/vaults/secrets/read",
"origin": null,
"properties": null
},
{
"description": "Creates a new secret or updates the value of an existing secret.",
"displayName": "Write Secret",
"isDataAction": false,
"name": "Microsoft.KeyVault/vaults/secrets/write",
"origin": null,
"properties": null
}
]
}
],
"type": "Microsoft.Authorization/providerOperations"
}
I would like to extract the description as well as the name from the object in the array resourceTypes
where resourceTypes.name=="vaults/secrets"
.
I am trying to build this gradually, but I am stuck on even filtering out this object. This is where I'm at, and I've derived this from the JMESPath documentation on filtering arrays:
az provider operation show --namespace Microsoft.KeyVault --query "[?resourceTypes.name=='vaults/secrets']"
This is just empty however.
The end result I am after is a tabular output with the description
and name
from each operation within the operations array.
Any help would be greatly appreciated.
CodePudding user response:
The issue in your current query is that doing []
would assume you address an array at the root of your JSON, when what you have at the root of your JSON is a map:
{
"resourceTypes": [
{
"name": "vaults/secrets",
}
]
}
So, the beginning of the query to address your JSON should rather be
resourceTypes[?name == 'vaults/secrets']
While your query would work on a JSON looking like:
[
{
"resourceTypes": {
"name": "vaults/secrets",
}
}
]
From there on, you need to query the operations
array, so operations[]
and do a multiselect hash to trim down the map to the fields you are looking for.
We end with the query:
resourceTypes[?name == 'vaults/secrets']
.operations[]
.{
description: description,
name: name
}
And, lastly, to format it in table, you can use the table output format of the Azure client.
We end up with the line:
az provider operation show \
--namespace Microsoft.KeyVault \
--query "resourceTypes[?name == 'vaults/secrets']
.operations[]
.{
description: description,
name: name
}" \
--out table