def set_qos(server_name, user_name, password, vm_uuids, new_min_value, new_max_value):

    # Get the preferred version
    r = tintri.api_get(server_name, '/info')
    json_info = r.json()
    preferred_version = json_info['preferredVersion']
    
    # Verify the correct major and minor versions.
    versions = preferred_version.split(".")
    major_version = versions[0]
    minor_version = int(versions[1])
    if major_version != "v310":
        print_error("Incorrect major version: " + major_version + ".  Should be v310.")
        return "Error"
    if minor_version < 21:
        print_error("Incorrect minor Version: " + str(minor_version) + ".  Should be 21 or greater")
        return "Error"

    # Login into the appropriate VMstore
    session_id = tintri.api_login(server_name, user_name, password)
    if session_id is None:
        return "Error"
    print_info("Logged onto " + server_name)

    # Create new QoS object with the fields to be changed
    modify_qos_info = {'minNormalizedIops': int(new_min_value),
                       'maxNormalizedIops': int(new_max_value),
                       'typeId': 'com.tintri.api.rest.v310.dto.domain.beans.vm.VirtualMachineQoSConfig'
                      }
                      
    # Create the MultipleSelectionRequest object
    MS_Request = {'typeId': 'com.tintri.api.rest.v310.dto.MultipleSelectionRequest',
                  'ids': vm_uuids,
                  'newValue': modify_qos_info,
                  'propertyNames': ["minNormalizedIops", "maxNormalizedIops"]
                 }
    
    print_debug("Changing min and max QOS values to (" + str(new_min_value) + ", " + str(new_max_value) + ")")
    
    # Update the min and max QoS IOPs
    modify_qos_url = "/v310/vm/qosConfig"
    r = tintri.api_put(server_name, modify_qos_url, MS_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 an exception
    if r.status_code != 204:
        print_error("The HTTP response for the put invoke to the server " +
              server_name + " is not 204, but is: " + str(r.status_code))
        print_error("url = " + modify_qos_url)
        print_error("payload = " + str(MS_Request))
        print_error("response: " + r.text)
        tintri.api_logout(server_name, session_id)
        print_info("Error log off " + server_name)
        return "Error"

    tintri.api_logout(server_name, session_id)
    print_info("Sucesss log off " + server_name)
    return "OK"
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
            '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("Number of Snapshots fetched from get Snapshots call to the server " +
      server_name + " is : " + str(number_of_snapshots))

if number_of_snapshots == 0:
    print_error("Cannot proceed, since this are no user generated snapshots")
    tintri.api_logout(server_name, session_id)
    sys.exit(-11)

items = snapshot_result["items"]
snapshot = items[0]
예제 #4
0
def get_vms(session_id):

    page_size = 25  # default

    # dictionary of VM objects
    vms = {}

    # Get a list of VMs a page size at a time
    get_vm_url = "/v310/vm"
    count = 1
    vm_paginated_result = {'live' : "TRUE",
                           'next' : "offset=0&limit=" + str(page_size)}
    
    # While there are more VMs, go get them
    while 'next' in vm_paginated_result:
        url = get_vm_url + "?" + vm_paginated_result['next']

        # This is a work-around for a TGC bug.
        chop_i = url.find("&replicationHasIssue")
        if chop_i != -1:
            url = url[:chop_i]
            print_debug("Fixing URL")

        print_debug("Next GET VM URL: " + str(count) + ": " + url)
    
        # Invoke the API
        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:
            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)
    
        # For each VM in the page, print the VM name and UUID.
        vm_paginated_result = r.json()
    
        # Check for the first time through the loop and
        # print the total number of VMs.
        if count == 1:
            num_filtered_vms = vm_paginated_result["filteredTotal"]
            if num_filtered_vms == 0:
                print_error("No VMs present")
                tintri.api_logout(server_name, session_id)
                sys_exit(-99)
    
        # Get and store the VM items and save in a VM object.
        items = vm_paginated_result["items"]
        for vm in items:
            vm_name = vm["vmware"]["name"]
            vm_uuid = vm["uuid"]["uuid"]
            vm_stats = VmStat(vm_name, vm_uuid, vm["stat"]["sortedStats"][0])
            print_debug(str(count) + ": " + vm_name + ", " + vm_uuid)
            count += 1
			
			# Store the VM stats object keyed by VM name.
            vms[vm_name] = vm_stats

    return vms
# Get a VM to work with
p_filter = {
    'limit': 1,
    'sortedBy': 'LATENCY',
    'sortOrder': 'DESC',
    'live': "TRUE"
}
url = "/v310/vm"
r = tintri.api_get_query(server_name, url, p_filter, session_id)
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)

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 + " " + str(vm['isLive']))

# Create filter to get the oldest user generated snapshot
q_filter = {  #'queryType': 'TOP_DOCS_BY_LATEST_TIME',
    'since': '2015-03-11T15:00:00.915-08:00',
    'offset': 0,
    'limit': page_size
}