Skip to content

Commit

Permalink
Allow not retrieving user props for datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
Qubad786 committed Jan 1, 2025
1 parent 9ef345f commit efd3651
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
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
28 changes: 28 additions & 0 deletions tests/api2/test_pool_dataset_user_props.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

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


def test_pool_dataset_query_default_user_props():
with dataset("query_test") as ds:
result = call(
"pool.dataset.query",
[["id", "=", ds]],
{"extra": {"flat": True, "properties": []}}
)
assert "user_properties" in result[0], f"'user_properties' not found in result: {result}"


@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}"

0 comments on commit efd3651

Please sign in to comment.