简体   繁体   中英

How to list locks applied on azure resource group using python

Using python azure sdk,I am applying some locks to resource group and which are getting inherited to individual resources in it. And also, I am applying individual locks to resources inside the group. There are some more locks I am applying only to resources linked to a VM when I pass vm name as parameter. Now, is there anyway list down at the end script what are the types of locks applied by our script? By checking previous and current state or whatever logic applicable. Not able to get the this information from azure sdk documentation.

The SDK lets you list all the management locks for a resource group with the list_at_resource_group_level method.

from azure.common.client_factory import get_client_from_auth_file
from azure.mgmt.resource import ResourceManagementClient, ManagementLockClient

# get_client_from_auth_file relies on the AZURE_AUTH_LOCATION environment variable
resource_client = get_client_from_auth_file(ResourceManagementClient)
management_lock_client = get_client_from_auth_file(ManagementLockClient)
    
def list_resource_group_locks():
    # Can optionally iterate across all RGs in the Subscription
    for rg in resource_client.resource_groups.list():
        for lock in management_lock_client.management_locks.list_at_resource_group_level(rg.name):
            print(f"\nResource Group: {rg.name}\nLock name: {lock.name}\nLock Type: {lock.level}")

# List all resource groups in the Subscription with applied locks
list_resource_group_locks()

Output:
Resource Group: rg-1
Lock name: testupdate
Lock Type: ReadOnly

Resource Group: rg-1
Lock name: testdelete
Lock Type: CanNotDelete

Other variants/methods of the ManagementLocksOperations class can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM