diff --git a/local-app/python-tools/ipam/export_ipam_discovery.py b/local-app/python-tools/ipam/export_ipam_discovery.py index 7efe6413..a4e67295 100755 --- a/local-app/python-tools/ipam/export_ipam_discovery.py +++ b/local-app/python-tools/ipam/export_ipam_discovery.py @@ -8,7 +8,7 @@ from datetime import datetime from botocore.exceptions import ClientError -__version__ = "1.0.3" +__version__ = "1.0.4" def get_args(): parser = argparse.ArgumentParser(description=f"Global AWS IPAM Export (v{__version__})") @@ -25,18 +25,19 @@ def cidr_sort_key(resource): net = ipaddress.ip_network(resource["ResourceCidr"]) return (net.version, int(net.network_address), net.prefixlen) except ValueError: - # Fallback for invalid/empty CIDRs return (0, 0, 0) def export_ipam_data(profile, region): session = boto3.Session(profile_name=profile, region_name=region) ec2 = session.client("ec2") - timestamp = datetime.now().strftime("%Y%m%d-%H%M%S") + # ISO8601 Basic Format: YYYYMMDDTHHMMSS + timestamp = datetime.now().strftime("%Y%m%dT%H%M%S") csv_file = f"ipam-export.{timestamp}.csv" json_file = f"ipam-export.{timestamp}.json" try: + # Get Discovery ID and Operating Regions discoveries = ec2.describe_ipam_resource_discoveries() if not discoveries["IpamResourceDiscoveries"]: print(f"Error: No IPAM Resource Discoveries found in {region}.") @@ -83,9 +84,8 @@ def export_ipam_data(profile, region): print("\nNo resources found.") return - # --- SORTING LOGIC --- - # Sort by ResourceType first, then by CIDR numerically - print("\nSorting records by type and CIDR...") + # Sort by ResourceType, then numerically by CIDR + print("\nSorting records...") all_resources.sort(key=lambda x: (x["ResourceType"], cidr_sort_key(x))) # Export JSON