#!/bin/bash

# A script to create an Organizational Unit (OU) in AWS Organizations using AWS CLI.
# This script assumes your AWS account is already part of an AWS Organization.

# --- Configuration ---
REGION="us-east-1"
OU_NAME="MyCLIOU"

# --- 1. Get Organization Root ID ---
echo "--- Getting Organization Root ID ---"
ROOT_ID=$(aws organizations list-roots \
  --region $REGION \
  --query 'Roots[0].Id' --output text)

if [ -z "$ROOT_ID" ]; then
  echo "Error: Could not find an AWS Organization root. Please ensure your account is part of an Organization."
  exit 1
fi
echo "Organization Root ID: $ROOT_ID"

# --- 2. Create OU ---
echo -e "\n--- Creating Organizational Unit: $OU_NAME ---"
OU_ID=$(aws organizations create-organizational-unit \
  --parent-id $ROOT_ID \
  --name $OU_NAME \
  --region $REGION \
  --query 'OrganizationalUnit.Id' --output text)

echo "Organizational Unit created with ID: $OU_ID"

# --- 3. Output OU ID ---
echo -e "\n--- AWS Organizations OU Setup Complete! ---"
echo "Organizational Unit ID: $OU_ID"
echo "You can now move accounts into this OU or attach policies to it."

read -p "Press Enter to delete the Organizational Unit..."

# --- Clean Up ---
echo -e "\n--- Cleaning up resources ---"

# Delete Organizational Unit
echo "Deleting Organizational Unit '$OU_ID'வுகளை..."
aws organizations delete-organizational-unit \
  --organizational-unit-id $OU_ID \
  --region $REGION

echo "Organizational Unit deleted."

echo -e "\n--- AWS Organizations demonstration and cleanup complete ---"
