Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

CORTX-33159: Codacy Fix #882

Merged
merged 4 commits into from
Aug 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
CORTX-33159: Codacy fix
Signed-off-by: Rohit Kolapkar <rohit.j.kolapkar@seagate.com>
Rohit Kolapkar committed Aug 5, 2022
commit 6fe97d631f7e3fc34e332958ae1ad3bac3984c5b
12 changes: 6 additions & 6 deletions csm/common/cluster.py
Original file line number Diff line number Diff line change
@@ -120,7 +120,7 @@ def __init__(self, inventory_file, ha_framework):
sw_components = self._inventory[node_type][const.KEY_COMPONENTS]
admin_user = self._inventory[node_type][const.ADMIN_USER]
for node_id in self._inventory[node_type][const.KEY_NODES]:
if node_id not in self._node_list.keys():
if node_id not in self._node_list:
node = Node(node_id, node_type, sw_components, admin_user)
self._node_list[node_id] = node

@@ -131,15 +131,15 @@ def init(self, force_flag):
def node_list(self, node_type=None):
"""Get nodes of specified type."""
if node_type is None:
return [self._node_list[x] for x in self._node_list.keys()]
return [self._node_list[x] for x in self._node_list.keys()
return [self._node_list[x] for x in self._node_list]
return [self._node_list[x] for x in self._node_list
if self._node_list[x].node_type() == node_type]

def host_list(self, node_type=None):
"""Get the list of all SSUs in the cluster."""
if node_type is None:
return [self._node_list[x].host_name() for x in self._node_list.keys()]
return [self._node_list[x].host_name() for x in self._node_list.keys()
return [self._node_list[x].host_name() for x in self._node_list]
return [self._node_list[x].host_name() for x in self._node_list
if self._node_list[x].node_type() == node_type]

def sw_components(self, node_type):
@@ -149,7 +149,7 @@ def sw_components(self, node_type):
def active_node_list(self):
"""Get all the active nodes in the cluster."""
# TODO - Scan the list and identify reachable nodes
return [self._node_list[x] for x in self._node_list.keys()
return [self._node_list[x] for x in self._node_list
if self._node_list[x].is_active()]

def state(self):
4 changes: 2 additions & 2 deletions csm/common/conf.py
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ def init():
def load(index, doc, force=False):
if not os.path.isfile(doc):
raise CsmError(-1, f'File {doc} does not exist')
if index in Conf._payloads.keys():
if index in Conf._payloads:
if not force:
raise Exception(f'index {index} is already loaded')
Conf.save(index)
@@ -67,7 +67,7 @@ def delete(index, key):

@staticmethod
def save(index=None):
indexes = [x for x in Conf._payloads.keys()] if index is None else [index]
indexes = [x for x in Conf._payloads] if index is None else [index]
for index in indexes:
Conf._payloads[index].dump()

8 changes: 4 additions & 4 deletions csm/common/fs_utils.py
Original file line number Diff line number Diff line change
@@ -135,21 +135,21 @@ def __init__(self, thread_pool_exec: ThreadPoolExecutor = None,
if thread_pool_exec is None else thread_pool_exec)
self._loop = asyncio.get_event_loop() if loop is None else loop

async def make_archive(self, base_name, format=ArchiveFormats.GZTAR.value,
async def make_archive(self, base_name, _format=ArchiveFormats.GZTAR.value,
root_dir=None, base_dir=None):

def _make_archive(_base_name, _format, _root_dir, _base_dir):
# imported function validates correctness/existence of archive and directories itself
make_archive(base_name=_base_name, format=_format,
root_dir=_root_dir, base_dir=_base_dir)
await self._loop.run_in_executor(self._pool, _make_archive,
base_name, format, root_dir, base_dir)
base_name, _format, root_dir, base_dir)

async def unpack_archive(self, filename, extract_dir=None, format=None):
async def unpack_archive(self, filename, extract_dir=None, _format=None):

def _unpack_archive(_filename, _extract_dir, _format):
# imported function validates correctness/existence of archive and directories itself
unpack_archive(filename=_filename, extract_dir=_extract_dir, format=_format)

await self._loop.run_in_executor(self._pool, _unpack_archive,
filename, extract_dir, format)
filename, extract_dir, _format)
12 changes: 6 additions & 6 deletions csm/common/timeseries.py
Original file line number Diff line number Diff line change
@@ -143,10 +143,10 @@ def init(self):
def _parse(self, nodes, panel, output):
for node in nodes:
if isinstance(node["val"], str):
if node["val"] in self._metric_set.keys():
if node["val"] in self._metric_set:
output = self._parse(node["node"], panel, output)
output = "(" + output + ")." + self._metric_set[node["val"]]
elif node["val"] in self._config_list.keys():
elif node["val"] in self._config_list:
cv = self._config_list[node["val"]]
if cv is None:
raise CsmInternalError('Can not load config parameter "%s"' % node["val"])
@@ -221,7 +221,7 @@ async def get_all_units(self):
if st not in mu:
mu.append(st)
if panel == "throughput":
for sz in self._SIZE_DIV.keys():
for sz in self._SIZE_DIV:
st = (str(panel) + '.' + str(metric.get('name')) + '.' + str(sz))
if st not in mu:
mu.append(st)
@@ -239,7 +239,7 @@ async def _get_metric_list(self, panel, metric_list, unit):
unit_li = unit
if len(metric_list) == 0:
metric_list = list(await self.get_labels(panel))
for i in range(0, len(metric_list)):
for i, _ in enumerate(metric_list):
if metric_list[i] not in aggr_panel:
raise CsmInternalError("Invalid label %s for %s" % (metric_list[i], panel))
if isinstance(unit, list):
@@ -367,7 +367,7 @@ async def _convert_payload(self, res, stats_id, panel, output_format, units):
res_payload['stats'] = panel
if "sheet" in timelion_payload:
data_list = timelion_payload["sheet"][0]["list"]
for i in range(0, len(data_list)):
for i, _ in enumerate(data_list):
datapoint = await self._modify_panel_val(data_list[i]["data"], panel, units[i])
if output_format == "gui":
datapoint = await self._get_list(datapoint)
@@ -398,7 +398,7 @@ async def _modify_throughput(self, datapoint, unit):
Modify throughput with unit
"""
li = []
if unit not in self._SIZE_DIV.keys():
if unit not in self._SIZE_DIV:
raise CsmInternalError("Invalid unit for stats %s" % unit)
unit_val = self._SIZE_DIV[unit]
for point in datapoint:
8 changes: 4 additions & 4 deletions csm/conf/configure.py
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ async def execute(self, command):
services = ["agent"]
else:
services=[services]
if not "agent" in services:
if "agent" not in services:
return Response(output=const.CSM_SETUP_PASS, rc=CSM_OPERATION_SUCESSFUL)
self._prepare_and_validate_confstore_keys()

@@ -142,7 +142,7 @@ def create(self):
def _set_csm_endpoint():
Log.info("Config: Setting CSM endpoint in configuration.")
csm_endpoint = Conf.get(const.CONSUMER_INDEX, const.CSM_AGENT_ENDPOINTS_KEY)
csm_protocol, csm_host, csm_port = ServiceUrls.parse_url(csm_endpoint)
csm_protocol, _, csm_port = ServiceUrls.parse_url(csm_endpoint)
Conf.set(const.CSM_GLOBAL_INDEX, const.AGENT_ENDPOINTS, csm_endpoint)
# Not considering Hostname. Bydefault 0.0.0.0 used
# Conf.set(const.CSM_GLOBAL_INDEX, const.AGENT_HOST, csm_host)
@@ -275,7 +275,7 @@ def _create_perf_stat_topic(mb_admin):
partitions = int(Conf.get(const.CSM_GLOBAL_INDEX,const.MSG_BUS_PERF_STAT_PARTITIONS))
retention_size = int(Conf.get(const.CSM_GLOBAL_INDEX,const.MSG_BUS_PERF_STAT_RETENTION_SIZE))
retention_period = int(Conf.get(const.CSM_GLOBAL_INDEX,const.MSG_BUS_PERF_STAT_RETENTION_PERIOD))
if not message_type in mb_admin.list_message_types():
if message_type not in mb_admin.list_message_types():
Log.info(f"Config: Registering message_type:{message_type}")
mb_admin.register_message_type(message_types=[message_type], partitions=partitions)
mb_admin.set_message_type_expire(message_type,
@@ -291,7 +291,7 @@ def _create_cluster_stop_topic(mb_admin):
partitions = int(Conf.get(const.CSM_GLOBAL_INDEX,const.MSG_BUS_CLUSTER_STOP_PARTITIONS))
retention_size = int(Conf.get(const.CSM_GLOBAL_INDEX,const.MSG_BUS_CLUSTER_STOP_RETENTION_SIZE))
retention_period = int(Conf.get(const.CSM_GLOBAL_INDEX,const.MSG_BUS_CLUSTER_STOP_RETENTION_PERIOD))
if not message_type in mb_admin.list_message_types():
if message_type not in mb_admin.list_message_types():
Log.info(f"Config: Registering message_type:{message_type}")
mb_admin.register_message_type(message_types=[message_type], partitions=partitions)
mb_admin.set_message_type_expire(message_type,
12 changes: 2 additions & 10 deletions csm/conf/csm_setup.py
Original file line number Diff line number Diff line change
@@ -65,23 +65,15 @@ def process(self):
response=response)

if __name__ == '__main__':


sys.path.append(os.path.join(os.path.dirname(pathlib.Path(__file__)), '..', '..'))
sys.path.append(os.path.join(os.path.dirname(pathlib.Path(os.path.realpath(__file__))), '..', '..'))
from cortx.utils.conf_store.conf_store import Conf
from csm.common.payload import Json
from cortx.utils.cli_framework.parser import CommandParser
from csm.core.blogic import const
from cortx.utils.cli_framework.client import CliClient
from csm.conf.post_install import PostInstall
from csm.conf.prepare import Prepare
from csm.conf.configure import Configure
from csm.conf.init import Init
from csm.conf.reset import Reset
from csm.conf.test import Test
from csm.conf.upgrade import Upgrade
from csm.conf.pre_upgrade import PreUpgrade
from csm.conf.post_upgrade import PostUpgrade
from csm.conf.cleanup import Cleanup

try:
csm_setup = CsmSetupCommand(sys.argv)
2 changes: 1 addition & 1 deletion csm/conf/init.py
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ async def execute(self, command):
services = ["agent"]
else:
services=[services]
if not "agent" in services:
if "agent" not in services:
return Response(output=const.CSM_SETUP_PASS, rc=CSM_OPERATION_SUCESSFUL)
Log.info("Setup: Successfully passed Init phase.")
return Response(output=const.CSM_SETUP_PASS, rc=CSM_OPERATION_SUCESSFUL)
2 changes: 1 addition & 1 deletion csm/conf/post_install.py
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ async def execute(self, command):
services = ["agent"]
else:
services=[services]
if not "agent" in services:
if "agent" not in services:
return Response(output=const.CSM_SETUP_PASS, rc=CSM_OPERATION_SUCESSFUL)

self._prepare_and_validate_confstore_keys()
2 changes: 1 addition & 1 deletion csm/conf/prepare.py
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ async def execute(self, command):
services = ["agent"]
else:
services=[services]
if not "agent" in services:
if "agent" not in services:
return Response(output=const.CSM_SETUP_PASS, rc=CSM_OPERATION_SUCESSFUL)

self._prepare_and_validate_confstore_keys()
94 changes: 51 additions & 43 deletions csm/core/agent/api.py
Original file line number Diff line number Diff line change
@@ -36,8 +36,6 @@
from cortx.utils.conf_store.conf_store import Conf
from csm.common.conf import ConfSection, DebugConf
from cortx.utils.log import Log
from cortx.utils.product_features import unsupported_features
from csm.common.payload import Json
from csm.core.blogic import const
from csm.common.errors import (CsmError, CsmNotFoundError, CsmPermissionDenied,
CsmInternalError, InvalidRequest, ResourceExist,
@@ -48,7 +46,6 @@
from csm.core.services.file_transfer import DownloadFileEntity
from csm.core.controllers.view import CsmView, CsmAuth, CsmHttpException
from csm.core.controllers.routes import CsmRoutes
import re
from cortx.utils.errors import DataAccessError
from marshmallow import ValidationError, fields
from csm.core.controllers.validators import ValidateSchema
@@ -85,7 +82,7 @@ def process_request(command, request: Request, callback=None):
Log.info('command=%s action=%s args=%s' % (command,
request.action(),
request.args()))
if command not in CsmApi._providers.keys():
if command not in CsmApi._providers:
CsmApi._providers[command] = ProviderFactory.get_provider(command,
CsmApi._cluster)
provider = CsmApi._providers[command]
@@ -390,8 +387,8 @@ async def set_secure_headers(request, handler):
async def rest_middleware(request, handler):
if CsmRestApi.__is_shutting_down:
return CsmRestApi.json_response("CSM agent is shutting down", status=503)
request.request_id = int(time.time())
try:
request_id = int(time.time())
request_body = dict(request.rel_url.query) if request.rel_url.query else {}
if not request_body and request.content_length and request.content_length > 0:
try:
@@ -433,69 +430,80 @@ async def rest_middleware(request, handler):
# by client to complete task which are await use atomic
except (ConcurrentCancelledError, AsyncioCancelledError):
Log.warn(f"Client cancelled call for {request.method} {request.path}")
return CsmRestApi.json_response(
CsmRestApi.error_response(
resp = CsmRestApi.error_response(
CsmRequestCancelled(desc="Call cancelled by client"),
request=request, request_id=request_id),
status=499)
request=request, request_id=request.request_id)
return CsmRestApi.json_response(resp, status=499)
except CsmHttpException as e:
raise e
except web.HTTPException as e:
Log.error(f'HTTP Exception {e.status}: {e.reason}')
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id),
status=e.status)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=e.status)
except DataAccessError as e:
Log.error(f"Failed to access the database: {e}")
resp = CsmRestApi.error_response(e, request=request, request_id=request_id)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=503)
except InvalidRequest as e:
Log.debug(f"Invalid Request: {e} \n {traceback.format_exc()}")
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=400)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=400)
except CsmNotFoundError as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=404)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=404)
except CsmPermissionDenied as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=403)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=403)
except ResourceExist as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id),
status=const.STATUS_CONFLICT)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=const.STATUS_CONFLICT)
except CsmInternalError as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=500)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=500)
except CsmNotImplemented as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=501)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=501)
except CsmGatewayTimeout as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=504)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=504)
except CsmServiceConflict as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=409)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=409)
except CsmUnauthorizedError as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=401)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=401)
except CsmError as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=400)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=400)
except KeyError as e:
Log.debug(f"Key Error: {e} \n {traceback.format_exc()}")
message = f"Missing Key for {e}"
return CsmRestApi.json_response(
CsmRestApi.error_response(KeyError(message), request=request,
request_id=request_id),
status=422)
resp = CsmRestApi.error_response(KeyError(message), request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=422)
except (ServerDisconnectedError, ClientConnectorError, ClientOSError,
ConcurrentTimeoutError) as e:
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=503)
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=503)
except Exception as e:
Log.critical(f"Unhandled Exception Caught: {e} \n {traceback.format_exc()}")
return CsmRestApi.json_response(
CsmRestApi.error_response(e, request=request, request_id=request_id), status=500)
Log.critical(f"Unhandled Exception Caught: {e} \n"
f"{traceback.format_exc()}")
resp = CsmRestApi.error_response(e, request=request,
request_id=request.request_id)
return CsmRestApi.json_response(resp, status=500)

@staticmethod
async def _shut_down(loop, site, server=None):
Loading