Skip to content

Commit 9620877

Browse files
committed
add Python to locks
1 parent b5385c8 commit 9620877

File tree

1 file changed

+148
-1
lines changed

1 file changed

+148
-1
lines changed

articles/azure-resource-manager/management/lock-resources.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
title: Protect your Azure resources with a lock
33
description: You can safeguard Azure resources from updates or deletions by locking all users and roles.
44
ms.topic: conceptual
5-
ms.date: 12/12/2022
5+
ms.date: 04/06/2023
66
ms.custom: devx-track-azurecli, devx-track-azurepowershell
77
---
88

99
# Lock your resources to protect your infrastructure
1010

1111
As an administrator, you can lock an Azure subscription, resource group, or resource to protect them from accidental user deletions and modifications. The lock overrides any user permissions.
1212

13+
[!INCLUDE [AI attribution](../../../includes/ai-generated-attribution.md)]
14+
1315
You can set locks that prevent either deletions or modifications. In the portal, these locks are called **Delete** and **Read-only**. In the command line, these locks are called **CanNotDelete** and **ReadOnly**.
1416

1517
- **CanNotDelete** means authorized users can read and modify a resource, but they can't delete it.
@@ -465,6 +467,151 @@ lockid=$(az lock show --name LockSite --resource-group exampleresourcegroup --o
465467
az lock delete --ids $lockid
466468
```
467469

470+
### Python
471+
472+
You lock deployed resources with Python by using the [ManagementLockClient.management_locks.create_or_update_at_resource_group_level](/python/api/azure-mgmt-resource/azure.mgmt.resource.locks.v2016_09_01.operations.managementlocksoperations#azure-mgmt-resource-locks-v2016-09-01-operations-managementlocksoperations-create-or-update-at-resource-group-level) command.
473+
474+
To lock a resource, provide the name of the resource, its resource type, and its resource group name.
475+
476+
```python
477+
lock_result = lock_client.management_locks.create_or_update_at_resource_level(
478+
"exampleGroup",
479+
"Microsoft.Web",
480+
"",
481+
"sites",
482+
"examplesite",
483+
"lockSite",
484+
{
485+
"level": "CanNotDelete"
486+
}
487+
)
488+
```
489+
490+
To lock a resource group, provide the name of the resource group.
491+
492+
```python
493+
import os
494+
from azure.identity import AzureCliCredential
495+
from azure.mgmt.resource import ManagementLockClient
496+
497+
credential = AzureCliCredential()
498+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
499+
500+
lock_client = ManagementLockClient(credential, subscription_id)
501+
502+
lock_result = lock_client.management_locks.create_or_update_at_resource_group_level(
503+
"exampleGroup",
504+
"lockGroup",
505+
{
506+
"level": "CanNotDelete"
507+
}
508+
)
509+
```
510+
511+
To get information about all locks in your subscription, use [ManagementLockClient.management_locks.get](/python/api/azure-mgmt-resource/azure.mgmt.resource.locks.v2016_09_01.operations.managementlocksoperations#azure-mgmt-resource-locks-v2016-09-01-operations-managementlocksoperations-list-at-subscription-level). To get all the locks in your subscription, use:
512+
513+
```python
514+
import os
515+
from azure.identity import AzureCliCredential
516+
from azure.mgmt.resource import ManagementLockClient
517+
518+
credential = AzureCliCredential()
519+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
520+
521+
lock_client = ManagementLockClient(credential, subscription_id)
522+
523+
lock_result = lock_client.management_locks.list_at_subscription_level()
524+
525+
for lock in lock_result:
526+
print(f"Lock name: {lock.name}")
527+
print(f"Lock level: {lock.level}")
528+
print(f"Lock notes: {lock.notes}")
529+
```
530+
531+
To get a lock for a resource, use:
532+
533+
```python
534+
import os
535+
from azure.identity import AzureCliCredential
536+
from azure.mgmt.resource import ManagementLockClient
537+
538+
credential = AzureCliCredential()
539+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
540+
541+
lock_client = ManagementLockClient(credential, subscription_id)
542+
543+
lock_result = lock_client.management_locks.get_at_resource_level(
544+
"exampleGroup",
545+
"Microsoft.Web",
546+
"",
547+
"sites",
548+
"examplesite",
549+
"lockSite"
550+
)
551+
552+
print(f"Lock ID: {lock_result.id}")
553+
print(f"Lock Name: {lock_result.name}")
554+
print(f"Lock Level: {lock_result.level}")
555+
```
556+
557+
To get a lock for a resource group, use:
558+
559+
```python
560+
import os
561+
from azure.identity import AzureCliCredential
562+
from azure.mgmt.resource import ManagementLockClient
563+
564+
credential = AzureCliCredential()
565+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
566+
567+
lock_client = ManagementLockClient(credential, subscription_id)
568+
569+
lock_result = lock_client.management_locks.get_at_resource_group_level(
570+
"exampleGroup",
571+
"lockGroup"
572+
)
573+
574+
print(f"Lock ID: {lock_result.id}")
575+
print(f"Lock Level: {lock_result.level}")
576+
```
577+
578+
To delete a lock for a resource, use:
579+
580+
```python
581+
import os
582+
from azure.identity import AzureCliCredential
583+
from azure.mgmt.resource import ManagementLockClient
584+
585+
credential = AzureCliCredential()
586+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
587+
588+
lock_client = ManagementLockClient(credential, subscription_id)
589+
590+
lock_client.management_locks.delete_at_resource_level(
591+
"exampleGroup",
592+
"Microsoft.Web",
593+
"",
594+
"sites",
595+
"examplesitetf0406",
596+
"lockSite"
597+
)
598+
```
599+
600+
To delete a lock for a resource group, use:
601+
602+
```python
603+
import os
604+
from azure.identity import AzureCliCredential
605+
from azure.mgmt.resource import ManagementLockClient
606+
607+
credential = AzureCliCredential()
608+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
609+
610+
lock_client = ManagementLockClient(credential, subscription_id)
611+
612+
lock_client.management_locks.delete_at_resource_group_level("exampleGroup", "lockGroup")
613+
```
614+
468615
### REST API
469616

470617
You can lock deployed resources with the [REST API for management locks](/rest/api/resources/managementlocks). The REST API lets you create and delete locks and retrieve information about existing locks.

0 commit comments

Comments
 (0)