Usage:
az resource list | python sort_for_deletion_parallel.py '^resources-to-delete.*' | while read CMD; do bash -xc "$CMD"; done
import json | |
import sys | |
import re | |
del_seq = [ | |
# 'Microsoft.Compute/virtualMachines/extensions', | |
'Microsoft.Compute/virtualMachines', | |
'Microsoft.Compute/virtualMachineScaleSets', | |
'Microsoft.Compute/availabilitySets', | |
'Microsoft.Network/loadBalancers', | |
'Microsoft.Network/networkInterfaces', | |
'Microsoft.Network/publicIPAddresses', | |
'Microsoft.Network/networkSecurityGroups', | |
'Microsoft.Network/virtualNetworks', | |
'Microsoft.Compute/disks', | |
] | |
if len(sys.argv) < 2: | |
sys.stderr.write("First positional argument must be filter regex") | |
sys.exit(1) | |
filter_regex = re.compile(sys.argv[1]) | |
all_res = json.load(sys.stdin) | |
parallel_del_list = [] | |
for res_type in del_seq: | |
parallel_del_list.append([_ for _ in all_res if _['type'] == res_type]) | |
for res_of_type in parallel_del_list: | |
parallel_del_cmd_list = [] | |
for res in res_of_type: | |
if not filter_regex.match(res['name']): | |
continue | |
del_cmd = "az resource delete --resource-group {0} --name {1} --resource-type {2}".format( | |
res['resourceGroup'], | |
res['name'], | |
res['type'] | |
) | |
parallel_del_cmd_list.append(del_cmd) | |
print " & ".join(parallel_del_cmd_list) |