Skip to content

Commit

Permalink
Added the optmize reports policy
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma committed Nov 29, 2023
1 parent c214730 commit 62ac58f
Show file tree
Hide file tree
Showing 16 changed files with 39,928 additions and 27 deletions.
Binary file added .DS_Store
Binary file not shown.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import boto3

from cloud_governance.common.logger.init_logger import logger

# @Todo, This class will be used in the feature releases.
# @Todo, it helps in find the resource data like tags by using search query


class ResourceExplorerOperations:
"""
This class performs the resource explorer operations
"""

def __init__(self):
self.__client = self.__set_client()

def __set_client(self):
view = self.list_views()
region = 'us-east-1'
if view:
region = view.split(':')[3]
return boto3.client('resource-explorer-2', region_name=region)

def __search(self, search_string: str):
try:
response = self.__client.search(QueryString=search_string)
return response.get('Resources', [])
except Exception as err:
logger.error(err)
return []

def find_resource_tags(self, resource_id: str):
search_results = self.__search(search_string=f'"{resource_id}"')
tags = []
for resource in search_results:
if resource_id in resource.get('Arn'):
if resource.get('Properties'):
tags = resource.get('Properties', {})[0].get('Data')
return tags

def list_views(self):
"""
This method returns list the views
:return:
:rtype:
"""
client = boto3.client('resource-explorer-2', region_name='us-east-1')
views = client.list_views()['Views']
if views:
return views[0]
else:
raise Exception("No Resource Explorer view found in Region: us-east-1, create one on Free of Charge")
Empty file.
44 changes: 44 additions & 0 deletions cloud_governance/common/clouds/aws/support/support_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import boto3

from cloud_governance.common.logger.init_logger import logger


class SupportOperations:
"""
This class performs the support operations
"""

def __init__(self):
self.__client = boto3.client('support', region_name='us-east-1')

def get_describe_trusted_advisor_checks(self):
"""
This method returns the trusted advisor check results
:return:
:rtype:
"""
try:
response = self.__client.describe_trusted_advisor_checks(language='en')
return response.get('checks', [])
except Exception as err:
logger.error(err)
return []

def get_trusted_advisor_reports(self):
"""
This method returns the reports of the checks
:return:
:rtype:
"""
result = {}
try:
advisor_checks_list = self.get_describe_trusted_advisor_checks()
for check in advisor_checks_list:
response = self.__client.describe_trusted_advisor_check_result(checkId=check.get('id'))
result.setdefault(check.get('category'), {}).setdefault(check.get('id'), {
'metadata': check,
'reports': response.get('result', [])
})
except Exception as err:
logger.err(err)
return result
28 changes: 28 additions & 0 deletions cloud_governance/common/clouds/aws/utils/common_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def get_tag_value_from_tags(tags: list, tag_name: str, cast_type: str = 'str',
default_value: any = '') -> any:
"""
This method return the tag value inputted by tag_name
:param tags:
:type tags:
:param tag_name:
:type tag_name:
:param cast_type:
:type cast_type:
:param default_value:
:type default_value:
:return:
:rtype:
"""
if tags:
for tag in tags:
key = tag.get('Key').lower().replace("_", '').replace("-", '').strip()
if key == tag_name.lower():
if cast_type:
if cast_type == 'int':
return int(tag.get('Value').split()[0].strip())
elif cast_type == 'float':
return float(tag.get('Value').strip())
else:
return str(tag.get('Value').strip())
return tag.get('Value').strip()
return default_value
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,16 @@ def upload_data_in_bulk(self, data_items: list, index: str, **kwargs):
if kwargs.get('id'):
item['_id'] = item.get(kwargs.get('id'))
if not item.get('timestamp'):
item['timestamp'] = datetime.strptime(item.get('CurrentDate'), "%Y-%m-%d")
if 'CurrentDate' in item:
item['timestamp'] = datetime.strptime(item.get('CurrentDate'), "%Y-%m-%d")
else:
item['timestamp'] = datetime.utcnow()
item['_index'] = index
item['AccountId'] = str(item.get('AccountId'))
item['Policy'] = self.__environment_variables_dict.get('policy')
if item.get('AccountId'):
item['AccountId'] = str(item.get('AccountId'))
if 'account' not in item:
item['account'] = self.__account
item['policy'] = self.__environment_variables_dict.get('policy')
response = bulk(self.__es, bulk_items)
if response:
total_uploaded += len(bulk_items)
Expand Down
2 changes: 1 addition & 1 deletion cloud_governance/main/environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self):
'empty_roles', 'ip_unattached',
'unused_nat_gateway',
'zombie_snapshots', 'skipped_resources',
'monthly_report']
'monthly_report', 'optimize_resources_report']
es_index = 'cloud-governance-policy-es-index'
self._environment_variables_dict['cost_policies'] = ['cost_explorer', 'cost_over_usage', 'cost_billing_reports',
'cost_explorer_payer_billings', 'spot_savings_analysis']
Expand Down
Loading

0 comments on commit 62ac58f

Please sign in to comment.