-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the database_idle policy (#780)
- Loading branch information
Showing
18 changed files
with
336 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from cloud_governance.common.clouds.aws.utils.common_methods import get_boto3_client | ||
from cloud_governance.common.clouds.aws.utils.utils import Utils | ||
from cloud_governance.common.logger.init_logger import logger | ||
|
||
|
||
class RDSOperations: | ||
""" | ||
This class performs the RDS operations | ||
""" | ||
|
||
def __init__(self, region_name: str): | ||
self._db_client = get_boto3_client('rds', region_name=region_name) | ||
|
||
def describe_db_instances(self, **kwargs): | ||
""" | ||
This method returns the rds databases | ||
:return: | ||
:rtype: | ||
""" | ||
rds_instances = [] | ||
try: | ||
rds_instances = Utils.iter_client_function(func_name=self._db_client.describe_db_instances, | ||
output_tag='DBInstances', | ||
iter_tag_name='Marker', **kwargs) | ||
except Exception as err: | ||
logger.error(f"Can't describe the rds instances: {err}") | ||
return rds_instances | ||
|
||
def add_tags_to_resource(self, resource_arn: str, tags: list): | ||
""" | ||
This method add/ update the tags to the database | ||
:param resource_arn: | ||
:type resource_arn: | ||
:param tags: | ||
:type tags: | ||
:return: | ||
:rtype: | ||
""" | ||
try: | ||
self._db_client.add_tags_to_resource(ResourceName=resource_arn, Tags=tags) | ||
logger.info(f"Tags are updated to the resource: {resource_arn}") | ||
except Exception as err: | ||
logger.error(f"Something went wrong in add/ update tags: {err}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import argparse | ||
import os | ||
|
||
import tempfile | ||
from ast import literal_eval | ||
|
||
import boto3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from cloud_governance.common.utils.configs import CLOUDWATCH_METRICS_AVAILABLE_DAYS | ||
from cloud_governance.common.utils.utils import Utils | ||
from cloud_governance.policy.helpers.aws.aws_policy_operations import AWSPolicyOperations | ||
|
||
|
||
class DatabaseIdle(AWSPolicyOperations): | ||
""" | ||
This class performs the idle database operations | ||
""" | ||
|
||
RESOURCE_ACTION = 'Delete' | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def run_policy_operations(self): | ||
""" | ||
This method returns the idle databases | ||
:return: | ||
:rtype: | ||
""" | ||
idle_dbs = [] | ||
dbs = self._rds_operations.describe_db_instances() | ||
for db in dbs: | ||
resource_id = db.get('DBInstanceIdentifier') | ||
create_date = db.get('InstanceCreateTime') | ||
tags = db.get('TagList', []) | ||
cluster_tag = self._get_cluster_tag(tags=tags) | ||
cleanup_result = False | ||
running_days = self.calculate_days(create_date=create_date) | ||
cleanup_days = 0 | ||
resource_arn = db.get('DBInstanceArn', '') | ||
if Utils.greater_than(val1=running_days, val2=CLOUDWATCH_METRICS_AVAILABLE_DAYS) \ | ||
and not cluster_tag \ | ||
and self.is_database_idle(resource_id): | ||
cleanup_days = self.get_clean_up_days_count(tags=tags) | ||
cleanup_result = self.verify_and_delete_resource(resource_id=resource_id, tags=tags, | ||
clean_up_days=cleanup_days) | ||
unit_price = self._resource_pricing.get_rds_price(region_name=self._region, | ||
instance_type=db.get('DBInstanceClass')) | ||
resource_data = self._get_es_schema(resource_id=resource_id, | ||
user=self.get_tag_name_from_tags(tags=tags, tag_name='User'), | ||
skip_policy=self.get_skip_policy_value(tags=tags), | ||
cleanup_days=cleanup_days, dry_run=self._dry_run, | ||
name=db.get('DBName'), | ||
region=self._region, | ||
cleanup_result=str(cleanup_result), | ||
resource_action=self.RESOURCE_ACTION, | ||
cloud_name=self._cloud_name, | ||
launch_time=str(create_date), | ||
resource_type=db.get('DBInstanceClass'), | ||
unit_price=unit_price, | ||
resource_state=db.get('DBInstanceStatus') | ||
if not cleanup_result else "Deleted" | ||
) | ||
idle_dbs.append(resource_data) | ||
if not cleanup_result: | ||
self.update_resource_day_count_tag(resource_id=resource_arn, cleanup_days=cleanup_days, tags=tags) | ||
|
||
return idle_dbs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.