# pulumi-vsphere lab suite against vmware-api-simulator.
# Usage:
#   cd pulumi-tests && make up && make test-pulumi
#   make pulumi-tests   # from repo root
name: vmware-pulumi-tests

networks:
  lab:
    driver: bridge

volumes:
  lab-postgres-data:
  lab-reports:

x-vsphere-env: &vsphere-env
  VSPHERE_USER: administrator@vsphere.local
  VSPHERE_PASSWORD: "VMware1!"
  VSPHERE_SERVER: api-gateway
  VSPHERE_BASE: https://api-gateway
  VSPHERE_DATACENTER: Datacenter
  VSPHERE_DATASTORE: datastore1
  VSPHERE_CLUSTER: Cluster
  VSPHERE_NETWORK: VM Network
  VSPHERE_VM_NAME: web-01
  VSPHERE_HOST: esxi01.lab.local
  VSPHERE_FOLDER_PATH: /Datacenter/vm/production
  VSPHERE_RESOURCE_POOL: Resources
  REPORT_DIR: /reports
  REPORT_PATH: /reports/pulumi-junit.xml
  HTML_REPORT_PATH: /reports/pulumi-report.html
  JSON_REPORT_PATH: /reports/pulumi-summary.json
  PULUMI_CONFIG_PASSPHRASE: lab
  PULUMI_BACKEND_URL: file:///tmp/pulumi-state

x-sim-env: &sim-env
  DATABASE_URL: postgresql://vmware:vmware@postgres:5432/vmware_simulator
  ENABLE_PVE_STUB: "false"
  SEED_VSPHERE_PROFILE: small
  LOG_LEVEL: INFO
  TICKET_SIGNING_KEY: lab-signing-key
  APP_PORT: "8080"

services:
  postgres:
    image: postgres:17.5-bookworm
    networks: [lab]
    environment:
      POSTGRES_DB: vmware_simulator
      POSTGRES_USER: vmware
      POSTGRES_PASSWORD: vmware
    volumes:
      - lab-postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U vmware -d vmware_simulator"]
      interval: 5s
      timeout: 3s
      retries: 20

  migrate:
    build:
      context: ..
      dockerfile: Dockerfile
      target: runtime
    networks: [lab]
    working_dir: /workspace
    volumes:
      - ..:/workspace
    environment:
      <<: *sim-env
      PYTHONPATH: /workspace
    depends_on:
      postgres:
        condition: service_healthy
    entrypoint: ["python"]
    command: ["-m", "app.db.migrate_cli"]
    restart: "no"

  simulator:
    build:
      context: ..
      dockerfile: Dockerfile
      target: dev
    networks: [lab]
    working_dir: /workspace
    volumes:
      - ..:/workspace
    environment:
      <<: *sim-env
      PYTHONPATH: /workspace
    depends_on:
      migrate:
        condition: service_completed_successfully
    entrypoint: []
    command:
      [
        "uvicorn",
        "app.main:app",
        "--host",
        "0.0.0.0",
        "--port",
        "8080",
        "--reload",
        "--reload-dir",
        "/workspace/app",
      ]
    healthcheck:
      test:
        [
          "CMD",
          "python",
          "-c",
          "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)",
        ]
      interval: 5s
      timeout: 3s
      retries: 30
      start_period: 15s

  seed:
    build:
      context: ..
      dockerfile: Dockerfile
      target: runtime
    networks: [lab]
    working_dir: /workspace
    volumes:
      - ..:/workspace
    environment:
      <<: *sim-env
      PYTHONPATH: /workspace
    depends_on:
      simulator:
        condition: service_healthy
    entrypoint: ["python"]
    command: ["-m", "app.simulation.seed_cli"]
    restart: "no"

  api-gateway:
    image: nginx:1.28-alpine
    networks: [lab]
    depends_on:
      simulator:
        condition: service_healthy
    volumes:
      - ../docker/gateway/vmware-ports.conf:/etc/nginx/conf.d/default.conf:ro
      - ../docker/tls/server.crt:/etc/nginx/tls/server.crt:ro
      - ../docker/tls/server.key:/etc/nginx/tls/server.key:ro
    ports:
      # Host-only alias; keep off 443/18443 so this stack can coexist with the
      # main repo compose (https://localhost) without stealing the lab gateway.
      - "127.0.0.1:28443:443"

  pulumi-runner:
    build:
      context: .
      dockerfile: docker/Dockerfile.pulumi-runner
    networks: [lab]
    working_dir: /suite
    volumes:
      - ./:/suite
      - ..:/workspace:ro
      - lab-reports:/reports
    environment:
      <<: *vsphere-env
    depends_on:
      seed:
        condition: service_completed_successfully
      api-gateway:
        condition: service_started
    profiles: ["test"]
