Skip to content

Commit 8673538

Browse files
authored
Merge pull request #233735 from tfitzmac/0406lock
add Python to locks
2 parents 4944368 + 1022b12 commit 8673538

File tree

1 file changed

+157
-1
lines changed

1 file changed

+157
-1
lines changed

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

Lines changed: 157 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,160 @@ 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+
import os
478+
from azure.identity import AzureCliCredential
479+
from azure.mgmt.resource import ManagementLockClient
480+
481+
credential = AzureCliCredential()
482+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
483+
484+
lock_client = ManagementLockClient(credential, subscription_id)
485+
486+
lock_result = lock_client.management_locks.create_or_update_at_resource_level(
487+
"exampleGroup",
488+
"Microsoft.Web",
489+
"",
490+
"sites",
491+
"examplesite",
492+
"lockSite",
493+
{
494+
"level": "CanNotDelete"
495+
}
496+
)
497+
```
498+
499+
To lock a resource group, provide the name of the resource group.
500+
501+
```python
502+
import os
503+
from azure.identity import AzureCliCredential
504+
from azure.mgmt.resource import ManagementLockClient
505+
506+
credential = AzureCliCredential()
507+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
508+
509+
lock_client = ManagementLockClient(credential, subscription_id)
510+
511+
lock_result = lock_client.management_locks.create_or_update_at_resource_group_level(
512+
"exampleGroup",
513+
"lockGroup",
514+
{
515+
"level": "CanNotDelete"
516+
}
517+
)
518+
```
519+
520+
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:
521+
522+
```python
523+
import os
524+
from azure.identity import AzureCliCredential
525+
from azure.mgmt.resource import ManagementLockClient
526+
527+
credential = AzureCliCredential()
528+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
529+
530+
lock_client = ManagementLockClient(credential, subscription_id)
531+
532+
lock_result = lock_client.management_locks.list_at_subscription_level()
533+
534+
for lock in lock_result:
535+
print(f"Lock name: {lock.name}")
536+
print(f"Lock level: {lock.level}")
537+
print(f"Lock notes: {lock.notes}")
538+
```
539+
540+
To get a lock for a resource, use:
541+
542+
```python
543+
import os
544+
from azure.identity import AzureCliCredential
545+
from azure.mgmt.resource import ManagementLockClient
546+
547+
credential = AzureCliCredential()
548+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
549+
550+
lock_client = ManagementLockClient(credential, subscription_id)
551+
552+
lock_result = lock_client.management_locks.get_at_resource_level(
553+
"exampleGroup",
554+
"Microsoft.Web",
555+
"",
556+
"sites",
557+
"examplesite",
558+
"lockSite"
559+
)
560+
561+
print(f"Lock ID: {lock_result.id}")
562+
print(f"Lock Name: {lock_result.name}")
563+
print(f"Lock Level: {lock_result.level}")
564+
```
565+
566+
To get a lock for a resource group, use:
567+
568+
```python
569+
import os
570+
from azure.identity import AzureCliCredential
571+
from azure.mgmt.resource import ManagementLockClient
572+
573+
credential = AzureCliCredential()
574+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
575+
576+
lock_client = ManagementLockClient(credential, subscription_id)
577+
578+
lock_result = lock_client.management_locks.get_at_resource_group_level(
579+
"exampleGroup",
580+
"lockGroup"
581+
)
582+
583+
print(f"Lock ID: {lock_result.id}")
584+
print(f"Lock Level: {lock_result.level}")
585+
```
586+
587+
To delete a lock for a resource, use:
588+
589+
```python
590+
import os
591+
from azure.identity import AzureCliCredential
592+
from azure.mgmt.resource import ManagementLockClient
593+
594+
credential = AzureCliCredential()
595+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
596+
597+
lock_client = ManagementLockClient(credential, subscription_id)
598+
599+
lock_client.management_locks.delete_at_resource_level(
600+
"exampleGroup",
601+
"Microsoft.Web",
602+
"",
603+
"sites",
604+
"examplesite",
605+
"lockSite"
606+
)
607+
```
608+
609+
To delete a lock for a resource group, use:
610+
611+
```python
612+
import os
613+
from azure.identity import AzureCliCredential
614+
from azure.mgmt.resource import ManagementLockClient
615+
616+
credential = AzureCliCredential()
617+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
618+
619+
lock_client = ManagementLockClient(credential, subscription_id)
620+
621+
lock_client.management_locks.delete_at_resource_group_level("exampleGroup", "lockGroup")
622+
```
623+
468624
### REST API
469625

470626
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)