Skip to content

Commit

Permalink
Merge pull request #10 from yiskaneto/feat-ticket-123-add-cw-delete-g…
Browse files Browse the repository at this point in the history
…roup-functions

feat: ticket-123 add cw delete group functions
  • Loading branch information
yiskaneto authored Jun 16, 2024
2 parents 53e7413 + 660a3e9 commit 35e07cb
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 11 deletions.
6 changes: 3 additions & 3 deletions buckets/delete_bucket_functions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys, time
import sys
from datetime import datetime
import botocore
sys.path.append( '../')
from common.args import bucket_args_call ## This is the only variable we should be importing from the args module and pass it to the functions below
from common.args import bucket_args ## This is the only variable we should be importing from the args module and pass it to the functions below
from common.boto_client_declaration import s3_client
from common.banners import operation_start_msg, outcome_banner
from list_bucket_functions import list_bucket_objects

args, s3 = bucket_args_call, s3_client(bucket_args_call)
args, s3 = bucket_args(), s3_client(bucket_args())

def delete_bucket_objects(args):
"""
Expand Down
90 changes: 90 additions & 0 deletions cloudwatch/delete_cloudwatch_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import sys
from datetime import datetime
import botocore
sys.path.append( '../')
from common.args import cloudwatch_logs_args ## This is the only variable we should be importing from the args module and pass it to the functions below
from common.boto_client_declaration import cloudwatch_logs_client
from common.exceptions import common
from common.banners import operation_start_msg, outcome_banner
from common.logging_setup import logger

args, logs = cloudwatch_logs_args(), cloudwatch_logs_client(cloudwatch_logs_args())

def cloudwatch_delete_log_group(args):
f"""
Calls the necessary functions to delete a given CloudWatch log group.\n
Example
-------
cloudwatch_delete_log_groups(cloudwatch_logs_args_call)
Parameters
------------
args: argparse.Namespace\n
The argparse generated by the ../common/args.py script, the best this is to import the 'cloudwatch_logs_args_call' variable from it.
"""
start_time = datetime.now()
action, resource = "'Delete CloudWatch'", "group"
operation_start_msg(action, args.group_name, resource)
return_code = search_log_groups(args.group_name) ## Objects are remove with this call
if (return_code == 0 and args.dry_run):
logger.info(f"The {args.group_name} log group was found but will not be deleted, to delete it set the --dry-run flag to False")
elif (return_code == 0 and not args.dry_run):
logger.info(f"Proceeding to delete the {args.group_name} log group")
return_code = cloudwatch_delete_log_group_execution(args.group_name)

total_time = datetime.now() - start_time
outcome_banner(action, args.group_name, resource, return_code, total_time)

def search_log_groups(group_name=""):
try:
response = logs.describe_log_groups(
limit=50,
logGroupNamePrefix=group_name
)
# logger.info(response)
if not response["logGroups"]:
logger.error("The log group doesn't exist")
return 1
return 0
except botocore.exceptions.ClientError as error_found:
if error_found.response['Error']['Code'] in common:
logger.error(f"Error Code: {format(error_found.response['Error']['Code'])}")
logger.error(f"Error Code: {format(error_found.response['Error']['Code'])}")
logger.error(f"Message: {format(error_found.response['Error']['Message'])}")
logger.error(f"Request ID: {format(error_found.response['ResponseMetadata']['RequestId'])}")
logger.error(f"Http code: {format(error_found.response['ResponseMetadata']['HTTPStatusCode'])}")
else:
logger.error(f"Error occured : {error_found}")
return 1


def cloudwatch_delete_log_group_execution(group_name=""):
"""
Description
-----------
Deletes CloudWatch log groups.
See more about this operation at:
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs/client/delete_log_group.html
"""
if not group_name:
logger.error("No log group name was provided, exiting")
return 1
try:
logs.delete_log_group(logGroupName=group_name)
return 0
except botocore.exceptions.ClientError as error_found:
if error_found.response['Error']['Code'] in common:
logger.error(f"Error Code: {format(error_found.response['Error']['Code'])}")
logger.error(f"Error Code: {format(error_found.response['Error']['Code'])}")
logger.error(f"Message: {format(error_found.response['Error']['Message'])}")
logger.error(f"Request ID: {format(error_found.response['ResponseMetadata']['RequestId'])}")
logger.error(f"Http code: {format(error_found.response['ResponseMetadata']['HTTPStatusCode'])}")
else:
logger.error(f"Error occured : {error_found}")
return 1

if __name__ == "__main__":
cloudwatch_delete_log_group(args)
# delete_bucket_objects_excecution()
2 changes: 1 addition & 1 deletion common/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ['args', 'boto_client_declaration', 'banners']
__all__ = ['args', 'boto_client_declaration', 'banners', 'exceptions', 'logging_setup']
43 changes: 38 additions & 5 deletions common/args.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import argparse

##########################################################################################################
AWS_REGION_CONST = "Target AWS region where to perform the action"

def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')

## Bucket
###############################################################################
def bucket_args():
"""
Description
-----------
Initializes the arguments needed to excecute operations on a given S3 bucket.
Example
Expand All @@ -12,13 +26,32 @@ def bucket_args():
"""
parser = argparse.ArgumentParser()
parser.add_argument("--bucket", required=True, help="Name of the bucket")
parser.add_argument("--aws_region", required=True, help="aws region where to look for the bucket")
parser.add_argument("--aws-region", required=True, help=AWS_REGION_CONST)
parser.add_argument("--prefix", required=True, help="Buckets Prefix")
args = parser.parse_args()
return args

bucket_args_call = bucket_args()
##########################################################################################################
###############################################################################

## CloudWatch Logs
###############################################################################
def cloudwatch_logs_args():
"""
Description
-----------
Initializes the arguments needed to excecute operations on a given cloudwatch log group.
Example
-------
a_cloudwatch_log_group_function(cloudwatch_logs_args())
"""
parser = argparse.ArgumentParser()
parser.add_argument("--group-name", required=True, help="CloudWatch log group name")
parser.add_argument("--dry-run", type=str2bool, required=True, help="Whether or not to to perfom a dry run, if set to True the log group will not be removed.")
parser.add_argument("--aws-region", required=True, help=AWS_REGION_CONST)
args = parser.parse_args()
return args
#################################################################################

if __name__ == "__main__":
print(bucket_args())
print("Test")
22 changes: 22 additions & 0 deletions common/boto_client_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,29 @@

def s3_client(args):
"""
Description
-----------
Initialize the S3 bucket client for the Python AWS SDK.
"""
s3 = boto3.client('s3', region_name=args.aws_region)
return s3

def cloudwatch_logs_client(args):
"""
Description
-----------
Initialize the cloudwatch logs client for the Python AWS SDK.
Parameters
------------
args: parser.parse_args()
The args object needed to initialize the client object.
Return
-----------
age : boto3.client()
Configured boto3 client.
"""
logs = boto3.client('logs', region_name=args.aws_region)
return logs

3 changes: 3 additions & 0 deletions common/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alarm_exceptions = ["InvalidNextToken"]
common = ["InvalidParameterException", "ServiceUnavailableException", "ResourceNotFoundException", "OperationAbortedException"]

48 changes: 48 additions & 0 deletions common/logging_definition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
version: 1
disable_existing_loggers: True

formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
datefmt: '%Y-%m-%d %H:%M:%S'

handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout

console_info:
class: logging.StreamHandler
level: INFO
formatter: simple
stream: ext://sys.stdout

file:
class: logging.FileHandler
level: INFO
formatter: simple
filename: app.log
mode: a

root:
level: DEBUG
handlers: [console]

loggers:
development:
level: DEBUG
handlers: [console]
propagate: no

staging:
level: INFO
handlers: [console, file]
propagate: no

production:
level: WARNING
handlers: [file]
propagate: no
11 changes: 11 additions & 0 deletions common/logging_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os, logging.config
import yaml

with open(f"{os.getcwd()}/../common/logging_definition.yaml", 'rt') as f:
config = yaml.safe_load(f.read())

logging.config.dictConfig(config)
logger = logging.getLogger("development") # Use one of the defined logger in the logging.yaml file

## You can now emit log meesage based on the selected Logger:
## logger.info('MESSAGE')
1 change: 0 additions & 1 deletion exceptions.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = aws-boto3-sdk-helpers
version = 1.0.4
version = 1.0.5
description = Contains helper functions for the AWS Python sdk (Boto3) package.
long_description = file: README.md, LICENSE
long_description_content_type = text/markdown
Expand Down

0 comments on commit 35e07cb

Please sign in to comment.