Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/resource-graph/HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.1.0
++++++++++++++++++

* Support CRUD commands for Azure shared query.

0.1.8
++++++++++++++++++

Expand Down
58 changes: 58 additions & 0 deletions src/resource-graph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Azure CLI blueprint Extension #
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be blueprint, this should Resource Graph.


This package is for the 'resource-graph' extension, i.e. 'az graph'.

### How to use ###
Install this extension using the below CLI command
```
az extension add --name resoure-graph
```

### Included Features
#### Resource Graph Shared Query Management:
*Examples:*

##### Create a shared query

```
az graph shared-query create \
-g MyResourceGroup \
-n MySharedQuery \
--query "project id, name, type, location, tags" \
--description "AzureCliTest"
```

##### Show the properties of a shared query

```
az graph shared-query show \
-g MyResourceGroup \
-n MySharedQuery
```

##### Delete a shared query

```
az graph shared-query delete \
-g MyResourceGroup \
-n MySharedQuery
```

##### List all shared query in a resource group

```
az graph shared-query list -g MyResourceGroup
```

#### Resource Graph Query:
*Examples:*

##### Query the resources managed by Azure Resource Manager.

```
az graph query -q "project id, name, type, location, tags"
```

See https://aka.ms/AzureResourceGraph-QueryLanguage to learn more about query language and browse examples

If you have issues, please give feedback by opening an issue at https://github.com/Azure/azure-cli-extensions/issues.
2 changes: 0 additions & 2 deletions src/resource-graph/README.rst

This file was deleted.

6 changes: 5 additions & 1 deletion src/resource-graph/azext_resourcegraph/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
def cf_resource_graph(cli_ctx, _):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from .vendored_sdks.resourcegraph import ResourceGraphClient
return get_mgmt_service_client(cli_ctx, ResourceGraphClient, subscription_bound=False)
return get_mgmt_service_client(cli_ctx, ResourceGraphClient)


def cf_resource_graph_graph_query(cli_ctx, _):
return cf_resource_graph(cli_ctx, _).graph_query
37 changes: 37 additions & 0 deletions src/resource-graph/azext_resourcegraph/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,40 @@
text: >
az graph query -q "where type =~ "Microsoft.Compute" | project name, tags" --subscriptions 11111111-1111-1111-1111-111111111111, 22222222-2222-2222-2222-222222222222
"""

helps['graph shared-query'] = """
type: group
short-summary: Manage shared query of Azure resource graph.
"""


helps['graph shared-query create'] = """
type: command
short-summary: Create a shared query.
examples:
- name: Create a shared query requesting a subset of resource fields.
text: >
az graph shared-query create -g MyResourceGroup -n MySharedQuery -q "project id, name, type, location, tags" -d "requesting a subset of resource fields." --tags key=value
"""


helps['graph shared-query delete'] = """
type: command
short-summary: Delete a shared query.
"""


helps['graph shared-query show'] = """
type: command
short-summary: Show the properties of a shared query.
"""


helps['graph shared-query list'] = """
type: command
short-summary: List all shared query in a resource group.
examples:
- name: List all shared query in a resource group.
text: >
az graph shared-query list -g MyResourceGroup
"""
9 changes: 9 additions & 0 deletions src/resource-graph/azext_resourcegraph/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


from azure.cli.core.commands.parameters import get_generic_completion_list
from azure.cli.core.commands.parameters import tags_type

from azext_resourcegraph.resource_graph_enums import IncludeOptionsEnum

Expand Down Expand Up @@ -37,3 +38,11 @@ def load_arguments(self, _):
help='List of subscriptions to run query against. By default all accessible subscriptions are queried.')
c.argument('include', options_list=['--include'], required=False,
help='Indicates if result should be extended with subscription and tenants names. Possible values: none, displayNames')

with self.argument_context('graph shared-query') as c:
c.argument('graph_query', options_list=['--graph-query', '--q', '-q'],
completer=get_generic_completion_list(_QUERY_EXAMPLES), help='Resource Graph query to execute.')
c.argument('resource_name', options_list=['--name', '-n'], help='Name of the graph shared query.')
c.argument('tags', tags_type)
c.argument('description', options_list=['-d', '--description'], help='Description of the graph shared query.')
c.ignore('location')
4 changes: 2 additions & 2 deletions src/resource-graph/azext_resourcegraph/azext_metadata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"azext.minCliCoreVersion": "2.0.45",
"azext.isPreview": false
"azext.minCliCoreVersion": "2.3.1",
"azext.isPreview": true
}
16 changes: 11 additions & 5 deletions src/resource-graph/azext_resourcegraph/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import CliCommandType
from ._client_factory import cf_resource_graph
from ._client_factory import cf_resource_graph, cf_resource_graph_graph_query
from ._validators import validate_query_args


def load_command_table(self, _):

graph_sdk = CliCommandType(
operations_tmpl='azext_resourcegraph.custom#{}',
client_factory=cf_resource_graph
graph_shared_query_sdk = CliCommandType(
operations_tmpl='azext_resourcegraph.vendored_sdks.resourcegraph.operations#GraphQueryOperations.{}',
client_factory=cf_resource_graph_graph_query
)

with self.command_group('graph', command_type=graph_sdk, client_factory=cf_resource_graph) as g:
with self.command_group('graph', client_factory=cf_resource_graph) as g:
g.custom_command('query', 'execute_query', validator=validate_query_args)

with self.command_group('graph shared-query', graph_shared_query_sdk, is_experimental=True) as g:
g.custom_command('create', 'create_shared_query')
g.command('list', 'list')
g.command('delete', 'delete')
g.show_command('show', 'get')
17 changes: 15 additions & 2 deletions src/resource-graph/azext_resourcegraph/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ def execute_query(client, graph_query, first, skip, subscriptions, include):
return results


def create_shared_query(client, resource_group_name,
resource_name, description,
graph_query, location='global', tags=None):
from azext_resourcegraph.vendored_sdks.resourcegraph.models import GraphQueryResource
graph_shared_query = GraphQueryResource(description=description,
query=graph_query,
tags=tags,
location=location)
return client.graph_query.create_or_update(resource_group_name=resource_group_name,
resource_name=resource_name,
properties=graph_shared_query)


def _get_cached_subscriptions():
# type: () -> list[str]

Expand All @@ -95,14 +108,14 @@ def _get_cached_subscriptions():


def _get_cached_detailed_subscriptions():
# type: () -> List[Tuple[Any, Any]]
# type: () -> list[tuple[any, any]]

cached_subs = Profile().load_cached_subscriptions()
return [(sub['id'], sub["name"]) for sub in cached_subs]


def _get_cached_detailed_tenant():
# type: () -> List[Tuple[Any, Any]]
# type: () -> list[tuple[any, any]]

token = Profile().get_raw_token()
bearer_token = token[0][0] + " " + token[0][1]
Expand Down
Loading