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 send agg mail message: #731

Merged
merged 2 commits into from
Feb 26, 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 @@ -38,7 +38,7 @@ def __init__(self, es_host: str = None, es_port: str = None, region: str = '', b
self.__account = self.__environment_variables_dict.get('account')
try:
self.__es = Elasticsearch([{'host': self.__es_host, 'port': self.__es_port}], timeout=self.__timeout, max_retries=2)
except:
except Exception as err:
self.__es = None

def __elasticsearch_get_index_hits(self, index: str, uuid: str = '', workload: str = '', fast_check: bool = False,
Expand Down Expand Up @@ -343,3 +343,25 @@ def check_elastic_search_connection(self):
if self.__es:
return self.__es.ping()
return False

def post_query(self, query: dict, es_index: str, result_agg: bool = False):
"""
This method returns the es data
:param result_agg:
:type result_agg:
:param query:
:type query:
:param es_index:
:type es_index:
:return:
:rtype:
"""
try:
response = self.__es.search(index=es_index, body=query)
if result_agg:
return response.get('aggregations')
else:
return response.get('hits', {}).get('hits', {})
except Exception as err:
logger.error(err)
raise err
16 changes: 16 additions & 0 deletions cloud_governance/common/mails/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,19 @@ def cro_monitor_budget_remain_high_alert(self, ticket_id: str, budget: int, user
'footer': self.FOOTER}
body = template_loader.render(context)
return subject, body

def get_policy_alert_message(self, policy_data: list, user: str = ''):
"""
This method returns the policy alert message
:return:
:rtype:
"""
if user:
display_name = self.get_user_ldap_details(user_name=user)
user = display_name if display_name else user
subject = f"Cloud Governance: {self.account} Policy Alerts"
template_loader = self.env_loader.get_template('policy_alert_agg_message.j2')
columns = ['User', 'PublicCloud', 'policy', 'RegionName', 'ResourceId', 'Name', 'DeleteDate']
context = {'records': policy_data, 'columns': columns, 'User': user, 'account': self.account, 'cloud_name': self.__public_cloud_name}
body = template_loader.render(context)
return subject, body
9 changes: 7 additions & 2 deletions cloud_governance/common/mails/postfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from cloud_governance.common.clouds.aws.s3.s3_operations import S3Operations
from cloud_governance.common.elasticsearch.elasticsearch_operations import ElasticSearchOperations
from cloud_governance.common.ldap.ldap_search import LdapSearch
from cloud_governance.common.logger.init_logger import logger

# https://github.com/redhat-performance/quads/blob/master/quads/tools/postman.py
Expand All @@ -28,6 +29,8 @@ class Postfix:

def __init__(self):
self.__environment_variables_dict = environment_variables.environment_variables_dict
self.__LDAP_HOST_NAME = self.__environment_variables_dict.get('LDAP_HOST_NAME')
self.__ldap_search = LdapSearch(ldap_host_name=self.__LDAP_HOST_NAME)
self.reply_to = self.__environment_variables_dict.get('REPLY_TO', '[email protected]')
self.__es_host = self.__environment_variables_dict.get('es_host', '')
self.__policy = self.__environment_variables_dict.get('policy', '')
Expand All @@ -36,7 +39,7 @@ def __init__(self):
self.__policy_output = self.__environment_variables_dict.get('policy_output', '')
self.__default_admins = self.__environment_variables_dict.get('DEFAULT_ADMINS')
self.__email_alert = self.__environment_variables_dict.get('EMAIL_ALERT')
self.__mail_to = self.__environment_variables_dict.get('EMAIL_TO') # testing purposes
self.__mail_to = self.__environment_variables_dict.get('EMAIL_TO')
self.__mail_cc = self.__environment_variables_dict.get('EMAIL_CC')
self.bucket_name, self.key = self.get_bucket_name()
self.__es_index = 'cloud-governance-mail-messages'
Expand All @@ -62,6 +65,8 @@ def send_email_postfix(self, subject: str, to: any, cc: list, content: str, **kw
to = self.__mail_to
if self.__mail_cc:
cc = self.__mail_cc
if not self.__ldap_search.get_user_details(user_name=to):
cc.append('[email protected]')
cc = [cc_user for cc_user in cc if to and to not in cc_user]
cc = [cc_user if '@redhat.com' in cc_user else f'{cc_user}@redhat.com' for cc_user in cc]
msg = MIMEMultipart('alternative')
Expand Down Expand Up @@ -115,7 +120,7 @@ def send_email_postfix(self, subject: str, to: any, cc: list, content: str, **kw
if kwargs.get('extra_purse'):
data['extra_purse'] = round(kwargs['extra_purse'], 3)
if self.__es_host:
self.__es_operations.upload_to_elasticsearch(data=data, index=self.__es_index)
# self.__es_operations.upload_to_elasticsearch(data=data, index=self.__es_index)
logger.warn(f'Uploaded to es index: {self.__es_index}')
else:
logger.warn('Error missing the es_host')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<html>
<head>
<style>
header{
margin-bottom: 10px;
}
footer{
margin-top: 10px;
color: gray;
}
.table-hover {
font-family: Verdana, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}

.table-hover td, .table-hover th {
border: 2px solid #000;
padding: 8px;
align-items: baseline;
color: black;
}

.table-hover th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #04AA6D;
color: white;
}
.bgcolor-red{
color: red;
}
</style>
</head>
<body>
<header>
Hi {{ User }},
</header>
<section>
<div style="margin-bottom: 10px">
<p>You can find below your unused resources in the {{ cloud_name }} account ({{ account }}).</p>
<p>If you want to keep them, please add "Policy=Not_Delete" or "Policy=skip" tag for each resource</p>
</div>
<table class="table-hover">
<thead>
<tr>
{% for col in columns %}
<th>{{ col | title }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for record in records %}
<tr>
{% for col in columns %}
{% if col == 'Action' %}
{% if record['ResourceDelete'] == "True" or record['ResourceStopped'] == "True" %}
<td class="bgcolor-red">{{ "Deleted" }}</td>
{% else %}
<td>{{ "Alert" }}</td>
{% endif %}
{% else %}
{% if col == 'PublicCloud' %}
<td>{{ record[col] or 'AWS' }}</td>
{% elif col == 'RegionName' %}
<td>{{ record[col] or record['region_name'] }}</td>
{% elif 'kubernetes.io/cluster' in record[col] %}
<td>{{ record[col].split('/')[-1] }}</td>
{% else %}
<td>{{ record[col] or 'NA' }}</td>
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</section>
<footer>
Thanks, <br />Cloud GovernanceTeam
</footer>
</body>
</html>
1 change: 1 addition & 0 deletions cloud_governance/main/environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def __init__(self):
self._environment_variables_dict['DEFAULT_ADMINS'] = literal_eval(EnvironmentVariables.get_env('DEFAULT_ADMINS', '[]'))
self._environment_variables_dict['KERBEROS_USERS'] = literal_eval(EnvironmentVariables.get_env('KERBEROS_USERS', '[]'))
self._environment_variables_dict['POLICIES_TO_ALERT'] = literal_eval(EnvironmentVariables.get_env('POLICIES_TO_ALERT', '[]'))
self._environment_variables_dict['ADMIN_MAIL_LIST'] = EnvironmentVariables.get_env('ADMIN_MAIL_LIST', '')
if self._environment_variables_dict.get('policy') in ['send_aggregated_alerts']:
self._environment_variables_dict['COMMON_POLICIES'] = True
# CRO -- Cloud Resource Orch
Expand Down
Loading
Loading