#!/bin/bash

# List VPC Endpoints and their details
vpc_endpoints=$(aws ec2 describe-vpc-endpoints | jq -r '.VpcEndpoints[] | @base64')

decode_base64() {
  echo "$1" | base64 --decode
}

echo "VPC Endpoints and their Details:"

for endpoint in $vpc_endpoints; do
  endpoint_json=$(decode_base64 "$endpoint")
  endpoint_id=$(echo "$endpoint_json" | jq -r '.VpcEndpointId')
  endpoint_type=$(echo "$endpoint_json" | jq -r '.VpcEndpointType')
  service_name=$(echo "$endpoint_json" | jq -r '.ServiceName')
  state=$(echo "$endpoint_json" | jq -r '.State')
  vpc_id=$(echo "$endpoint_json" | jq -r '.VpcId')
  subnet_ids=$(echo "$endpoint_json" | jq -r '.SubnetIds[]')
  security_groups=$(echo "$endpoint_json" | jq -r '.Groups[] | {GroupId: .GroupId, GroupName: .GroupName}')
  dns_names=$(echo "$endpoint_json" | jq -r '.DnsEntries[] | {DnsName: .DnsName}')
  tags=$(echo "$endpoint_json" | jq -r '.Tags[] | {Key: .Key, Value: .Value}')
  network_interface_ids=$(echo "$endpoint_json" | jq -r '.NetworkInterfaceIds[]')

  echo "VPC Endpoint ID: $endpoint_id"
  echo "  VPC Endpoint Type: $endpoint_type"
  echo "  Service Name: $service_name"
  echo "  State: $state"
  echo "  VPC ID: $vpc_id"
  echo "  Subnet IDs:"
  for subnet_id in $subnet_ids; do
    echo "    - $subnet_id"
  done
  echo "  Security Groups:"
  echo "$security_groups" | jq -r '. | "    - Group ID: \(.GroupId), Group Name: \(.GroupName)"'
  echo "  DNS Names:"
  echo "$dns_names" | jq -r '. | "    - \(.DnsName)"'
  echo "  Tags:"
  echo "$tags" | jq -r '. | "    - Key: \(.Key), Value: \(.Value)"'

  echo "  Network Interfaces and Private IP Addresses:"
  for ni_id in $network_interface_ids; do
    private_ip=$(aws ec2 describe-network-interfaces --network-interface-ids "$ni_id" | jq -r '.NetworkInterfaces[].PrivateIpAddress')
    echo "    - Network Interface ID: $ni_id"
    echo "      Private IP Address: $private_ip"
  done
done
