diff --git a/cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py b/cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py index 72abd627..bd85d283 100644 --- a/cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py +++ b/cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py @@ -159,12 +159,13 @@ def __prepare_update_es_data(self, source: dict, instance_data: list, user_cost: for instance in instance_data: instance_meta_data = instance.get('instance_data') if instance.get('cluster_name') and instance.get('cluster_name') not in es_cluster_names: - source.setdefault('cluster_name', []).append(instance.get('cluster_name')) + source.setdefault('cluster_names', []).append(instance.get('cluster_name')) if type(instance_meta_data) is not list: instance_meta_data = [instance.get('instance_data')] for data in instance_meta_data: if data not in es_instance_data: source.setdefault('instance_data', []).append(data) + source['cluster_names'] = list(set(source.get('cluster_names', []))) source['duration'] = int(instance_data[self.ZERO].get('duration')) source['estimated_cost'] = round(cost_estimation, self.DEFAULT_ROUND_DIGITS) source['actual_cost'] = user_cost 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 50064d9c..402239d3 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,