export TAG_ENGINE_URL="https://tag-engine-api-eshsagj3ta-uc.a.run.app" # replace with your Cloud Run URL

# Bearer token, used for each API request
export IAM_TOKEN=$(gcloud auth print-identity-token)

####### tag template section #######
# Note: tag templates are referenced by the following unit tests. 
# Create the tag templates before creating the Tag Engine configurations. 

export TEMPLATE_PROJECT="tag-engine-run"
export TEMPLATE_REGION="us-central1"

cd datacatalog-templates/
pip install -r requirements.txt

python create_template.py $TEMPLATE_PROJECT $TEMPLATE_REGION tag_templates/data_governance.yaml
python create_template.py $TEMPLATE_PROJECT $TEMPLATE_REGION tag_templates/data_sensitivity.yaml
python create_template.py $TEMPLATE_PROJECT $TEMPLATE_REGION tag_templates/data_discovery.yaml
python create_template.py $TEMPLATE_PROJECT $TEMPLATE_REGION tag_templates/compliance_template.yaml

cd datacatalog-tag-engine/

####### dynamic tags #######

# create config
curl -X POST $TAG_ENGINE_URL/create_dynamic_table_config -d @examples/configs/dynamic_table/tag_templates/dynamic_table_ondemand.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"DYNAMIC_TAG_TABLE","config_uuid":"416f9694e46911ed96c5acde48001122"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

# create config
curl -X POST $TAG_ENGINE_URL/create_dynamic_column_config -d @examples/configs/dynamic_column/tag_templates/dynamic_column_ondemand.json \
  -H "Authorization: Bearer $IAM_TOKEN"

# trigger job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"DYNAMIC_TAG_COLUMN","config_uuid":"18e06b5ad64e11edb9fdf1930a40c33e"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

####### Import tags from CSV to BigQuery table and column #######

# create an import config for tagging tables
curl -X POST $TAG_ENGINE_URL/create_import_config -d @examples/configs/import/tag_templates/bigquery_table_config.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger the job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_IMPORT","config_uuid":"0e674d78eddd11ed8d3d09299afaece0"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

# alternatively, trigger the job with metadata
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_IMPORT","config_uuid":"0e674d78eddd11ed8d3d09299afaece0", "job_metadata": {"source": "Collibra", "workflow": "process_sensitive_data"}}' \
  -H "Authorization: Bearer $IAM_TOKEN"

# get the job status 
curl -X POST $TAG_ENGINE_URL/get_job_status -d '{"job_uuid":"2755de6ceddd11ed9e0e3f1388bde9d6"}' \
	-H "Authorization: Bearer $IAM_TOKEN"

# create an import config for tagging columns
curl -X POST $TAG_ENGINE_URL/create_import_config -d @examples/configs/import/tag_templates/bigquery_column_config.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger the job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_IMPORT","config_uuid":"426ddac4eddd11ed9e0e3f1388bde9d6"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

# get the job status 
curl -X POST $TAG_ENGINE_URL/get_job_status -d '{"job_uuid":"4abb1a0ceddd11edb0341b486213f8b6"}' \
	-H "Authorization: Bearer $IAM_TOKEN"

# create an import config for tagging datasets
curl -X POST $TAG_ENGINE_URL/create_import_config -d @examples/configs/import/tag_templates/bigquery_dataset_config.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger the job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_IMPORT","config_uuid":"7d4618be478211efa28142004e494300"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

# get the job status 
curl -X POST $TAG_ENGINE_URL/get_job_status -d '{"job_uuid":"75052fd2478211efa28142004e494300"}' \
	-H "Authorization: Bearer $IAM_TOKEN"


####### Import tags from CSV to GCS filesets #######

# create an import config for tagging filesets
curl -X POST $TAG_ENGINE_URL/create_import_config -d @examples/configs/import/tag_templates/fileset_config.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_IMPORT","config_uuid":"0e674d78eddd11ed8d3d09299afaece0"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

# create an import config for tagging fileset columns
curl -X POST $TAG_ENGINE_URL/create_import_config -d @examples/configs/import/tag_templates/fileset_column_config.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_IMPORT","config_uuid":"0e674d78eddd11ed8d3d09299afaece0"}' \
  -H "Authorization: Bearer $IAM_TOKEN"


####### Import tags from CSV to Spanner tables #######

# create an import config for tagging Spanner tables 
curl -X POST $TAG_ENGINE_URL/create_import_config -d @examples/configs/import/tag_templates/spanner_table_config.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_IMPORT","config_uuid":"0e674d78eddd11ed8d3d09299afaece0"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

# create an import config for tagging Spanner table columns
curl -X POST $TAG_ENGINE_URL/create_import_config -d @examples/configs/import/tag_templates/spanner_column_config.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_IMPORT","config_uuid":"0e674d78eddd11ed8d3d09299afaece0"}' \
  -H "Authorization: Bearer $IAM_TOKEN"


####### Export tags to BQ #######

# create config
curl -X POST $TAG_ENGINE_URL/create_export_config -d @examples/configs/export/export_by_project.json \
	-H "Authorization: Bearer $IAM_TOKEN"

# trigger job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"TAG_EXPORT","config_uuid":"ffe5e8bae07411ed8776acde48001122"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

# get job status
curl -X POST $TAG_ENGINE_URL/get_job_status -d '{"job_uuid":"f106ae4aef4911edb86ee96cdaa8e7ae"}' \
	-H "Authorization: Bearer $IAM_TOKEN"


####### Trigger job by uris #######

curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"DYNAMIC_TAG_TABLE","included_tables_uris":"bigquery/project/tag-engine-run/dataset/GCP_Mockup/*","template_id":"data_governance","template_project":"tag-engine-run","template_region":"us-central1"}' \
  -H "Authorization: Bearer $IAM_TOKEN"


####### Job status #######

curl -X POST $TAG_ENGINE_URL/get_job_status -d '{"job_uuid":"0d5cee42e0a911edb2ec3becb01f33e6"}' \
	-H "Authorization: Bearer $IAM_TOKEN"


####### Scheduled auto updates #######

curl -i -X POST $TAG_ENGINE_URL/scheduled_auto_updates \
  -H "Authorization: Bearer $IAM_TOKEN"


####### List configs #######
curl -i -X POST $TAG_ENGINE_URL/list_configs \
  -d '{"config_type":"ALL"}' \
  -H "Authorization: Bearer $IAM_TOKEN"


####### Read config #######
curl -i -X POST $TAG_ENGINE_URL/get_config \
  -d '{"config_type":"DYNAMIC_TAG_COLUMN", "config_uuid": "96cb3764d5ab11ed936ef9fa48b6860b"}' \
  -H "Authorization: Bearer $IAM_TOKEN"


####### Delete config #######
curl -i -X POST $TAG_ENGINE_URL/delete_config \
  -d '{"config_type":"DYNAMIC_TAG_COLUMN", "config_uuid": "13bea024d56811ed95362762b95fd865"}' \
  -H "Authorization: Bearer $IAM_TOKEN"


####### Purge inactive configs #######
curl -i -X POST $TAG_ENGINE_URL/purge_inactive_configs \
  -d '{"config_type":"ALL"}' \
  -H "Authorization: Bearer $IAM_TOKEN" 

####### Testing with service account override #######

export IAM_TOKEN=$(gcloud auth print-identity-token)
export GOOGLE_APPLICATION_CREDENTIALS="private_key.json"

# create config
curl -X POST $TAG_ENGINE_URL/create_dynamic_table_config -d @examples/configs/dynamic_table/tag_templates/dynamic_dataset_non_default_service_account.json \
	-H "Authorization: Bearer $IAM_TOKEN" 

# trigger job
curl -i -X POST $TAG_ENGINE_URL/trigger_job \
  -d '{"config_type":"DYNAMIC_TAG_TABLE","config_uuid":"b4028a74e44911eda52be10c4bdcfc22"}' \
  -H "Authorization: Bearer $IAM_TOKEN"

curl -X POST $TAG_ENGINE_URL/get_job_status -d '{"job_uuid":"a6384b16e45011ed85e3d3e918adc4d7"}' \
	-H "Authorization: Bearer $IAM_TOKEN"
