2017-05-15 44 views
1

我们在我们的OpenStack中有名为<OS> <version>:<build no>的图像(例如,CentOS 7.2.0:160708.0)。与the Python novaclient,我可以使用client.glance.find_image与三鹰之前的版本。为什么冒号打破novaclient的glance.find_image?

$ cat test.py 
#! /usr/bin/env python3 
import os 
import sys 
from novaclient import client 
nova = client.Client("2", 
        os.environ["OS_USERNAME"], 
        os.environ["OS_PASSWORD"], 
        os.environ["OS_TENANT_ID"], 
        os.environ["OS_AUTH_URL"], 
        cacert=os.environ["OS_CACERT"]) 
print(nova.glance.find_image(sys.argv[1])) 

随着自由:

$ python3 test.py "CentOS 7.2.0:170210.0" 
<Image: CentOS 7.2.0:170210.0> 

随着三鹰:

$ python3 test.py "CentOS 7.2.0:170210.0" 
Traceback (most recent call last): 
    File "test.py", line 11, in <module> 
    print(nova.glance.find_image(sys.argv[1])) 
    File "/usr/local/lib/python3.6/site-packages/novaclient/v2/images.py", line 53, in find_image 
    "images") 
    File "/usr/local/lib/python3.6/site-packages/novaclient/base.py", line 254, in _list 
    resp, body = self.api.client.get(url) 
    File "/usr/local/lib/python3.6/site-packages/keystoneauth1/adapter.py", line 223, in get 
    return self.request(url, 'GET', **kwargs) 
    File "/usr/local/lib/python3.6/site-packages/novaclient/client.py", line 80, in request 
    raise exceptions.from_response(resp, body, url, method) 
novaclient.exceptions.BadRequest: Unable to filter by unknown operator 'CentOS 7.2.0'.<br /><br /> 


(HTTP 400) 

注意,当这个名字的图像不存在的错误是不同的:

$ python3 test.py "CentOS 7.2.0" 
Traceback (most recent call last): 
    File "test.py", line 11, in <module> 
    print(nova.glance.find_image(sys.argv[1])) 
    File "/usr/local/lib/python3.6/site-packages/novaclient/v2/images.py", line 58, in find_image 
    raise exceptions.NotFound(404, msg) 
novaclient.exceptions.NotFound: No Image matching CentOS 7.2.0. (HTTP 404) 

这就好像find_image一样期望的形式operator: value的字符串,但the documentation has only this to say about find_image

find_imagename_or_id
通过名称或ID(用户提供的输入)找到的图像。

如何在使用三鹰时找到名称中包含冒号的图像?


$ nova --version 
8.0.0 

回答

1

该错误是从图像服务(概览)到来。在新版本的Glance中,GET API语法发生了变化,其中有人可以指定“in:”运算符进行过滤。您可以在

https://developer.openstack.org/api-ref/image/v2/index.html?expanded=show-images-detail#show-images

阅读更多关于这对于你的代码工作,则可以“:”用引号将图像名称和使用前缀字符串:

print(nova.glance.find_image('in:"' + sys.argv[1] + '"')) 

注意一览对报价相当严格;您的图片名称只能用双引号包裹 - 单引号不起作用。因此,我在上面的命令中使用了单引号。

另一种非常低效,但功能选项是使用list()函数在nova.images,然后明确地查找图像与名称sys.argv中[1]:

ilist = nova.images.list() 
for image in ilist: 
    if image.name == sys.argv[1]: 
     print image 
     break 
+0

过滤列表可能是低效的,但它比'find_image'更灵活,它需要一个精确的图像名称。并且'in:“...”'方式在旧版本中不起作用,所以它不是很有用。感谢您的信息! – muru