-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
139 additions
and
28 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,4 @@ | |
|
|
||
| # Log file name | ||
| LOG_FILE = "aws_resource_management.log" | ||
| ACTION_CSV_FILE = 'resource_actions.csv' | ||
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
91 changes: 69 additions & 22 deletions
91
local-app/python-tools/gfl-resource-actions/managers/base.py
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,55 +1,102 @@ | ||
| """ | ||
| Base resource manager class with common functionality for AWS resources. | ||
| Base resource manager class that provides common functionality. | ||
| """ | ||
| import datetime | ||
| from aws_utils import create_boto3_client_for_account, get_partition_for_region | ||
| import config | ||
| from aws_utils import create_boto3_client, get_partition_for_region | ||
| from logging_utils import setup_logging | ||
|
|
||
| logger = setup_logging() | ||
|
|
||
| class ResourceManager: | ||
| """Base class for AWS resource managers.""" | ||
| """Base class for all resource managers.""" | ||
|
|
||
| def __init__(self, credentials, account_id): | ||
| def __init__(self, credentials, account_id, account_name=None): | ||
| """ | ||
| Initialize resource manager. | ||
| Initialize resource manager with credentials. | ||
| Args: | ||
| credentials: AWS credentials dictionary | ||
| account_id: AWS account ID | ||
| account_id: AWS account ID | ||
| account_name: AWS account name (optional) | ||
| """ | ||
| self.credentials = credentials | ||
| self.account_id = account_id | ||
|
|
||
| self.account_name = account_name or account_id | ||
| self.resource_type = "generic" | ||
|
|
||
| def create_client(self, service, region): | ||
| """Create a boto3 client for the resource service.""" | ||
| return create_boto3_client(service, self.credentials, region) | ||
| """Create a boto3 client for the specified service and region.""" | ||
| if not self.credentials: | ||
| return None | ||
|
|
||
| return boto3.client( | ||
| service, | ||
| region_name=region, | ||
| aws_access_key_id=self.credentials['AccessKeyId'], | ||
| aws_secret_access_key=self.credentials['SecretAccessKey'], | ||
| aws_session_token=self.credentials['SessionToken'] | ||
| ) | ||
|
|
||
| def get_partition_for_region(self, region): | ||
| """Get AWS partition for the specified region.""" | ||
| return get_partition_for_region(region) | ||
|
|
||
| def get_timestamp(self): | ||
| """Get ISO 8601 timestamp.""" | ||
| return datetime.datetime.now().isoformat() | ||
| """Get ISO 8601 timestamp for tagging.""" | ||
| return datetime.datetime.utcnow().isoformat() | ||
|
|
||
| def log_action(self, resource_id, region, action, details="", dry_run=False, existing_schedule=None): | ||
| """Log a resource action to CSV for audit trail.""" | ||
| def should_exclude(self, resource): | ||
| """Check if resource should be excluded based on tags.""" | ||
| tags = resource.get("tags", {}) | ||
| return config.EXCLUSION_TAG in tags | ||
|
|
||
| def log_action(self, resource_id, region, action, resource_name=None, details=None, dry_run=False, existing_schedule=None): | ||
| """ | ||
| Log an action performed on a resource. | ||
| Args: | ||
| resource_id: Resource identifier | ||
| region: AWS region | ||
| action: Action performed (stop, start, etc.) | ||
| resource_name: Optional resource name | ||
| details: Optional additional details | ||
| dry_run: Whether this was a dry run | ||
| existing_schedule: Existing schedule for the resource | ||
| """ | ||
| dry_run_prefix = "[DRY RUN] " if dry_run else "" | ||
|
|
||
| # Build a detailed log message | ||
| resource_desc = f"{resource_id}" | ||
| if resource_name: | ||
| resource_desc = f"{resource_name} ({resource_id})" | ||
|
|
||
| account_desc = f"account {self.account_id}" | ||
| if self.account_name and self.account_name != self.account_id: | ||
| account_desc = f"{self.account_name} ({self.account_id})" | ||
|
|
||
| action_desc = f"{action}ed" if action[-1] != 'e' else f"{action}d" | ||
|
|
||
| message = f"{dry_run_prefix}{self.resource_type.upper()} {resource_desc} {action_desc} in {account_desc} region {region}" | ||
|
|
||
| if details: | ||
| message += f" - {details}" | ||
|
|
||
| # Add schedule information if available | ||
| if existing_schedule: | ||
| message += f" (Schedule: {existing_schedule})" | ||
|
|
||
| log_action_to_csv( | ||
| account_id=self.account_id, | ||
| account_name=self.account_name, | ||
| resource_type=self.resource_type, | ||
| resource_id=resource_id, | ||
| resource_name=resource_id, # Using ID as name for simplicity | ||
| resource_name=resource_name or resource_id, # Use ID as name if not provided | ||
| action=action, | ||
| region=region, | ||
| status="simulated" if dry_run else "success", | ||
| details=details, | ||
| dry_run=dry_run, | ||
| existing_schedule=existing_schedule | ||
| ) | ||
|
|
||
| def should_exclude(self, resource): | ||
| """Check if resource should be excluded based on tags.""" | ||
| return config.EXCLUSION_TAG in resource.get("tags", {}) | ||
|
|
||
| def get_partition_for_region(self, region): | ||
| """Get AWS partition for region.""" | ||
| return get_partition_for_region(region) | ||
| logger.info(message) |
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