Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the bugs in CRO #871

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,20 @@ def __send_ticket_status_alerts(self, tickets: dict, ticket_status: str):
if not self.__es_operations.verify_elastic_index_doc_id(index=self.es_cro_index, doc_id=ticket_id):
if ticket_status == self.REFINEMENT:
ticket_status = 'manager-approved'
source = {'cloud_name': description.get('CloudName'), 'account_name': description.get('AccountName').replace('OPENSHIFT-', ''),
source = {'cloud_name': description.get('CloudName'),
'account_name': description.get('AccountName').replace('OPENSHIFT-', ''),
'region_name': description.get('Region'), 'user': '',
'user_cro': description.get('EmailAddress').split('@')[0], 'user_cost': 0, 'ticket_id': ticket_id, 'ticket_id_state': ticket_status.lower(),
'estimated_cost': description.get('CostEstimation'), 'instances_count': 0, 'monitored_days': 0,
'ticket_opened_date': description.get('TicketOpenedDate').date(), 'duration': description.get('Days'), 'approved_manager': '',
'user_manager': '', 'project': description.get('Project'), 'owner': f'{description.get("FirstName")} {description.get("LastName")}'.upper(), 'total_spots': 0,
'total_ondemand': 0, 'AllocatedBudget': [], 'instances_list': [], 'instance_types_list': []}
'user_cro': description.get('EmailAddress').split('@')[0], 'user_cost': 0,
'ticket_id': ticket_id, 'ticket_id_state': ticket_status.lower(),
'estimated_cost': description.get('CostEstimation'), 'instances_count': 0,
'monitored_days': 0,
'ticket_opened_date': description.get('TicketOpenedDate').date(),
'duration': description.get('Days'), 'approved_manager': '',
'user_manager': '', 'project': description.get('Project'),
'owner': f'{description.get("FirstName")} {description.get("LastName")}'.upper(),
'total_spots': 0,
'total_ondemand': 0, 'AllocatedBudget': [], 'instances_list': [],
'instance_types_list': []}
self.__es_operations.upload_to_elasticsearch(index=self.es_cro_index, data=source, id=ticket_id)
current_date = datetime.now().date()
ticket_opened_date = description.get('TicketOpenedDate').date()
Expand All @@ -112,22 +119,30 @@ def __send_ticket_status_alerts(self, tickets: dict, ticket_status: str):
elif ticket_opened_days >= self.__manager_escalation_days + 2:
to = self.__global_admin_name
extra_message = f"<b>Missing manager approval.<br />The user {user} is waiting for approval for last {ticket_opened_days} days.<br/>Please review the below details and approve/reject"
subject, body = self.__mail_message.cro_request_for_manager_approval(manager=to, request_user=user, cloud_name=self.__cloud_name, ticket_id=ticket_id, description=description, extra_message=extra_message)
subject, body = self.__mail_message.cro_request_for_manager_approval(manager=to,
request_user=user,
cloud_name=self.__cloud_name,
ticket_id=ticket_id,
description=description,
extra_message=extra_message)
else: # alert user if doesn't add tag name
user_tickets.setdefault(user, []).append(f"{ticket_id} : {description.get('Project')}")
if user_tickets:
for user, ticket_ids in user_tickets.items():
active_instances = self.__ec2_operations.get_active_instances(ignore_tag='TicketId', tag_value=user, tag_name='User')
active_instances = self.__ec2_operations.get_active_instances(ignore_tag='TicketId', tag_value=user,
tag_name='User')
if active_instances:
for region, instances_list in active_instances.items():
active_instances_ids = {region: [instance.get('InstanceId') for instance in instances_list]}
to = user
cc = self.__default_admins
subject, body = self.__mail_message.cro_send_user_alert_to_add_tags(user=user, ticket_ids=ticket_ids)
subject, body = self.__mail_message.cro_send_user_alert_to_add_tags(user=user,
ticket_ids=ticket_ids)
with tempfile.NamedTemporaryFile(mode='w', suffix='.json') as filename:
filename.write(json.dumps(active_instances_ids))
filename.flush()
self.__postfix.send_email_postfix(to=to, cc=cc, subject=subject, content=body, mime_type='html', filename=filename.name)
self.__postfix.send_email_postfix(to=to, cc=cc, subject=subject, content=body,
mime_type='html', filename=filename.name)

@logger_time_stamp
def __track_tickets(self):
Expand All @@ -136,7 +151,8 @@ def __track_tickets(self):
:return:
"""
self.__send_ticket_status_alerts(ticket_status=self.NEW, tickets=self.get_tickets(ticket_status=self.NEW))
self.__send_ticket_status_alerts(ticket_status=self.REFINEMENT, tickets=self.get_tickets(ticket_status=self.REFINEMENT))
self.__send_ticket_status_alerts(ticket_status=self.REFINEMENT,
tickets=self.get_tickets(ticket_status=self.REFINEMENT))

def update_budget_tag_to_resources(self, region_name: str, ticket_id: str, updated_budget: int):
"""
Expand Down Expand Up @@ -187,7 +203,8 @@ def update_duration_tag_to_resources(self, region_name: str, ticket_id: str, upd
previous_duration = get_tag_value_by_name(tags=resource.get('Tags'), tag_name=tag_to_be_updated)
updated_duration += int(float(previous_duration))
update_tags_dict = {tag_to_be_updated: str(updated_duration)}
tagging_operations.tag_resources_list(resources_list=resource_arn_list, update_tags_dict=update_tags_dict)
tagging_operations.tag_resources_list(resources_list=resource_arn_list,
update_tags_dict=update_tags_dict)
else:
logger.info('No AWS resources to update the costs')
except Exception as err:
Expand Down Expand Up @@ -244,6 +261,38 @@ def __prepare_athena_query_for_cluster_cost(self, names: list):
"""
return query

def notify_ticket_closed(self, ticket_id: str, region_name: str = ''):
"""
This method deletes the tag of the resources
:param region_name:
:param ticket_id:
:return:
"""
self.delete_tags_to_resource(ticket_id, region_name)

def delete_tags_to_resource(self, ticket_id: str, region_name: str):
"""
This method deletes the TicketId tag of the resources
:param region_name:
:param ticket_id:
:return:
"""
try:
tag_to_be_deleted = ['Duration', 'TicketId', 'EstimatedCost', 'ApprovedManager']
tagging_operations = AWSTaggingOperations(region_name=region_name)
resources_list_to_update = tagging_operations.get_resources_list(tag_name='TicketId', tag_value=ticket_id)
if resources_list_to_update:
resource_arn_list = []
for resource in resources_list_to_update:
resource_arn_list.append(resource.get('ResourceARN'))
tagging_operations.untag_resources_list(resources_list=resource_arn_list,
delete_tag_keys_list=tag_to_be_deleted)
else:
logger.info('No AWS resources tags were deleted')
except Exception as err:
logger.error(err)
raise err

@logger_time_stamp
def run(self):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from abc import ABC

import typeguard

from cloud_governance.cloud_resource_orchestration.clouds.common.abstract_tagging_operations import \
Expand All @@ -10,7 +8,7 @@

class AWSTaggingOperations(AbstractTaggingOperations):
"""
This class is performs the tagging operations on AWS
This class is performing the tagging operations on AWS
"""

def __init__(self, region_name: str):
Expand All @@ -28,6 +26,16 @@ def tag_resources_list(self, resources_list: list, update_tags_dict: dict):
self.__resource_tag_api_operations.tag_resources(resource_arn_list=resources_list,
update_tags_dict=update_tags_dict)

def untag_resources_list(self, resources_list: list, delete_tag_keys_list: list):
"""
This method untags the resources
:param resources_list:
:param delete_tag_keys_list:
:return:
"""
self.__resource_tag_api_operations.untag_resources(resource_arn_list=resources_list,
delete_tags_keys=delete_tag_keys_list)

@typeguard.typechecked
@logger_time_stamp
def get_resources_list(self, tag_name: str, tag_value: str = ''):
Expand Down
Loading