我们的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]))


拥有自由:

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


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



$ nova --version
8.0.0

最佳答案

错误来自图像服务(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

关于python - 为什么冒号会破坏novaclient的glance.find_image?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43971617/

10-10 00:18