如何使用pythonnovaclient验证Nova会话?

2024-05-29 02:30:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设置一个nova客户端,并确保在创建客户端时没有身份验证错误。我的尝试如下:

from novaclient import client
from keystoneauth1 import session
from keystoneauth1.identity import v3

def setup_nova(creds):
    """
    Creates a nova instance with which we can leverage the OpenStack virtualization
    platform.

    :param creds: A dictionary containing the authorization url, username,
                  password, version, and project ID associated with the OpenStack
                  cluster.
    :type creds: dict
    """
    password = v3.PasswordMethod(username=creds['USERNAME'],
                                 password=creds['PASSWORD'],
                                 user_domain_name='default')

    auth = v3.Auth(auth_url=creds['AUTH_URL'],
                   auth_methods=[password],
                   project_id=creds['PROJECT_ID'])

    sess = session.Session(auth=auth)
    nova = client.Client(creds['VERSION'], session=sess)
    nova.authenticate()
    return nova

但是,nova.authenticate()调用抛出一个novaclient.exceptions.InvalidUsage,它告诉我对会话对象进行身份验证。不过,Session对象似乎没有进行身份验证的方法。在

虽然OpenStack将尝试在第一次请求时进行身份验证并缓存身份验证,但我更希望立即知道用户是否有权根据所提供的凭据发出请求。在

如何按需验证会话对象?在


Tags: the对象fromimportclientauth身份验证客户端
1条回答
网友
1楼 · 发布于 2024-05-29 02:30:15

也许您可以尝试使用下面显示的keystoneclient document示例来验证nova会话:

>>> from keystoneauth1.identity import v3
>>> from keystoneauth1 import session
>>> from keystoneclient.v3 import client

>>> auth = v3.Password(auth_url='https://my.keystone.com:5000/v3',
...                    username='myuser',
...                    password='mypassword',
...                    project_id='proj',
...                    user_domain_id='domain')
>>> sess = session.Session(auth=auth,
...                        verify='/path/to/ca.cert')
>>> ks = client.Client(session=sess)
>>> users = ks.users.list()

我参考上面的例子来修改您的代码:

^{pr2}$

相关问题 更多 >

    热门问题