# Test, build, deploy and live targets for fortinet-socaas.
#   make playbooks INSTANCE=<alias>     regenerate ingestion.yaml + compile playbooks.json
#   make test                           offline pytest against the loopback SOCaaS fake (no tenant)
#   make pack                           build dist/fortinet-socaas-<version>.tgz
#   make deploy INSTANCE=<alias>        install/replace the connector tgz on the box
#   make configure INSTANCE=<alias>     write the connector configuration from LIVE_ENV
#   make healthcheck INSTANCE=<alias>   run the connector's health check on the box
#   make smoke INSTANCE=<alias>         read-only live op checks (needs LIVE_ENV for the config)
#   make probe                          read-only live probe of the SOCaaS API (needs LIVE_ENV)
#   make ingestion-setup INSTANCE=<alias>  set up the Data Ingestion Wizard (schedule left DISABLED)
#   make ingestion INSTANCE=<alias>     live proof of the ingestion playbooks (C1, C2, C4; C3 is N/A)
# Credentials load from an envrun file (never printed):
#   make probe LIVE_ENV=.env.socaas
# Setup: see README "Setting up a test environment".

VENV ?= .venv
PY ?= python3
PYTHON := $(VENV)/bin/python
PYFSR ?= pyfsr
PYFSR_PY ?= $(HOME)/PycharmProjects/pyfsr/.venv/bin/python
INSTANCE ?=
LIVE_ENV ?=
ENVRUN := $(if $(LIVE_ENV),envrun $(LIVE_ENV) --,)

# the fsr-playbook-framework checkout (ingestion template + fsrpb), for regenerating
FSR_FRAMEWORK ?= $(HOME)/PycharmProjects/fsr-playbook-framework

.PHONY: setup test playbooks mocks pack deploy configure healthcheck smoke probe ingestion-setup ingestion

setup: $(VENV)/.installed

$(VENV)/.installed: requirements.txt
	$(PY) -m venv $(VENV)
	$(VENV)/bin/pip install -q pytest pytest-cov pydantic requests pyyaml
	@touch $@

test: setup
	PYTHONPATH="$(FSR_FRAMEWORK)" \
		$(PYTHON) -m pytest tests/ -q --cov=. --cov-config=.coveragerc --cov-report=term

playbooks: setup
	PYTHONPATH="$(FSR_FRAMEWORK):$(HOME)/PycharmProjects/pyfsr" \
		$(FSR_FRAMEWORK)/.venv/bin/python playbooks/ingestion_spec.py
	PATH="$(FSR_FRAMEWORK)/.venv/bin:$(PATH)" \
		PYTHONPATH="$(HOME)/PycharmProjects/pyfsr" \
		$(HOME)/PycharmProjects/pyfsr/.venv/bin/python playbooks/build.py
	$(FSR_FRAMEWORK)/.venv/bin/fsrpb graph playbooks/ingestion.yaml
	$(FSR_FRAMEWORK)/.venv/bin/fsrpb graph playbooks/playbooks.yaml

mocks: setup
	$(PYTHON) tools/build_mocks.py

pack: setup
	$(PYFSR) connector package .

deploy:
	@test -n "$(INSTANCE)" || { echo "usage: make deploy INSTANCE=<pyfsr-alias>" >&2; exit 2; }
	$(PYFSR) connector deploy dist/fortinet-socaas-*.tgz --instance $(INSTANCE) --replace

configure:
	@test -n "$(INSTANCE)" || { echo "usage: make configure INSTANCE=<pyfsr-alias>" >&2; exit 2; }
	$(ENVRUN) env $(PYFSR_PY) tools/setup_config.py --instance $(INSTANCE)

healthcheck:
	@test -n "$(INSTANCE)" || { echo "usage: make healthcheck INSTANCE=<pyfsr-alias>" >&2; exit 2; }
	$(ENVRUN) env $(PYFSR_PY) tools/healthcheck.py --instance $(INSTANCE)

smoke:
	@test -n "$(INSTANCE)" || { echo "usage: make smoke INSTANCE=<pyfsr-alias>" >&2; exit 2; }
	$(ENVRUN) env $(PYFSR_PY) tools/smoke_ops.py --instance $(INSTANCE)

probe: setup
	$(ENVRUN) env $(PYTHON) tools/probe_socaas.py $(ARGS)

ingestion-setup: setup
	@test -n "$(INSTANCE)" || { echo "usage: make ingestion-setup INSTANCE=<pyfsr-alias>" >&2; exit 2; }
	$(ENVRUN) env PYFSR_INSTANCE=$(INSTANCE) $(PYFSR_PY) tools/ingestion_setup.py

ingestion: setup
	@test -n "$(INSTANCE)" || { echo "usage: make ingestion INSTANCE=<pyfsr-alias>" >&2; exit 2; }
	$(ENVRUN) env PYFSR_INSTANCE=$(INSTANCE) $(PYFSR_PY) tools/live_ingestion.py
