]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr/smb: add results.py for result reporting types
authorJohn Mulligan <jmulligan@redhat.com>
Fri, 2 Feb 2024 00:42:40 +0000 (19:42 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 25 Apr 2024 23:10:39 +0000 (19:10 -0400)
Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/pybind/mgr/smb/results.py [new file with mode: 0644]

diff --git a/src/pybind/mgr/smb/results.py b/src/pybind/mgr/smb/results.py
new file mode 100644 (file)
index 0000000..13d8392
--- /dev/null
@@ -0,0 +1,92 @@
+from typing import Iterator, List, Optional
+
+import errno
+
+from .proto import Simplified, one
+from .resources import SMBResource
+
+_DOMAIN = 'domain'
+
+
+class Result:
+    """Result of applying a single smb resource update to the system."""
+
+    # Compatible with object formatter, thus suitable for being returned
+    # directly to mgr module.
+    def __init__(
+        self,
+        src: SMBResource,
+        success: bool,
+        msg: str = '',
+        status: Optional[Simplified] = None,
+    ) -> None:
+        self.src = src
+        self.success = success
+        self.msg = msg
+        self.status = status
+
+    def to_simplified(self) -> Simplified:
+        ds: Simplified = dict(self.status or {})
+        ds['resource'] = self.src.to_simplified()
+        ds['success'] = self.success
+        if self.msg:
+            ds['msg'] = self.msg
+        return ds
+
+    def mgr_return_value(self) -> int:
+        return 0 if self.success else -errno.EAGAIN
+
+    def mgr_status_value(self) -> str:
+        if self.success:
+            return ""
+        return "resource failed to apply (see response data for details)"
+
+
+class ErrorResult(Result, Exception):
+    """A Result subclass for wrapping an error condition."""
+
+    def __init__(
+        self,
+        src: SMBResource,
+        msg: str = '',
+        status: Optional[Simplified] = None,
+    ) -> None:
+        super().__init__(src, success=False, msg=msg, status=status)
+
+
+class ResultGroup:
+    """Result of applying multiple smb resource updates to the system."""
+
+    # Compatible with object formatter, thus suitable for being returned
+    # directly to mgr module.
+    def __init__(self) -> None:
+        self._contents: List[Result] = []
+
+    def append(self, result: Result) -> None:
+        self._contents.append(result)
+
+    def one(self) -> Result:
+        return one(self._contents)
+
+    def __iter__(self) -> Iterator[Result]:
+        return iter(self._contents)
+
+    @property
+    def success(self) -> bool:
+        return all(r.success for r in self._contents)
+
+    def to_simplified(self) -> Simplified:
+        return {
+            'success': self.success,
+            'results': [r.to_simplified() for r in self._contents],
+        }
+
+    def mgr_return_value(self) -> int:
+        return 0 if self.success else -errno.EAGAIN
+
+    def mgr_status_value(self) -> str:
+        if self.success:
+            return ""
+        ct = sum(0 if r.success else 1 for r in self._contents)
+        s = '' if ct <= 1 else 's'
+        return f"{ct} resource{s} failed to apply (see response data for details)"