def end_reservation(session: CloudShellAPISession, reservation_id: str) -> None:
    """ End and delete reservation. """
    try:
        session.EndReservation(reservation_id)
        while session.GetReservationDetails(reservation_id).ReservationDescription.Status != 'Completed':
            time.sleep(1)
        session.DeleteReservation(reservation_id)
    except Exception as _:
        pass
Example #2
0

if __name__ == "__main__":
    TARGET_SERVICE_NAME = "IxNetwork Controller"

    server = credentials["server"]
    user = credentials["username"]
    password = credentials["password"]
    domain = credentials["domain"]

    # automation api session for getting sandbox details
    auto_api = CloudShellAPISession(host=server, username=user, password=password, domain=domain)

    # sandbox REST api for getting list of historical sandboxes
    sb_rest = SandboxRest(server=server, username=user, password=password, domain=domain)

    all_sandboxes = sb_rest.get_sandboxes(show_historic=True)
    print("total sandboxes in system: {}".format(str(len(all_sandboxes))))
    print("searching for target sandboxes...")

    filtered_sandboxes = [sb for sb in all_sandboxes
                          if is_service_in_sandbox(auto_api, sb["id"], TARGET_SERVICE_NAME)]

    if not filtered_sandboxes:
        print("NO SANDBOXES FOUND WITH TARGET SERVICE '{}'".format(TARGET_SERVICE_NAME))
    else:
        print("Service found in sandboxes, deleting...")
        for index, sandbox in enumerate(filtered_sandboxes):
            print("{}. {}".format(str(index + 1), sandbox["name"]))
            auto_api.DeleteReservation(reservationId=sandbox["id"])
                           password=password,
                           domain=domain)

# FIND SANDBOXES
all_sandboxes = api.GetScheduledReservations(
    fromTime="01/01/2020 00:00", untilTime="04/02/2021 00:00").Reservations

print("starting operation for {} sandboxes...".format(len(all_sandboxes)))
start = default_timer()
for index, curr_sb in enumerate(all_sandboxes):
    print("checking index: {}".format(index))
    sb_id = curr_sb.Id
    try:
        details = api.GetReservationDetails(sb_id).ReservationDescription
    except Exception as e:
        print("issue getting details for {}".format(sb_id))
        continue

    sb_services = details.Services
    sb_service_names = [service.ServiceName for service in sb_services]
    for sb_service_name in sb_service_names:
        if TARGET_SERVICE_MODEL == sb_service_name:
            try:
                print("deleting: {}".format(sb_id))
                api.DeleteReservation(sb_id)
            except Exception as e:
                print("issue deleting {}".format(sb_id))
                # raise

print("Total time: {}".format(default_timer() - start))