Merge "Adds Support for ``glance cache-clear``"

This commit is contained in:
Zuul 2023-04-11 16:47:25 +00:00 committed by Gerrit Code Review
commit d5aba8a6b1
5 changed files with 45 additions and 2 deletions

View File

@ -89,5 +89,4 @@ Cache Operations
.. autoclass:: openstack.image.v2._proxy.Proxy
:noindex:
:members: cache_delete_image, queue_image, get_image_cache
:members: cache_delete_image, queue_image, get_image_cache, clear_cache

View File

@ -96,7 +96,17 @@ class Proxy(proxy.Proxy):
cache = self._get_resource(_cache.Cache, None)
return cache.queue(self, image_id)
def clear_cache(self, target):
""" Clear all images from cache, queue or both
:param target: Specify which target you want to clear
One of: ``both``(default), ``cache``, ``queue``.
"""
cache = self._get_resource(_cache.Cache, None)
return cache.clear(self, target)
# ====== IMAGES ======
def create_image(
self,
name,

View File

@ -50,3 +50,17 @@ class Cache(resource.Resource):
response = session.put(url, microversion=microversion)
exceptions.raise_from_response(response)
return response
def clear(self, session, target='both'):
"""Clears the cache.
:param session: The session to use for making this request
:param target: Specify which target you want to clear
One of: ``both``(default), ``cache``, ``queue``.
:returns: The server response
"""
headers = {}
if target != "both":
headers = {'x-image-cache-clear-target': target}
response = session.delete(self.base_path, headers=headers)
exceptions.raise_from_response(response)
return response

View File

@ -59,3 +59,12 @@ class TestCache(base.TestCase):
sess.put.assert_called_with('cache/image_id',
microversion=sess.default_microversion)
@mock.patch.object(exceptions, 'raise_from_response', mock.Mock())
def test_clear(self):
sot = cache.Cache(**EXAMPLE)
session = mock.Mock()
session.delete = mock.Mock()
sot.clear(session, 'both')
session.delete.assert_called_with('/cache', headers={})

View File

@ -913,3 +913,14 @@ class TestCache(TestImageProxy):
method_args=['image-id'],
expected_args=[self.proxy, 'image-id'])
mock_get_resource.assert_called_once_with(_cache.Cache, None)
@mock.patch.object(proxy_base.Proxy, '_get_resource')
def test_image_clear_cache(self, mock_get_resource):
fake_cache = _cache.Cache()
mock_get_resource.return_value = fake_cache
self._verify(
"openstack.image.v2.cache.Cache.clear",
self.proxy.clear_cache,
method_args=['both'],
expected_args=[self.proxy, 'both'])
mock_get_resource.assert_called_once_with(_cache.Cache, None)