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.