]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm/binary: add `query_endpoint()` method
authorGuillaume Abrioux <gabrioux@ibm.com>
Thu, 14 Sep 2023 15:32:38 +0000 (15:32 +0000)
committerGuillaume Abrioux <gabrioux@ibm.com>
Thu, 25 Jan 2024 14:53:48 +0000 (14:53 +0000)
This encapsulates the existing code in a new method
`query_endpoint()`.
The idea is to avoid duplicating code if we need to make multiple
calls to the agent endpoint from the `run()` method.

Signed-off-by: Guillaume Abrioux <gabrioux@ibm.com>
(cherry picked from commit 7544406be33a579b3d0c63ee4c78ae91b02dfb0e)

src/cephadm/cephadm.py

index d8af9d3b9185ea20296f0b4b10d416cb6d9fd932..0674377a4162ead69c38867bc701296258e469b8 100755 (executable)
@@ -4893,6 +4893,28 @@ WantedBy=ceph-{fsid}.target
             self.device_enhanced_scan = True
         self.volume_gatherer.update_func(lambda: self._ceph_volume(enhanced=self.device_enhanced_scan))
 
+    def query_endpoint(self,
+                       addr: str = '',
+                       port: str = '',
+                       data: Optional[Union[Dict[str, str], str]] = None,
+                       endpoint: str = '',
+                       ssl_ctx: Optional[Any] = None) -> str:
+        _addr = addr if addr else self.target_ip
+        _port = port if port else self.target_port
+        url = f'https://{_addr}:{_port}{endpoint}'
+
+        try:
+            req = Request(url, data, {'Content-Type': 'application/json'})
+            send_time = time.monotonic()
+            with urlopen(req, context=ssl_ctx) as response:
+                response_str = response.read()
+                response_json = json.loads(response_str)
+                total_request_time = datetime.timedelta(seconds=(time.monotonic() - send_time)).total_seconds()
+                logger.info(f'Received mgr response: "{response_json["result"]}" {total_request_time} seconds after sending request.')
+        except Exception:
+            raise
+        return response_str
+
     def run(self) -> None:
         self.pull_conf_settings()
 
@@ -4949,15 +4971,10 @@ WantedBy=ceph-{fsid}.target
                                'port': self.listener_port})
             data = data.encode('ascii')
 
-            url = f'https://{self.target_ip}:{self.target_port}/data/'
             try:
-                req = Request(url, data, {'Content-Type': 'application/json'})
-                send_time = time.monotonic()
-                with urlopen(req, context=ssl_ctx) as response:
-                    response_str = response.read()
-                    response_json = json.loads(response_str)
-                    total_request_time = datetime.timedelta(seconds=(time.monotonic() - send_time)).total_seconds()
-                    logger.info(f'Received mgr response: "{response_json["result"]}" {total_request_time} seconds after sending request.')
+                self.query_endpoint(data=data,
+                                    endpoint='/data/',
+                                    ssl_ctx=ssl_ctx)
             except Exception as e:
                 logger.error(f'Failed to send metadata to mgr: {e}')