Skip to content

Commit

Permalink
make functional again
Browse files Browse the repository at this point in the history
  • Loading branch information
morga471 committed Sep 30, 2025
1 parent b8d72ce commit c26d2bf
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,90 @@
"""
AWS Resource Management package.
AWS Resource Management Tool
A Python-based tool for discovering, starting, stopping, and managing AWS resources
across multiple accounts, primarily designed for GovCloud environments to help
with cost management.
"""

# Re-export key modules from consolidated utility modules
from aws_resource_management.utils.logging_utils import (
setup_logging,
__version__ = "1.0.0"
__author__ = "AWS Resource Management Team"

# Import core components and expose them at the package level
from aws_resource_management.core import ResourceManager
from aws_resource_management.resource_registry import get_registry
from aws_resource_management.discovery import ResourceDiscovery, get_account_resources

# Import utility functions from the new consolidated utils package
from aws_resource_management.utils import (
# AWS Core utilities
get_credentials,
get_session_for_account,
get_account_list,
get_organization_accounts,

# Logging utilities
logger,
log_operation,
configure_logging,

# File utilities
log_action_to_csv,
write_csv_row,

# API utilities
paginate_aws_response,
format_tags,

# Region utilities
# Note: These would be imported from region_utils, but we don't have that file's content
)

# Import the managers package for resource-specific implementations
from aws_resource_management.managers import (
ResourceManager as BaseResourceManager,
EC2Manager,
RDSManager,
)

# Set up a default logger
logger = setup_logging()
# Try importing optional managers
try:
from aws_resource_management.managers import EKSManager
except ImportError:
EKSManager = None

try:
from aws_resource_management.managers import EMRManager
except ImportError:
EMRManager = None

try:
from aws_resource_management.managers import ECRManager
except ImportError:
ECRManager = None

# Get the resource registry instance
registry = get_registry()

# Export all important components
__all__ = [
"ResourceManager",
"ResourceDiscovery",
"get_account_resources",
"get_registry",
"registry",
"BaseResourceManager",
"EC2Manager",
"RDSManager",
"get_credentials",
"get_session_for_account",
"logger",
"configure_logging",
]

__version__ = "0.1.0"
# Conditionally add optional managers to __all__
if EKSManager:
__all__.append("EKSManager")
if EMRManager:
__all__.append("EMRManager")
if ECRManager:
__all__.append("ECRManager")
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _pre_validate_accounts(
for account in accounts:
account_id = account.get("account_id")
try:
credentials = get_credentials(account_id, self)
credentials = get_credentials(account_id, self.profile_name)
if credentials:
valid_accounts.append(account)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ def __init__(

# Log with account name and ID
logger.info(
f"Resource discovery initialized for account \
{self.account_name} ({self.account_id}) in partition {self.partition}"
f"Resource discovery initialized for account {self.account_name} ({self.account_id}) in partition {self.partition}"
)

def discover_resources(
Expand Down Expand Up @@ -99,8 +98,7 @@ def discover_resources(
if not valid_regions:
valid_regions = DEFAULT_REGIONS.get(self.partition, DEFAULT_REGIONS["aws"])
logger.warning(
f"No valid regions found for partition {self.partition}, \
using defaults: {', '.join(valid_regions)}"
f"No valid regions found for partition {self.partition}, using defaults: {', '.join(valid_regions)}"
)

# Use validated regions
Expand Down Expand Up @@ -132,8 +130,7 @@ def discover_resources(
region_resources = future.result()
resources.extend(region_resources)
logger.debug(
f"Discovered {len(region_resources)} {resource_type} \
resources in {region}"
f"Discovered {len(region_resources)} {resource_type} resources in {region}"
)
except Exception as e:
logger.error(f"Error discovering {resource_type} in {region}: {e}")
Expand Down Expand Up @@ -360,7 +357,7 @@ def _discover_emr(
"id": cluster["Id"],
"name": cluster.get("Name", "Unnamed"),
"status": state,
"state": state, # Add both fields to ensure compatibility
"state": state,
"region": region,
"tags": tags,
"creation_time": (
Expand Down Expand Up @@ -732,7 +729,7 @@ def _get_account_resources_impl(
"eks_clusters": [],
"emr_clusters": [],
"ecr_images": [],
"ecr_old_images": [], # Add a new key for old ECR images
"ecr_old_images": [],
}

try:
Expand Down Expand Up @@ -771,8 +768,7 @@ def _get_account_resources_impl(
# Discover EC2 instances - only if not excluded
if "ec2" not in exclude_resources:
logger.info(
f"Discovering EC2 instances for account {account_name} ({account_id}) \
in {len(regions)} regions"
f"Discovering EC2 instances for account {account_name} ({account_id}) in {len(regions)} regions"
)
resources["ec2_instances"] = discovery.discover_resources("ec2", regions)
else:
Expand All @@ -781,8 +777,7 @@ def _get_account_resources_impl(
# Discover RDS instances - only if not excluded
if "rds" not in exclude_resources:
logger.info(
f"Discovering RDS instances for account {account_name} ({account_id}) \
in {len(regions)} regions"
f"Discovering RDS instances for account {account_name} ({account_id}) in {len(regions)} regions"
)
resources["rds_instances"] = discovery.discover_resources("rds", regions)
else:
Expand All @@ -791,8 +786,7 @@ def _get_account_resources_impl(
# Discover EKS clusters - only if not excluded
if "eks" not in exclude_resources:
logger.info(
f"Discovering EKS clusters for account {account_name} ({account_id}) \
in {len(regions)} regions"
f"Discovering EKS clusters for account {account_name} ({account_id}) in {len(regions)} regions"
)
try:
resources["eks_clusters"] = discovery.discover_resources("eks", regions)
Expand All @@ -805,8 +799,7 @@ def _get_account_resources_impl(
# Discover EMR clusters - only if not excluded
if "emr" not in exclude_resources:
logger.info(
f"Discovering EMR clusters for account {account_name} ({account_id}) \
in {len(regions)} regions"
f"Discovering EMR clusters for account {account_name} ({account_id}) in {len(regions)} regions"
)
try:
resources["emr_clusters"] = discovery.discover_resources("emr", regions)
Expand All @@ -819,8 +812,7 @@ def _get_account_resources_impl(
# Discover ECR images - only if not excluded
if "ecr" not in exclude_resources:
logger.info(
f"Discovering ECR images for account {account_name} ({account_id}) \
in {len(regions)} regions"
f"Discovering ECR images for account {account_name} ({account_id}) in {len(regions)} regions"
)
try:
# Use our helper function that properly enriches ECR resources
Expand Down Expand Up @@ -855,14 +847,12 @@ def _get_account_resources_impl(
"emr_clusters",
]:
logger.info(
f"Total {resource_type.split('_')[0]} resources discovered: \
{len(resource_list)}"
f"Total {resource_type.split('_')[0]} resources discovered: {len(resource_list)}"
)
elif resource_type == "ecr_images":
# ECR images are already logged in discover_ecr_images
logger.info(
f"Total {resource_type.split('_')[0]} resources discovered: \
{len(resource_list)}"
f"Total {resource_type.split('_')[0]} resources discovered: {len(resource_list)}"
)

# Also add a log entry for the total resources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Any, Callable, Dict, List, Optional

from aws_resource_management.utils import (
get_account_alias, # Add the new import
get_config,
get_session_for_account,
normalize_resource_state,
Expand Down Expand Up @@ -346,3 +347,13 @@ def normalize_resources(resources: List[Dict[str, Any]]) -> None:
# If still no account name, use account ID as fallback
if "accountName" not in resource and "accountId" in resource:
resource["accountName"] = resource["accountId"]

def get_account_alias(self) -> str:
"""
Get the AWS account alias for cleaner reporting.
Returns:
Account alias or account ID if alias not found
"""
# Call the centralized utility function
return get_account_alias(self.account_id, self.region, self.credentials)
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
from aws_resource_management.utils.aws_core_utils import (
create_client,
ensure_valid_account_name,
get_account_alias,
get_account_list,
get_credentials,
get_organization_accounts,
get_session_for_account,
)
from aws_resource_management.utils.config_utils import (
ConfigManager,
get_config,
get_config_value,
update_config,
Expand All @@ -43,6 +45,7 @@
setup_logging,
)
from aws_resource_management.utils.region_utils import (
RegionManager,
DEFAULT_PARTITION,
DEFAULT_REGIONS,
detect_partition_from_credentials,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,47 @@ def create_client(
except Exception as e:
logger.error(f"Error creating {service_name} client: {e}")
raise


def get_account_alias(account_id: str, region: Optional[str] = None, credentials: Optional[Dict[str, Any]] = None) -> str:
"""
Get the AWS account alias for an account.
Args:
account_id: AWS account ID
region: AWS region to use for the API call (optional)
credentials: AWS credentials to use (optional)
Returns:
Account alias or account ID if no alias is found
"""
try:
# Try to get a session for the account
if credentials:
session = boto3.Session(
aws_access_key_id=credentials.get("aws_access_key_id"),
aws_secret_access_key=credentials.get("aws_secret_access_key"),
aws_session_token=credentials.get("aws_session_token"),
region_name=region or "us-east-1" # Default to us-east-1 if no region provided
)
else:
session = get_session_for_account(account_id, region=region)

if not session:
logger.debug(f"Could not create session for account {account_id}")
return account_id

# Try to get account aliases from IAM
iam_client = session.client("iam")
response = iam_client.list_account_aliases()
aliases = response.get("AccountAliases", [])

# Return the first alias if any exist
if aliases:
return aliases[0]

except Exception as e:
logger.debug(f"Failed to get account alias for account {account_id}: {str(e)}")

# Default to account ID
return account_id
Loading

0 comments on commit c26d2bf

Please sign in to comment.