Skip to content

Commit

Permalink
fix timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Mar 12, 2026
1 parent 7cf8ce9 commit b990fb6
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions local-app/python-tools/ipam/export_ipam_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__})")
Expand All @@ -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}.")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b990fb6

Please sign in to comment.