Search code examples
pythonopenstackopenstack-nova

OpenStack Python Nova API Getting Specific Limit Values?


I've been using the following code to get the limits:

compute_limits = novaClient.limits.get().absolute
for s in compute_limits:
    print s.name + " = " + str(s.value)

However I only want specific values from the limits.get(), namely totalRAMUsed and maxTotalRAMSize. There seems to be very little on the internet about using the Python API (mainly all about the CLI). Is there a way to get these specific values to avoid displaying all the limit values?


Solution

  • You can display only one specific value:

    compute_limits = novaClient.limits.get().absolute
    for s in compute_limits:
        if s.name == 'totalRAMUsed':
            print s.name + " = " + str(s.value)
            break
    

    compute_limits is generator, you can't receive only one specific value by limit name. But you can convert compute_limits to dict. For example:

    compute_limits = novaClient.limits.get().absolute
    l = list(compute_limits)
    limits = dict(map(lambda x: (x.name, x.value), l))
    print limits['totalRAMUsed']