def set_maintenance_mode(server_name, session_id, maint_mode): is_enabled = maint_mode["isEnabled"] new_is_enabled = not is_enabled pline = "Set maintenance mode from " + str(is_enabled) + " to " + str( new_is_enabled) + "? (y/n) " line = raw_input(pline) if (line != "y"): return # Get now and 6 hours from now. This is what the GUI sets. now = datetime.datetime.now() add_6 = now + datetime.timedelta(hours=6) time_zone = my_timezone() now_str = now.isoformat() + time_zone add_6_str = add_6.isoformat() + time_zone print_debug("Start time: " + now_str) print_debug("End time: " + add_6_str) if (new_is_enabled): # Create the maintenance mode DTO for enabling. new_maint_mode_info = \ {"typeId": "com.tintri.api.rest.v310.dto.domain.beans.hardware.ApplianceMaintenanceMode", "endTime" : add_6_str, "isEnabled" : new_is_enabled, "startTime" : now_str } else: # Create the maintenance mode DTO for disabling. new_maint_mode_info = \ {"typeId": "com.tintri.api.rest.v310.dto.domain.beans.hardware.ApplianceMaintenanceMode", "isEnabled" : new_is_enabled, } # Create the Appliance object wit the new ApplianceDns DTO. new_appliance = \ {"typeId": "com.tintri.api.rest.v310.dto.domain.Appliance", "maintenanceMode": new_maint_mode_info } # Create the Request object with the Appliance DTO. Request = \ {"typeId": "com.tintri.api.rest.v310.dto.Request", "objectsWithNewValues": [new_appliance], "propertiesToBeUpdated": ["maintenanceMode"] } print_debug("Request:\n" + str(Request)) # Invoke the appliance API to set the maintenance mode. url = APPLIANCE_URL r = tintri.api_put(server_name, url, Request, session_id) print_debug("The JSON response of the put invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 204 then raise exception if r.status_code != 204: message = "The HTTP response for put call to the server is not 204." raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text)
def update_data_ip(server_name, session_id, new_ip_configs): # Create the Appliance object with the new configIps DTO. new_appliance = \ {'typeId': 'com.tintri.api.rest.v310.dto.domain.Appliance', 'configIps': new_ip_configs } # Create the Request object with the Appliance DTO. Request = \ {'typeId': 'com.tintri.api.rest.v310.dto.Request', 'objectsWithNewValues': new_appliance, 'propertiesToBeUpdated': ['configIps'] } # Update the VMstore wit the new data IP configuration. url = APPLIANCE_URL r = tintri.api_put(server_name, url, Request, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 204 then raise exception if r.status_code != 204: message = "The HTTP response for put call to the server is not 204." raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text)
def take_snapshot(vm_uuid, snapshot_name, consistency_type, server_name, session_id): snapshot_spec = { 'typeId': "com.tintri.api.rest.v310.dto.domain.beans.snapshot.SnapshotSpec", 'consistency': consistency_type, 'retentionMinutes': 240, # 4 hours 'snapshotName': snapshot_name, 'sourceVmTintriUUID': vm_uuid } # The API needs a list of snapshot specifications. snapshot_specs = [snapshot_spec] ss_url = "/v310/snapshot" r = tintri.api_post(server_name, ss_url, snapshot_specs, session_id) if (r.status_code != 200): msg = "The HTTP response for the post invoke to the server is " + \ server_name + "not 200, but is: " + str(r.status_code) + "." raise tintri.TintriApiException(msg, r.status_code, vm_url, str(snapshot_specs), r.text) print_debug("The JSON response of the post invoke to the server " + server_name + " is: " + r.text) # The result is a liset of snapshot UUIDs. snapshot_result = r.json() print_info(snapshot_name + ": " + snapshot_result[0]) return
def get_ip_configs(server_name, session_id): url = APPLIANCE_URL + "/ips" try: # Make the get call r = tintri.api_get(server_name, url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) except tintri.TintriRequestsException as tre: message = "HTTP error for the get IP addresses invoke to the server." raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text) except tintri.TintriApiException as tae: message = "The HTTP response for the get IP addresses invoke to the server is not 200." raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text) ip_configs = r.json() return ip_configs
def process_vmstore(vmstore_name, user_name, password, new_dns_primary): server_name = vmstore_name # Get the server type r = tintri.api_version(server_name) json_info = r.json() if json_info['productName'] != "Tintri VMstore": this_error = "Server needs to be a VMstore" return this_error session_id = tintri.api_login(server_name, user_name, password) dns_info = get_dns_info(server_name, session_id) print_dns_info(dns_info, server_name + " current: ") new_dns_secondary = dns_info['dnsSecondary'] # Create the ApplianceDns DTO. new_dns_info = \ {'typeId': 'com.tintri.api.rest.v310.dto.domain.beans.hardware.ApplianceDns', 'dnsPrimary': new_dns_primary, 'dnsSecondary': new_dns_secondary } # Create the Appliance object wit the new ApplianceDns DTO. new_appliance = \ {'typeId': 'com.tintri.api.rest.v310.dto.domain.Appliance', 'dnsConfig': new_dns_info } # Create the Request object with the Appliance DTO. Request = \ {'typeId': 'com.tintri.api.rest.v310.dto.Request', 'objectsWithNewValues': new_appliance, 'propertiesToBeUpdated': ['dnsConfig'] } url = APPLIANCE_URL r = tintri.api_put(server_name, url, Request, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 204 then raise exception if r.status_code != 204: tintri.api_logout(server_name, session_id) message = "The HTTP response for put call to the server is not 204." raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text) dns_info = get_dns_info(server_name, session_id) print_dns_info(dns_info, server_name + " now: ") # All pau, log out tintri.api_logout(server_name, session_id)
def execute_reco(server, tgc_sess_id, pool): reco_url = "/v310/vmstorePool/" + pool.get_uuid() + "/recommendation/" + \ pool.get_reco_uuid() + "/accept" r = tintri.api_post(server, reco_url, None, tgc_sess_id) print_debug("The JSON response of the accept reco invoke to the server " + server + " is: " + r.text) if (r.status_code != 204): msg = "The HTTP response for the accept reco post invoke to the server is " + \ server + "not 200, but is: " + str(r.status_code) + "." raise tintri.TintriApiException(msg, r.status_code, reco_url, "No payload", r.text)
def set_vm_affinity(server_name, session_id, vm_uuids, affinity_rule): url = "/v310/vm/" for vm_uuid in vm_uuids: rule_url = url + vm_uuid + "/affinity" r = tintri.api_put(server_name, rule_url, affinity_rule, session_id) if r.status_code != 204: tintri.api_logout(server_name, session_id) message = "The HTTP response for put affinity rule to the server is not 204." raise tintri.TintriApiException(message, r.status_code, rule_url, str(affinity_rule), r.text) sys.stdout.write(".") print("")
def get_maintenance_mode(server_name, session_id): url = APPLIANCE_URL + "/maintenanceMode" # Make the get call r = tintri.api_get(server_name, url, 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: message = "The HTTP response for the get invoke to the server is not 200." raise tintri.TintriApiException(message, r.status_code, url, "no request", r.text) maintenance_mode = r.json() return maintenance_mode
def get_dns_info(server_name, session_id): url = APPLIANCE_URL + "/dns" # Make the get call r = tintri.api_get(server_name, url, 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: message = "The HTTP response for the get invoke to the server is not 200." tintri.api_logout(server_name, session_id) raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text) appliance_dns = r.json() return appliance_dns
def take_snapshot(vm_uuid, snapshot_name, consistency_type, snapshot_retention_minutes, server_name, session_id): snapshot_spec = { 'typeId': "com.tintri.api.rest.v310.dto.domain.beans.snapshot.SnapshotSpec", 'consistency': consistency_type, 'retentionMinutes': snapshot_retention_minutes, 'snapshotName': snapshot_name, 'sourceVmTintriUUID': vm_uuid } # The API needs a list of snapshot specifications. snapshot_specs = [snapshot_spec] ss_url = "/v310/snapshot" r = tintri.api_post(server_name, ss_url, snapshot_specs, session_id) if (r.status_code != 200): msg = "The HTTP response for the post invoke to the server is " + \ server_name + "not 200, but is: " + str(r.status_code) + "." raise tintri.TintriApiException(msg, r.status_code, ss_url, str(snapshot_specs), r.text) print_debug("The JSON response of the post invoke to the server " + server_name + " is: " + r.text) # The result is a liset of snapshot UUIDs. snapshot_result = r.json() print_info(snapshot_name + ": " + snapshot_result[0]) if snapshot_retention_minutes > -1: now = datetime.datetime.now() expiration = now + datetime.timedelta( minutes=snapshot_retention_minutes) print_info(snapshot_name + " will expire " + expiration.ctime()) else: print_info(snapshot_name + " will be kept until manually deleted") return
url = "/v310/vm/vmListDownloadable" # Create the report filter. report_filter = {"typeId" : BEANS_VM + "VirtualMachineDownloadableReportFilter", "attachment" : csv_file_name, "attributes" : attributes, "since" : "", "until" : "", "format" : "CSV" } # Invoke API to get a useable report URL. r = tintri.api_post(server_name, url, report_filter, session_id) if r.status_code != 200: message = "The HTTP response for report call to the server is not 200." raise tintri.TintriApiException(message, r.status_code, url, report_filter, r.text) report_url = r.text # Print the URL. print("URL: {" + report_url + "} is good for 30 days") # Get the report. tintri.download_file(server_name, report_url, session_id, csv_file_name) print(csv_file_name + " is ready") tintri.api_logout(server_name, session_id) except tintri.TintriRequestsException as tre:
# 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) # Get the time 30 minutes ago in UTC. now = datetime.datetime.utcnow() minus_30 = now - datetime.timedelta(minutes=30) # Add UTC time zone and print. now_str = now.isoformat() + "-00:00" minus_30_str = minus_30.isoformat() + "-00:00"
vm_count = 0 for vm in vms_from_file: if (not (vm in vms)): print_info(vm + " not in TGC VM list.") continue members_to_add['objectIdsAdded'] = vms[vm] # Add VM to service group r = tintri.api_put(server_name, add_members_url, members_to_add, session_id) if (r.status_code != 204): msg = "The HTTP response for the put invoke to the server " + \ server_name + " is not 204, but is: " + str(r.status_code) + "." raise tintri.TintriApiException(msg, r.status_code, add_members_url, str(members_to_add), r.text) print("Added vm: " + vm) vm_count += 1 print( str(vm_count) + " VMs added to service group '" + service_group + "'.") except tintri.TintriRequestsException as tre: print_error(tre.__str__()) tintri.api_logout(server_name, session_id) exit(-5) except tintri.TintriApiException as tae: print_error(tae.__str__()) tintri.api_logout(server_name, session_id) exit(-6)