Ask Your Question

login as admin and retrieve server data from different tenants via python

asked 2015-07-07 14:02:45 -0600

I'm writing some cron job in python that should read server ids from a database and then get the servers from the API by using the python-novaclient.

In pseudo code things should look like this

session = login_to_keystone(user="admin", password="something") #or token="aaaa-bbbbb-cccc"
nova_client = get_nova_client(session)

#the servers array holds dictionaries with server, user and tenant ids as string
# e.g. {"server_id": "1-2-3", "tentant_id": "456", user_id: "11111-2222"}
for server in servers:
   server_obj = nova_client.servers.get(server.server_id) stuff with server_obj (read data from it, delete,...)...

What I've come up with is the following, but it's not right as I get a EndpointNotFound exception.

     #I'm on devstack Juno
     keystone = keystone_client.Client(token="my-admin-token", auth_url="", endpoint="")
    key_session = session.Session(auth=keystone)
    nova = nova_client.Client(2, session=key_session)

    for server in servers:
        server_obj = nova.servers.get(server.server_id) #Exception happens here

I need to run this as admin as servers can belong to any tenant and they might even be deleted by the cron job.

Thanks for any help!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-07-22 07:54:36 -0600

Found the answer thanks to @larsks at stackoverflow.

The trick is that you use the admin's tenant as this allows you to retrieve servers from all users. My solution looks like this.

from keystoneclient.auth.identity import v2
from keystoneclient import session
from novaclient import client as nova_client

auth = v2.Password(auth_url="",
               tenant_name="admin") # the admin's tenant

auth_session = session.Session(auth=auth)
nova = nova_client.Client(2, session=auth_session)

for server in servers:
    ... do stuff like nova.servers.get("some id")
edit flag offensive delete link more

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower


Asked: 2015-07-07 14:02:45 -0600

Seen: 524 times

Last updated: Jul 22 '15