Ask Your Question
0

Python nova-client: get image id using name

asked 2016-04-04 23:54:44 -0500

itsme gravatar image

I am using python nova-client in my development environment.

I need to get the image id of image using the image-name as a input.

I can b e able to achieve the same in Shell command. Command for the same is as follows:

nova image-show cirros-0.3.4-x86_64-uec

It is displaying the result.

But now I need to achieve the using python script.

net = nt.images.list()

But here I am unable to get the result. I have used get instead of list.

It seems of no use.

Some on let me know for getting the same.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-04-05 05:39:38 -0500

dbaxps gravatar image

Code and configuration

[root@ip-192-169-142-127 test1(keystone_admin)]# ls -l
total 16
-rw-r--r--. 1 root root 508 Apr  5 13:32 credentials.py
-rw-r--r--. 1 root root 784 Apr  5 13:34 credentials.pyc
-rw-r--r--. 1 root root 220 Apr  5 13:33 getImageID.py
-rw-r--r--. 1 root root 419 Apr  5 13:34 getImageID.pyc

[root@ip-192-169-142-127 test1(keystone_admin)]# cat credentials.py

#!/usr/bin/env python
import os

def get_keystone_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['password'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['tenant_name'] = os.environ['OS_TENANT_NAME']
    return d

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

[root@ip-192-169-142-127 test1(keystone_admin)]# cat getImageID.py

import novaclient.v2.client as nvclient
name='rastasheep/ubuntu-sshd'
from credentials import get_nova_creds
creds = get_nova_creds()
nova = nvclient.Client(**creds)
image = nova.images.find(name=name)
print (image.id)

Now run

[root@ip-192-169-142-127 test1(keystone_admin)]# python  getImageID.py
af71fe64-699c-4605-908b-676f9d536fb1
[root@ip-192-169-142-127 test1(keystone_admin)]# glance image-list | grep af71fe64-699c-4605-908b-676f9d536fb1
| af71fe64-699c-4605-908b-676f9d536fb1 | rastasheep/ubuntu-sshd   |
edit flag offensive delete link more

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: 2016-04-04 23:54:44 -0500

Seen: 998 times

Last updated: Apr 05 '16