简体   繁体   English

使用OpenStack传递会话对象-KeyStone

[英]Passing Session Object Using OpenStack - KeyStone

I'm trying to pass the session object from one file to another in order to use the same session object across all the different things I need to do (list/create VMs etc.) So far I've tried using Pickle (which hasn't worked as there is a lock instance on the object). 我试图将会话对象从一个文件传递到另一个文件,以便在需要执行的所有不同操作(列出/创建VM等)中使用同一会话对象。到目前为止,我已经尝试使用Pickle(该方法没有无效,因为该对象上有一个锁实例)。 I'm now trying to use Cherrypy to enable me to store the object as a cookie (which I'm already doing for name and fedid). 我现在正在尝试使用Cherrypy使我能够将对象存储为cookie(我已经在做name和fedid的操作了)。 But I'm getting an error which says the session object isn't JSON serializable. 但是我遇到一个错误,该错误表明会话对象不是JSON可序列化的。

How do I get past this error and is this the right way to go about passing session objects? 如何克服此错误,这是传递会话对象的正确方法吗? If it's not the correct solution, how do I pass session objects (the documentation isn't exactly helpful)? 如果这不是正确的解决方案,那么我该如何传递会话对象(文档并不能完全有帮助)?

import cherrypy
import ldap
import xmlrpclib
import xml.etree.ElementTree as ET
from keystoneauth1 import loading
from keystoneauth1 import session as session
import novaclient.client as client
import json
import pickle


NOVA_VERSION="2.1"
KEYSTONE_URL = cherrypy.request.config.get("keystone")
OPENSTACK_HOST = cherrypy.request.config.get("openstack_host")
OPENSTACK_DEFAULT_DOMAIN = 
cherrypy.request.config.get("openstack_default_domain")
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(auth_url=KEYSTONE_URL, username=data.get("username"), password=data.get("password").replace(" ","%20"),user_domain_name=OPENSTACK_DEFAULT_DOMAIN)
sess = session.Session(auth=auth)
nova = client.Client(NOVA_VERSION,session=sess)
data = {
        'name'    : name,
        'fedid'   : data.get("username"),
        'sessionTest' : json.dumps(sess),
        'expires' : EXPIRE
    }

Error: 错误:

File "/usr/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/cherrypy/lib/jsontools.py", line 61, in json_handler
value = cherrypy.serving.request._json_inner_handler(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/cherrypy/_cpdispatch.py", line 34, in __call__
return self.callable(*self.args, **self.kwargs)
File "/var/www/frontend/controllers/api/user.py", line 102, in PUT
'sessionTest' : json.dumps(sess),
File "/usr/lib64/python2.7/json/__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <keystoneauth1.session.Session object at 0x7f023003c710> is not JSON serializable

The issue is with the loader object. 问题出在loader对象上。

You should load the auth credentials at the reciever 您应该在收件人处加载身份验证凭据

you can pickle loader and the options but not the auth object 您可以腌制loader和选项,但不能auth对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM