Skip to content

Commit

Permalink
functional
Browse files Browse the repository at this point in the history
  • Loading branch information
morga471 committed Mar 14, 2025
1 parent a589bbb commit a84c6fc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
46 changes: 40 additions & 6 deletions local-app/python-tools/gfl-resource-actions/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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}"
Expand Down
17 changes: 16 additions & 1 deletion local-app/python-tools/gfl-resource-actions/managers/rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit a84c6fc

Please sign in to comment.