我正在尝试使用Python:中的libcloud api在CloudStack中列出图像
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
from libcloud.common.base import Response
#import libcloud.security as sec
#sec.VERIFY_SSL_CERT = False
#USER = 'ACCESSKEY'
#API_KEY = 'SECRETKEY'
Driver = get_driver(Provider.CLOUDSTACK)
url = 'MY URL'
conn = Driver(key=USER, secret=API_KEY, url=url)
print "Connection established"
images = conn.list_images()
print images
当运行此代码时,我得到以下错误:
body = self.parse_body()
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/base.py", line 195,
in parse_body driver=self.connection.driver)
libcloud.common.types.MalformedResponseError: <MalformedResponseException in
<libcloud.compute.drivers.cloudstack.CloudStackNodeDriver object at 0x7fc356f55b50>
'Failed to parse JSON'>: 'Unknown_ApiKey'
我错过了什么?
带有CloudStack的libcloud不需要'region':有关工作示例,请参阅此链接。
从你的评论中,我看到你使用interoute.com。通过快速搜索,我找到了如何在interoute上使用libcloud的教程。你可以在github上找到更多的例子。
您的代码列出所有图像似乎是正确的。但错误消息包含Unknown_ApiKey
,因此请检查您是否为您的请求提供了有效凭据。
(编辑)CloudStack(interoute.com)的一个工作示例是:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
VDCDriver=get_driver(Provider.CLOUDSTACK)
vdc_apikey= 'INSERT YOUR VDC ACCOUNT API KEY HERE'
vdc_secretkey= 'INSERT YOUR VDC ACCOUNT SECRET KEY HERE'
vdc_url= 'https://myservices.interoute.com/myservices/api/vdc'
conn=VDCDriver(key=vdc_apikey, secret=vdc_secretkey, url=vdc_url)
images = conn.list_images()
for i in images:
print "%s - %s" % (i.extra['displaytext'],i.id)
这将输出所有可用图像的名称和id:
openSUSE 13.2 - abcde-1111-abc-1111-abcde
Ubuntu 14.10 - abcde-2222-abc-2222-abcde
Fedora 21 - abcde-3333-abc-333-abcde