使用 Python API 创建 OpenStack 客户端实例时如何指定区域名称

How to specify Region Name when create OpenStack client instance with Python API

在使用 Python API 创建 OpenStack 客户端实例时,我能够使用关键字 RegionName 指定区域名称。就像:

novaclient.Client(Version, User, Password, Project_ID, Auth_URL, RegionName='RegionName')

但在最新版本的 OpenStack 中,我收到错误消息:

TypeError: init() got an unexpected keyword argument 'RegionName'

我必须删除 RegionName 参数:

novaclient.Client(Version, User, Password, Project_ID, Auth_URL)

但是我不知道在我的 Python 程序中的什么地方指定区域名称。来自 OpenStack 官方文档, https://docs.openstack.org/python-novaclient/latest/reference/api/index.html 我找不到有关设置区域名称的任何信息。无论是凭据还是 keystoneauth 会话,都无法指定区域名称。与其他 OpenStack 客户端相同。

我的问题是在使用 Python API 创建 OpenStack 客户端实例时如何指定区域名称?感谢您的回答!

您需要在创建 nova 客户端时将 region_name 作为关键字参数传递给提及区域。

novaclient.Client(Version, User, Password, Project_ID, Auth_URL, region_name='Region1')

这是 Nova Client 的源参考 class,

https://github.com/openstack/python-novaclient/blob/4707422377214829126f1d6352119ea08a7ec104/novaclient/v2/client.py#L80

我想在这里分享我的测试结果:

使用 keystoneauth 会话 API 和身份 API v3:

from novaclient import client as novaclient
from keystoneauth1.identity import v3
from keystoneauth1 import session
auth = v3.Password(URL,username="username",password="password", project_name="admin", user_domain_id="default", project_domain_id="default")
sess = session.Session(auth=auth)
nova_client = novaclient.Client(2,  session=sess, region_name="RegionName")

很好

具有凭据和身份API v2:

from novaclient import client as novaclient nova=novaclient.Client(版本, 用户, 密码,Project_ID, Auth_URL_v2, region_name='RegionName')

很好

具有凭据和身份API v3:

from novaclient import client as novaclient
nova=novaclient.Client(Version, User, Password,Project_ID, Auth_URL_v3, region_name='RegionName')

它实际上不工作,错误消息:

nova.hypervisors.list()
keystoneauth1.exceptions.http.BadRequest: Expecting to find domain in user - the server could not comply with the request since it is either malformed or otherwise incorrect. The client is assumed to be in error. (HTTP 400)

所以我的结论是 novaclient 不支持 3) - Identity API V3 with credentials。我们可以使用 1) - Identity API v3 with keystoneauth session 和 2) - Identity API V2 with credentials