From a84c6fc01905f82b263cc2b6fc4474d0d1edf7dd Mon Sep 17 00:00:00 2001 From: "Matthew C. Morgan" Date: Fri, 14 Mar 2025 18:28:37 -0400 Subject: [PATCH] functional --- .../gfl-resource-actions/discovery.py | 46 ++++++++++++++++--- .../gfl-resource-actions/managers/rds.py | 17 ++++++- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/local-app/python-tools/gfl-resource-actions/discovery.py b/local-app/python-tools/gfl-resource-actions/discovery.py index 4ce7cc4e..0fde7627 100644 --- a/local-app/python-tools/gfl-resource-actions/discovery.py +++ b/local-app/python-tools/gfl-resource-actions/discovery.py @@ -112,8 +112,18 @@ def get_rds_instances(credentials, region, account_id=None, account_name=None): rds_client = create_boto3_client('rds', credentials, region) instances = [] + logger.info(f"Discovering RDS instances in region {region}...") response = rds_client.describe_db_instances() + + # Log the raw count of instances returned from API + raw_count = len(response.get('DBInstances', [])) + logger.info(f"Raw RDS API returned {raw_count} instances in region {region}") + for instance in response.get('DBInstances', []): + # Log the raw status as received from AWS + raw_status = instance['DBInstanceStatus'] + logger.debug(f"RDS instance {instance['DBInstanceIdentifier']} raw status: {raw_status}") + # Capture tags for exclusion check tags = {} try: @@ -129,10 +139,14 @@ def get_rds_instances(credentials, region, account_id=None, account_name=None): if 'Endpoint' in instance and 'Address' in instance['Endpoint']: endpoint_address = instance['Endpoint']['Address'] + # Standardize status to lowercase for consistent comparison + standardized_status = raw_status.lower() + instance_data = { "id": instance['DBInstanceIdentifier'], "name": instance['DBInstanceIdentifier'], # Using ID as name - "status": instance['DBInstanceStatus'], + "status": standardized_status, # Using standardized status + "raw_status": raw_status, # Keep original status for debugging "region": region, "endpoint": endpoint_address, "tags": tags @@ -151,13 +165,17 @@ def get_rds_instances(credentials, region, account_id=None, account_name=None): action="discover", region=region, status="success", - details=f"Status: {instance['DBInstanceStatus']}, Endpoint: {endpoint_address}" + details=f"Status: {raw_status}, Endpoint: {endpoint_address}" ) - + # Handle pagination while 'Marker' in response: response = rds_client.describe_db_instances(Marker=response['Marker']) for instance in response.get('DBInstances', []): + # Log the raw status as received from AWS + raw_status = instance['DBInstanceStatus'] + logger.debug(f"RDS instance {instance['DBInstanceIdentifier']} raw status: {raw_status}") + # Capture tags for exclusion check tags = {} try: @@ -173,10 +191,14 @@ def get_rds_instances(credentials, region, account_id=None, account_name=None): if 'Endpoint' in instance and 'Address' in instance['Endpoint']: endpoint_address = instance['Endpoint']['Address'] + # Standardize status to lowercase for consistent comparison + standardized_status = raw_status.lower() + instance_data = { "id": instance['DBInstanceIdentifier'], "name": instance['DBInstanceIdentifier'], # Using ID as name - "status": instance['DBInstanceStatus'], + "status": standardized_status, # Using standardized status + "raw_status": raw_status, # Keep original status for debugging "region": region, "endpoint": endpoint_address, "tags": tags @@ -195,9 +217,21 @@ def get_rds_instances(credentials, region, account_id=None, account_name=None): action="discover", region=region, status="success", - details=f"Status: {instance['DBInstanceStatus']}, Endpoint: {endpoint_address}" + details=f"Status: {raw_status}, Endpoint: {endpoint_address}" ) - + + # Log final count of instances found + logger.info(f"Found {len(instances)} RDS instances in region {region}") + + # Log status distribution for debugging + status_counts = {} + for inst in instances: + status = inst['status'] + status_counts[status] = status_counts.get(status, 0) + 1 + + if status_counts: + logger.info(f"RDS instance status distribution in {region}: {status_counts}") + return instances except Exception as e: error_msg = f"Error getting RDS instances in region {region}: {e}" 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 2e903d83..7db05a94 100644 --- a/local-app/python-tools/gfl-resource-actions/managers/rds.py +++ b/local-app/python-tools/gfl-resource-actions/managers/rds.py @@ -12,12 +12,20 @@ class RDSManager(base.ResourceManager): def stop(self, instances, dry_run=False): """Stop available RDS instances.""" + logger.info(f"Evaluating {len(instances)} RDS instances for stop action") + stopped_count = 0 + skipped_count = 0 + for instance in instances: # Skip instances with the exclusion tag if self.should_exclude(instance): logger.info(f"Skipping RDS instance {instance['id']} with exclusion tag {config.EXCLUSION_TAG}") + skipped_count += 1 continue - + + # Log status for debugging + logger.debug(f"RDS instance {instance['id']} status: {instance['status']}") + if instance["status"] == "available": try: region = instance["region"] @@ -46,8 +54,15 @@ def stop(self, instances, dry_run=False): # Log with detailed information self.log_action(instance['id'], region, "stop", dry_run=dry_run) + stopped_count += 1 + except Exception as e: logger.error(f"Failed to {'simulate stopping' if dry_run else 'stop'} RDS instance {instance['id']}: {e}") + else: + logger.debug(f"Skipping RDS instance {instance['id']} with non-available status: {instance['status']}") + skipped_count += 1 + + logger.info(f"RDS stop summary: {stopped_count} instances processed, {skipped_count} instances skipped") def start(self, instances, dry_run=False): """Start stopped RDS instances."""