|
| 1 | +""" |
| 2 | +Find all apps in "Deployed Apps" Folder and delete resource if not currently in Reservation |
| 3 | +This script currently supports V-CENTER cloud provider |
| 4 | +Script must have direct connectivity to: |
| 5 | +- Quali Application Server |
| 6 | +- vCenter Server |
| 7 | +""" |
| 8 | +from cloudshell.api.cloudshell_api import CloudShellAPISession |
| 9 | +import time |
| 10 | +import os |
| 11 | + |
| 12 | +user = "admin" |
| 13 | +password = "admin" |
| 14 | +server = "localhost" |
| 15 | + |
| 16 | +def ping_vcenter_server(server_ip): |
| 17 | + pass |
| 18 | + |
| 19 | +api = CloudShellAPISession(host=server, username=user, password=password, domain="Global") |
| 20 | +cloud_provider_details = api.GetResourceDetails(resourceFullPath="my vCenter") |
| 21 | +all_app_resources = api.FindResources(resourceFamily="Generic App Family").Resources |
| 22 | +deployed_app_folder_resources = [resource for resource in all_app_resources |
| 23 | + if "Deployed Apps" in resource.FullPath] |
| 24 | +if not deployed_app_folder_resources: |
| 25 | + print("No deployed app resources. Ending Script.") |
| 26 | + exit(0) |
| 27 | + |
| 28 | +un_reserved_deployed_apps = [resource for resource in deployed_app_folder_resources |
| 29 | + if resource.ReservedStatus == "Not In Reservations"] |
| 30 | + |
| 31 | +if not un_reserved_deployed_apps: |
| 32 | + print("No UNRESERVED deployed app resources. Ending Script.") |
| 33 | + exit(0) |
| 34 | + |
| 35 | +failed_deletions = [] |
| 36 | +successful_deletions = [] |
| 37 | +for resource in un_reserved_deployed_apps: |
| 38 | + try: |
| 39 | + print("Deleting deployed app resource: '{}'...".format(resource.Name)) |
| 40 | + response = api.DeleteResource(resourceFullPath=resource.Name) |
| 41 | + pass |
| 42 | + except Exception as e: |
| 43 | + print("FAILED deletion '{}': Exception: {}".format(resource.Name, str(e))) |
| 44 | + print("=============") |
| 45 | + failed_deletions.append(resource.Name) |
| 46 | + else: |
| 47 | + if 'Success="true"' in response: |
| 48 | + print("SUCCESSFUL deletion: '{}'".format(resource.Name)) |
| 49 | + print("=============") |
| 50 | + successful_deletions.append(resource.Name) |
| 51 | + else: |
| 52 | + print("FAILED deletion '{}'. Response: {}".format(resource.Name, response)) |
| 53 | + print("=============") |
| 54 | + failed_deletions.append(resource.Name) |
| 55 | + |
| 56 | +if failed_deletions: |
| 57 | + if successful_deletions: |
| 58 | + print("Following apps deleted SUCCESSFULLY: {}".format(successful_deletions)) |
| 59 | + print("Following app resource deletion FAILED: {}".format(failed_deletions)) |
| 60 | + print("=============") |
| 61 | + time.sleep(1) # just so exception appears after print statements |
| 62 | + raise Exception("There were failed app resource deletions in this operation.") |
| 63 | + |
| 64 | +print("ALL Un-reserved deployed apps deleted SUCCESSFULLY: {}".format(successful_deletions)) |
| 65 | +print("=============") |
| 66 | + |
0 commit comments