def get_vms_in_list(server_name, session_id, vms): vm_uuids = [] vm_name_to_uuid = {} # Get a list of VMs, but return a page size at a time get_vm_url = "/v310/vm" vm_paginated_result = {'next': "offset=0&limit=" + str(page_size)} vm_filter = {"includeFields": ["uuid", "vmware"]} # While there are more VMs, go get them, and build a dictionary # of name to UUID. while 'next' in vm_paginated_result: url = get_vm_url + "?" + vm_paginated_result['next'] r = tintri.api_get_query(server_name, url, vm_filter, session_id) print_debug("The JSON response of the VM get invoke to the server " + server_name + " is: " + r.text) # For each VM in the page, print the VM name and UUID. vm_paginated_result = r.json() print_debug("VMs:\n" + format_json(vm_paginated_result)) # Build a dictionary items = vm_paginated_result["items"] for vm in items: vm_name_to_uuid[vm["vmware"]["name"]] = vm["uuid"]["uuid"] for vm in vms: if vm in vm_name_to_uuid: vm_uuids.append(vm_name_to_uuid[vm]) else: print_info("VM, " + vm + ", is unknown to TGC, " + server_name) return vm_uuids
def get_vms(server_name, session_id, vm_filter): vm_uuids = [] # Get a list of VMs, but return a page size at a time get_vm_url = "/v310/vm" vm_paginated_result = {'next': "offset=0&limit=" + str(page_size)} # While there are more VMs, go get them, and build a dictionary # of name to UUID. while 'next' in vm_paginated_result: url = get_vm_url + "?" + vm_paginated_result['next'] r = tintri.api_get_query(server_name, url, vm_filter, session_id) print_debug("The JSON response of the VM get invoke to the server " + server_name + " is: " + r.text) # For each VM in the page, print the VM name and UUID. vm_paginated_result = r.json() print_debug("VMs:\n" + format_json(vm_paginated_result)) # Get the VMs items = vm_paginated_result["items"] for vm in items: vm_uuids.append(vm["uuid"]["uuid"]) return vm_uuids
def get_sg_members(server_name, session_id, sg_uuid): sg_members = [] # Create filter to obtain live VMs and VMs that belong in specified # the service group. sg_filter = {'live': 'TRUE', 'serviceGroupIds': sg_uuid} url = "/v310/vm" r = tintri.api_get_query(server_name, url, sg_filter, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 200 then raise an error if r.status_code != 200: print_error("The HTTP response for the get invoke to the server " + server_name + " is not 200, but is: " + str(r.status_code)) print_error("url = " + url) print_error("response: " + r.text) tintri.api_logout(server_name, session_id) sys.exit(-10) member_paginated_result = r.json() num_members = int(member_paginated_result["filteredTotal"]) if num_members == 0: print_debug("No Service Groups members present") return sg_members print_debug(str(num_members) + " Service Group Members present") # For each live VM, create a VM info object items = member_paginated_result["items"] for vm in items: if not vm["isLive"]: continue member_vm = vm["vmware"]["name"] member_vmstore = vm["vmstoreName"] member_vm_uuid = vm["uuid"]["uuid"] print_debug(" " + member_vm + " (" + member_vmstore + ")") vm_info = VmInfo(member_vm, member_vm_uuid, member_vmstore) sg_members.append(vm_info) return sg_members
print_info("API Version: " + json_info['preferredVersion']) # Login to VMstore session_id = tintri.api_login(server_name, user_name, password) # Create filter to get the oldest user generated snapshot q_filter = { 'queryType': 'TOP_DOCS_BY_TIME', 'limit': '1', 'type': 'USER_GENERATED_SNAPSHOT' } # Get the oldest user generated snapshot url = "/v310/snapshot" r = tintri.api_get_query(server_name, url, q_filter, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 200 then raise an exception if r.status_code != 200: print_error("The HTTP response for the get invoke to the server " + server_name + " is not 200, but is: " + str(r.status_code)) print_error("url = " + url) print_error("response: " + r.text) tintri.api_logout(server_name, session_id) sys.exit(-10) snapshot_result = r.json() number_of_snapshots = int(snapshot_result["filteredTotal"]) print_debug(
print_error(tre.__str__()) sys.exit(-10) except tintri.TintriApiException as tae: print_error(tae.__str__()) sys.exit(-11) try: # Get a VM to work with vm_filter = { 'limit': 1, 'sortedBy': 'LATENCY', 'sortOrder': 'DESC', 'live': "TRUE" } url = "/v310/vm" r = tintri.api_get_query(server_name, url, vm_filter, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) if (r.status_code != 200): msg = "The HTTP response for the get invoke to the server is " + \ server_name + "not 200, but is: " + str(r.status_code) + "." raise tintri.TintriApiException(msg, r.status_code, url, str(vm_filter), r.text) vm_paginated_result = r.json() items = vm_paginated_result['items'] vm = items[0] vm_uuid = vm['uuid']['uuid'] vm_name = vm['vmware']['name'] print_info("VM: " + vm_name + " : " + vm_uuid)
def tintri_snapshot(args, vm): try: # Confirm the consistency type. if (args.tvmstore_consistency_type == "crash"): consistency_type = "CRASH_CONSISTENT" elif (args.tvmstore_consistency_type == "vm"): consistency_type = "VM_CONSISTENT" else: raise tintri.TintriRequestsException( "tvmstore_consistency_type is not 'crash' or 'vm': " + args.tvmstore_consistency_type) # Get the preferred version r = tintri.api_version(args.tvmstore) json_info = r.json() print_info("Tintri API Version: " + json_info['preferredVersion']) # Login to VMstore or TGC session_id = tintri.api_login(args.tvmstore, args.tvmstore_user, args.tvmstore_password) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) sys.exit(-10) except tintri.TintriApiException as tae: print_error(tae.__str__()) sys.exit(-11) try: # Create query filter to get the VM specified by the VM name. q_filter = {'name': vm.name} # Get the UUID of the specified VM vm_url = "/v310/vm" r = tintri.api_get_query(args.tvmstore, vm_url, q_filter, session_id) print_debug("The JSON response of the get invoke to the server " + args.tvmstore + " is: " + r.text) vm_paginated_result = r.json() num_vms = int(vm_paginated_result["filteredTotal"]) if num_vms == 0: raise tintri.TintriRequestsException("VM " + vm.name + " doesn't exist") # Get the information from the first item and hopefully the only item. items = vm_paginated_result["items"] vm = items[0] vm_name = vm["vmware"]["name"] vm_uuid = vm["uuid"]["uuid"] print_info(vm_name + " UUID: " + vm_uuid) # Get the time for the snapshot description. now = datetime.datetime.now() now_sec = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second) snapshot_name = args.tvmstore_snapshot_name # Take a manual snapshot. take_snapshot(vm_uuid, snapshot_name, consistency_type, args.tvmstore_snapshot_lifetime, args.tvmstore, session_id) # All pau, log out. tintri.api_logout(args.tvmstore, session_id) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) tintri.api_logout(args.tvmstore, session_id) sys.exit(-20) except tintri.TintriApiException as tae: print_error(tae.__str__()) tintri.api_logout(args.tvmstore, session_id) sys.exit(-21)