attstore / scripts /delete_tenant.sh
dn-scribe's picture
feat: support tenant deletion
38536e2
Raw History Blame Contribute Delete
2.9 kB
#!/bin/bash
# Script to delete a tenant by name
# Usage: ./delete_tenant.sh <tenant_name_or_slug> [API_URL] [USERNAME] [PASSWORD]
set -e
TENANT_NAME="${1}"
API_URL="${2:-http://localhost:5555}"
USERNAME="${3:-admin}"
PASSWORD="${4:-admin}"
if [ -z "$TENANT_NAME" ]; then
echo "Usage: $0 <tenant_name_or_slug> [API_URL] [USERNAME] [PASSWORD]"
echo ""
echo "Arguments:"
echo " tenant_name_or_slug - The name or slug of the tenant to delete (required)"
echo " API_URL - Base URL of the API (default: http://localhost:5555)"
echo " USERNAME - Admin username (default: admin)"
echo " PASSWORD - Admin password (default: admin)"
echo ""
echo "Example:"
echo " $0 my-tenant"
echo " $0 my-tenant http://localhost:5555 admin mypassword"
exit 1
fi
echo "Authenticating as $USERNAME..."
# Login and get JWT token
TOKEN=$(curl -s -X POST "${API_URL}/v1/login" \
-H "Content-Type: application/json" \
-d "{\"username\":\"${USERNAME}\",\"password\":\"${PASSWORD}\"}" \
| grep -o '"access_token":"[^"]*"' \
| cut -d'"' -f4)
if [ -z "$TOKEN" ]; then
echo "Error: Failed to authenticate. Please check your credentials."
exit 1
fi
echo "Authentication successful."
echo ""
echo "Fetching tenant list..."
# Get all tenants
TENANTS=$(curl -s -X GET "${API_URL}/api/saml/tenants" \
-H "Authorization: Bearer ${TOKEN}")
echo "Available tenants:"
echo "$TENANTS" | python3 -m json.tool 2>/dev/null || echo "$TENANTS"
echo ""
# Find tenant by name or slug
TENANT_ID=$(echo "$TENANTS" | python3 -c "
import sys
import json
try:
tenants = json.load(sys.stdin)
search = '${TENANT_NAME}'.lower()
for tenant in tenants:
if tenant.get('name', '').lower() == search or tenant.get('slug', '').lower() == search:
print(tenant['id'])
break
except:
pass
" 2>/dev/null)
if [ -z "$TENANT_ID" ]; then
echo "Error: Tenant '$TENANT_NAME' not found."
echo "Please check the tenant name or slug and try again."
exit 1
fi
echo "Found tenant with ID: $TENANT_ID"
echo ""
read -p "Are you sure you want to delete this tenant? This will remove all associated SAML configurations and unlink users. (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
echo "Deletion cancelled."
exit 0
fi
echo ""
echo "Deleting tenant..."
# Delete the tenant
RESPONSE=$(curl -s -X DELETE "${API_URL}/api/saml/tenants/${TENANT_ID}" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json")
echo "Response:"
echo "$RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$RESPONSE"
echo ""
# Check if deletion was successful
if echo "$RESPONSE" | grep -q "deleted successfully"; then
echo "✓ Tenant deleted successfully!"
exit 0
else
echo "✗ Failed to delete tenant. Please check the response above."
exit 1
fi