2025-10-07 10:59:52 +01:00

302 lines
8.1 KiB
Bash
Executable File

#!/bin/bash
# Provisioning API Client Example
set -e
# Configuration
API_URL="${API_URL:-http://localhost:8083}"
USERNAME="${USERNAME:-admin}"
PASSWORD="${PASSWORD:-admin123}"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Functions
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
# Login and get token
login() {
log_info "Logging in as $USERNAME..."
RESPONSE=$(curl -s -X POST "$API_URL/v1/auth/login" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}")
TOKEN=$(echo "$RESPONSE" | jq -r '.token')
if [ "$TOKEN" = "null" ] || [ -z "$TOKEN" ]; then
log_error "Login failed"
echo "$RESPONSE" | jq .
exit 1
fi
log_info "Login successful"
export TOKEN
}
# Health check
health_check() {
log_info "Checking server health..."
RESPONSE=$(curl -s "$API_URL/health")
STATUS=$(echo "$RESPONSE" | jq -r '.status')
VERSION=$(echo "$RESPONSE" | jq -r '.version')
UPTIME=$(echo "$RESPONSE" | jq -r '.uptime_seconds')
if [ "$STATUS" = "healthy" ]; then
log_info "Server is healthy"
echo " Version: $VERSION"
echo " Uptime: $UPTIME seconds"
else
log_error "Server is unhealthy"
exit 1
fi
}
# Create server
create_server() {
local workspace="${1:-default}"
local hostname="${2:-test-server}"
log_info "Creating server $hostname in workspace $workspace..."
RESPONSE=$(curl -s -X POST "$API_URL/v1/servers/create" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"workspace\": \"$workspace\",
\"provider\": \"upcloud\",
\"plan\": \"1xCPU-2GB\",
\"hostname\": \"$hostname\",
\"zone\": \"de-fra1\",
\"check_mode\": true
}")
OPERATION_ID=$(echo "$RESPONSE" | jq -r '.id')
if [ "$OPERATION_ID" = "null" ]; then
log_error "Server creation failed"
echo "$RESPONSE" | jq .
exit 1
fi
log_info "Server creation started - Operation ID: $OPERATION_ID"
echo "$OPERATION_ID"
}
# Check operation status
check_operation() {
local operation_id="$1"
log_info "Checking operation $operation_id..."
RESPONSE=$(curl -s "$API_URL/v1/operations/$operation_id" \
-H "Authorization: Bearer $TOKEN")
STATUS=$(echo "$RESPONSE" | jq -r '.status')
echo "Status: $STATUS"
echo "$RESPONSE" | jq .
}
# Wait for operation to complete
wait_for_operation() {
local operation_id="$1"
local max_wait="${2:-300}"
local wait_time=0
log_info "Waiting for operation $operation_id to complete..."
while [ $wait_time -lt $max_wait ]; do
RESPONSE=$(curl -s "$API_URL/v1/operations/$operation_id" \
-H "Authorization: Bearer $TOKEN")
STATUS=$(echo "$RESPONSE" | jq -r '.status')
case "$STATUS" in
"completed")
log_info "Operation completed successfully"
echo "$RESPONSE" | jq .
return 0
;;
"failed")
log_error "Operation failed"
echo "$RESPONSE" | jq .
return 1
;;
"cancelled")
log_warning "Operation was cancelled"
return 1
;;
*)
echo -n "."
sleep 5
wait_time=$((wait_time + 5))
;;
esac
done
log_error "Operation timed out after $max_wait seconds"
return 1
}
# List servers
list_servers() {
log_info "Listing servers..."
RESPONSE=$(curl -s "$API_URL/v1/servers" \
-H "Authorization: Bearer $TOKEN")
echo "$RESPONSE" | jq .
}
# Create taskserv
create_taskserv() {
local workspace="${1:-default}"
local name="${2:-kubernetes}"
local servers="${3:-test-server}"
log_info "Creating taskserv $name in workspace $workspace..."
SERVERS_JSON=$(echo "$servers" | jq -R 'split(",")' | jq -c .)
RESPONSE=$(curl -s -X POST "$API_URL/v1/taskservs/create" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"workspace\": \"$workspace\",
\"name\": \"$name\",
\"servers\": $SERVERS_JSON,
\"check_mode\": true
}")
OPERATION_ID=$(echo "$RESPONSE" | jq -r '.id')
if [ "$OPERATION_ID" = "null" ]; then
log_error "Taskserv creation failed"
echo "$RESPONSE" | jq .
exit 1
fi
log_info "Taskserv creation started - Operation ID: $OPERATION_ID"
echo "$OPERATION_ID"
}
# Submit workflow
submit_workflow() {
local workspace="${1:-default}"
local workflow_type="${2:-cluster_create}"
log_info "Submitting workflow $workflow_type in workspace $workspace..."
RESPONSE=$(curl -s -X POST "$API_URL/v1/workflows/submit" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"workspace\": \"$workspace\",
\"workflow_type\": \"$workflow_type\",
\"parameters\": {
\"cluster_name\": \"test-cluster\",
\"node_count\": 3
}
}")
OPERATION_ID=$(echo "$RESPONSE" | jq -r '.id')
if [ "$OPERATION_ID" = "null" ]; then
log_error "Workflow submission failed"
echo "$RESPONSE" | jq .
exit 1
fi
log_info "Workflow submitted - Operation ID: $OPERATION_ID"
echo "$OPERATION_ID"
}
# Main menu
show_menu() {
echo ""
echo "Provisioning API Client"
echo "======================="
echo "1. Health check"
echo "2. Login"
echo "3. List servers"
echo "4. Create server"
echo "5. Create taskserv"
echo "6. Submit workflow"
echo "7. Check operation status"
echo "8. Exit"
echo ""
}
# Main execution
main() {
if [ $# -eq 0 ]; then
# Interactive mode
while true; do
show_menu
read -p "Select option: " choice
case $choice in
1) health_check ;;
2) login ;;
3) list_servers ;;
4)
read -p "Workspace [default]: " workspace
read -p "Hostname [test-server]: " hostname
create_server "${workspace:-default}" "${hostname:-test-server}"
;;
5)
read -p "Workspace [default]: " workspace
read -p "Taskserv name [kubernetes]: " name
read -p "Servers (comma-separated) [test-server]: " servers
create_taskserv "${workspace:-default}" "${name:-kubernetes}" "${servers:-test-server}"
;;
6)
read -p "Workspace [default]: " workspace
read -p "Workflow type [cluster_create]: " workflow_type
submit_workflow "${workspace:-default}" "${workflow_type:-cluster_create}"
;;
7)
read -p "Operation ID: " operation_id
check_operation "$operation_id"
;;
8) exit 0 ;;
*) log_error "Invalid option" ;;
esac
done
else
# Command-line mode
case "$1" in
health) health_check ;;
login) login ;;
list-servers) login && list_servers ;;
create-server) login && create_server "$2" "$3" ;;
create-taskserv) login && create_taskserv "$2" "$3" "$4" ;;
submit-workflow) login && submit_workflow "$2" "$3" ;;
check-operation) login && check_operation "$2" ;;
wait-operation) login && wait_for_operation "$2" "$3" ;;
*)
echo "Usage: $0 {health|login|list-servers|create-server|create-taskserv|submit-workflow|check-operation|wait-operation}"
exit 1
;;
esac
fi
}
main "$@"