Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
morga471 committed Mar 14, 2025
1 parent 44f166e commit 38d19b4
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 29 deletions.
2 changes: 1 addition & 1 deletion local-app/python-tools/gfl-resource-actions/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ setup: install-dev

# Install development dependencies
install-dev:
pip install boto3
pip install boto3 pytest pytest-mock

# Basic code check - no external tools needed
code-check:
Expand Down
101 changes: 101 additions & 0 deletions local-app/python-tools/gfl-resource-actions/aws_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""
AWS utility functions for resource management.
"""
import boto3
import config
from logging_utils import setup_logging

logger = setup_logging()

def create_boto3_client(service, credentials, region):
"""
Create a boto3 client for a specific service using the provided credentials.
Args:
service (str): AWS service name (e.g., 'ec2', 'rds')
credentials (dict): AWS credentials dict with access key, secret key, and token
region (str): AWS region name
Returns:
boto3.client: Configured boto3 client
"""
return boto3.client(
service,
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'],
region_name=region
)

def get_available_regions():
"""
Get a list of all available AWS regions.
Returns:
list: List of region names
"""
try:
ec2_client = boto3.client('ec2')
response = ec2_client.describe_regions()
return [region['RegionName'] for region in response['Regions']]
except Exception as e:
logger.error(f"Error getting available regions: {e}")
# Return a default list of common regions as fallback
return ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']

def assume_role(account_id):
"""
Assume the OrganizationAccountAccessRole in the specified account.
Args:
account_id (str): AWS account ID to assume role in
Returns:
dict: Credentials dictionary or None if failed
"""
try:
role_arn = f"arn:aws:iam::{account_id}:role/{config.ASSUME_ROLE_NAME}"
sts_client = boto3.client('sts')

response = sts_client.assume_role(
RoleArn=role_arn,
RoleSessionName="ResourceManagementSession"
)

return response['Credentials']
except Exception as e:
logger.error(f"Error assuming role in account {account_id}: {e}")
return None

def get_organization_accounts(exclude_accounts=None):
"""
Get a list of accounts in the AWS organization.
Args:
exclude_accounts (list): List of account IDs to exclude
Returns:
list: List of account dicts with id and name
"""
if exclude_accounts is None:
exclude_accounts = []

try:
org_client = boto3.client('organizations')
accounts = []

# Get all accounts in the organization
paginator = org_client.get_paginator('list_accounts')
for page in paginator.paginate():
for account in page['Accounts']:
# Skip suspended accounts and accounts in exclude list
if account['Status'] == 'ACTIVE' and account['Id'] not in exclude_accounts:
accounts.append({
'id': account['Id'],
'name': account['Name']
})

return accounts
except Exception as e:
logger.error(f"Error getting organization accounts: {e}")
return []
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
Resource discovery functions for AWS resources.
"""
from . import config
from .aws_utils import create_boto3_client
from .logging_utils import setup_logging
import config
from aws_utils import create_boto3_client
from logging_utils import setup_logging

logger = setup_logging()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import logging
import sys
from . import config
import config

def setup_logging():
"""Configure logging for the application."""
Expand Down
10 changes: 5 additions & 5 deletions local-app/python-tools/gfl-resource-actions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"""

import argparse
from . import config
from .logging_utils import setup_logging
from .aws_utils import get_available_regions, assume_role, get_organization_accounts
from .discovery import get_account_resources
from .managers import EC2Manager, RDSManager, EKSManager, EMRManager
import config
from logging_utils import setup_logging
from aws_utils import get_available_regions, assume_role, get_organization_accounts
from discovery import get_account_resources
from managers import EC2Manager, RDSManager, EKSManager, EMRManager

logger = setup_logging()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Resource managers for different AWS resource types.
"""

from .ec2 import EC2Manager
from .rds import RDSManager
from .eks import EKSManager
from .emr import EMRManager
from managers.ec2 import EC2Manager
from managers.rds import RDSManager
from managers.eks import EKSManager
from managers.emr import EMRManager
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions local-app/python-tools/gfl-resource-actions/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Base resource manager class with common functionality for AWS resources.
"""
import datetime
from .. import config
from ..aws_utils import create_boto3_client
from ..logging_utils import setup_logging
import config
from aws_utils import create_boto3_client
from logging_utils import setup_logging

logger = setup_logging()

Expand Down
6 changes: 3 additions & 3 deletions local-app/python-tools/gfl-resource-actions/managers/ec2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
EC2 resource manager class.
"""
from . import base
from .. import config
from ..logging_utils import setup_logging
from managers import base
import config
from logging_utils import setup_logging

logger = setup_logging()

Expand Down
6 changes: 3 additions & 3 deletions local-app/python-tools/gfl-resource-actions/managers/eks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
EKS resource manager class.
"""
from . import base
from .. import config
from ..logging_utils import setup_logging
from managers import base
import config
from logging_utils import setup_logging

logger = setup_logging()

Expand Down
6 changes: 3 additions & 3 deletions local-app/python-tools/gfl-resource-actions/managers/emr.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
EMR resource manager class.
"""
from . import base
from .. import config
from ..logging_utils import setup_logging
from managers import base
import config
from logging_utils import setup_logging

logger = setup_logging()

Expand Down
6 changes: 3 additions & 3 deletions local-app/python-tools/gfl-resource-actions/managers/rds.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
RDS resource manager class.
"""
from . import base
from .. import config
from ..logging_utils import setup_logging
from managers import base
import config
from logging_utils import setup_logging

logger = setup_logging()

Expand Down

0 comments on commit 38d19b4

Please sign in to comment.