Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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.
Comment thread
mmyyrroonn marked this conversation as resolved.

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

Expand Down
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)
Comment thread
mmyyrroonn marked this conversation as resolved.
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 a shared query.
Comment thread
mmyyrroonn marked this conversation as resolved.
Outdated
"""


helps['graph shared-query list'] = """
Comment thread
mmyyrroonn marked this conversation as resolved.
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')
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) as g:
Comment thread
mmyyrroonn marked this conversation as resolved.
Outdated
g.custom_command('create', 'create_shared_query')
g.command('list', 'list')
g.command('delete', 'delete')
g.command('show', 'get')
Comment thread
mmyyrroonn marked this conversation as resolved.
Outdated
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
interactions:
- request:
body: '{"tags": {"a": "b"}, "properties": {"description": "AzureCliTest", "query":
"project id, name, type, location, tags"}, "location": "global"}'
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- graph shared-query create
Connection:
- keep-alive
Content-Length:
- '140'
Content-Type:
- application/json; charset=utf-8
ParameterSetName:
- -g -n -q -d --tags
User-Agent:
- python/3.7.4 (Windows-10-10.0.18362-SP0) msrest/0.6.9 msrest_azure/0.6.2 azure-mgmt-resourcegraph/2.1.0
Azure-SDK-For-Python AZURECLI/2.2.0
accept-language:
- en-US
method: PUT
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ResourceGraph/queries/clitest000002?api-version=2018-09-01-preview
response:
body:
string: '{"properties":{"resultKind":"UnKnown","timeModified":"2020-03-17T04:31:12.0470691Z","query":"project
id, name, type, location, tags","description":"AzureCliTest"},"type":"microsoft.resourcegraph/queries","location":"global","subscriptionId":"0b1f6471-1bf0-4dda-aec3-cb9272f09590","resourceGroup":"clitest.rg000001","name":"clitest000002","id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/microsoft.resourcegraph/queries/clitest000002","tags":{"a":"b"},"etag":"\"12001e1a-0000-1800-0000-5e7052900000\""}'
headers:
cache-control:
- no-cache
content-length:
- '684'
content-type:
- application/json; charset=utf-8
date:
- Tue, 17 Mar 2020 04:31:14 GMT
expires:
- '-1'
pragma:
- no-cache
server:
- Kestrel
strict-transport-security:
- max-age=31536000; includeSubDomains
transfer-encoding:
- chunked
vary:
- Accept-Encoding
x-content-type-options:
- nosniff
x-ms-ratelimit-remaining-subscription-writes:
- '1199'
x-ms-ratelimit-remaining-tenant-resource-requests:
- '1'
x-ms-user-quota-remaining:
- '1'
x-ms-user-quota-resets-after:
- 00:00:00
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- graph shared-query show
Connection:
- keep-alive
ParameterSetName:
- -g -n
User-Agent:
- python/3.7.4 (Windows-10-10.0.18362-SP0) msrest/0.6.9 msrest_azure/0.6.2 azure-mgmt-resourcegraph/2.1.0
Azure-SDK-For-Python AZURECLI/2.2.0
accept-language:
- en-US
method: GET
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ResourceGraph/queries/clitest000002?api-version=2018-09-01-preview
response:
body:
string: '{"properties":{"resultKind":"UnKnown","timeModified":"2020-03-17T04:31:12.0470691Z","query":"project
id, name, type, location, tags","description":"AzureCliTest"},"type":"microsoft.resourcegraph/queries","location":"global","subscriptionId":"0b1f6471-1bf0-4dda-aec3-cb9272f09590","resourceGroup":"clitest.rg000001","name":"clitest000002","id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/microsoft.resourcegraph/queries/clitest000002","tags":{"a":"b"},"etag":"\"12001e1a-0000-1800-0000-5e7052900000\""}'
headers:
cache-control:
- no-cache
content-length:
- '684'
content-type:
- application/json; charset=utf-8
date:
- Tue, 17 Mar 2020 04:31:15 GMT
expires:
- '-1'
pragma:
- no-cache
server:
- Kestrel
strict-transport-security:
- max-age=31536000; includeSubDomains
transfer-encoding:
- chunked
vary:
- Accept-Encoding
x-content-type-options:
- nosniff
x-ms-ratelimit-remaining-tenant-resource-requests:
- '1'
x-ms-user-quota-remaining:
- '1'
x-ms-user-quota-resets-after:
- 00:00:00
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- graph shared-query list
Connection:
- keep-alive
ParameterSetName:
- -g
User-Agent:
- python/3.7.4 (Windows-10-10.0.18362-SP0) msrest/0.6.9 msrest_azure/0.6.2 azure-mgmt-resourcegraph/2.1.0
Azure-SDK-For-Python AZURECLI/2.2.0
accept-language:
- en-US
method: GET
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ResourceGraph/queries?api-version=2018-09-01-preview
response:
body:
string: '{"value":[{"properties":{"resultKind":"UnKnown","timeModified":"2020-03-17T04:31:12.0470691Z","query":"project
id, name, type, location, tags","description":"AzureCliTest"},"type":"microsoft.resourcegraph/queries","location":"global","subscriptionId":"0b1f6471-1bf0-4dda-aec3-cb9272f09590","resourceGroup":"clitest.rg000001","name":"clitest000002","id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/microsoft.resourcegraph/queries/clitest000002","tags":{"a":"b"},"etag":"\"12001e1a-0000-1800-0000-5e7052900000\""}]}'
headers:
cache-control:
- no-cache
content-length:
- '696'
content-type:
- application/json; charset=utf-8
date:
- Tue, 17 Mar 2020 04:31:16 GMT
expires:
- '-1'
pragma:
- no-cache
server:
- Kestrel
strict-transport-security:
- max-age=31536000; includeSubDomains
transfer-encoding:
- chunked
vary:
- Accept-Encoding
x-content-type-options:
- nosniff
x-ms-ratelimit-remaining-tenant-resource-requests:
- '1'
x-ms-user-quota-remaining:
- '1'
x-ms-user-quota-resets-after:
- 00:00:00
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- graph shared-query delete
Connection:
- keep-alive
Content-Length:
- '0'
ParameterSetName:
- -g -n
User-Agent:
- python/3.7.4 (Windows-10-10.0.18362-SP0) msrest/0.6.9 msrest_azure/0.6.2 azure-mgmt-resourcegraph/2.1.0
Azure-SDK-For-Python AZURECLI/2.2.0
accept-language:
- en-US
method: DELETE
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ResourceGraph/queries/clitest000002?api-version=2018-09-01-preview
response:
body:
string: ''
headers:
cache-control:
- no-cache
content-length:
- '0'
date:
- Tue, 17 Mar 2020 04:31:24 GMT
expires:
- '-1'
pragma:
- no-cache
server:
- Kestrel
strict-transport-security:
- max-age=31536000; includeSubDomains
x-content-type-options:
- nosniff
x-ms-ratelimit-remaining-subscription-deletes:
- '14999'
x-ms-ratelimit-remaining-tenant-resource-requests:
- '1'
x-ms-user-quota-remaining:
- '1'
x-ms-user-quota-resets-after:
- 00:00:00
status:
code: 200
message: OK
version: 1
Loading