From c0e8027e8374732037bba827631001c126ed0582 Mon Sep 17 00:00:00 2001 From: Mridula Joshi Date: Tue, 10 Jan 2023 12:21:26 +0000 Subject: [PATCH] Add support for glance cache Change-Id: I539807d329b9529580b298cc96172c3a120950e1 --- openstack/image/v2/_proxy.py | 6 +++ openstack/image/v2/cache.py | 33 ++++++++++++++ openstack/tests/unit/image/v2/test_cache.py | 45 +++++++++++++++++++ openstack/tests/unit/image/v2/test_proxy.py | 13 ++++++ ...-image-cache-support-3f8c13550a84d749.yaml | 4 ++ 5 files changed, 101 insertions(+) create mode 100644 openstack/image/v2/cache.py create mode 100644 openstack/tests/unit/image/v2/test_cache.py create mode 100644 releasenotes/notes/add-image-cache-support-3f8c13550a84d749.yaml diff --git a/openstack/image/v2/_proxy.py b/openstack/image/v2/_proxy.py index a43abe999..0ea8fefd8 100644 --- a/openstack/image/v2/_proxy.py +++ b/openstack/image/v2/_proxy.py @@ -15,6 +15,7 @@ import time import warnings from openstack import exceptions +from openstack.image.v2 import cache as _cache from openstack.image.v2 import image as _image from openstack.image.v2 import member as _member from openstack.image.v2 import metadef_namespace as _metadef_namespace @@ -50,6 +51,7 @@ def _get_name_and_filename(name, image_format): class Proxy(proxy.Proxy): _resource_registry = { + "cache": _cache.Cache, "image": _image.Image, "image_member": _member.Member, "metadef_namespace": _metadef_namespace.MetadefNamespace, @@ -72,6 +74,10 @@ class Proxy(proxy.Proxy): _SHADE_IMAGE_SHA256_KEY = 'owner_specified.shade.sha256' _SHADE_IMAGE_OBJECT_KEY = 'owner_specified.shade.object' + # ====== CACHE MANAGEMENT====== + def get_image_cache(self): + return self._get(_cache.Cache, requires_id=False) + # ====== IMAGES ====== def create_image( self, diff --git a/openstack/image/v2/cache.py b/openstack/image/v2/cache.py new file mode 100644 index 000000000..fdc8630e1 --- /dev/null +++ b/openstack/image/v2/cache.py @@ -0,0 +1,33 @@ +# 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. + +from openstack import resource + + +class CachedImage(resource.Resource): + image_id = resource.Body('image_id') + hits = resource.Body('hits') + last_accessed = resource.Body('last_accessed') + last_modified = resource.Body('last_modified') + size = resource.Body('size') + + +class Cache(resource.Resource): + base_path = '/cache' + + allow_fetch = True + + _max_microversion = '2.14' + + cached_images = resource.Body('cached_images', type=list, + list_type=CachedImage) + queued_images = resource.Body('queued_images', type=list) diff --git a/openstack/tests/unit/image/v2/test_cache.py b/openstack/tests/unit/image/v2/test_cache.py new file mode 100644 index 000000000..70c41294f --- /dev/null +++ b/openstack/tests/unit/image/v2/test_cache.py @@ -0,0 +1,45 @@ +# 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. + +from openstack.image.v2 import cache +from openstack.tests.unit import base + + +EXAMPLE = { + 'cached_images': [ + {'hits': 0, + 'image_id': '1a56983c-f71f-490b-a7ac-6b321a18935a', + 'last_accessed': 1671699579.444378, + 'last_modified': 1671699579.444378, + 'size': 0}, + ], + 'queued_images': [ + '3a4560a1-e585-443e-9b39-553b46ec92d1', + '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810' + ] +} + + +class TestCache(base.TestCase): + def test_basic(self): + sot = cache.Cache() + self.assertIsNone(sot.resource_key) + self.assertEqual('/cache', sot.base_path) + self.assertTrue(sot.allow_fetch) + + def test_make_it(self): + sot = cache.Cache(**EXAMPLE) + self.assertEqual( + [cache.CachedImage(**e) for e in EXAMPLE['cached_images']], + sot.cached_images, + ) + self.assertEqual(EXAMPLE['queued_images'], sot.queued_images) diff --git a/openstack/tests/unit/image/v2/test_proxy.py b/openstack/tests/unit/image/v2/test_proxy.py index 047eba04c..491e702fb 100644 --- a/openstack/tests/unit/image/v2/test_proxy.py +++ b/openstack/tests/unit/image/v2/test_proxy.py @@ -17,6 +17,7 @@ import requests from openstack import exceptions from openstack.image.v2 import _proxy +from openstack.image.v2 import cache as _cache from openstack.image.v2 import image as _image from openstack.image.v2 import member as _member from openstack.image.v2 import metadef_namespace as _metadef_namespace @@ -882,3 +883,15 @@ class TestMetadefSchema(TestImageProxy): 'requires_id': False, }, ) + + +class TestCache(TestImageProxy): + def test_image_cache_get(self): + self._verify( + "openstack.proxy.Proxy._get", + self.proxy.get_image_cache, + expected_args=[_cache.Cache], + expected_kwargs={ + 'requires_id': False + }, + ) diff --git a/releasenotes/notes/add-image-cache-support-3f8c13550a84d749.yaml b/releasenotes/notes/add-image-cache-support-3f8c13550a84d749.yaml new file mode 100644 index 000000000..36dc0fb83 --- /dev/null +++ b/releasenotes/notes/add-image-cache-support-3f8c13550a84d749.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Add support for glance Cache API.