Ask Your Question
0

Get the details of a vm using api

asked 2015-03-06 05:04:21 -0500

sreejithp gravatar image

updated 2015-03-06 09:16:13 -0500

larsks gravatar image

Hello All,

I am learning openstack api programming, trying to get the flavor details of a running vm but i am not able to find any information's relating to that on python-novaclient api. The code is as follows

import novaclient.v1_1.client as nvclient

def get_nova_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['api_key'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['project_id'] = os.environ['OS_TENANT_NAME']
    return d

creds = get_nova_creds()
nova = nvclient.Client(**creds)
server_list = nova.servers.list()
print server_list

i need to get the size of the vm in the server_list. but i cannot find any function or attributes. But the same information is displayed in Horizon under instances. Can you please point to any article relating to it?

Thanks in advance Sreejith P

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-03-06 09:26:05 -0500

larsks gravatar image

Take your above code, and run it in the interactive interpreter.

Once you get here:

>>> server_list = nova.servers.list()

Extract the first server in the list:

>>> server = server_list[0]

And use python's dir function to see what methods and attributes are available to you:

>>> dir(server)
['HUMAN_ID', 'NAME_ATTR', 'OS-DCF:diskConfig',
'OS-EXT-AZ:availability_zone', 'OS-EXT-STS:power_state',
'OS-EXT-STS:task_state', 'OS-EXT-STS:vm_state',
'OS-SRV-USG:launched_at', 'OS-SRV-USG:terminated_at', '__class__',
'__delattr__', '__dict__', '__doc__', '__eq__', '__format__',
'__getattr__', '__getattribute__', '__hash__', '__init__',
'__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__', '_add_details', '_info',
'_init_completion_cache', '_loaded', 'accessIPv4', 'accessIPv6',
'add_fixed_ip', 'add_floating_ip', 'add_security_group',
'addresses', 'backup', 'change_password', 'clear_password',
'config_drive', 'confirm_resize', 'create_image', 'created',
'delete', 'diagnostics', 'evacuate', 'flavor', 'force_delete',
'get', 'get_console_output', 'get_password', 'get_rdp_console',
'get_serial_console', 'get_spice_console', 'get_vnc_console',
'hostId', 'human_id', 'id', 'image', 'interface_attach',
'interface_detach', 'interface_list', 'is_loaded', 'key_name',
'links', 'list_security_group', 'live_migrate', 'lock', 'manager',
'metadata', 'migrate', 'name', 'networks',
'os-extended-volumes:volumes_attached', 'pause', 'progress',
'reboot', 'rebuild', 'remove_fixed_ip', 'remove_floating_ip',
'remove_security_group', 'rescue', 'reset_network', 'reset_state',
'resize', 'restore', 'resume', 'revert_resize', 'security_groups',
'set_loaded', 'shelve', 'shelve_offload', 'start', 'status',
'stop', 'suspend', 'tenant_id', 'to_dict', 'unlock', 'unpause',
'unrescue', 'unshelve', 'update', 'updated', 'user_id']

Well, let's see, there a flavor attribute there:

>>> server.flavor
{u'id': u'2', u'links': [{u'href':
u'http://127.0.0.1:8774/95d9bbd9b446438a89a353d8adb60704/flavors/2',
u'rel': u'bookmark'}]}

We can use nova.flavors just like you used nova.servers to lookup information about a server:

>>> flavor = nova.flavors.get(server.flavor['id'])
>>> flavor.disk
20
>>> flavor.vcpus
1
>>> flavor.name
u'm1.small'
edit flag offensive delete link more

Comments

Thanks for the answer. The following code added to the existing got the vm flavor details.

for each in server_list: flavorlist = nvclient.flavors.get(each.flavor['id']) print flavorlist.name

sreejithp gravatar imagesreejithp ( 2015-03-10 05:07:49 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2015-03-06 05:04:21 -0500

Seen: 1,929 times

Last updated: Mar 06 '15