|
| 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) |
0 commit comments