为什么冒号会破坏 novaclient 的 glance.find_image?
Why does a colon break novaclient's glance.find_image?
我们的 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
(name_or_id)
Find an image by name or id (user provided input).
如何在使用 Mitaka 时查找名称中包含冒号的图像?
$ 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
我们的 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
(name_or_id)
Find an image by name or id (user provided input).
如何在使用 Mitaka 时查找名称中包含冒号的图像?
$ 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