2

I am using azure SDK python to lock all resources in a specific resource group. I am not able to find proper help/documentation for the same.

There are no methods related to this in the azure-mgmt-resource package

Can anyone suggest any?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
uday kiran
  • 63
  • 5

1 Answers1

0

You will need to instantiate a ManagementLockClient object, then call the create_or_update_at_resource_group_level method to create the CanNotDelete lock at resource group level.

We can apply the lock at resource group level, because all child resources inherit the same lock from the parent scope.

Demo

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

# Authenticate with Azure CLI credentials
client = get_client_from_cli_profile(ResourceManagementClient)

resource_group_name = "myResourceGroup"

# Ensure resource group exists
if client.resource_groups.check_existence(resource_group_name):

    # Create lock client to do lock operations
    lock_client = get_client_from_cli_profile(ManagementLockClient)

    # Add delete lock at resource group level
    lock_client.management_locks.create_or_update_at_resource_group_level(
        resource_group_name=resource_group_name,
        lock_name="DeleteLock",
        parameters={"level": "CanNotDelete"},
    )
else:
    print(
        f"Resource group {resource_group_name} does not exist in your subscription!"
    )

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • Thank you very much, I tried to fetch details from the documentation of ResourceManagementClient but at the methods places it is showing text like "2016-02-01: models", after clicking the model link, can't find any relevant data. Please suggest on how to use this documentation to get full details. – uday kiran Aug 16 '20 at 11:58
  • @udaykiran The documentation is not the most detailed. I use the [source code](https://github.com/Azure/azure-sdk-for-python/tree/36f2cea9770bb0f95c6981c159df8972ea2af604/sdk/resources/azure-mgmt-resource/azure/mgmt/resource/locks) when I need more detail. – RoadRunner Aug 16 '20 at 11:59
  • I tried to parse the repo but not able to get the parameters you provided. May be my approach is wrong, could you please show on which file you got these parameters. – uday kiran Aug 18 '20 at 13:38