Skip to content

Commit 972ec16

Browse files
committed
add Python manage resources
1 parent 978d581 commit 972ec16

File tree

5 files changed

+254
-16
lines changed

5 files changed

+254
-16
lines changed

articles/azure-resource-manager/management/manage-resources-cli.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
---
22
title: Manage resources - Azure CLI
33
description: Use Azure CLI and Azure Resource Manager to manage your resources. Shows how to deploy and delete resources.
4-
author: mumian
54
ms.topic: conceptual
65
ms.date: 02/11/2019
7-
ms.author: jgao
86
ms.custom: devx-track-azurecli, devx-track-arm-template
97
---
108
# Manage Azure resources by using Azure CLI
119

1210
Learn how to use Azure CLI with [Azure Resource Manager](overview.md) to manage your Azure resources. For managing resource groups, see [Manage Azure resource groups by using Azure CLI](manage-resource-groups-cli.md).
1311

14-
Other articles about managing resources:
15-
16-
- [Manage Azure resources by using the Azure portal](manage-resources-portal.md)
17-
- [Manage Azure resources by using Azure PowerShell](manage-resources-powershell.md)
18-
1912
## Deploy resources to an existing resource group
2013

2114
You can deploy Azure resources directly by using Azure CLI, or deploy a Resource Manager template to create Azure resources.

articles/azure-resource-manager/management/manage-resources-portal.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
---
22
title: Manage resources - Azure portal
33
description: Use the Azure portal and Azure Resource Manager to manage your resources. Shows how to deploy and delete resources.
4-
author: mumian
54
ms.topic: conceptual
65
ms.custom: devx-track-arm-template
76
ms.date: 02/11/2019
8-
ms.author: jgao
97
---
108
# Manage Azure resources by using the Azure portal
119

1210
Learn how to use the [Azure portal](https://portal.azure.com) with [Azure Resource Manager](overview.md) to manage your Azure resources. For managing resource groups, see [Manage Azure resource groups by using the Azure portal](manage-resource-groups-portal.md).
1311

14-
Other articles about managing resources:
15-
16-
- [Manage Azure resources by using Azure CLI](manage-resources-cli.md)
17-
- [Manage Azure resources by using Azure PowerShell](manage-resources-powershell.md)
18-
1912
[!INCLUDE [Handle personal data](../../../includes/gdpr-intro-sentence.md)]
2013

2114
## Deploy resources to a resource group

articles/azure-resource-manager/management/manage-resources-powershell.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
---
22
title: Manage resources - Azure PowerShell
33
description: Use Azure PowerShell and Azure Resource Manager to manage your resources. Shows how to deploy and delete resources.
4-
author: mumian
54
ms.topic: conceptual
65
ms.date: 02/11/2019
7-
ms.author: jgao
86
ms.custom: devx-track-azurepowershell, devx-track-arm-template
97
---
108
# Manage Azure resources by using Azure PowerShell
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
---
2+
title: Manage resources - Python
3+
description: Use Python and Azure Resource Manager to manage your resources. Shows how to deploy and delete resources.
4+
ms.topic: conceptual
5+
ms.date: 04/21/2023
6+
ms.custom: devx-track-azurepowershell, devx-track-arm-template, ai-gen-docs
7+
---
8+
9+
# Manage Azure resources by using Python
10+
11+
Learn how to use Azure Python with [Azure Resource Manager](overview.md) to manage your Azure resources. For managing resource groups, see [Manage Azure resource groups by using Python](manage-resource-groups-python.md).
12+
13+
[!INCLUDE [AI attribution](../../../includes/ai-generated-attribution.md)]
14+
15+
## Deploy resources to an existing resource group
16+
17+
You can deploy Azure resources directly by using Python, or deploy an Azure Resource Manager template (ARM template) to create Azure resources.
18+
19+
### Deploy resources by using Python classes
20+
21+
The following example creates a storage account by using [StorageManagementClient.storage_accounts.begin_create](/python/api/azure-mgmt-storage/azure.mgmt.storage.v2022_09_01.operations.storageaccountsoperations#azure-mgmt-storage-v2022-09-01-operations-storageaccountsoperations-begin-create). The name for the storage account must be unique across Azure.
22+
23+
```python
24+
import os
25+
import random
26+
from azure.identity import AzureCliCredential
27+
from azure.mgmt.storage import StorageManagementClient
28+
29+
credential = AzureCliCredential()
30+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
31+
32+
random_postfix = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz1234567890', k=13))
33+
storage_account_name = "demostore" + random_postfix
34+
35+
storage_client = StorageManagementClient(credential, subscription_id)
36+
37+
storage_account_result = storage_client.storage_accounts.begin_create(
38+
"exampleGroup",
39+
storage_account_name,
40+
{
41+
"location": "westus",
42+
"sku": {
43+
"name": "Standard_LRS"
44+
}
45+
}
46+
)
47+
```
48+
49+
### Deploy a template
50+
51+
To deploy an ARM template, use [ResourceManagementClient.deployments.begin_create_or_update](/python/api/azure-mgmt-resource/azure.mgmt.resource.resources.v2022_09_01.operations.deploymentsoperations#azure-mgmt-resource-resources-v2022-09-01-operations-deploymentsoperations-begin-create-or-update). The following example deploys a [remote template](https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.storage/storage-account-create). That template creates a storage account.
52+
53+
```python
54+
import os
55+
from azure.identity import AzureCliCredential
56+
from azure.mgmt.resource import ResourceManagementClient
57+
from azure.mgmt.resource.resources.models import DeploymentMode
58+
59+
credential = AzureCliCredential()
60+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
61+
62+
resource_client = ResourceManagementClient(credential, subscription_id)
63+
64+
resource_group_name = input("Enter the resource group name: ")
65+
location = input("Enter the location (i.e. centralus): ")
66+
template_uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.storage/storage-account-create/azuredeploy.json"
67+
68+
rg_deployment_result = resource_client.deployments.begin_create_or_update(
69+
resource_group_name,
70+
"exampleDeployment",
71+
{
72+
"properties": {
73+
"templateLink": {
74+
"uri": template_uri
75+
},
76+
"parameters": {
77+
"location": {
78+
"value": location
79+
},
80+
},
81+
"mode": DeploymentMode.incremental
82+
}
83+
}
84+
)
85+
```
86+
87+
### Deploy a resource group and resources
88+
89+
You can create a resource group and deploy resources to the group. For more information, see [Create resource group and deploy resources](../templates/deploy-to-subscription.md#resource-groups).
90+
91+
### Deploy resources to multiple subscriptions or resource groups
92+
93+
Typically, you deploy all the resources in your template to a single resource group. However, there are scenarios where you want to deploy a set of resources together but place them in different resource groups or subscriptions. For more information, see [Deploy Azure resources to multiple subscriptions or resource groups](../templates/deploy-to-resource-group.md).
94+
95+
## Delete resources
96+
97+
The following example shows how to delete a storage account.
98+
99+
```python
100+
import os
101+
from azure.identity import AzureCliCredential
102+
from azure.mgmt.storage import StorageManagementClient
103+
104+
credential = AzureCliCredential()
105+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
106+
107+
storage_client = StorageManagementClient(credential, subscription_id)
108+
109+
resource_group_name = "demoGroup"
110+
storage_account_name = "demostore"
111+
112+
storage_account = storage_client.storage_accounts.delete(
113+
resource_group_name,
114+
storage_account_name
115+
)
116+
```
117+
118+
For more information about how Azure Resource Manager orders the deletion of resources, see [Azure Resource Manager resource group deletion](delete-resource-group.md).
119+
120+
## Move resources
121+
122+
The following example shows how to move a storage account from one resource group to another resource group.
123+
124+
```python
125+
import os
126+
from azure.identity import AzureCliCredential
127+
from azure.mgmt.resource import ResourceManagementClient
128+
129+
credential = AzureCliCredential()
130+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
131+
132+
resource_client = ResourceManagementClient(credential, subscription_id)
133+
134+
src_resource_group_name = "sourceGroup"
135+
dest_resource_group_name = "destinationGroup"
136+
storage_account_name = "demostore"
137+
138+
dest_resource_group = resource_client.resource_groups.get(dest_resource_group_name)
139+
140+
storage_account = resource_client.resources.get(
141+
src_resource_group_name, "Microsoft.Storage", "", "storageAccounts", storage_account_name, "2022-09-01"
142+
)
143+
144+
move_result = resource_client.resources.begin_move_resources(
145+
src_resource_group_name,
146+
{
147+
"resources": [storage_account.id],
148+
"targetResourceGroup": dest_resource_group.id,
149+
}
150+
)
151+
```
152+
153+
For more information, see [Move resources to new resource group or subscription](move-resource-group-and-subscription.md).
154+
155+
## Lock resources
156+
157+
Locking prevents other users in your organization from accidentally deleting or modifying critical resources, such as Azure subscription, resource group, or resource.
158+
159+
The following example locks a web site so it can't be deleted.
160+
161+
```python
162+
import os
163+
from azure.identity import AzureCliCredential
164+
from azure.mgmt.resource import ManagementLockClient
165+
166+
credential = AzureCliCredential()
167+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
168+
169+
lock_client = ManagementLockClient(credential, subscription_id)
170+
171+
lock_result = lock_client.management_locks.create_or_update_at_resource_level(
172+
"exampleGroup",
173+
"Microsoft.Web",
174+
"",
175+
"sites",
176+
"examplesite",
177+
"lockSite",
178+
{
179+
"level": "CanNotDelete"
180+
}
181+
)
182+
```
183+
184+
The following script gets all locks for a storage account:
185+
186+
```python
187+
import os
188+
from azure.identity import AzureCliCredential
189+
from azure.mgmt.resource import ResourceManagementClient
190+
from azure.mgmt.resource.locks import ManagementLockClient
191+
192+
credential = AzureCliCredential()
193+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
194+
195+
resource_client = ResourceManagementClient(credential, subscription_id)
196+
lock_client = ManagementLockClient(credential, subscription_id)
197+
198+
resource_group_name = "demoGroup"
199+
storage_account_name = "demostore"
200+
201+
resource = resource_client.resources.get_by_id(
202+
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
203+
"2021-04-01"
204+
)
205+
206+
locks = lock_client.management_locks.list_at_resource_level(
207+
resource_group_name,
208+
"Microsoft.Storage",
209+
"",
210+
"storageAccounts",
211+
storage_account_name
212+
)
213+
214+
for lock in locks:
215+
print(f"Lock Name: {lock.name}, Lock Level: {lock.level}")
216+
```
217+
218+
The following script deletes a lock of a web site:
219+
220+
```python
221+
import os
222+
from azure.identity import AzureCliCredential
223+
from azure.mgmt.resource import ManagementLockClient
224+
225+
credential = AzureCliCredential()
226+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
227+
228+
lock_client = ManagementLockClient(credential, subscription_id)
229+
230+
lock_client.management_locks.delete_at_resource_level(
231+
"exampleGroup",
232+
"Microsoft.Web",
233+
"",
234+
"sites",
235+
"examplesite",
236+
"lockSite"
237+
)
238+
```
239+
240+
For more information, see [Lock resources with Azure Resource Manager](lock-resources.md).
241+
242+
## Tag resources
243+
244+
Tagging helps organizing your resource group and resources logically. For information, see [Using tags to organize your Azure resources](tag-resources-python.md).
245+
246+
## Next steps
247+
248+
- To learn Azure Resource Manager, see [Azure Resource Manager overview](overview.md).
249+
- To learn the Resource Manager template syntax, see [Understand the structure and syntax of Azure Resource Manager templates](../templates/syntax.md).
250+
- To learn how to develop templates, see the [step-by-step tutorials](../index.yml).
251+
- To view the Azure Resource Manager template schemas, see [template reference](/azure/templates/).

articles/azure-resource-manager/management/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@
180180
- name: Azure PowerShell
181181
displayName: open,delete,deploy,move,lock,tag
182182
href: manage-resources-powershell.md
183+
- name: Python
184+
displayName: open,delete,deploy,move,lock,tag
185+
href: manage-resources-python.md
183186
- name: Resource providers and types
184187
href: resource-providers-and-types.md
185188
- name: Preview features

0 commit comments

Comments
 (0)