]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
node-proxy: explicitly set NodeProxy's attributes
authorGuillaume Abrioux <gabrioux@ibm.com>
Tue, 19 Dec 2023 09:14:31 +0000 (09:14 +0000)
committerGuillaume Abrioux <gabrioux@ibm.com>
Thu, 25 Jan 2024 16:01:04 +0000 (16:01 +0000)
The current logic using `setattr()` makes mypy complain:

"NodeProxy" has no attribute "xxx"

Using `self.__dict['xxx']` addresses this mypy error but the
downside of this is that the code isn't clear and less readable.

Explicitly setting the different attributes makes the code clearer
and more readable.

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

src/cephadm/cephadmlib/node_proxy/baseredfishsystem.py
src/cephadm/cephadmlib/node_proxy/main.py
src/cephadm/cephadmlib/node_proxy/redfish_client.py
src/cephadm/cephadmlib/node_proxy/reporter.py
src/cephadm/cephadmlib/node_proxy/util.py

index 587974f8db20e9fcb845527d957d76e8637c31c8..6102c1a8044dbdbc56f9bf5d9a980170f9e91496 100644 (file)
@@ -15,7 +15,7 @@ class BaseRedfishSystem(BaseSystem):
         self.chassis_endpoint: str = kw.get('chassis_endpoint', '/Chassis/System.Embedded.1')
         self.log = Logger(__name__)
         self.host: str = kw['host']
-        self.port: int = kw['port']
+        self.port: str = kw['port']
         self.username: str = kw['username']
         self.password: str = kw['password']
         # move the following line (class attribute?)
index 813b3e3edf8439eea7e6c4e72129949b2056c637..968dfd3c1ca66dd7a548df376e7eb68c3e4d87ff 100644 (file)
@@ -24,10 +24,17 @@ DEFAULT_CONFIG = {
 
 
 class NodeProxy(Thread):
-    def __init__(self, **kw: Dict[str, Any]) -> None:
+    def __init__(self, **kw: Any) -> None:
         super().__init__()
-        for k, v in kw.items():
-            setattr(self, k, v)
+        self.username: str = kw.get('username', '')
+        self.password: str = kw.get('password', '')
+        self.host: str = kw.get('host', '')
+        self.port: int = kw.get('port', 443)
+        self.cephx: Dict[str, Any] = kw.get('cephx', {})
+        self.reporter_scheme: str = kw.get('reporter_scheme', 'https')
+        self.mgr_target_ip: str = kw.get('mgr_target_ip', '')
+        self.mgr_target_port: str = kw.get('mgr_target_port', '')
+        self.reporter_endpoint: str = kw.get('reporter_endpoint', '/node-proxy/data')
         self.exc: Optional[Exception] = None
         self.log = Logger(__name__)
 
@@ -45,8 +52,8 @@ class NodeProxy(Thread):
         self.reporter_agent.stop()
 
     def check_auth(self, realm: str, username: str, password: str) -> bool:
-        return self.__dict__['username'] == username and \
-            self.__dict__['password'] == password
+        return self.username == username and \
+            self.password == password
 
     def check_status(self) -> bool:
         if self.__dict__.get('system') and not self.system.run:
@@ -65,10 +72,10 @@ class NodeProxy(Thread):
         # create the redfish system and the obsever
         self.log.logger.info('Server initialization...')
         try:
-            self.system = RedfishDellSystem(host=self.__dict__['host'],
-                                            port=self.__dict__.get('port', 443),
-                                            username=self.__dict__['username'],
-                                            password=self.__dict__['password'],
+            self.system = RedfishDellSystem(host=self.host,
+                                            port=self.port,
+                                            username=self.username,
+                                            password=self.password,
                                             config=self.config)
         except RuntimeError:
             self.log.logger.error("Can't initialize the redfish system.")
@@ -76,11 +83,11 @@ class NodeProxy(Thread):
 
         try:
             self.reporter_agent = Reporter(self.system,
-                                           self.__dict__['cephx'],
-                                           reporter_scheme=self.__dict__.get('reporter_scheme', 'https'),
-                                           reporter_hostname=self.__dict__['mgr_target_ip'],
-                                           reporter_port=self.__dict__['mgr_target_port'],
-                                           reporter_endpoint=self.__dict__.get('reporter_endpoint', '/node-proxy/data'))
+                                           self.cephx,
+                                           reporter_scheme=self.reporter_scheme,
+                                           reporter_hostname=self.mgr_target_ip,
+                                           reporter_port=self.mgr_target_port,
+                                           reporter_endpoint=self.reporter_endpoint)
             self.reporter_agent.run()
         except RuntimeError:
             self.log.logger.error("Can't initialize the reporter.")
index c07e1a6b6f393dc3c9b4ac9fba638e776dda6845..040db8ce2521f99730a5fc3f02af1e36bc2b0976 100644 (file)
@@ -11,14 +11,14 @@ class RedFishClient(BaseClient):
 
     def __init__(self,
                  host: str = '',
-                 port: int = 443,
+                 port: str = '443',
                  username: str = '',
                  password: str = ''):
         super().__init__(host, username, password)
         self.log: Logger = Logger(__name__)
         self.log.logger.info(f'Initializing redfish client {__name__}')
         self.host: str = host
-        self.port: int = port
+        self.port: str = port
         self.url: str = f'https://{self.host}:{self.port}'
         self.token: str = ''
         self.location: str = ''
index 21183c9803575a6242d1d3faa061049610b64ee8..765374483b1f35f4f1a8fed2feb3dfeb42ad753f 100644 (file)
@@ -12,7 +12,7 @@ class Reporter:
                  cephx: Dict[str, Any],
                  reporter_scheme: str = 'https',
                  reporter_hostname: str = '',
-                 reporter_port: int = 443,
+                 reporter_port: str = '443',
                  reporter_endpoint: str = '/node-proxy/data') -> None:
         self.system = system
         self.data: Dict[str, Any] = {}
@@ -21,7 +21,7 @@ class Reporter:
         self.data['cephx'] = self.cephx
         self.reporter_scheme: str = reporter_scheme
         self.reporter_hostname: str = reporter_hostname
-        self.reporter_port: int = reporter_port
+        self.reporter_port: str = reporter_port
         self.reporter_endpoint: str = reporter_endpoint
         self.log = Logger(__name__)
         self.reporter_url: str = (f'{reporter_scheme}:{reporter_hostname}:'
index f154d83daafbc1b4d8f63fafcbedbb03c1256b7a..31c1c00a0a15b14f9c928ec9d149ca148cc9117b 100644 (file)
@@ -104,7 +104,7 @@ def retry(exceptions: Any = Exception, retries: int = 20, delay: int = 1) -> Cal
 
 
 def http_req(hostname: str = '',
-             port: int = 443,
+             port: str = '443',
              method: Optional[str] = None,
              headers: MutableMapping[str, str] = {},
              data: Optional[str] = None,
@@ -122,7 +122,7 @@ def http_req(hostname: str = '',
         else:
             ssl_ctx.verify_mode = ssl.CERT_REQUIRED
 
-    url: str = f'{scheme}://{hostname}:{str(port)}{endpoint}'
+    url: str = f'{scheme}://{hostname}:{port}{endpoint}'
     _data = bytes(data, 'ascii') if data else None
 
     try: