{
  "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_vpc_name(tags):\n    if tags:\n        for tag in tags:\n            if tag[\"Key\"] == \"Name\":\n                return tag[\"Value\"]\n    return \"Unnamed VPC\"\n\n\ndef list_vpcs(ec2_client):\n    vpcs_info = []\n    paginator = ec2_client.get_paginator(\"describe_vpcs\")\n    for page in paginator.paginate():\n        for vpc in page[\"Vpcs\"]:\n            vpcs_info.append(\n                {\n                    \"VPCName\": get_vpc_name(vpc.get(\"Tags\")),\n                    \"VPCID\": vpc[\"VpcId\"],\n                    \"CIDRBlock\": vpc.get(\"CidrBlock\", \"N/A\"),\n                }\n            )\n    return vpcs_info\n\n\ndef main():\n    get_boto_session()\n    regions_to_check = [\"eu-west-1\", \"eu-west-2\"]\n    total_vpcs_all_accounts = 0\n    rows_for_sheets = []\n\n    with open(\"aws_vpcs_by_region.csv\", mode=\"w\", newline=\"\") as csvfile:\n        fieldnames = [\n            \"AccountName\",\n            \"AccountID\",\n            \"Region\",\n            \"VPCName\",\n            \"VPCID\",\n            \"CIDRBlock\",\n        ]\n        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n        writer.writeheader()\n\n        for account, profile_name in setup_org_accounts_session():\n            account_id = account[\"Id\"]\n            account_name = account[\"Name\"]\n            try:\n                boto3.setup_default_session(profile_name=profile_name)\n                for region in regions_to_check:\n                    try:\n                        ec2_client = boto3.client(\"ec2\", region_name=region)\n                        vpcs_info = list_vpcs(ec2_client)\n                        total_vpcs_all_accounts += len(vpcs_info)\n\n                        for vpc in vpcs_info:\n                            row = {\n                                \"AccountName\": account_name,\n                                \"AccountID\": account_id,\n                                \"Region\": region,\n                                \"VPCName\": vpc[\"VPCName\"],\n                                \"VPCID\": vpc[\"VPCID\"],\n                                \"CIDRBlock\": vpc[\"CIDRBlock\"],\n                            }\n                            writer.writerow(row)\n                            rows_for_sheets.append(list(row.values()))\n\n                        if vpcs_info:\n                            print(\n                                f'\"{account_name}\" (ID: {account_id}, Region: {region}) VPCs found: {len(vpcs_info)}'\n                            )\n                    except ClientError as e:\n                        print(f\"Error in {region} for {account_name}: {e}\")\n            except (ClientError, ProfileNotFound) as e:\n                print(f\"Error accessing account {account_name}: {e}\")\n\n    print(f\"\\nTotal number of VPCs found: {total_vpcs_all_accounts}\")\n    print(\"CSV file 'aws_vpcs_by_region.csv' generated successfully.\")\n\n    # Export to Google Sheets\n    export_to_sheets(\"aws-vpc\", fieldnames, rows_for_sheets)\n\n\nif __name__ == \"__main__\":\n    main()\n"
}