forked from openstack/glance
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a /v2/info/usage API endpoint which exposes to the user their current limits and usage. The discovery API does not (appear to) have existing tests, so this adds a module for that, although only usage tests are added currently. Implements: blueprint quota-api Change-Id: I50c98bac50f815bdb9baae024e77afd388f74554
- Loading branch information
Showing
12 changed files
with
259 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"usage": { | ||
"image_size_total": { | ||
"limit": 1024, | ||
"usage": 256 | ||
}, | ||
"image_count_total": { | ||
"limit": 10, | ||
"usage": 2 | ||
}, | ||
"image_stage_total": { | ||
"limit": 512, | ||
"usage": 0 | ||
}, | ||
"image_count_uploading": { | ||
"limit": 2, | ||
"usage": 0 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2021 Red Hat, Inc. | ||
# All Rights Reserved. | ||
# | ||
# 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 fixtures | ||
|
||
from oslo_utils import units | ||
|
||
from glance.quota import keystone as ks_quota | ||
from glance.tests import functional | ||
from glance.tests.functional.v2.test_images import get_enforcer_class | ||
from glance.tests import utils as test_utils | ||
|
||
|
||
class TestDiscovery(functional.SynchronousAPIBase): | ||
def setUp(self): | ||
super(TestDiscovery, self).setUp() | ||
self.config(use_keystone_limits=True) | ||
|
||
self.enforcer_mock = self.useFixture( | ||
fixtures.MockPatchObject(ks_quota, 'limit')).mock | ||
|
||
def set_limit(self, limits): | ||
self.enforcer_mock.Enforcer = get_enforcer_class(limits) | ||
|
||
def _assert_usage(self, expected): | ||
usage = self.api_get('/v2/info/usage') | ||
usage = usage.json['usage'] | ||
for item in ('count', 'size', 'stage'): | ||
key = 'image_%s_total' % item | ||
self.assertEqual(expected[key], usage[key], | ||
'Mismatch in %s' % key) | ||
self.assertEqual(expected['image_count_uploading'], | ||
usage['image_count_uploading']) | ||
|
||
def test_quota_with_usage(self): | ||
self.set_limit({'image_size_total': 5, | ||
'image_count_total': 10, | ||
'image_stage_total': 15, | ||
'image_count_uploading': 20}) | ||
|
||
self.start_server() | ||
|
||
# Initially we expect no usage, but our limits in place. | ||
expected = { | ||
'image_size_total': {'limit': 5, 'usage': 0}, | ||
'image_count_total': {'limit': 10, 'usage': 0}, | ||
'image_stage_total': {'limit': 15, 'usage': 0}, | ||
'image_count_uploading': {'limit': 20, 'usage': 0}, | ||
} | ||
self._assert_usage(expected) | ||
|
||
# Stage 1MiB and see our total count, uploading count, and | ||
# staging area usage increase. | ||
data = test_utils.FakeData(1 * units.Mi) | ||
image_id = self._create_and_stage(data_iter=data) | ||
expected['image_count_uploading']['usage'] = 1 | ||
expected['image_count_total']['usage'] = 1 | ||
expected['image_stage_total']['usage'] = 1 | ||
self._assert_usage(expected) | ||
|
||
# Doing the import does not change anything (since we are | ||
# synchronous and the task will not have run yet). | ||
self._import_direct(image_id, ['store1']) | ||
self._assert_usage(expected) | ||
|
||
# After the import is complete, our usage of the staging area | ||
# drops to zero, and our consumption of actual store space | ||
# reflects the new active image. | ||
self._wait_for_import(image_id) | ||
expected['image_count_uploading']['usage'] = 0 | ||
expected['image_stage_total']['usage'] = 0 | ||
expected['image_size_total']['usage'] = 1 | ||
self._assert_usage(expected) | ||
|
||
# Upload also yields a new active image and store usage. | ||
data = test_utils.FakeData(1 * units.Mi) | ||
image_id = self._create_and_upload(data_iter=data) | ||
expected['image_count_total']['usage'] = 2 | ||
expected['image_size_total']['usage'] = 2 | ||
self._assert_usage(expected) | ||
|
||
# Deleting an image drops the usage down. | ||
self.api_delete('/v2/images/%s' % image_id) | ||
expected['image_count_total']['usage'] = 1 | ||
expected['image_size_total']['usage'] = 1 | ||
self._assert_usage(expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
releasenotes/notes/added-quota-usage-api-f1914054132f2021.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
features: | ||
- | | ||
This release brings additional functionality to the unified quota | ||
work done in the previous release. A usage API is now available, | ||
which provides a way for users to see their current quota limits | ||
and their active resource usage towards them. For more | ||
information, see the discovery section in the `api-ref | ||
<https://developer.openstack.org/api-ref/image/v2/index.html#image-service-info-discovery>`_. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters