Openstack无法连接到使用rest api

时间:2017-11-15 04:37:46

标签: openstack openstack-nova

我在虚拟机上的openstack上进行了本地安装。我正在尝试使用lib cloud api来连接并获取图像,风味等列表。

以下是我尝试执行的代码

    from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver

# Authentication information so you can authenticate to DreamCompute
# copy the details from the OpenStack RC file
# https://dashboard.dreamcompute.com/project/access_and_security/api_access/openrc/

auth_username = 'admin'
auth_password = 'f882e2f4eaad434c'
TENANT_NAME = 'admin'
project_name = 'admin'
auth_url = 'http://192.168.56.101:5000/v3/tokens'
region_name = 'RegionOne'

provider = get_driver(Provider.OPENSTACK)
conn = provider(auth_username,
                auth_password,
                ex_force_auth_url=auth_url,
                ex_force_auth_version='2.0_password',
                ex_tenant_name=project_name,
                ex_force_service_type='compute',
                ex_force_service_name='compute',
                ex_force_base_url='http://192.168.56.101:8774/v2.1/29a8949bc3a04bfead0654be8e552017')


# Get the image that we want to use by its id
# NOTE: the image_id may change. See the documentation to find
# all the images available to your user
image_id = '4525d442-e9f4-4d19-889f-49ab03be93df'  
image = conn.get_image(image_id)

# Get the flavor that we want to use by its id
flavor_id = '100'
flavor = conn.ex_get_size(flavor_id)

# Create the node with the name “PracticeInstance”
# and the size and image we chose above
instance = conn.create_node(name='PracticeInstance', image=image, size=flavor)

当我运行上面的代码时,我收到以下错误:

C:\Python dev\website\music\openstack>python openstack.py
Traceback (most recent call last):
  File "openstack.py", line 30, in <module>
    image = conn.get_image(image_id)
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\compute\drivers\openstack.py", line 2028, in get_image
    '/images/%s' % (image_id,)).object['image'])
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack.py", line 223, in request
    raw=raw)
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\base.py", line 536, in request
    action = self.morph_action_hook(action)
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack.py", line 290, in morph_action_hook
    self._populate_hosts_and_request_paths()
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack.py", line 324, in _populate_hosts_and_request_paths
    osa = osa.authenticate(**kwargs)  # may throw InvalidCreds
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack_identity.py", line 855, in authenticate
    return self._authenticate_2_0_with_password()
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack_identity.py", line 880, in _authenticate_2_0_with_password
    return self._authenticate_2_0_with_body(reqbody)
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack_identity.py", line 885, in _authenticate_2_0_with_body
    method='POST')
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\base.py", line 637, in request
    response = responseCls(**kwargs)
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\base.py", line 157, in __init__
    message=self.parse_error())
libcloud.common.exceptions.BaseHTTPError: {"error": {"message": "The resource could not be found.", "code": 404, "title": "Not Found"}}

我已经在/ var / log / keystone检查了我服务器中的日志,并且没有出现任何错误,所以我猜我能够登录。

还有许多例子表明,经过上述步骤,我应该能够获得图像/口味/服务器列表。

不确定我无法连接的原因。有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为你应该修改auth_url和提供者参数。 以下参数集在我的环境中工作。

auth_username = 'admin'
auth_password = 'f882e2f4eaad434c'
TENANT_NAME = 'admin'
project_name = 'admin'
auth_url = 'http://192.168.56.101:5000'
region_name = 'RegionOne'

provider = get_driver(Provider.OPENSTACK)
conn = provider(auth_username,
                auth_password,
                ex_force_auth_url=auth_url,
                ex_force_auth_version='2.0_password',
                ex_tenant_name=project_name)

更新2017.11.16

@ user8040338您的错误信息是第一个线索, The resource could not be found.和状态code 404,其消息和状态代码的最主要原因是错误的其他api url格式。

首先,您需要检查keystone v2.0 rest api format。 同时,再次检查Libcloud reference。 例如,参数ex_force_auth_version已经指定了api版本2.0_password(v2),但是auth_url变量形成了包含版本资源/v3的url,它是错误的版本和使用您指定的libcloud API参数的API。 auth_url应该是API使用的基本网址。

关于API的每个参数的类似过程应该重复进行,直到解决问题。