# Docker registry to upload the image
DOCKER_REGISTRY = devxci
# K8S namespace to deploy the project artifacts (should the the ns where the services are provisioned)
NAMESPACE = <%= namespace %>
# Docker tag
DOCKER_TAG = latest
# Application events params: compassURL, origin, token, tenant, query
# compass URL - target URL for Management plan query (fixed for POC)
COMPASS_URL =
# origin - origin URL (fixed for POC)
ORIGIN =
# token - authorization Bearer key (input from user)
TOKEN =
# tenant - tenant id of Application (input from user)
TENANT =
# query - Object query of Runtime, Applications and events (fixed for POC)
QUERY_RUNTIME = '{"query":"query {\nruntimes {\n  data {\n    id,\n    name\n    \n  }\n}\n}\n"}'
QUERY_APP = '{"query":"query {\napplications {\n  data {\n    id,\n    providerName,\n    description,\n    name\n    \n  }\n}\n}\n"}'
QUERY_APP2 = '{"query":" Write your query or mutation here\nquery {\napplications {\n  data {\n    id,\n    providerName,\n    description,\n    name\n    \n  }\n}\n}\n"}'
REQUEST_HEADERS = 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1'
QUERY_APP_EVENTS = '{"query":"query {\n	application(id: "$(appid)") {\n    id,\n    name\n    apiDefinitions {\n     data {\n      id\n      name\      description\n      targetURL\n      defaultAuth {\n       credential {\n ....}\n}\n}\n"}'

# build docker image
build:
	img build -t $(DOCKER_REGISTRY)/<%= appName %>:$(DOCKER_TAG) .
# push docker image
push:
	img push $(DOCKER_REGISTRY)/<%= appName %>:$(DOCKER_TAG)
# update deployment yaml with the new image
update-deployment:
	yq w -i  k8s/deployment.yaml 'spec.template.spec.containers[0].image' $(DOCKER_REGISTRY)/<%= appName %>:$(DOCKER_TAG)
# apply application resources to k8s
k8s-apply:
	kubectl apply -f k8s/ -n $(NAMESPACE)
# delete all k8s resources
k8s-delete:
	kubectl delete -f k8s/ -n $(NAMESPACE)
# apply k8s deployment
k8s-apply-deployment:
	kubectl apply -f k8s/deployment.yaml -n $(NAMESPACE)
# delete k8s deployment
k8s-delete-deployment:
	kubectl delete --wait=false -f k8s/deployment.yaml -n $(NAMESPACE)
# execute build push & update-deployment targets
deploy: build push update-deployment k8s-apply
# trigger events
application-events: validate-token
	curl -i -k $(COMPASS_URL) -H  $(REQUEST_HEADERS) -H $(ORIGIN) -H $(TOKEN) -H $(TENANT) --data-binary $(QUERY_APP) --compressed
application-events2:
	curl -i -k $(COMPASS_URL) -H $(REQUEST_HEADERS) -H $(ORIGIN) -H $(TOKEN) -H $(TENANT) --data-binary $(QUERY_APP2) --compressed
# the token is a pre-requisite for triggeing events
validate-token: validate-token
	test -n "$(TOKEN)"


