Skip to content

Commit

Permalink
default output name to include org id
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Dec 31, 2025
1 parent 3eab1f5 commit 9c35af5
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions local-app/python-tools/test-cross-organization/test-cross-org.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
from botocore.exceptions import ClientError, ProfileNotFound

# --- VERSIONING ---
__version__ = "1.0.20"
__version__ = "1.0.21"

def get_args():
parser = argparse.ArgumentParser(description=f"AWS Org Connectivity Test v{__version__}")
parser.add_argument("--profile", default=os.environ.get("AWS_PROFILE"), help="AWS CLI profile")
parser.add_argument("--region", default=os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), help="AWS Region")
parser.add_argument("--role-name", required=True, help="Role name to assume in member accounts")
parser.add_argument("--sort", choices=['id', 'name'], default='name', help="Field to sort results by")
parser.add_argument("--output", help="Optional filename to save the results (will auto-append timestamp)")

# Updated output argument to accept optional values (nargs='?')
parser.add_argument("--output", nargs='?', const='DEFAULT',
help="Save results to file. If no name provided, uses results_ORGID.ISO-DATE.txt")

parser.add_argument("--debug", action="store_true", help="Enable verbose wire-trace logging")
return parser.parse_args()

Expand Down Expand Up @@ -62,13 +66,26 @@ def log_print(message=""):
caller = sts_client.get_caller_identity()
partition = caller['Arn'].split(':')[1]

# Determine output filename with timestamp if requested
# Resolve Organization ID for naming
org_id = "unknown_org"
try:
org_info = org_client.describe_organization()
org_id = org_info['Organization']['Id']
except Exception:
pass

# Handle Default Naming Logic
final_filename = None
if args.output:
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
p = Path(args.output)
# Insert timestamp before the extension (e.g., results.txt -> results_20251231_101500.txt)
final_filename = f"{p.stem}_{ts}{p.suffix}"
# ISO timestamp: YYYY-MM-DDTHHMMSS
datestamp = datetime.now().strftime("%Y-%m-%dT%H%M%S")

if args.output == 'DEFAULT':
final_filename = f"results_{org_id}.{datestamp}.txt"
else:
p = Path(args.output)
# If user provided a name, we still append the timestamp for safety
final_filename = f"{p.stem}_{datestamp}{p.suffix}"

log_print("-" * 100)
log_print(f"AWS ORG CONNECTIVITY TEST - Version {__version__}")
Expand Down

0 comments on commit 9c35af5

Please sign in to comment.