SHELL := /bin/bash

ORGNAME := vod
APPNAME := achou-test
ACCOUNTNAME := twitch-shoryuken-aws
ACCOUNTID := $(shell aws sts get-caller-identity --profile ${ACCOUNTNAME} --output text --query 'Account')
AWSREGION := us-west-2
SERVICEBUCKET := $(ORGNAME)-$(APPNAME)-$(ACCOUNTID).$(AWSREGION).service-artifacts

update_privatelink: validate_templates
	# Usage: make update_privatelink ENVIRONMENT='development'
	aws s3 mb s3://$(SERVICEBUCKET) --profile $(ACCOUNTNAME) || true
	aws cloudformation package --template-file cloudformation/infra/privatelink.yaml \
		--s3-bucket $(SERVICEBUCKET) \
		--s3-prefix privatelink \
		--region $(AWSREGION) \
		--profile $(ACCOUNTNAME) \
		--output-template-file cloudformation/infra/transformed-privatelink.yaml
	aws cloudformation deploy --stack-name $(APPNAME)-$(ENVIRONMENT)-privatelink \
		--template-file cloudformation/infra/transformed-privatelink.yaml \
		--profile $(ACCOUNTNAME) \
		--parameter-overrides $$(jq -r '.[] | [.ParameterKey, .ParameterValue] | join("=")' cloudformation/infra/privatelink-params.json) \
		--capabilities CAPABILITY_NAMED_IAM
	rm cloudformation/infra/transformed-privatelink.yaml

update_vpcendpoint: validate_templates
	aws s3 mb s3://$(SERVICEBUCKET) --profile $(ACCOUNTNAME) || true
	aws cloudformation package --template-file cloudformation/global/vpcendpoint.yaml \
		--s3-bucket $(SERVICEBUCKET) \
		--s3-prefix vpcendpoint \
		--region $(AWSREGION) \
		--profile $(ACCOUNTNAME) \
		--output-template-file cloudformation/global/transformed-vpcendpoint.yaml
	aws cloudformation deploy --stack-name $(APPNAME)-vpcendpoint \
		--template-file cloudformation/global/transformed-vpcendpoint.yaml \
		--profile $(ACCOUNTNAME) \
		--parameter-overrides $$(jq -r '.[] | [.ParameterKey, .ParameterValue] | join("=")' cloudformation/global/vpcendpoint-params.json) \
		--capabilities CAPABILITY_NAMED_IAM
	rm cloudformation/global/transformed-vpcendpoint.yaml

validate_templates:
	aws cloudformation validate-template --template-body file://./cloudformation/infra/privatelink.yaml 1>/dev/null
	aws cloudformation validate-template --template-body file://./cloudformation/global/vpcendpoint.yaml 1>/dev/null
	@echo "CloudFormation templates are valid."
