From 9e835496dc2e87d1137749e6420852e41735bf4f Mon Sep 17 00:00:00 2001 From: Kiran Pawar Date: Thu, 31 Aug 2023 08:33:54 +0000 Subject: [PATCH] Fix count in the response of shares/snapshots list API Share/Snapshot list API returns count of shares/snapshotsi along-with list of resource objects. The list returned by API contains correct entries of shares/snapshots but it returns the wrong count as it ShareInstances/SnapshotInstances. So filter queries by unique share_id/snapshot_id. Closes-bug: #2033604 Change-Id: I13c415767b0d126563f9553df415de564b3901d6 --- manila/db/sqlalchemy/api.py | 4 ++-- manila/tests/db/sqlalchemy/test_api.py | 9 +++++++-- ...n-shares-and-snapshots-list-api-683f3103e587b898.yaml | 8 ++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/bug-2033604-fix-count-in-shares-and-snapshots-list-api-683f3103e587b898.yaml diff --git a/manila/db/sqlalchemy/api.py b/manila/db/sqlalchemy/api.py index b1e9036a2e..2e7e5932c9 100644 --- a/manila/db/sqlalchemy/api.py +++ b/manila/db/sqlalchemy/api.py @@ -2359,7 +2359,7 @@ def _share_get_all_with_filters(context, project_id=None, share_server_id=None, # NOTE(carloss): Count must be calculated before limit and offset are # applied into the query. if show_count: - count = query.count() + count = query.order_by(models.Share.id).distinct().count() if 'limit' in filters: offset = filters.get('offset', 0) @@ -3445,7 +3445,7 @@ def _share_snapshot_get_all_with_filters(context, project_id=None, count = None if show_count: - count = query.count() + count = query.order_by(models.ShareSnapshot.id).distinct().count() if limit is not None: query = query.limit(limit) diff --git a/manila/tests/db/sqlalchemy/test_api.py b/manila/tests/db/sqlalchemy/test_api.py index 3c174bcb8e..750e13ed5a 100644 --- a/manila/tests/db/sqlalchemy/test_api.py +++ b/manila/tests/db/sqlalchemy/test_api.py @@ -685,8 +685,13 @@ class ShareDatabaseAPITestCase(test.TestCase): @ddt.unpack def test_share_get_all_with_count(self, filters, amount_of_shares, expected_shares_len): - [db_utils.create_share(display_name='fake_name_%s' % str(i)) - for i in range(amount_of_shares)] + c_shares = [ + db_utils.create_share(display_name='fake_name_%s' % str(i)) + for i in range(amount_of_shares)] + + # create one more share instance + db_utils.create_share_instance(share_id=c_shares[0]['id']) + db_utils.create_share_instance(share_id=c_shares[1]['id']) count, shares = db_api.share_get_all_with_count( self.ctxt, filters=filters) diff --git a/releasenotes/notes/bug-2033604-fix-count-in-shares-and-snapshots-list-api-683f3103e587b898.yaml b/releasenotes/notes/bug-2033604-fix-count-in-shares-and-snapshots-list-api-683f3103e587b898.yaml new file mode 100644 index 0000000000..0b6972227b --- /dev/null +++ b/releasenotes/notes/bug-2033604-fix-count-in-shares-and-snapshots-list-api-683f3103e587b898.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + The 'count' returned by shares and snapshots list API is fixed to provide + correct value i.e. count of shares/snapshots instead of + shareInstances/shareSnapshotInstances respectively. Please refer to the + `Launchpad bug #2033604 `_ + for more details.