Last active
August 3, 2017 12:34
-
-
Save kevinhillinger/20f757aa6e1f05de5e77a476f3f12517 to your computer and use it in GitHub Desktop.
Azure SDK - Python - Delete Managed Data Disk with VM Lease check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from delete_datadisk import delete_data_disk | |
def main(): | |
os.environ['AZURE_SUBSCRIPTION_ID'] = "" | |
os.environ['AZURE_CLIENT_ID'] = "" | |
os.environ['AZURE_CLIENT_SECRET'] = "" | |
os.environ['AZURE_TENANT_ID'] = "" | |
resource_group = 'managed-disks' | |
disk_name = 'disk0' | |
delete_data_disk(resource_group, disk_name) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script expects that the following environment vars are set: | |
AZURE_TENANT_ID: your Azure Active Directory tenant id or domain | |
AZURE_CLIENT_ID: your Azure Active Directory Application Client ID | |
AZURE_CLIENT_SECRET: your Azure Active Directory Application Secret | |
AZURE_SUBSCRIPTION_ID: your Azure Subscription Id | |
""" | |
import os | |
import re | |
import traceback | |
from azure.common.credentials import ServicePrincipalCredentials | |
from azure.mgmt.resource import ResourceManagementClient | |
from azure.mgmt.compute import ComputeManagementClient | |
from azure.mgmt.compute.models import DiskCreateOption | |
from msrestazure.azure_exceptions import CloudError | |
from haikunator import Haikunator | |
def get_credentials(): | |
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID'] | |
credentials = ServicePrincipalCredentials( | |
client_id=os.environ['AZURE_CLIENT_ID'], | |
secret=os.environ['AZURE_CLIENT_SECRET'], | |
tenant=os.environ['AZURE_TENANT_ID'] | |
) | |
return credentials, subscription_id | |
def get_resource_group_name(resource_id): | |
return re.search('(?<=resourceGroups/)(\S+?)/', vm_resource.id).group(1) | |
def delete_data_disk(resource_group_name, data_disk_name): | |
credentials, subscription_id = get_credentials() | |
resource_client = ResourceManagementClient(credentials, subscription_id) | |
compute_client = ComputeManagementClient(credentials, subscription_id) | |
# get the data disk and check if it has a lease. | |
target_disk = compute_client.disks.get(resource_group_name, data_disk_name) | |
vmid = target_disk.managed_by | |
if vmid: #has a lease | |
vm_resource = resource_client.resources.get_by_id(vmid, '2017-03-30') | |
vm_name = vm_resource.name | |
vm_resource_group_name = get_resource_group_name(vm_resource.id) | |
virtual_machine = compute_client.virtual_machines.get(vm_resource_group_name, vm_name) | |
# detach data disk | |
data_disks = virtual_machine.storage_profile.data_disks | |
data_disks[:] = [disk for disk in data_disks if disk.name != target_disk.name] | |
async_vm_update = compute_client.virtual_machines.create_or_update( | |
vm_resource_group_name, | |
vm_name, | |
virtual_machine | |
) | |
virtual_machine = async_vm_update.result() | |
# delete disk | |
async_deletion = compute_client.disks.delete(resource_group_name, data_disk_name) | |
deletion_resource = async_deletion.result() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment