如何在python中使用libcloud库

时间:2015-03-22 08:14:37

标签: python api interface libcloud apiclient

我正在尝试使用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'

我错过了什么?

1 个答案:

答案 0 :(得分:1)

A&#39;地区&#39;使用CloudStack的libcloud不需要:请参阅this link for a working example

根据您的评论,我看到您使用interoute.com。通过快速搜索,我发现了tutorial how to work with libcloud on interoute。您可以在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