Skip to content

Commit 94fdba8

Browse files
authored
Merge pull request #115250 from DCtheGeek/dmc-arg-pythonqs
Add First Query quickstart for Python
2 parents 174bc45 + 52efc6d commit 94fdba8

File tree

6 files changed

+189
-21
lines changed

6 files changed

+189
-21
lines changed

articles/governance/resource-graph/first-query-azurecli.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Quickstart: Your first Azure CLI query"
33
description: In this quickstart, you follow the steps to enable the Resource Graph extension for Azure CLI and run your first query.
4-
ms.date: 11/21/2019
4+
ms.date: 05/20/2020
55
ms.topic: quickstart
66
---
77
# Quickstart: Run your first Resource Graph query using Azure CLI
@@ -76,8 +76,9 @@ Resource Graph query. The query will return the first five Azure resources with
7676

7777
> [!NOTE]
7878
> Just as with the first query, running this query multiple times is likely to yield a different
79-
> set of resources per request. The order of the query commands is important. In this example, the
80-
> `order by` comes after the `limit`. This will first limit the query results and then order them.
79+
> set of resources per request. The order of the query commands is important. In this example,
80+
> the `order by` comes after the `limit`. This command order first limits the query results and
81+
> then orders them.
8182
8283
1. Update the query to first `order by` the **Name** property and then `limit` to the top five results:
8384

@@ -87,8 +88,8 @@ Resource Graph query. The query will return the first five Azure resources with
8788
```
8889

8990
When the final query is run several times, assuming that nothing in your environment is changing,
90-
the results returned will be consistent and as expected -- ordered by the **Name** property, but
91-
still limited to the top five results.
91+
the results returned are consistent and ordered by the **Name** property, but still limited to the
92+
top five results.
9293

9394
## Clean up resources
9495

@@ -103,7 +104,7 @@ az extension remove -n resource-graph
103104
## Next steps
104105

105106
In this quickstart, you've added the Resource Graph extension to your Azure CLI environment and run
106-
your first query. To learn more about the Resource graph language, continue to the query language
107+
your first query. To learn more about the Resource Graph language, continue to the query language
107108
details page.
108109

109110
> [!div class="nextstepaction"]

articles/governance/resource-graph/first-query-portal.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Quickstart: Your first portal query"
33
description: In this quickstart, you follow the steps to run your first query from Azure portal using Azure Resource Graph Explorer.
4-
ms.date: 11/21/2019
4+
ms.date: 05/20/2020
55
ms.topic: quickstart
66
---
77
# Quickstart: Run your first Resource Graph query using Azure Resource Graph Explorer
@@ -44,16 +44,16 @@ following these steps to run your first Resource Graph query:
4444
> [!NOTE]
4545
> Just as with the first query, running this query multiple times is likely to yield a different
4646
> set of resources per request. The order of the query commands is important. In this example,
47-
> the `order by` comes after the `limit`. This will first limit the query results and then order
48-
> them.
47+
> the `order by` comes after the `limit`. This command order first limits the query results and
48+
> then orders them.
4949
5050
1. Update the query to first `order by` the **Name** property and then `limit` to the top five
5151
results: `Resources | project name, type | order by name asc | limit 5`. Then, select **Run
5252
query**.
5353

5454
When the final query is run several times, assuming that nothing in your environment is changing,
55-
the results returned are consistent and as expected -- ordered by the **Name** property, but still
56-
limited to the top five results.
55+
the results returned are consistent and ordered by the **Name** property, but still limited to the
56+
top five results.
5757

5858
### Schema browser
5959

@@ -128,7 +128,7 @@ your Azure portal workflow, try out these example dashboards.
128128
[![Example image for Sample Dashboard #2](./media/arge-sample2-small.png)](./media/arge-sample2-large.png#lightbox)
129129

130130
> [!NOTE]
131-
> Counts and charts in the above example dashboard screenshots will vary depending on your Azure
131+
> Counts and charts in the above example dashboard screenshots vary depending on your Azure
132132
> environment.
133133
134134
1. Select and download the sample dashboard you want to evaluate.
@@ -158,7 +158,7 @@ can do so with the following steps:
158158
## Next steps
159159

160160
In this quickstart, you've used Azure Resource Graph Explorer to run your first query and looked at
161-
dashboard examples powered by Resource Graph. To learn more about the Resource graph language,
161+
dashboard examples powered by Resource Graph. To learn more about the Resource Graph language,
162162
continue to the query language details page.
163163

164164
> [!div class="nextstepaction"]

articles/governance/resource-graph/first-query-powershell.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Quickstart: Your first PowerShell query"
33
description: In this quickstart, you follow the steps to enable the Resource Graph module for Azure PowerShell and run your first query.
4-
ms.date: 11/21/2019
4+
ms.date: 05/20/2020
55
ms.topic: quickstart
66
---
77
# Quickstart: Run your first Resource Graph query using Azure PowerShell
@@ -55,7 +55,7 @@ The Resource Graph module for PowerShell is **Az.ResourceGraph**.
5555
## Run your first Resource Graph query
5656

5757
With the Azure PowerShell module added to your environment of choice, it's time to try out a simple
58-
Resource Graph query. The query will return the first five Azure resources with the **Name** and
58+
Resource Graph query. The query returns the first five Azure resources with the **Name** and
5959
**Resource Type** of each resource.
6060

6161
1. Run your first Azure Resource Graph query using the `Search-AzGraph` cmdlet:
@@ -81,8 +81,8 @@ Resource Graph query. The query will return the first five Azure resources with
8181
> [!NOTE]
8282
> Just as with the first query, running this query multiple times is likely to yield a different
8383
> set of resources per request. The order of the query commands is important. In this example,
84-
> the `order by` comes after the `limit`. This will first limit the query results and then order
85-
> them.
84+
> the `order by` comes after the `limit`. This command order first limits the query results and
85+
> then orders them.
8686
8787
1. Update the query to first `order by` the **Name** property and then `limit` to the top five
8888
results:
@@ -93,8 +93,8 @@ Resource Graph query. The query will return the first five Azure resources with
9393
```
9494

9595
When the final query is run several times, assuming that nothing in your environment is changing,
96-
the results returned will be consistent and as expected -- ordered by the **Name** property, but
97-
still limited to the top five results.
96+
the results returned are consistent and ordered by the **Name** property, but still limited to the
97+
top five results.
9898

9999
> [!NOTE]
100100
> If the query does not return results from a subscription you already have access to, then note
@@ -125,7 +125,7 @@ Uninstall-Module -Name 'Az.ResourceGraph'
125125
## Next steps
126126

127127
In this quickstart, you've added the Resource Graph module to your Azure PowerShell environment and
128-
run your first query. To learn more about the Resource graph language, continue to the query
128+
run your first query. To learn more about the Resource Graph language, continue to the query
129129
language details page.
130130

131131
> [!div class="nextstepaction"]
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
title: "Quickstart: Your first Python query"
3+
description: In this quickstart, you follow the steps to enable the Resource Graph library for Python and run your first query.
4+
ms.date: 05/26/2020
5+
ms.topic: quickstart
6+
---
7+
# Quickstart: Run your first Resource Graph query using Python
8+
9+
The first step to using Azure Resource Graph is to check that the required libraries for Python are
10+
installed. This quickstart walks you through the process of adding the libraries to your Python
11+
installation.
12+
13+
At the end of this process, you'll have added the libraries to your Python installation and run your
14+
first Resource Graph query.
15+
16+
## Prerequisites
17+
18+
If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account
19+
before you begin.
20+
21+
[!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)]
22+
23+
## Add the Resource Graph library
24+
25+
To enable Python to query Azure Resource Graph, the library must be added. This library works
26+
wherever Python can be used, including [bash on Windows 10](/windows/wsl/install-win10) or locally
27+
installed.
28+
29+
1. Check that the latest Python is installed (at least **3.8**). If it isn't yet installed, download
30+
it at [Python.org](https://www.python.org/downloads/).
31+
32+
1. Check that the latest Azure CLI is installed (at least **2.5.1**). If it isn't yet installed, see
33+
[Install the Azure CLI](/cli/azure/install-azure-cli).
34+
35+
> [!NOTE]
36+
> Azure CLI is required to enable Python to use the **CLI-based authentication** in the following
37+
> examples. For information about other options, see
38+
> [Authenticate using the Azure management libraries for Python](/azure/developer/python/azure-sdk-authenticate).
39+
40+
1. Authenticate through Azure CLI.
41+
42+
```azurecli
43+
az login
44+
```
45+
46+
1. In your Python environment of choice, install the required libraries for Azure Resource Graph:
47+
48+
```bash
49+
# Add the Resource Graph library for Python
50+
pip install azure-mgmt-resourcegraph
51+
52+
# Add the Resources library for Python
53+
pip install azure-mgmt-resource
54+
55+
# Add the CLI Core library for Python for authentication (development only!)
56+
pip install azure-cli-core
57+
```
58+
59+
> [!NOTE]
60+
> If Python is installed for all users, this command must be run from an elevated console.
61+
62+
1. Validate that the libraries have been installed. `azure-mgmt-resourcegraph` should be **2.0.0**
63+
or higher, `azure-mgmt-resource` should be **9.0.0** or higher, and `azure-cli-core` should be
64+
**2.5.0** or higher.
65+
66+
```bash
67+
# Check each installed library
68+
pip show azure-mgmt-resourcegraph
69+
pip show azure-mgmt-resource
70+
pip show azure-cli-core
71+
```
72+
73+
## Run your first Resource Graph query
74+
75+
With the Python libraries added to your environment of choice, it's time to try out a simple
76+
Resource Graph query. The query returns the first five Azure resources with the **Name** and
77+
**Resource Type** of each resource.
78+
79+
1. Run your first Azure Resource Graph query using the installed libraries and the `resources`
80+
method:
81+
82+
```python
83+
# Import Azure Resource Graph library
84+
import azure.mgmt.resourcegraph as arg
85+
86+
# Import specific methods and models from other libraries
87+
from azure.common.credentials import get_azure_cli_credentials
88+
from azure.common.client_factory import get_client_from_cli_profile
89+
from azure.mgmt.resource import SubscriptionClient
90+
91+
# Wrap all the work in a function
92+
def getresources( strQuery ):
93+
# Get your credentials from Azure CLI (development only!) and get your subscription list
94+
subClient = get_client_from_cli_profile(SubscriptionClient)
95+
subsRaw = [sub.as_dict() for sub in subClient.subscriptions.list()]
96+
subList = []
97+
for sub in subsRaw:
98+
subList.append(sub.get('subscription_id'))
99+
100+
# Create Azure Resource Graph client and set options
101+
argClient = get_client_from_cli_profile(arg.ResourceGraphClient)
102+
argQueryOptions = arg.models.QueryRequestOptions(result_format="objectArray")
103+
104+
# Create query
105+
argQuery = arg.models.QueryRequest(subscriptions=subList, query=strQuery, options=argQueryOptions)
106+
107+
# Run query
108+
argResults = argClient.resources(argQuery)
109+
110+
# Show JSON results
111+
print(argResults)
112+
113+
getresources("Resources | project name, type | limit 5")
114+
```
115+
116+
> [!NOTE]
117+
> As this query example does not provide a sort modifier such as `order by`, running this query multiple
118+
> times is likely to yield a different set of resources per request.
119+
120+
1. Update the call to `getresources` and change the query to `order by` the **Name** property:
121+
122+
```python
123+
getresources("Resources | project name, type | limit 5 | order by name asc")
124+
```
125+
126+
> [!NOTE]
127+
> Just as with the first query, running this query multiple times is likely to yield a different
128+
> set of resources per request. The order of the query commands is important. In this example,
129+
> the `order by` comes after the `limit`. This command order first limits the query results and
130+
> then orders them.
131+
132+
1. Update the call to `getresources` and change the query to first `order by` the **Name** property
133+
and then `limit` to the top five results:
134+
135+
```python
136+
getresources("Resources | project name, type | order by name asc | limit 5")
137+
```
138+
139+
When the final query is run several times, assuming that nothing in your environment is changing,
140+
the results returned are consistent and ordered by the **Name** property, but still limited to the
141+
top five results.
142+
143+
## Clean up resources
144+
145+
If you wish to remove the installed libraries from your Python environment, you can do so by using
146+
the following command:
147+
148+
```bash
149+
# Remove the installed libraries from the Python environment
150+
pip uninstall azure-mgmt-resourcegraph
151+
pip uninstall azure-mgmt-resource
152+
pip uninstall azure-cli-core
153+
```
154+
155+
## Next steps
156+
157+
In this quickstart, you've added the Resource Graph libraries to your Python environment and run
158+
your first query. To learn more about the Resource Graph language, continue to the query language
159+
details page.
160+
161+
> [!div class="nextstepaction"]
162+
> [Get more information about the query language](./concepts/query-language.md)

articles/governance/resource-graph/index.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ metadata:
1111
ms.collection: collection
1212
author: DCtheGeek
1313
ms.author: dacoulte
14-
ms.date: 09/01/2019
14+
ms.date: 05/20/2020
1515

1616
landingContent:
1717
- title: About Azure Resource Graph
@@ -39,6 +39,8 @@ landingContent:
3939
url: ./first-query-powershell.md
4040
- text: First query (CLI)
4141
url: ./first-query-azurecli.md
42+
- text: First query (Python)
43+
url: ./first-query-python.md
4244
- text: Share queries (portal)
4345
url: ./tutorials/create-share-query.md
4446
- linkListType: concept

articles/governance/resource-graph/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
- name: Run your first query - PowerShell
1818
displayName: module, docker, Az.ResourceGraph
1919
href: first-query-powershell.md
20+
- name: Run your first query - Python
21+
displayName: library, azure-mgmt, cli
22+
href: first-query-python.md
2023
- name: Deploy a shared query - ARM template
2124
displayName: resource manager, queries
2225
href: shared-query-template.md

0 commit comments

Comments
 (0)