Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-133056 / 25.04 / Allow not retrieving user props for datasets #15287

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/middlewared/middlewared/plugins/pool_/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async def get_instance_quick(self, name, options=None):
def _internal_user_props(self):
return TNUserProp.values()

def __transform(self, datasets, retrieve_children, children_filters):
def __transform(self, datasets, retrieve_children, children_filters, retrieve_user_props):
"""
We need to transform the data zfs gives us to make it consistent/user-friendly,
making it match whatever pool.dataset.{create,update} uses as input.
Expand All @@ -138,9 +138,10 @@ def transform(dataset):
if dataset['type'] == 'VOLUME':
dataset['mountpoint'] = None

dataset['user_properties'] = {
k: v for k, v in dataset['properties'].items() if ':' in k and k not in self._internal_user_props()
}
if retrieve_user_props:
dataset['user_properties'] = {
k: v for k, v in dataset['properties'].items() if ':' in k and k not in self._internal_user_props()
}
del dataset['properties']

if all(k in dataset for k in ('encrypted', 'key_loaded')):
Expand Down Expand Up @@ -194,7 +195,9 @@ def query(self, filters, options):
In case only some properties are desired to be retrieved for datasets, consumer should specify
`query-options.extra.properties` which when `null` ( which is the default ) will retrieve all properties
and otherwise a list can be specified like `["type", "used", "available"]` to retrieve selective properties.
If no properties are desired, in that case an empty list should be sent.
If no properties are desired, in that case an empty list should be sent. It should be noted that specifying
empty list will still retrieve user properties. If user properties are not desired, in that case
`query-options.extra.retrieve_user_props` should be set to `false`.
`query-options.extra.snapshots` can be set to retrieve snapshot(s) of dataset in question.
Expand All @@ -220,6 +223,7 @@ def query(self, filters, options):
snapshots = extra.get('snapshots')
snapshots_recursive = extra.get('snapshots_recursive')
snapshots_count = extra.get('snapshots_count')
retrieve_user_props = extra.get('retrieve_user_props', True)
return filter_list(
self.__transform(self.middleware.call_sync(
'zfs.dataset.query', zfsfilters, {
Expand All @@ -230,10 +234,11 @@ def query(self, filters, options):
'snapshots': snapshots,
'snapshots_recursive': snapshots_recursive,
'snapshots_count': snapshots_count,
'snapshots_properties': extra.get('snapshots_properties', [])
'snapshots_properties': extra.get('snapshots_properties', []),
'user_properties': retrieve_user_props,
}
}
), retrieve_children, internal_datasets_filters,
), retrieve_children, internal_datasets_filters, retrieve_user_props,
), filters, options
)

Expand Down
18 changes: 18 additions & 0 deletions tests/api2/test_pool_dataset_user_props.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from middlewared.test.integration.assets.pool import dataset
from middlewared.test.integration.utils import call


@pytest.mark.parametrize('user_props', [True, False])
def test_pool_dataset_query_user_props_true_false(user_props):
with dataset("query_test") as ds:
result = call(
"pool.dataset.query",
[["id", "=", ds]],
{"extra": {"flat": False, "properties": [], "retrieve_user_props": user_props}}
)
if user_props:
assert "user_properties" in result[0], f"'user_properties' not found in result: {result}"
else:
assert "user_properties" not in result[0], f"'user_properties' found in result: {result}"
Loading