diff --git a/local-app/python-tools/gfl-resource-actions/Makefile b/local-app/python-tools/gfl-resource-actions/Makefile index 05025e45..9eb927df 100644 --- a/local-app/python-tools/gfl-resource-actions/Makefile +++ b/local-app/python-tools/gfl-resource-actions/Makefile @@ -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: diff --git a/local-app/python-tools/gfl-resource-actions/aws_utils.py b/local-app/python-tools/gfl-resource-actions/aws_utils.py index e69de29b..f1b6d899 100644 --- a/local-app/python-tools/gfl-resource-actions/aws_utils.py +++ b/local-app/python-tools/gfl-resource-actions/aws_utils.py @@ -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 [] diff --git a/local-app/python-tools/gfl-resource-actions/discovery/discovery.py b/local-app/python-tools/gfl-resource-actions/discovery.py similarity index 97% rename from local-app/python-tools/gfl-resource-actions/discovery/discovery.py rename to local-app/python-tools/gfl-resource-actions/discovery.py index d4966145..6925a2a4 100644 --- a/local-app/python-tools/gfl-resource-actions/discovery/discovery.py +++ b/local-app/python-tools/gfl-resource-actions/discovery.py @@ -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() diff --git a/local-app/python-tools/gfl-resource-actions/logging_utils/logging_utils.py b/local-app/python-tools/gfl-resource-actions/logging_utils.py similarity index 95% rename from local-app/python-tools/gfl-resource-actions/logging_utils/logging_utils.py rename to local-app/python-tools/gfl-resource-actions/logging_utils.py index 07e0c927..180b7c08 100644 --- a/local-app/python-tools/gfl-resource-actions/logging_utils/logging_utils.py +++ b/local-app/python-tools/gfl-resource-actions/logging_utils.py @@ -3,7 +3,7 @@ """ import logging import sys -from . import config +import config def setup_logging(): """Configure logging for the application.""" diff --git a/local-app/python-tools/gfl-resource-actions/main.py b/local-app/python-tools/gfl-resource-actions/main.py index c2604ba4..f7bb649e 100644 --- a/local-app/python-tools/gfl-resource-actions/main.py +++ b/local-app/python-tools/gfl-resource-actions/main.py @@ -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() diff --git a/local-app/python-tools/gfl-resource-actions/managers/__init__.py b/local-app/python-tools/gfl-resource-actions/managers/__init__.py index f34abf8a..72dde61f 100644 --- a/local-app/python-tools/gfl-resource-actions/managers/__init__.py +++ b/local-app/python-tools/gfl-resource-actions/managers/__init__.py @@ -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 diff --git a/local-app/python-tools/gfl-resource-actions/managers/__pycache__/__init__.cpython-311.pyc b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..507fb427 Binary files /dev/null and b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/__init__.cpython-311.pyc differ diff --git a/local-app/python-tools/gfl-resource-actions/managers/__pycache__/base.cpython-311.pyc b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/base.cpython-311.pyc new file mode 100644 index 00000000..006f7d38 Binary files /dev/null and b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/base.cpython-311.pyc differ diff --git a/local-app/python-tools/gfl-resource-actions/managers/__pycache__/ec2.cpython-311.pyc b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/ec2.cpython-311.pyc new file mode 100644 index 00000000..6f65d8ce Binary files /dev/null and b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/ec2.cpython-311.pyc differ diff --git a/local-app/python-tools/gfl-resource-actions/managers/__pycache__/eks.cpython-311.pyc b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/eks.cpython-311.pyc new file mode 100644 index 00000000..14fb9f00 Binary files /dev/null and b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/eks.cpython-311.pyc differ diff --git a/local-app/python-tools/gfl-resource-actions/managers/__pycache__/emr.cpython-311.pyc b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/emr.cpython-311.pyc new file mode 100644 index 00000000..4fb13a48 Binary files /dev/null and b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/emr.cpython-311.pyc differ diff --git a/local-app/python-tools/gfl-resource-actions/managers/__pycache__/rds.cpython-311.pyc b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/rds.cpython-311.pyc new file mode 100644 index 00000000..7a3a8d4f Binary files /dev/null and b/local-app/python-tools/gfl-resource-actions/managers/__pycache__/rds.cpython-311.pyc differ diff --git a/local-app/python-tools/gfl-resource-actions/managers/base.py b/local-app/python-tools/gfl-resource-actions/managers/base.py index 67453fa2..8d5d756d 100644 --- a/local-app/python-tools/gfl-resource-actions/managers/base.py +++ b/local-app/python-tools/gfl-resource-actions/managers/base.py @@ -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() diff --git a/local-app/python-tools/gfl-resource-actions/managers/ec2.py b/local-app/python-tools/gfl-resource-actions/managers/ec2.py index 6eead1a1..fbf55841 100644 --- a/local-app/python-tools/gfl-resource-actions/managers/ec2.py +++ b/local-app/python-tools/gfl-resource-actions/managers/ec2.py @@ -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() diff --git a/local-app/python-tools/gfl-resource-actions/managers/eks.py b/local-app/python-tools/gfl-resource-actions/managers/eks.py index b48fae57..2e2c9261 100644 --- a/local-app/python-tools/gfl-resource-actions/managers/eks.py +++ b/local-app/python-tools/gfl-resource-actions/managers/eks.py @@ -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() diff --git a/local-app/python-tools/gfl-resource-actions/managers/emr.py b/local-app/python-tools/gfl-resource-actions/managers/emr.py index dd3596c7..02cb8ef5 100644 --- a/local-app/python-tools/gfl-resource-actions/managers/emr.py +++ b/local-app/python-tools/gfl-resource-actions/managers/emr.py @@ -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() diff --git a/local-app/python-tools/gfl-resource-actions/managers/rds.py b/local-app/python-tools/gfl-resource-actions/managers/rds.py index 8f63d772..003ed155 100644 --- a/local-app/python-tools/gfl-resource-actions/managers/rds.py +++ b/local-app/python-tools/gfl-resource-actions/managers/rds.py @@ -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()