Created
August 23, 2021 16:18
-
-
Save kenazk/3588319c0c2f2c37c24d39b20cd313d1 to your computer and use it in GitHub Desktop.
Filtering the VMs (and exiting 1 if empty)
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
#!/usr/bin/env python | |
# File: filter-vms.py | |
# Description: This is an example script that you can author or modify that retrieves | |
# a list of Azure Virtual Machines from the Relay Interface (in the form of parameters) | |
# and filters the VMs that have no tags. It then sets the output variable `resourceIDs` | |
# to the list of Azure Virtual Machines resource IDs that are untagged. | |
# Inputs: | |
# - virtualMachines - list of Azure Virtual Machines | |
# Outputs: | |
# - resourceIDs - list of Azure Virtual Machine resource IDs to be terminated in the subsequent step | |
from relay_sdk import Interface, Dynamic as D | |
relay = Interface() | |
to_terminate = [] | |
to_keep = [] | |
# Filtering Azure virtual machines with no tags | |
print('Looking for all Virtual Machines with no tags') | |
vms = relay.get(D.virtualMachines) # Queries for `virtual_machines` parameter from Relay | |
for vm in vms: | |
if 'tags' in vm.keys(): | |
to_keep.append(vm['id']) | |
continue | |
else: | |
try: | |
to_terminate.append(vm['id']) | |
except Exception as e: | |
print('\nAzure Virtual Machine {0} not considered for termination because of a processing error: {1}'.format(vm['name'], e)) | |
print('\nFound {} Virtual machines (with tags) not considered for termination:'.format(len(to_keep))) | |
print(*[vm_id for vm_id in to_keep], sep = "\n") | |
if len(to_terminate) == 0: | |
print('\nNo Virtual Machines to terminate! Exiting.') | |
exit(1) | |
else: | |
print('\nAdding {} Virtual machines (without tags) to terminate:'.format(len(to_terminate))) | |
print(*[vm_id for vm_id in to_terminate], sep = "\n") | |
print('\nSetting output `resourceIDs` to list of {0} virtual machine resource ids to terminate:'.format(len(to_terminate))) | |
relay.outputs.set('resourceIDs', to_terminate) | |
print(*[vm_id for vm_id in to_terminate], sep = "\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment