diff --git a/doc/source/user/proxies/image_v2.rst b/doc/source/user/proxies/image_v2.rst index 5327eaa55..c838fd898 100644 --- a/doc/source/user/proxies/image_v2.rst +++ b/doc/source/user/proxies/image_v2.rst @@ -89,4 +89,5 @@ Cache Operations .. autoclass:: openstack.image.v2._proxy.Proxy :noindex: - :members: cache_delete_image + :members: cache_delete_image, queue_image, get_image_cache + diff --git a/openstack/image/v2/_proxy.py b/openstack/image/v2/_proxy.py index cf00639dd..6de411237 100644 --- a/openstack/image/v2/_proxy.py +++ b/openstack/image/v2/_proxy.py @@ -89,7 +89,12 @@ class Proxy(proxy.Proxy): the metadef namespace does not exist. :returns: ``None`` """ - self._delete(_cache.Cache, image, ignore_missing=ignore_missing) + return self._delete(_cache.Cache, image, ignore_missing=ignore_missing) + + def queue_image(self, image_id): + """Queue image(s) for caching.""" + cache = self._get_resource(_cache.Cache, None) + return cache.queue(self, image_id) # ====== IMAGES ====== def create_image( diff --git a/openstack/image/v2/cache.py b/openstack/image/v2/cache.py index e63e954d5..e9d59072c 100644 --- a/openstack/image/v2/cache.py +++ b/openstack/image/v2/cache.py @@ -10,7 +10,9 @@ # License for the specific language governing permissions and limitations # under the License. +from openstack import exceptions from openstack import resource +from openstack import utils class CachedImage(resource.Resource): @@ -26,9 +28,25 @@ class Cache(resource.Resource): allow_fetch = True allow_delete = True + allow_create = True _max_microversion = '2.14' cached_images = resource.Body('cached_images', type=list, list_type=CachedImage) queued_images = resource.Body('queued_images', type=list) + + def queue(self, session, image, *, microversion=None): + """Queue an image into cache. + :param session: The session to use for making this request + :param image: The image to be queued into cache. + :returns: The server response + """ + if microversion is None: + microversion = self._get_microversion(session, action='commit') + image_id = resource.Resource._get_id(image) + url = utils.urljoin(self.base_path, image_id) + + response = session.put(url, microversion=microversion) + exceptions.raise_from_response(response) + return response diff --git a/openstack/tests/unit/image/v2/test_cache.py b/openstack/tests/unit/image/v2/test_cache.py index fc1cfc678..6f29dbdf7 100644 --- a/openstack/tests/unit/image/v2/test_cache.py +++ b/openstack/tests/unit/image/v2/test_cache.py @@ -10,6 +10,9 @@ # License for the specific language governing permissions and limitations # under the License. +from unittest import mock + +from openstack import exceptions from openstack.image.v2 import cache from openstack.tests.unit import base @@ -44,3 +47,15 @@ class TestCache(base.TestCase): sot.cached_images, ) self.assertEqual(EXAMPLE['queued_images'], sot.queued_images) + + @mock.patch.object(exceptions, 'raise_from_response', mock.Mock()) + def test_queue(self): + sot = cache.Cache() + sess = mock.Mock() + sess.put = mock.Mock() + sess.default_microversion = '2.14' + + sot.queue(sess, image='image_id') + + sess.put.assert_called_with('cache/image_id', + microversion=sess.default_microversion) diff --git a/openstack/tests/unit/image/v2/test_proxy.py b/openstack/tests/unit/image/v2/test_proxy.py index 4a98e2632..b32026a0d 100644 --- a/openstack/tests/unit/image/v2/test_proxy.py +++ b/openstack/tests/unit/image/v2/test_proxy.py @@ -26,6 +26,7 @@ from openstack.image.v2 import metadef_schema as _metadef_schema from openstack.image.v2 import schema as _schema from openstack.image.v2 import service_info as _service_info from openstack.image.v2 import task as _task +from openstack import proxy as proxy_base from openstack.tests.unit.image.v2 import test_image as fake_image from openstack.tests.unit import test_proxy_base @@ -901,3 +902,14 @@ class TestCache(TestImageProxy): self.proxy.cache_delete_image, _cache.Cache, ) + + @mock.patch.object(proxy_base.Proxy, '_get_resource') + def test_image_queue(self, mock_get_resource): + fake_cache = _cache.Cache() + mock_get_resource.return_value = fake_cache + self._verify( + "openstack.image.v2.cache.Cache.queue", + self.proxy.queue_image, + method_args=['image-id'], + expected_args=[self.proxy, 'image-id']) + mock_get_resource.assert_called_once_with(_cache.Cache, None) diff --git a/releasenotes/notes/add-image-cache-support-78477e1686c52e56.yaml b/releasenotes/notes/add-image-cache-support-78477e1686c52e56.yaml new file mode 100644 index 000000000..36dc0fb83 --- /dev/null +++ b/releasenotes/notes/add-image-cache-support-78477e1686c52e56.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Add support for glance Cache API.