]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/smb: reimplement part of the _search_resources function 67615/head
authorJohn Mulligan <jmulligan@redhat.com>
Mon, 2 Mar 2026 21:09:16 +0000 (16:09 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Tue, 3 Mar 2026 15:55:45 +0000 (10:55 -0500)
Reimplement part of the _search_resources function to avoid using yet
another static mapping between the SMBResource type and it's partner
entry type which is one more place you forget to update when you
add a new type. Now, the type mapping is based on the matcher class
and the typ mapping function provided by the internal.py module.

Fixes: 5712016c2133870da3f704d8457358ad06efc87f
Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/pybind/mgr/smb/handler.py

index 35c371652bdc68d10af6404629dfd84ab856b0fa..9feea2df828095d23c58fc7568213f57461ce38e 100644 (file)
@@ -8,6 +8,7 @@ from typing import (
     Optional,
     Set,
     Tuple,
+    Type,
     Union,
     cast,
 )
@@ -33,11 +34,13 @@ from .enums import (
 )
 from .internal import (
     ClusterEntry,
+    CommonResourceEntry,
     ExternalCephClusterEntry,
     JoinAuthEntry,
     ShareEntry,
     TLSCredentialEntry,
     UsersAndGroupsEntry,
+    map_resource_entry,
 )
 from .proto import (
     AccessAuthorizer,
@@ -238,6 +241,9 @@ class _Matcher:
             f'{txt!r} does not match a valid resource type'
         )
 
+    def resources(self) -> List[Type[SMBResource]]:
+        return list(self._match_resources)
+
 
 class ClusterConfigHandler:
     """The central class for ingesting and handling smb configuration change
@@ -382,6 +388,7 @@ class ClusterConfigHandler:
     def _search_resources(self, matcher: _Matcher) -> List[SMBResource]:
         log.debug("performing search with matcher: %s", matcher)
         out: List[SMBResource] = []
+        # clusters and shares (with parent-child relationship)
         if resources.Cluster in matcher or resources.Share in matcher:
             log.debug("searching for clusters and/or shares")
             cluster_shares = self.share_ids_by_cluster()
@@ -395,21 +402,20 @@ class ClusterConfigHandler:
                                 cluster_id, share_id
                             ).get_share()
                         )
-        _resources = (
-            (resources.JoinAuth, JoinAuthEntry),
-            (resources.UsersAndGroups, UsersAndGroupsEntry),
-            (resources.TLSCredential, TLSCredentialEntry),
-        )
-        for rtype, ecls in _resources:
-            if rtype in matcher:
-                log.debug("searching for %s", cast(Any, rtype).resource_type)
-                out.extend(
-                    ecls.from_store(
-                        self.internal_store, rid
-                    ).get_resource_type(rtype)
-                    for rid in ecls.ids(self.internal_store)
-                    if (rtype, rid) in matcher
-                )
+        # other common top-level resources
+        for rtype in matcher.resources():
+            if rtype in (resources.Cluster, resources.Share):
+                continue  # already handled above
+            if rtype not in matcher:
+                continue
+            log.debug("searching for %s", cast(Any, rtype).resource_type)
+            ecls = map_resource_entry(rtype)
+            assert issubclass(ecls, CommonResourceEntry)
+            for rid in ecls.ids(self.internal_store):
+                if (rtype, rid) in matcher:
+                    entry = ecls.from_store(self.internal_store, rid)
+                    res = entry.get_resource_type(rtype)
+                    out.append(cast(SMBResource, res))
         log.debug("search found %d resources", len(out))
         return out