Custom Search

Monday, November 18, 2013

How to call keystone API from Horizon

How to call keystone API from Horizon

* Location of Keystone API in Horizon
./horizon/openstack_dashboard/api/keystone.py

a)
* Get endpoing url

def _get_endpoint_url(request, endpoint_type, catalog=None):
    auth_url = getattr(settings, 'OPENSTACK_KEYSTONE_URL')
    return url

b)
* Get a client connected to the Keystone backend

def keystoneclient(request, admin=False):
    api_version = VERSIONS.get_active_version()
    cache_attr = "_keystoneclient_admin" if admin else backend.KEYSTONE_CLIENT_ATTR
    endpoint = _get_endpoint_url(request, endpoint_type)
        conn = api_version['client'].Client(token=user.token.id,
                                            endpoint=endpoint)
        setattr(request, cache_attr, conn)
        return conn

c)
* Create User

def user_create(request, name=None, email=None, password=None, project=None, enabled=None, domain=None):
    ###Get a client connected to the Keystone backend
    manager = keystoneclient(request, admin=True).users
    if VERSIONS.active < 3:
    ###Make API call
        user = manager.create(name, password, email, project, enabled)
        return VERSIONS.upgrade_v2_user(user)
    else:
    ###Make API call
        return manager.create(name, password=password, email=email, project=project, enabled=enabled, domain=domain)

d)
VERSIONS = IdentityAPIVersionManager("identity", preferred_version=3)

e)

# Set up our data structure for managing Identity API versions, and
# add a couple utility methods to it.
class IdentityAPIVersionManager(base.APIVersionManager):
    def upgrade_v2_user(self, user):
        if getattr(user, "project_id", None) is None:
            user.project_id = getattr(user, "tenantId", None)
        return user

    def get_project_manager(self, *args, **kwargs):
        if VERSIONS.active < 3:
            manager = keystoneclient(*args, **kwargs).tenants
        else:
            manager = keystoneclient(*args, **kwargs).projects
        return manager

f)
./horizon/openstack_dashboard/api/base.py
class APIVersionManager(object):

No comments:

Post a Comment