为什么冒号打破了novaclient的glance.find_image?

时间:2017-05-15 04:32:48

标签: python openstack openstack-nova

我们的OpenStack中有一些名为<OS> <version>:<build no>的图片(例如CentOS 7.2.0:160708.0)。使用the Python novaclient,我可以在Mitaka之前使用client.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]))

使用Liberty:

$ 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_image name_or_id
  按名称或ID(用户提供的输入)查找图像。

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

$ nova --version
8.0.0

1 个答案:

答案 0 :(得分:1)

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

了解更多相关信息

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

要使代码生效,您可以用引号括起图像名称,并在其前面添加“in:”字符串:

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

请注意,Glance对引号非常严格;您的图片名称只能用双引号括起来 - 单引号不起作用。因此,我在上面的命令中使用了单引号。

另一个非常低效但功能强大的选项是在nova.images中使用list()函数,然后显式查找名为sys.argv [1]的图像:

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