Skip to content

Commit 3633bfb

Browse files
mgr/smb: move prune function to be a staging store method
The prune function was tightly linked to the staging store. Re-implement it as more generic operation on the staging store. Continue shrinking handler.py a bit and preparing to make it simpler to add future SMBResource types. Signed-off-by: John Mulligan <[email protected]>
1 parent ee6b29b commit 3633bfb

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/pybind/mgr/smb/handler.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def apply(
308308
)
309309
with _store_transaction(staging.destination_store):
310310
results = staging.save()
311-
_prune_linked_entries(staging)
311+
staging.prune_linked_entries()
312312
with _store_transaction(staging.destination_store):
313313
self._sync_modified(results)
314314
return results
@@ -676,25 +676,6 @@ def _keyfunc(r: SMBResource) -> int:
676676
return sorted(resource_objs, key=_keyfunc)
677677

678678

679-
def _prune_linked_entries(staging: Staging) -> None:
680-
cids = set(ClusterEntry.ids(staging))
681-
for auth_id in JoinAuthEntry.ids(staging):
682-
join_auth = staging.get_join_auth(auth_id)
683-
if (
684-
join_auth.linked_to_cluster
685-
and join_auth.linked_to_cluster not in cids
686-
):
687-
JoinAuthEntry.from_store(
688-
staging.destination_store, auth_id
689-
).remove()
690-
for ug_id in UsersAndGroupsEntry.ids(staging):
691-
ug = staging.get_users_and_groups(ug_id)
692-
if ug.linked_to_cluster and ug.linked_to_cluster not in cids:
693-
UsersAndGroupsEntry.from_store(
694-
staging.destination_store, ug_id
695-
).remove()
696-
697-
698679
def _generate_share(
699680
share: resources.Share, resolver: PathResolver, cephx_entity: str
700681
) -> Dict[str, Dict[str, str]]:

src/pybind/mgr/smb/staging.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
List,
77
Optional,
88
Set,
9+
Type,
910
)
1011

1112
import functools
@@ -27,6 +28,7 @@
2728
from .internal import (
2829
ClusterEntry,
2930
JoinAuthEntry,
31+
ResourceEntry,
3032
ShareEntry,
3133
UsersAndGroupsEntry,
3234
resource_entry,
@@ -141,6 +143,27 @@ def _save(self, resource: SMBResource) -> Result:
141143
result = Result(resource, success=True, status={'state': state})
142144
return result
143145

146+
def _prune(
147+
self,
148+
cids: Collection[str],
149+
ecls: Type[ResourceEntry],
150+
rcls: Type[SMBResource],
151+
) -> None:
152+
for _id in ecls.ids(self):
153+
assert isinstance(_id, str)
154+
rentry = ecls.from_store_by_key(self.destination_store, _id)
155+
resource = rentry.get_resource_type(rcls)
156+
_linked_to_cluster = getattr(resource, 'linked_to_cluster', None)
157+
if not _linked_to_cluster:
158+
continue
159+
if _linked_to_cluster not in cids:
160+
rentry.remove()
161+
162+
def prune_linked_entries(self) -> None:
163+
cids = set(ClusterEntry.ids(self))
164+
self._prune(cids, JoinAuthEntry, resources.JoinAuth)
165+
self._prune(cids, UsersAndGroupsEntry, resources.UsersAndGroups)
166+
144167

145168
def auth_refs(cluster: resources.Cluster) -> Collection[str]:
146169
"""Return all IDs for join_auth resources referenced by the supplied

0 commit comments

Comments
 (0)