Skip to content

Commit 96bca11

Browse files
authored
Merge pull request #174246 from DCtheGeek/dmc-gov-python
Gov: Fix python scripts for new auth method
2 parents e70f8a4 + a159fbf commit 96bca11

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

articles/governance/policy/assign-policy-python.md

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Quickstart: New policy assignment with Python"
33
description: In this quickstart, you use Python to create an Azure Policy assignment to identify non-compliant resources.
4-
ms.date: 08/17/2021
4+
ms.date: 10/01/2021
55
ms.topic: quickstart
66
ms.custom: devx-track-python
77
---
@@ -54,6 +54,9 @@ Python can be used, including [bash on Windows 10](/windows/wsl/install-win10) o
5454

5555
# Add the CLI Core library for Python for authentication (development only!)
5656
pip install azure-cli-core
57+
58+
# Add the Azure identity library for Python
59+
pip install azure.identity
5760
```
5861

5962
> [!NOTE]
@@ -65,7 +68,7 @@ Python can be used, including [bash on Windows 10](/windows/wsl/install-win10) o
6568

6669
```bash
6770
# Check each installed library
68-
pip show azure-mgmt-policyinsights azure-mgmt-resource azure-cli-core
71+
pip show azure-mgmt-policyinsights azure-mgmt-resource azure-cli-core azure.identity
6972
```
7073

7174
## Create a policy assignment
@@ -78,16 +81,21 @@ Run the following code to create a new policy assignment:
7881

7982
```python
8083
# Import specific methods and models from other libraries
81-
from azure.common.credentials import get_azure_cli_credentials
82-
from azure.common.client_factory import get_client_from_cli_profile
8384
from azure.mgmt.resource.policy import PolicyClient
84-
from azure.mgmt.resource.policy.models import PolicyAssignment
85+
from azure.mgmt.resource.policy.models import PolicyAssignment, Identity, UserAssignedIdentitiesValue, PolicyAssignmentUpdate
86+
from azure.identity import AzureCliCredential
87+
88+
# Set subscription
89+
subId = "{subId}"
90+
assignmentLocation = "westus2"
8591

8692
# Get your credentials from Azure CLI (development only!) and get your subscription list
87-
policyClient = get_client_from_cli_profile(PolicyClient)
93+
credential = AzureCliCredential()
94+
policyClient = PolicyClient(credential, subId, base_url=none)
8895

8996
# Create details for the assignment
90-
policyAssignmentDetails = PolicyAssignment(display_name="Audit VMs without managed disks Assignment", policy_definition_id="/providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d", scope="{scope}", description="Shows all virtual machines not using managed disks")
97+
policyAssignmentIdentity = Identity(type="SystemAssigned")
98+
policyAssignmentDetails = PolicyAssignment(display_name="Audit VMs without managed disks Assignment", policy_definition_id="/providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d", description="Shows all virtual machines not using managed disks", identity=policyAssignmentIdentity, location=assignmentLocation)
9199

92100
# Create new policy assignment
93101
policyAssignment = policyClient.policy_assignments.create("{scope}", "audit-vm-manageddisks", policyAssignmentDetails)
@@ -99,6 +107,8 @@ print(policyAssignment)
99107
The preceding commands use the following information:
100108

101109
Assignment details:
110+
- **subId** - Your subscription. Needed for authentication. Replace `{subId}` with your
111+
subscription.
102112
- **display_name** - Display name for the policy assignment. In this case, you're using _Audit VMs
103113
without managed disks Assignment_.
104114
- **policy_definition_id** - The policy definition path, based on which you're using to create the
@@ -131,25 +141,29 @@ you created. Run the following code:
131141

132142
```python
133143
# Import specific methods and models from other libraries
134-
from azure.common.client_factory import get_client_from_cli_profile
135144
from azure.mgmt.policyinsights._policy_insights_client import PolicyInsightsClient
136145
from azure.mgmt.policyinsights.models import QueryOptions
146+
from azure.identity import AzureCliCredential
147+
148+
# Set subscription
149+
subId = "{subId}"
137150

138151
# Get your credentials from Azure CLI (development only!) and get your subscription list
139-
policyInsightsClient = get_client_from_cli_profile(PolicyInsightsClient)
152+
credential = AzureCliCredential()
153+
policyClient = PolicyInsightsClient(credential, subId, base_url=none)
140154

141155
# Set the query options
142156
queryOptions = QueryOptions(filter="IsCompliant eq false and PolicyAssignmentId eq 'audit-vm-manageddisks'",apply="groupby((ResourceId))")
143157

144158
# Fetch 'latest' results for the subscription
145-
results = policyInsightsClient.policy_states.list_query_results_for_subscription(policy_states_resource="latest", subscription_id="{subscriptionId}", query_options=queryOptions)
159+
results = policyInsightsClient.policy_states.list_query_results_for_subscription(policy_states_resource="latest", subscription_id=subId, query_options=queryOptions)
146160

147161
# Show results
148162
print(results)
149163
```
150164

151-
Replace `{subscriptionId}` with the subscription you want to see the compliance results for this
152-
policy assignment. For a list of other scopes and ways to summarize the data, see
165+
Replace `{subId}` with the subscription you want to see the compliance results for this policy
166+
assignment. For a list of other scopes and ways to summarize the data, see
153167
[Policy State methods](/python/api/azure-mgmt-policyinsights/azure.mgmt.policyinsights.operations.policystatesoperations#methods).
154168

155169
Your results resemble the following example:
@@ -174,11 +188,15 @@ To remove the assignment created, use the following command:
174188

175189
```python
176190
# Import specific methods and models from other libraries
177-
from azure.common.client_factory import get_client_from_cli_profile
178191
from azure.mgmt.resource.policy import PolicyClient
192+
from azure.identity import AzureCliCredential
193+
194+
# Set subscription
195+
subId = "{subId}"
179196

180197
# Get your credentials from Azure CLI (development only!) and get your subscription list
181-
policyClient = get_client_from_cli_profile(PolicyClient)
198+
credential = AzureCliCredential()
199+
policyClient = PolicyClient(credential, subId, base_url=none)
182200

183201
# Delete the policy assignment
184202
policyAssignment = policyClient.policy_assignments.delete("{scope}", "audit-vm-manageddisks")
@@ -187,7 +205,8 @@ policyAssignment = policyClient.policy_assignments.delete("{scope}", "audit-vm-m
187205
print(policyAssignment)
188206
```
189207

190-
Replace `{scope}` with the same scope you used to create the policy assignment.
208+
Replace `{subId}` with your subscription and `{scope}` with the same scope you used to create the
209+
policy assignment.
191210

192211
## Next steps
193212

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 'Quickstart: Your first Python query'
33
description: In this quickstart, you follow the steps to enable the Resource Graph library for Python and run your first query.
4-
ms.date: 07/09/2021
4+
ms.date: 10/01/2021
55
ms.topic: quickstart
66
ms.custom:
77
- devx-track-python
@@ -57,6 +57,9 @@ installed.
5757

5858
# Add the CLI Core library for Python for authentication (development only!)
5959
pip install azure-cli-core
60+
61+
# Add the Azure identity library for Python
62+
pip install azure.identity
6063
```
6164

6265
> [!NOTE]
@@ -68,7 +71,7 @@ installed.
6871

6972
```bash
7073
# Check each installed library
71-
pip show azure-mgmt-resourcegraph azure-mgmt-resource azure-cli-core
74+
pip show azure-mgmt-resourcegraph azure-mgmt-resource azure-cli-core azure.identity
7275
```
7376

7477
## Run your first Resource Graph query
@@ -87,14 +90,14 @@ subscription-based Resource Graph query. The query returns the first five Azure
8790
import azure.mgmt.resourcegraph as arg
8891

8992
# Import specific methods and models from other libraries
90-
from azure.common.credentials import get_azure_cli_credentials
91-
from azure.common.client_factory import get_client_from_cli_profile
9293
from azure.mgmt.resource import SubscriptionClient
94+
from azure.identity import AzureCliCredential
9395

9496
# Wrap all the work in a function
9597
def getresources( strQuery ):
9698
# Get your credentials from Azure CLI (development only!) and get your subscription list
97-
subsClient = get_client_from_cli_profile(SubscriptionClient)
99+
credential = AzureCliCredential()
100+
subsClient = SubscriptionClient(credential)
98101
subsRaw = []
99102
for sub in subsClient.subscriptions.list():
100103
subsRaw.append(sub.as_dict())
@@ -103,7 +106,7 @@ subscription-based Resource Graph query. The query returns the first five Azure
103106
subsList.append(sub.get('subscription_id'))
104107

105108
# Create Azure Resource Graph client and set options
106-
argClient = get_client_from_cli_profile(arg.ResourceGraphClient)
109+
argClient = arg.ResourceGraphClient(credential)
107110
argQueryOptions = arg.models.QueryRequestOptions(result_format="objectArray")
108111

109112
# Create query
@@ -152,7 +155,7 @@ the following command:
152155

153156
```bash
154157
# Remove the installed libraries from the Python environment
155-
pip uninstall azure-mgmt-resourcegraph azure-mgmt-resource azure-cli-core
158+
pip uninstall azure-mgmt-resourcegraph azure-mgmt-resource azure-cli-core azure.identity
156159
```
157160

158161
## Next steps

0 commit comments

Comments
 (0)