-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from yiskaneto/feat-ticket-123-add-cw-delete-g…
…roup-functions feat: ticket-123 add cw delete group functions
- Loading branch information
Showing
10 changed files
with
217 additions
and
11 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
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() |
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 +1 @@ | ||
__all__ = ['args', 'boto_client_declaration', 'banners'] | ||
__all__ = ['args', 'boto_client_declaration', 'banners', 'exceptions', 'logging_setup'] |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
alarm_exceptions = ["InvalidNextToken"] | ||
common = ["InvalidParameterException", "ServiceUnavailableException", "ResourceNotFoundException", "OperationAbortedException"] | ||
|
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,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 |
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,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') |
This file was deleted.
Oops, something went wrong.
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