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
def f(jj): k = randint(0, 300) log('%d: Sleeping %d seconds' % (jj, k)) sleep(k) while True: t0 = time() fail = 'ok' api = CloudShellAPISession('localhost', 'admin', 'admin', 'Global') try: id = api.CreateImmediateTopologyReservation( reservationName='vmx%d' % jj, topologyFullPath=toponame, durationInMinutes=120, owner='admin').Reservation.Id for _ in range(100): rd = api.GetReservationDetails(id).ReservationDescription log('%d: %s Status=%s SetupStage=%s ProvisioningStatus=%s' % (jj, id, rd.Status, rd.SetupStage, rd.ProvisioningStatus)) if rd.ProvisioningStatus == 'Error': fail = 'setup error' break if rd.SetupStage == 'Ended': break sleep(10) else: log('%d: Setup never finished, ending reservation %s' % (jj, id)) fail = 'setup timeout' api.EndReservation(id) for _ in range(100): rd = api.GetReservationDetails(id).ReservationDescription if rd.Status == 'Completed': log('%d: %s Status=%s' % (jj, id, rd.Status)) break sleep(10) else: log('%d: Teardown never finished %s' % (jj, id)) fail = 'teardown fail' log('%d: Cycle (%s) finished in %d seconds' % (jj, str(fail), time() - t0)) except Exception as e: log('Exception: %s' % str(e)) sleep(randint(1, 30))
def main(): """ Example workflow of starting a sandbox, waiting for it to setup, then stopping it :return: """ session = CloudShellAPISession('localhost', "admin", "admin", "Global") # Create the sandbox sandbox = session.CreateImmediateTopologyReservation( 'test sandbox', owner='admin', durationInMinutes=120, topologyFullPath='Simple Blueprint1', globalInputs=[ UpdateTopologyGlobalInputsRequest('Target Cloud', 'AWS') ]).Reservation sandbox_details = SandboxHelpers().wait_for_sandbox_setup( sandbox.Id, session, 10) run_some_tests_or_other_code(session, sandbox_details) session.EndReservation(sandbox.Id)
from time import sleep from cloudshell.api.cloudshell_api import CloudShellAPISession import xmltodict user = "******" password = "******" server = "localhost" domain = "Global" SANDDBOX_ID = "37170584-46a5-4634-91aa-54c50e13b552" api = CloudShellAPISession(host=server, username=user, password=password, domain=domain) x = api.EndReservation(reservationId=SANDDBOX_ID) result = xmltodict.parse(x)["Response"]["@Success"] if result == "true": print("passed") pass
login_result = requests.put(api_root_url + "/Auth/Login", {"token": connectivityContext["adminAuthToken"], "domain": "Master"}) master_domain_authcode = "Basic " + login_result.content[1:-1] export_result = requests.post(api_root_url + "/Package/ExportPackage", {"TopologyNames": blueprints_to_export}, headers={"Authorization": master_domain_authcode}) login_result = requests.put(api_root_url + "/Auth/Login", {"token": connectivityContext["adminAuthToken"], "domain": domain_name}) new_domain_authcode = "Basic " + login_result.content[1:-1] import_result = requests.post(api_root_url + "/Package/ImportPackage", headers={"Authorization": new_domain_authcode}, files={'QualiPackage': export_result.content}) topologies_in_new_domain = [blueprint.Name for blueprint in api.GetDomainDetails(domain_name).Topologies] api.RemoveTopologiesFromDomain(domain_name, ["Master topologies/Vanilla Operating Systems"]) if topologies_in_new_domain != blueprints_to_export: email_title = "CloudShell Trial: Failed to setup trial" email_body = "Failed to setup trail for {user} because there was an error during import of blueprints to the new domain".format(user=new_username) smtp_client.send_email(",".join([owner_email, admin_email]), email_title, email_body, False) api.WriteMessageToReservationOutput(reservationContext["id"], "Topologies not imported successfully, aborting trial creation") api.EndReservation(reservationContext["id"]) # Send Trial start notifications api.WriteMessageToReservationOutput(reservationContext["id"], "5. Creating or updating Hubspot contact") # Calculate reservation end time in miliseconds reservation_details = api.GetReservationDetails(reservationContext["id"]).ReservationDescription try: reservation_end_date = datetime.datetime.strptime(reservation_details.EndTime, "%d/%m/%Y %H:%M").replace(hour=0, minute=0, second=0, microsecond=0) except ValueError: reservation_end_date = datetime.datetime.strptime(reservation_details.EndTime, "%m/%d/%Y %H:%M").replace(hour=0, minute=0, second=0, microsecond=0) reservation_end_time_in_ms = int((reservation_end_date - datetime.datetime.utcfromtimestamp(0)).total_seconds() * 1000) # Create Hubspot contact and enroll to Hubspot Workflow
from cloudshell.api.cloudshell_api import CloudShellAPISession api = CloudShellAPISession('localhost', 'admin', 'admin', 'Global') for r in api.GetCurrentReservations('admin').Reservations: try: print r.Id api.EndReservation(r.Id) except: print 'Failed'
user = "******" password = "******" server = "localhost" domain = "Global" TERMINATE_TEARDOWN = True api = CloudShellAPISession(host=server, username=user, password=password, domain=domain) # Get all active sandboxes active_sandboxes = api.GetCurrentReservations().Reservations if not active_sandboxes: print("No currently active sandboxes") exit(0) print("===== ending reservations... =====") for sandbox in active_sandboxes: print(sandbox.Name) api.EndReservation(reservationId=sandbox.Id) if TERMINATE_TEARDOWN: print("===== terminating teardowns... =====") sleep(10) for sandbox in active_sandboxes: if "teardown" in sandbox.Status.lower(): print(sandbox.Name) api.TerminateReservation(sandbox.Id)