原文转自: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

Authenticating the python ceilometer client against the Openstack APIs – bloody lambda functions!相关推荐

  1. pythonjava app切出后无网络连接_写了一个java的Server 用python的client访问却访问不通问题。...

    首先给出这个java的Server代码 try{//1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口 ServerSocket serverSocket=ne ...

  2. Python内置函数filter()和匿名函数lambda解析

    一.内置函数filter filter()函数是 Python 内置的一个高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回由符合条件迭代器 ...

  3. Python中常用最神秘的函数! lambda 函数深度总结!

    今天我们来学习 Python 中的 lambda 函数,并探讨使用它的优点和局限性 Let's do it! 什么是 Python 中的 Lambda 函数 lambda 函数是一个匿名函数(即,没有 ...

  4. python 的高级特性:函数式编程,lambda表达式,装饰器

    一.Python语言的高级特性 函数式编程 基于Lambda演算的一种编程方式 程序中只有函数 函数可以作为参数,同样可以作为返回值 纯函数式编程语言:LISP,Haaskell Python函数式编 ...

  5. 【数据平台】基于pyhs2库Python作为client driver连接HiveServer

    1.安装pyhs2 A Python client driver for HiveServer2 is available at https://github.com/BradRuderman/pyh ...

  6. 异步赠书1月书讯:Python/深度学习/贝叶斯/OpenStack/DevOps/Docker

    Ladys and乡亲们! 异步图书君祝大家元旦快乐! 忙着写2017年总结的真爱粉儿们 2018年,你定了哪些新计划? 留言说给大家听听? 异步图书君送出好书一本! 2018年异步图书君有一个超逼格 ...

  7. python -socket -client

    socket client 发起连接. 流程为: 创建接口 发起连接 创建接口参数同socket server相同 发起连接的函数为socket.connect(ip,port) 这个地方的ip与po ...

  8. python suds.client Client 设置超时时间

    看suds的源码后,没有发现timeout,但是发现了set_options函数,具体代码如下: class Client(UnicodeMixin):# ....def __init__(self, ...

  9. python udp client

    udp_p = Process(target=udp_client.init, args=(status,HOST,)) # 申请子进程 udp_p.start() # 运行进程 # 获取本机电脑名 ...

最新文章

  1. BootStrap2学习日记8---表单
  2. PHP $_FILES中error返回值详解
  3. 浅谈js本地图片预览
  4. 皮卡丘为什么不进化_皮卡丘为啥不进化成雷丘?小智都暗示得这么明显皮卡丘还是拒绝了...
  5. rfcomm工具的使用方法 创建/dev/rfcomm0 并检测
  6. POJ(1195)(单点修改,区间查询)(二维)
  7. Windows Server 2003 DNS服务安装篇
  8. 批处理命令无法连续执行
  9. redistemplate.opsforvalue 设置不过期_民法典即将实施!“离婚冷静期”倍受关注
  10. javascript编辑器
  11. 1. JanusGraph的优势
  12. HDU 4009 不定根最小树形图
  13. 【深入浅出】Java中 this关键字的四种用法
  14. 代购集运系统平台一键上传淘宝商品至韩国coupang经验分享
  15. 青龙面板 精易论坛签到脚本
  16. 如何修改w7计算机用户名和密码忘记,w7系统开机密码忘记解决方法_win7忘记密码如何进入系统...
  17. 统计文件中元音字母的数量
  18. MAC上有哪些优秀的日常软件| 入门级Mac OS 用户必备软件
  19. 【华为OD机试真题 JAVA】机器人走迷宫
  20. Vector CANoe修改Panel的名字

热门文章

  1. could only be replicated to 1 nodes instead of minReplication (=2). There are 3 datanode(s) running
  2. 利用手上无用的云主机实现内网穿透
  3. 每天一道面试题-谈谈对面向对象思想的理解
  4. javascript cookies操作
  5. AppCode的一些设置
  6. pagehelper联表分页查询
  7. 关于使用IE浏览特制网页时允许远程执行代码漏洞(MS10-018)的复现
  8. 【非编系统】非编系统是从事影视后期编辑的系统工具
  9. EndNote更改打开PDF的方式(Microsoft Edge-Adobe Acrobat)
  10. VB图书管理系统毕业设计