Skip to content

Commit

Permalink
Flake8 and sonar changes with zip file updated
Browse files Browse the repository at this point in the history
  • Loading branch information
shashank-shah1719 committed Nov 11, 2024
1 parent f2cd259 commit c275058
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import datetime
import logging
import time
import azure.functions as func
import json
from .sentinel import AzureSentinel
Expand Down Expand Up @@ -124,7 +123,9 @@ def get_activity_data(self, activity_uuids):
logging.error(consts.LOG_FORMAT.format(__method_name, "Error while fetching Activity : {}.".format(err)))
raise ArmisException()

def post_alert_activity_data(self, alerts_data_to_post, activity_uuid_list, offset_to_post, checkpoint_table_object: ExportsTableStore):
def post_alert_activity_data(
self, alerts_data_to_post, activity_uuid_list, offset_to_post, checkpoint_table_object: ExportsTableStore
):
"""Post alert and activity data to respective table in sentinel.
Args:
Expand Down Expand Up @@ -156,8 +157,12 @@ def post_alert_activity_data(self, alerts_data_to_post, activity_uuid_list, offs
)
)
offset_to_post += len(alerts_data_to_post)
logging.info(consts.LOG_FORMAT.format(__method_name, "Saving offset '{}' in checkpoint".format(offset_to_post)))
checkpoint_table_object.merge("armisalertactivity", "alertactivitycheckpoint", {"offset": offset_to_post})
logging.info(
consts.LOG_FORMAT.format(__method_name, "Saving offset '{}' in checkpoint".format(offset_to_post))
)
checkpoint_table_object.merge(
"armisalertactivity", "alertactivitycheckpoint", {"offset": offset_to_post}
)
return offset_to_post
except ArmisException:
raise ArmisException()
Expand Down Expand Up @@ -185,7 +190,9 @@ def process_alerts_data(self, alerts, offset_to_post, checkpoint_table_object: E
activity_uuid_list.extend(activity_uuids)
alerts_data_to_post.append(alert)
else:
offset_to_post = self.post_alert_activity_data(alerts_data_to_post, activity_uuid_list, offset_to_post, checkpoint_table_object)
offset_to_post = self.post_alert_activity_data(
alerts_data_to_post, activity_uuid_list, offset_to_post, checkpoint_table_object
)
alerts_data_to_post = []
activity_uuid_list = []
if len(activity_uuids) < consts.CHUNK_SIZE:
Expand Down Expand Up @@ -216,9 +223,17 @@ def process_alerts_data(self, alerts, offset_to_post, checkpoint_table_object: E
logging.info(consts.LOG_FORMAT.format(__method_name, "Posted Alerts count : 1."))
self.total_alerts_posted += 1
offset_to_post += 1
logging.info(consts.LOG_FORMAT.format(__method_name, "Saving offset '{}' in checkpoint".format(offset_to_post)))
checkpoint_table_object.merge("armisalertactivity", "alertactivitycheckpoint", {"offset": offset_to_post})
self.post_alert_activity_data(alerts_data_to_post, activity_uuid_list, offset_to_post, checkpoint_table_object)
logging.info(
consts.LOG_FORMAT.format(
__method_name, "Saving offset '{}' in checkpoint".format(offset_to_post)
)
)
checkpoint_table_object.merge(
"armisalertactivity", "alertactivitycheckpoint", {"offset": offset_to_post}
)
self.post_alert_activity_data(
alerts_data_to_post, activity_uuid_list, offset_to_post, checkpoint_table_object
)
except ArmisException:
raise ArmisException()

Expand All @@ -230,7 +245,9 @@ def process_alerts_data(self, alerts, offset_to_post, checkpoint_table_object: E
)
raise ArmisException()

def fetch_alert_data(self, alert_parameter, is_checkpoint_not_exist, checkpoint_table_object: ExportsTableStore, last_time=None):
def fetch_alert_data(
self, alert_parameter, is_checkpoint_not_exist, checkpoint_table_object: ExportsTableStore, last_time=None
):
"""Fetch_alert_data is used to push all the data into table.
Args:
Expand Down Expand Up @@ -265,17 +282,13 @@ def fetch_alert_data(self, alert_parameter, is_checkpoint_not_exist, checkpoint_
alert_time = datetime.datetime.strptime(alert_time, "%Y-%m-%dT%H:%M:%S")
alert_time += datetime.timedelta(seconds=1)
alert_time = alert_time.strftime("%Y-%m-%dT%H:%M:%S")
logging.info(consts.LOG_FORMAT.format(__method_name, "Saving offset '0' in checkpoint"))
logging.info(
consts.LOG_FORMAT.format(
__method_name, "Saving offset '0' in checkpoint"
)
consts.LOG_FORMAT.format(__method_name, "Adding last timestamp in checkpoint: {}".format(alert_time))
)
logging.info(
consts.LOG_FORMAT.format(
__method_name, "Adding last timestamp in checkpoint: {}".format(alert_time)
)
checkpoint_table_object.merge(
"armisalertactivity", "alertactivitycheckpoint", {"time": alert_time, "offset": 0}
)
checkpoint_table_object.merge("armisalertactivity", "alertactivitycheckpoint", {"time": alert_time, "offset": 0})
except ArmisException:
raise ArmisException()

Expand All @@ -295,7 +308,9 @@ def check_data_exists_or_not_alert(self):
"fields": ",".join(consts.ALERT_FIELDS),
}
last_time_alerts = self.state_manager_obj.get()
checkpoint_table = ExportsTableStore(connection_string=consts.CONNECTION_STRING, table_name=consts.CHECKPOINT_TABLE_NAME)
checkpoint_table = ExportsTableStore(
connection_string=consts.CONNECTION_STRING, table_name=consts.CHECKPOINT_TABLE_NAME
)

if last_time_alerts is not None:
logging.info(
Expand All @@ -304,13 +319,11 @@ def check_data_exists_or_not_alert(self):
)
)
checkpoint_table.create()
checkpoint_table.merge("armisalertactivity", "alertactivitycheckpoint", {"time": last_time_alerts, "offset": 0})
self.state_manager_obj.delete()
logging.info(
consts.LOG_FORMAT.format(
__method_name, "checkpoint file deleted from fileshare."
)
checkpoint_table.merge(
"armisalertactivity", "alertactivitycheckpoint", {"time": last_time_alerts, "offset": 0}
)
self.state_manager_obj.delete()
logging.info(consts.LOG_FORMAT.format(__method_name, "checkpoint file deleted from fileshare."))
self.fetch_alert_data(
parameter_alert,
False,
Expand All @@ -328,15 +341,20 @@ def check_data_exists_or_not_alert(self):
logging.info(consts.LOG_FORMAT.format(__method_name, "Fetching Entity from checkpoint table"))
last_time_alerts = record.get("time")
self.data_alert_from = record.get("offset") if record.get("offset") else 0
logging.info(consts.LOG_FORMAT.format(
__method_name, "Checkpoint table: Last timestamp: {}, Offset: {}".format(
last_time_alerts, self.data_alert_from
logging.info(
consts.LOG_FORMAT.format(
__method_name,
"Checkpoint table: Last timestamp: {}, Offset: {}".format(
last_time_alerts, self.data_alert_from
),
)
))
)
if last_time_alerts is None:
logging.info(consts.LOG_FORMAT.format(
__method_name, "time value not available in checkpoint table. Setting time as None."
))
logging.info(
consts.LOG_FORMAT.format(
__method_name, "time value not available in checkpoint table. Setting time as None."
)
)
fetch_data_from_scratch = True
self.fetch_alert_data(
parameter_alert,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def post(self, pk: str, rk: str, data: dict = None):
def get(self, pk: str, rk: str):
with TableClient.from_connection_string(self.connection_string, self.table_name) as table_client:
try:
logging.info(
"looking for {} - {} on table {}".format(pk, rk, self.table_name))
logging.info("looking for {} - {} on table {}".format(pk, rk, self.table_name))
return table_client.get_entity(pk, rk)
except ResourceNotFoundError:
return None
Expand Down Expand Up @@ -73,8 +72,7 @@ def batch(self, operations):
return table_client.submit_transaction(operations=operations)

def list_all(self):
table_client = TableClient.from_connection_string(
self.connection_string, self.table_name)
table_client = TableClient.from_connection_string(self.connection_string, self.table_name)
return table_client.list_entities()

def merge(self, pk: str, rk: str, data: dict = None):
Expand All @@ -87,5 +85,3 @@ def merge(self, pk: str, rk: str, data: dict = None):
if data is not None:
entity_template.update(data)
return table_client.upsert_entity(mode=UpdateMode.MERGE, entity=entity_template)


Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This module will help to save file to statemanager."""

from azure.storage.fileshare import ShareClient
from azure.storage.fileshare import ShareFileClient
from azure.core.exceptions import ResourceNotFoundError
Expand Down Expand Up @@ -36,7 +37,7 @@ def get(self):
return self.file_cli.download_file().readall().decode()
except ResourceNotFoundError:
return None

def delete(self):
"""Delete method for deleting the data from Azure Storage.
Expand All @@ -45,4 +46,4 @@ def delete(self):
try:
self.file_cli.delete_file()
except ResourceNotFoundError:
raise ResourceNotFoundError("File not found to be deleted.")
raise ResourceNotFoundError("File not found to be deleted.")
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ class ArmisDataNotFoundException(Exception):
"""ArmisDataNotFoundException class will inherit Exception class."""

pass

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _get_access_token_device(self, armis_link_suffix):
)
else:
raise ArmisException(
"Armis Device Connector: Error while generating the access token. error code: {}. error message: {}.".format(
"Armis Device Connector: Error while generating the access token. Code: {} Message: {}.".format(
response.status_code, response.text
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import logging

from enum import Enum
from azure.data.tables import TableClient, UpdateMode
from azure.core.exceptions import ResourceNotFoundError, ResourceExistsError, HttpResponseError

Expand Down Expand Up @@ -88,5 +86,3 @@ def merge(self, pk: str, rk: str, data: dict = None):
if data is not None:
entity_template.update(data)
return table_client.upsert_entity(mode=UpdateMode.MERGE, entity=entity_template)


Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ def delete(self):
self.file_cli.delete_file()
except ResourceNotFoundError:
raise ResourceNotFoundError("File not found to be deleted.")

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class ArmisDataNotFoundException(Exception):
class ArmisTimeOutException(Exception):
"""ArmisTimeOutException class will inherit Exception class."""

pass
pass

0 comments on commit c275058

Please sign in to comment.