根据华为云文档说明:
Python SDK使用还需要提前装好openstack的python模块,否则报openstack 模块不存在错误。
Python SDK提供GitHub和pip两种安装方式。
方式一:下载GitHub源码安装方式,如下:
将代码下载到您项目合适的位置,以pythonsdk作为您选择的位置举例,执行下面的命令完成源码下载和安装:
git clone https://github.com/huaweicloud/huaweicloud-sdk-python pythonsdk
cd pythonsdk
pip install -r requirements.txt
python setup.py install
方式二:使用pip安装,请执行以下命令:
pip install huaweicloud-sdk-python
当采用方式一时,如果只git clone了sdk就直接使用它里面的例子,在AK/SK认证的时候就会报:
openstack.exceptions.ConfigException: Cloud myhuaweicloud.com was not found.
我使用方式二,个人感觉sdk比较方便
认证方式中他包含有两种方式:
1.token认证
2.AK/SK认证
因为1方式需要传入账号密码等多个参数,所以采用第二种
认证例子test.py 如下:
#encoding=utf-8
from openstack import connection
projectId = "***"
cloud = "myhuaweicloud.com"
region= "***" # example: region = "cn-north-1"
AK = "***"
SK = "***"
conn = connection.Connection(
project_id=projectId,
cloud=cloud,
region=region,
ak = AK,
sk = SK)
def test_compute():
servers = conn.compute.servers(limit = 3)
for server in servers:
print server
if __name__ == "__main__":
test_compute()
来源于华为云的官方文档:https://support.huaweicloud.com/devg-sdk/sdk_02_0005.html
参数在里面有说明。
当我使用这个例子运行时它报错:
[root@localhost huaweicloud]# python test.py
Traceback (most recent call last):
File "test.py", line 26, in <module>
test_compute()
File "test.py", line 22, in test_compute
for server in servers:
File "/usr/lib/python2.7/site-packages/openstack/resource2.py", line 872, in list
params=query_params)
File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 1123, in get
return self.request(url, 'GET', **kwargs)
File "/usr/lib/python2.7/site-packages/openstack/session.py", line 66, in map_exceptions_wrapper
return func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/openstack/aksksession.py", line 308, in request
service_type=endpoint_filter.service_type)
File "/usr/lib/python2.7/site-packages/openstack/aksksession.py", line 444, in get_endpoint
base_url = self._get_endpoint_from_iamdata(service_type)
File "/usr/lib/python2.7/site-packages/openstack/aksksession.py", line 460, in _get_endpoint_from_iamdata
self.__iam_endpoint = self.__fetch_all_endpoint_service_project_level()
File "/usr/lib/python2.7/site-packages/openstack/aksksession.py", line 502, in __fetch_all_endpoint_service_project_level
endpoint_filter=identity_service.AdminService())
File "/usr/lib/python2.7/site-packages/openstack/session.py", line 66, in map_exceptions_wrapper
return func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/openstack/aksksession.py", line 398, in request
logger=_logger)
File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 466, in _http_log_request
if self._get_split_loggers(split_loggers):
File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 454, in _get_split_loggers
split_loggers = self._split_loggers
AttributeError: 'ASKSession' object has no attribute '_split_loggers'
Exception AttributeError: "'ASKSession' object has no attribute '_session'" in <bound method ASKSession.__del__ of <openstack.aksksession.ASKSession object at 0x7fce44772c50>> ignored
思考应该不是代码问题,因为代码是直接复制于官方的例子,工单询问华为云技术,说是环境或者参数问题,因为环境看文档只是说明要装好openstack模块,没有其他要求,所以认为是参数问题,之后核对完参数还是同样的报错。。。之后猜想可能是环境上还有特殊要求,因此在另一台机器上使用pip install huaweicloud-sdk-python命令尝试安装了一把,运行没有报错了。对比了两个环境,发现各个包版本都是一致的,最后仔细看了下报错:
File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 466, in _http_log_request
if self._get_split_loggers(split_loggers):
File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 454, in _get_split_loggers
它追踪到keystoneauth1这个东西有报错,然后使用
pip show keystoneauth1
命令查看发现能跑的哪个环境keystoneauth1版本是3.4.0,报错环境keystoneauth1版本是3.18.0
之后卸载掉keystoneauth1 3.18.0安装keystoneauth1 3.4.0后可以正常运行认证流程了,猜想keystoneauth1 3.18.0对这sdk不兼容吧。。。
之后的各种sdk调用都顺顺利利了
网友评论