简体   繁体   中英

not able to connect using api openstack

I am trying to use the lib cloud library to connect to my local openstack installation.

below is the code I am trying to execute:

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)
"""

However I keep getting the error message resource not found:

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"}}

I know that I am providing a malformed url but I am not able to figure out what the url should be.

I use libcloud with ex_force_auth_token . But according to this page: http://libcloud.readthedocs.io/en/latest/compute/drivers/openstack.html

I believe what you want is to have:

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')

I notice that you put the ex_force_base_url there as well. That should work but your ex_force_base_url should be http://192.168.56.101:8774/v2

Here port 8774 is for nova and v2 is the nova version. You should check the version of nova you are using. A simple way to check is to use the CLI or the OpenStack Dashboard.

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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