def is_connector_in_blueprint(api, blueprint_name, target_res_names):
    """
    search current blueprint resources for presence of the Target Resource
    :param CloudShellAPISession api:
    :param str blueprint_name:
    :param [str] target_res_names:
    :return:
    """
    details = api.GetTopologyDetails(blueprint_name)
    connectors = details.Connectors
    if connectors:
        return True
    else:
        return False


all_blueprints = api.GetTopologiesByCategory().Topologies
target_blueprints = [bp_name for bp_name in all_blueprints
                     if is_connector_in_blueprint(api, bp_name, TARGET_RESOURCES)]

print("=== Target Blueprints containing connectors ===")
print(target_blueprints)

for bp in target_blueprints:
    api.DeleteTopology(topologyFullPath=bp)

# add blueprints to text file
# with open('target_blueprints.txt', 'w') as f:
#     for bp in target_blueprints:
#         print >> f, bp
from cloudshell.api.cloudshell_api import CloudShellAPISession

user = "******"
password = "******"
server = "localhost"

api = CloudShellAPISession(host=server,
                           username=user,
                           password=password,
                           domain="Global")

keeper_categories = [
    "Swisscom", "SQC", "Mobile Center Demo", "vodaphone bracknell"
]
keepers = []

for category in keeper_categories:
    bps = api.GetTopologiesByCategory(category).Topologies
    keepers.extend(bps)

all_bps = api.GetTopologiesByCategory().Topologies

keepers_set = set(keepers)
all_bps_set = set(all_bps)

throw_aways = list(all_bps_set - keepers_set)

for bp in throw_aways:
    api.DeleteTopology(bp)
from os import environ as parameter
import json

connectivity = json.loads(parameter["QUALICONNECTIVITYCONTEXT"])
reservationDetails = json.loads(parameter["RESERVATIONCONTEXT"])
resourceDetails = json.loads(parameter["RESOURCECONTEXT"])

approved_blueprints_file = open(parameter["input_file"], 'r')
domains_to_scan = parameter["domains"].split(',')
dry_run = 'false' not in parameter["dry_run"].lower()

api = CloudShellAPISession(host=connectivity["serverAddress"],
                           token_id=connectivity["adminAuthToken"],
                           domain=reservationDetails["domain"])
approved_blueprints = [(line.split('\t')[0].strip(),
                        line.split('\t')[1].strip())
                       for line in approved_blueprints_file.readlines()]
for domain in domains_to_scan:
    domain_details = api.GetDomainDetails(domain)
    blueprints_in_domain = domain_details.Topologies
    blueprints_to_delete = [
        blueprint.Name for blueprint in blueprints_in_domain
        if (domain, blueprint.Name) not in approved_blueprints
    ]
    for blueprint in blueprints_to_delete:
        if not dry_run:
            api.DeleteTopology(domain_details.TopologiesFolder + '\\' +
                               blueprint)
        print("Deleted Blueprint {}".format(domain_details.TopologiesFolder +
                                            '\\' + blueprint))