Authenticating the python ceilometer client against the Openstack APIs – bloody lambda functions!

呼延珂
2023-12-01

原文转自:Authenticating the python ceilometer client against the Openstack APIs – bloody lambda functions!

We were doing some work with Ceilometer – it appears in a few of our activities – and I was trying to get up to speed with it. Playing with the python Ceilometer client proved a little more difficult then envisaged – mostly due to deficiencies in documentation. Here’s a small note on an issue I faced with authentication.

The starting point for the work was to use the approach proposed the documentation to obtain a list of meters. This was done after a basic install of the libraries locally (pip install python-ceilometerclient) to work against a remote set of APIs which are known to be stable and working.

Using this basic setup, authentication would not work when the following approach to creation of the client (see full code below) was used; this is very much in line with the approaches of other Openstack client libraries.

from ceilometerclient import client
ceilometer = Client('2', endpoint=OS_IMAGE_ENDPOINT, token=OS_AUTH_TOKEN)

The following error was produced:

“File "/usr/local/lib/python2.7/dist-packages/ceilometerclient/common/http.py", line 137, in _http_request
auth_token = self.auth_token()
TypeError: 'unicode' object is not callable”

The problem turned out to be one of lambda functions – it was not obvious that we should make the token an anonymous function: once we called it with lambda, everything worked fine.

ceilometer = client.Client('2',endpoint = ceilo_endpoint, token = lambda: token)

Subsequently, one of our colleagues (thanks Florian!) noted that there is a simpler approach which uses get_client. (See the second file below for how we used this).

Now we’re trying to do something useful with it!

—–

#!/usr/bin/python
# -*- coding: utf-8 -*-
# list meters - client initialized with lambda’d auth_token
from ceilometerclient import client
from os import environ as env
import keystoneclient.v2_0.client as ksclient

#getting the credentials
keystone = {}
keystone['username']=env['OS_USERNAME']
keystone['password']=env['OS_PASSWORD']
keystone['auth_url']=env['OS_AUTH_URL']
keystone['tenant_name']=env['OS_TENANT_NAME']

#creating a keystone client
ceilometer_client = client._get_ksclient(**keystone)
token = ceilometer_client.auth_token

#creating an endpoint
ceilo_endpoint = client._get_endpoint(ceilometer_client, **keystone)

#creating a ceilometer client
ceilometer = client.Client('2',endpoint = ceilo_endpoint, token = token)

#tests
meters = ceilometer.meters.list()
print meters

——

#!/usr/bin/python
# -*- coding: utf-8 -*-
# list meters - client initialized with get_client
#imports
from ceilometerclient import client
from os import environ as env
from ceilometerclient.common import utils

#getting the credentials
keystone = {}
keystone['os_username']=env['OS_USERNAME']
keystone['os_password']=env['OS_PASSWORD']
keystone['os_auth_url']=env['OS_AUTH_URL']
keystone['os_tenant_name']=env['OS_TENANT_NAME']

#creating an authenticated client
ceilometer_client = client.get_client(2,**keystone)

#now you should be able to use the API
meters = ceilometer_client.meters.list()
print meters
 类似资料:

相关阅读

相关文章

相关问答