{
  "output": "import boto3\nimport csv\nfrom botocore.exceptions import ClientError, ProfileNotFound\nfrom aws_utils import setup_org_accounts_session, get_boto_session, export_to_sheets\n\n\ndef get_cloudfront_details(account_id, account_name, cloudfront_client):\n    \"\"\"\n    Lists all CloudFront distributions for the account and determines\n    their associated details.\n    \"\"\"\n    dist_info_list = []\n    try:\n        paginator = cloudfront_client.get_paginator(\"list_distributions\")\n        for page in paginator.paginate():\n            if \"DistributionList\" in page and \"Items\" in page[\"DistributionList\"]:\n                for dist in page[\"DistributionList\"][\"Items\"]:\n                    dist_info_list.append(\n                        {\n                            \"AccountName\": account_name,\n                            \"AccountID\": account_id,\n                            \"ID\": dist[\"Id\"],\n                            \"DomainName\": dist[\"DomainName\"],\n                            \"Status\": dist[\"Status\"],\n                            \"Enabled\": dist[\"Enabled\"],\n                            \"Comment\": dist.get(\"Comment\", \"N/A\"),\n                        }\n                    )\n    except ClientError as e:\n        print(f\"  -> Error listing distributions: {e}\")\n    return dist_info_list\n\n\ndef main():\n    get_boto_session()\n    all_dist_info = []\n    rows_for_sheets = []\n    csv_filepath = \"cloudfront_distributions_info.csv\"\n    fieldnames = [\n        \"AccountName\",\n        \"AccountID\",\n        \"ID\",\n        \"DomainName\",\n        \"Status\",\n        \"Enabled\",\n        \"Comment\",\n    ]\n\n    with open(csv_filepath, mode=\"w\", newline=\"\") as csvfile:\n        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n        writer.writeheader()\n\n        print(\"\\nIterating through accounts to collect CloudFront data...\")\n        for account, profile_name in setup_org_accounts_session():\n            account_id = account[\"Id\"]\n            account_name = account[\"Name\"]\n            print(f\"\\n--- Checking Account: {account_name} (ID: {account_id}) ---\")\n\n            try:\n                boto3.setup_default_session(profile_name=profile_name)\n                cloudfront_client = boto3.client(\"cloudfront\")\n\n                dist_data = get_cloudfront_details(\n                    account_id, account_name, cloudfront_client\n                )\n                for dist in dist_data:\n                    all_dist_info.append(dist)\n                    writer.writerow(dist)\n                    rows_for_sheets.append(list(dist.values()))\n                    print(\n                        f\"  [FOUND] ID: {dist['ID']}, Domain: {dist['DomainName']}, Status: {dist['Status']}\"\n                    )\n            except (ClientError, ProfileNotFound) as e:\n                print(f\"  -> Error setting up session/profile '{profile_name}': {e}\")\n\n    print(f\"\\nTotal distributions found: {len(all_dist_info)}\")\n    print(f\"CSV file '{csv_filepath}' generated successfully.\")\n    export_to_sheets(\"cloudfront-distributions\", fieldnames, rows_for_sheets)\n\n\nif __name__ == \"__main__\":\n    main()\n"
}