# Setup for the metadb gcs_bulk_load source. Single source of truth is the load
# source.yaml: the reader SA's project and the external bucket are read from it, so infra
# and the engine never drift. The bucket itself is externally owned (not created here) —
# this only provisions the read credential Cloud Lake uses for COPY/INFER_SCHEMA.

terraform {
  source = "${get_terragrunt_dir()}/../modules//gcs-reader-setup"
}

locals {
  src = yamldecode(file("${get_terragrunt_dir()}/../../../sources/metadb/source.yaml"))
}

generate "backend" {
  path      = "backend.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
terraform {
  backend "local" {
    path = "${get_terragrunt_dir()}/terraform.tfstate"
  }
}
EOF
}

inputs = {
  project         = local.src.gcs.reader.project
  source_name     = local.src.load.name
  external_bucket = split("/", trimprefix(local.src.external.uri, "gs://"))[0]
}
