无法使用api openstack连接

时间:2017-11-15 09:29:04

标签: python python-3.x rest django-rest-framework openstack

我正在尝试使用lib云库连接到我的本地openstack安装。

下面是我试图执行的代码:

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'
#auth_url = 'http://192.168.56.101:5000'
region_name = 'RegionOne'

OpenStack = get_driver(Provider.OPENSTACK)
driver = OpenStack('auth_username',
                   'auth_password',
                   ex_force_auth_url=auth_url,
                   ex_force_base_url='http://192.168.56.101',
                   ex_force_auth_version='2.0_password',
                   ex_tenant_name='admin',
                   ex_force_service_name='nova',
                   ex_force_service_region=region_name)

print(dir(driver.list_nodes))
print(driver.api_name)
print(driver.VOLUME_STATE_MAP)
#print(driver.connection.auth_user_info)
images = driver.name
print(driver.list_volumes())
"""for image in images:
    print(image)
"""

但是我一直收到错误消息资源:

C:\Python dev\website\music\openstack>python openstack.py
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
openstack
{'creating': 'creating', 'available': 'available', 'attaching': 'attaching', 'in-use': 'inuse', 'deleting': 'deleting', 'error': 'error', 'error_deleting': 'error', 'backing-up': 'backup', 'restoring-backup': 'backup', 'error_restoring': 'error', 'error_extending': 'error'}
Traceback (most recent call last):
  File "openstack.py", line 31, in <module>
    print(driver.list_volumes())
  File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\compute\drivers\openstack.py", line 265, in list_volumes
    self.connection.request('/os-volumes').object)
  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"}}

我知道我提供了一个格式错误的网址,但我无法弄清楚网址应该是什么。

1 个答案:

答案 0 :(得分:1)

我将libcloudex_force_auth_token一起使用。但是根据这个页面:http://libcloud.readthedocs.io/en/latest/compute/drivers/openstack.html

我相信你想要的是:

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

import libcloud.security

# This assumes you don't have SSL set up.
# Note: Code like this poses a security risk (MITM attack) and
# that's the reason why you should never use it for anything else
# besides testing. You have been warned.
libcloud.security.VERIFY_SSL_CERT = False

OpenStack = get_driver(Provider.OPENSTACK)
driver = OpenStack('your_auth_username', 'your_auth_password',
                   ex_force_auth_url='http://192.168.1.101:5000',
                   ex_force_auth_version='2.0_password',
                   ex_force_service_type='compute',
                   ex_force_service_name='novaCompute',
                   ex_force_service_region='MyRegion')

我注意到你也把ex_force_base_url放在那里。这应该有用,但ex_force_base_url应为http://192.168.56.101:8774/v2

此处端口8774用于novav2是nova版本。您应该检查您正在使用的nova版本。一种简单的检查方法是使用CLI或OpenStack Dashboard。

HTH。