#!/usr/bin/env bash
# fake maas: serves read endpoints from fixtures. `vlans read <fab>` is filtered by
# fabric_id and MIMICS REAL MAAS by erroring (non-JSON, non-zero) on an unknown/absent
# fabric id -- this is what tripped the live run when a placeholder id leaked through.
prof="${1:-}"; obj="${2:-}"; act="${3:-}"; fab="${4:-}"
case "$obj $act" in
  "subnets read")  cat "${FIX_SUBNETS:?}";  exit 0 ;;
  "spaces read")   cat "${FIX_SPACES:?}";   exit 0 ;;
  "ipranges read") cat "${FIX_IPRANGES:?}"; exit 0 ;;
  "fabrics read")  cat "${FIX_FABRICS:?}";  exit 0 ;;
  "vlans read")
    if jq -e --arg f "$fab" 'any(.[]; (.fabric_id|tostring)==$f)' "${FIX_VLANS:?}" >/dev/null 2>&1; then
      jq --arg f "$fab" '[.[]|select((.fabric_id|tostring)==$f)]' "${FIX_VLANS:?}"; exit 0
    fi
    echo "Unable to find fabric with id '$fab'." >&2; echo "Not Found"; exit 2 ;;
esac
echo "fake-maas: UNMATCHED call: $*" >&2; exit 1   # fail loud: an uncovered call must never pass silently
