Merge "Add ability to search images by name"
This commit is contained in:
commit
4f49f94ce7
@ -195,7 +195,8 @@ def plugins_convert_to_cluster_template(plugin_name, version, name, data):
|
|||||||
@acl.enforce("images:get_all")
|
@acl.enforce("images:get_all")
|
||||||
def images_list():
|
def images_list():
|
||||||
tags = u.get_request_args().getlist('tags')
|
tags = u.get_request_args().getlist('tags')
|
||||||
return u.render(images=[i.dict for i in api.get_images(tags)])
|
name = u.get_request_args().get('name', None)
|
||||||
|
return u.render(images=[i.dict for i in api.get_images(name, tags)])
|
||||||
|
|
||||||
|
|
||||||
@rest.get('/images/<image_id>')
|
@rest.get('/images/<image_id>')
|
||||||
|
@ -201,8 +201,8 @@ def construct_ngs_for_scaling(cluster, additional_node_groups):
|
|||||||
# Image Registry
|
# Image Registry
|
||||||
|
|
||||||
|
|
||||||
def get_images(tags):
|
def get_images(name, tags):
|
||||||
return nova.client().images.list_registered(tags)
|
return nova.client().images.list_registered(name, tags)
|
||||||
|
|
||||||
|
|
||||||
def get_image(**kwargs):
|
def get_image(**kwargs):
|
||||||
|
57
sahara/tests/unit/utils/openstack/test_images.py
Normal file
57
sahara/tests/unit/utils/openstack/test_images.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
# Copyright (c) 2015 Mirantis Inc.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
# implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import mock
|
||||||
|
|
||||||
|
from sahara.tests.unit import base
|
||||||
|
from sahara.utils.openstack import nova as nova_client
|
||||||
|
|
||||||
|
|
||||||
|
class FakeImage(object):
|
||||||
|
def __init__(self, name, tags, username):
|
||||||
|
self.name = name
|
||||||
|
self.tags = tags
|
||||||
|
self.username = username
|
||||||
|
|
||||||
|
|
||||||
|
class TestImages(base.SaharaTestCase):
|
||||||
|
@mock.patch('sahara.utils.openstack.base.url_for', return_value='')
|
||||||
|
def test_list_registered_images(self, url_for_mock):
|
||||||
|
some_images = [
|
||||||
|
FakeImage('foo', ['bar', 'baz'], 'test'),
|
||||||
|
FakeImage('baz', [], 'test'),
|
||||||
|
FakeImage('spam', [], "")]
|
||||||
|
|
||||||
|
with mock.patch('novaclient.v1_1.images.ImageManager.list',
|
||||||
|
return_value=some_images):
|
||||||
|
nova = nova_client.client()
|
||||||
|
|
||||||
|
images = nova.images.list_registered()
|
||||||
|
self.assertEqual(2, len(images))
|
||||||
|
|
||||||
|
images = nova.images.list_registered(name='foo')
|
||||||
|
self.assertEqual(1, len(images))
|
||||||
|
self.assertEqual('foo', images[0].name)
|
||||||
|
self.assertEqual('test', images[0].username)
|
||||||
|
|
||||||
|
images = nova.images.list_registered(name='eggs')
|
||||||
|
self.assertEqual(0, len(images))
|
||||||
|
|
||||||
|
images = nova.images.list_registered(tags=['bar'])
|
||||||
|
self.assertEqual(1, len(images))
|
||||||
|
self.assertEqual('foo', images[0].name)
|
||||||
|
|
||||||
|
images = nova.images.list_registered(tags=['bar', 'eggs'])
|
||||||
|
self.assertEqual(0, len(images))
|
@ -119,10 +119,14 @@ class SaharaImageManager(images.ImageManager):
|
|||||||
tags = _ensure_tags(tags)
|
tags = _ensure_tags(tags)
|
||||||
return [i for i in self.list() if set(tags).issubset(i.tags)]
|
return [i for i in self.list() if set(tags).issubset(i.tags)]
|
||||||
|
|
||||||
def list_registered(self, tags=None):
|
def list_registered(self, name=None, tags=None):
|
||||||
tags = _ensure_tags(tags)
|
tags = _ensure_tags(tags)
|
||||||
return [i for i in self.list()
|
images = [i for i in self.list()
|
||||||
if i.username and set(tags).issubset(i.tags)]
|
if i.username and set(tags).issubset(i.tags)]
|
||||||
|
if name:
|
||||||
|
return [i for i in images if i.name == name]
|
||||||
|
else:
|
||||||
|
return images
|
||||||
|
|
||||||
def get_registered_image(self, image):
|
def get_registered_image(self, image):
|
||||||
img = self.get(image)
|
img = self.get(image)
|
||||||
|
Loading…
Reference in New Issue
Block a user