|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Generate test fixtures from actual Redis Cloud API responses |
| 3 | +# This script captures real API responses to ensure our type definitions match reality |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# Configuration |
| 8 | +REDIS_CLOUD_API_KEY="${REDIS_CLOUD_API_KEY:-}" |
| 9 | +REDIS_CLOUD_SECRET_KEY="${REDIS_CLOUD_SECRET_KEY:-}" |
| 10 | +REDIS_CLOUD_BASE_URL="${REDIS_CLOUD_BASE_URL:-https://api.redislabs.com/v1}" |
| 11 | +OUTPUT_DIR="crates/redis-cloud/tests/fixtures" |
| 12 | + |
| 13 | +# Colors for output |
| 14 | +RED='\033[0;31m' |
| 15 | +GREEN='\033[0;32m' |
| 16 | +YELLOW='\033[1;33m' |
| 17 | +NC='\033[0m' # No Color |
| 18 | + |
| 19 | +# Check credentials |
| 20 | +if [ -z "$REDIS_CLOUD_API_KEY" ] || [ -z "$REDIS_CLOUD_SECRET_KEY" ]; then |
| 21 | + echo -e "${RED}Error: REDIS_CLOUD_API_KEY and REDIS_CLOUD_SECRET_KEY must be set${NC}" |
| 22 | + echo "Export these environment variables with your Redis Cloud credentials" |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +# Ensure output directory exists |
| 27 | +mkdir -p "$OUTPUT_DIR" |
| 28 | + |
| 29 | +echo "Generating Redis Cloud API fixtures..." |
| 30 | +echo "URL: $REDIS_CLOUD_BASE_URL" |
| 31 | +echo "Output: $OUTPUT_DIR" |
| 32 | +echo "" |
| 33 | +echo -e "${YELLOW}WARNING: This will make real API calls to your Cloud account${NC}" |
| 34 | +echo -e "${YELLOW}No resources will be created, only GET requests${NC}" |
| 35 | +echo "" |
| 36 | + |
| 37 | +# Function to capture API response |
| 38 | +capture_endpoint() { |
| 39 | + local endpoint="$1" |
| 40 | + local filename="$2" |
| 41 | + local description="$3" |
| 42 | + |
| 43 | + echo -n "Capturing $description... " |
| 44 | + |
| 45 | + if curl -s -f \ |
| 46 | + -H "x-api-key: $REDIS_CLOUD_API_KEY" \ |
| 47 | + -H "x-api-secret-key: $REDIS_CLOUD_SECRET_KEY" \ |
| 48 | + "$REDIS_CLOUD_BASE_URL$endpoint" \ |
| 49 | + -o "$OUTPUT_DIR/$filename" 2>/dev/null; then |
| 50 | + |
| 51 | + # Validate it's valid JSON |
| 52 | + if jq empty "$OUTPUT_DIR/$filename" 2>/dev/null; then |
| 53 | + echo -e "${GREEN}✓${NC}" |
| 54 | + else |
| 55 | + echo -e "${RED}✗ Invalid JSON${NC}" |
| 56 | + rm "$OUTPUT_DIR/$filename" |
| 57 | + return 1 |
| 58 | + fi |
| 59 | + else |
| 60 | + echo -e "${YELLOW}⚠ Endpoint not available or no data${NC}" |
| 61 | + return 1 |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +# Account information |
| 66 | +capture_endpoint "/account" "account.json" "Account info" |
| 67 | + |
| 68 | +# Subscriptions |
| 69 | +capture_endpoint "/subscriptions" "subscriptions_list.json" "Subscriptions list" |
| 70 | + |
| 71 | +# Databases (flexible/pro) |
| 72 | +capture_endpoint "/subscriptions" "databases_for_subscription.json" "Databases for subscription" || true |
| 73 | + |
| 74 | +# Cloud accounts |
| 75 | +capture_endpoint "/cloud-accounts" "cloud_accounts_list.json" "Cloud accounts list" |
| 76 | + |
| 77 | +# Payment methods |
| 78 | +capture_endpoint "/payment-methods" "payment_methods_list.json" "Payment methods" || true |
| 79 | + |
| 80 | +# Regions |
| 81 | +capture_endpoint "/regions" "regions_list.json" "Regions list" || true |
| 82 | + |
| 83 | +# Tasks (may be empty) |
| 84 | +capture_endpoint "/tasks" "tasks_list.json" "Tasks list" || true |
| 85 | + |
| 86 | +# Users |
| 87 | +capture_endpoint "/users" "users_list.json" "Users list" |
| 88 | + |
| 89 | +# ACLs |
| 90 | +capture_endpoint "/acls" "acls_list.json" "ACLs list" || true |
| 91 | + |
| 92 | +# Fixed (Essentials) subscriptions |
| 93 | +capture_endpoint "/fixed/subscriptions" "fixed_subscriptions_list.json" "Fixed subscriptions" || true |
| 94 | + |
| 95 | +# API Keys (careful - these are sensitive, only if needed for testing) |
| 96 | +# capture_endpoint "/account/api-keys" "api_keys_list.json" "API keys list" || true |
| 97 | + |
| 98 | +echo "" |
| 99 | +echo "Fixture generation complete!" |
| 100 | +echo "Captured fixtures in: $OUTPUT_DIR" |
| 101 | +echo "" |
| 102 | +echo -e "${YELLOW}IMPORTANT: Review fixtures for sensitive data before committing!${NC}" |
| 103 | +echo "- Remove account IDs, subscription IDs if needed" |
| 104 | +echo "- Check for any PII or secrets" |
| 105 | +echo "" |
| 106 | +echo "Files generated:" |
| 107 | +ls -lh "$OUTPUT_DIR"/*.json 2>/dev/null | awk '{print " " $9 " (" $5 ")"}' |
0 commit comments