From 54dcf7fcaa2633643d6918be53538007302d9471 Mon Sep 17 00:00:00 2001 From: Thirumalesh Aaraveti Date: Wed, 27 Sep 2023 20:58:24 +0530 Subject: [PATCH] Updated the cost in the es --- .../common/abstract_monitor_tickets.py | 19 ++++++++++++++----- .../main/environment_variables.py | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cloud_governance/cloud_resource_orchestration/common/abstract_monitor_tickets.py b/cloud_governance/cloud_resource_orchestration/common/abstract_monitor_tickets.py index 50064d9c6..402239d3e 100644 --- a/cloud_governance/cloud_resource_orchestration/common/abstract_monitor_tickets.py +++ b/cloud_governance/cloud_resource_orchestration/common/abstract_monitor_tickets.py @@ -87,11 +87,12 @@ def update_cluster_cost(self): raise NotImplemented("This method is not implemented") @logger_time_stamp - def extend_tickets_budget(self, ticket_id: str, region_name: str): + def extend_tickets_budget(self, ticket_id: str, region_name: str, current_budget: int = 0): """ This method extends the ticket budget if any :param ticket_id: :param region_name: + :param current_budget: :return: """ ticket_extended = False @@ -101,6 +102,9 @@ def extend_tickets_budget(self, ticket_id: str, region_name: str): if string_equal_ignore_case(self.__cloud_name, 'AWS'): self.update_budget_tag_to_resources(region_name=region_name, ticket_id=ticket_id, updated_budget=total_budget_to_extend) + update_data = {'estimated_cost': int(current_budget) + int(total_budget_to_extend)} + self.__es_operations.update_elasticsearch_index(index=self.__es_index_cro, metadata=update_data, + id=ticket_id) for sub_ticket_id in sub_ticket_ids: self.__jira_operations.move_issue_state(ticket_id=sub_ticket_id, state='closed') logger.info(f'Updated the budget of the ticket: {ticket_id}') @@ -111,7 +115,7 @@ def extend_tickets_budget(self, ticket_id: str, region_name: str): @typeguard.typechecked @logger_time_stamp - def extend_ticket_duration(self, ticket_id: str, region_name: str): + def extend_ticket_duration(self, ticket_id: str, region_name: str, current_duration: int = 0): """ This method extends the duration of the ticket if any :param ticket_id: @@ -125,6 +129,9 @@ def extend_ticket_duration(self, ticket_id: str, region_name: str): if string_equal_ignore_case(self.__cloud_name, 'AWS'): self.update_duration_tag_to_resources(region_name=region_name, ticket_id=ticket_id, updated_duration=total_duration_to_extend) + update_data = {'duration': int(current_duration) + int(total_duration_to_extend)} + self.__es_operations.update_elasticsearch_index(index=self.__es_index_cro, metadata=update_data, + id=ticket_id) for sub_ticket_id in sub_ticket_ids: self.__jira_operations.move_issue_state(ticket_id=sub_ticket_id, state='closed') logger.info(f'Updated the Duration of the ticket: {ticket_id}') @@ -160,7 +167,8 @@ def _monitor_ticket_duration(self, ticket_id: str, region_name: str, duration: i remaining_duration = duration - completed_duration subject = body = None if remaining_duration <= FIRST_CRO_ALERT: - ticket_extended = self.extend_ticket_duration(ticket_id=ticket_id, region_name=region_name) + ticket_extended = self.extend_ticket_duration(ticket_id=ticket_id, region_name=region_name, + current_duration=duration) if not ticket_extended: if remaining_duration == FIRST_CRO_ALERT: subject, body = self.__mail_message.cro_monitor_alert_message(user=user, days=FIRST_CRO_ALERT, ticket_id=ticket_id) @@ -192,8 +200,9 @@ def _monitor_ticket_budget(self, ticket_id: str, region_name: str, budget: int, remaining_budget = budget - used_budget threshold_budget = budget - (budget * (self.__ticket_over_usage_limit / 100)) subject = body = None - if threshold_budget >= remaining_budget >= 0: - ticket_extended = self.extend_tickets_budget(ticket_id=ticket_id, region_name=region_name) + if threshold_budget >= remaining_budget: + ticket_extended = self.extend_tickets_budget(ticket_id=ticket_id, region_name=region_name, + current_budget=budget) if not ticket_extended: subject, body = self.__mail_message.cro_monitor_budget_remain_alert(user=user, budget=budget, ticket_id=ticket_id, diff --git a/cloud_governance/main/environment_variables.py b/cloud_governance/main/environment_variables.py index fcbba8f8a..de339ba4f 100644 --- a/cloud_governance/main/environment_variables.py +++ b/cloud_governance/main/environment_variables.py @@ -15,6 +15,7 @@ class EnvironmentVariables: """ def __init__(self): + boto3.setup_default_session(profile_name="psap") self._environment_variables_dict = {} # env files override true ENV. Not best order, but easier to write :/