# Terraform CLI configuration (~/.terraformrc)
# Documentation:
#   CLI config / provider_installation / dev_overrides:
#     https://developer.hashicorp.com/terraform/cli/config/config-file
#   Provider mirror:
#     https://developer.hashicorp.com/terraform/cli/commands/providers/mirror
#   Custom / local providers (dev_overrides + local binary):
#     https://developer.hashicorp.com/terraform/plugin/debugging
#     https://developer.hashicorp.com/terraform/plugin/how-terraform-works
#     https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-provider
#   Plugin Framework (implement a provider):
#     https://developer.hashicorp.com/terraform/plugin/framework
#   Publish / private registry (optional later):
#     https://developer.hashicorp.com/terraform/registry/providers/publishing
#
# Deployed by `infra setup terraform`. Non-empty existing files are preserved
# (recommended copy at ~/.terraformrc.infra).
#
# IMPORTANT: .terraformrc does NOT expand $HOME or ~ in path settings.
# Always use absolute paths under /opt/terraform/… — never hardcode a personal
# home directory, and never use $HOME or ~ in path values.
# See: https://github.com/hashicorp/terraform/issues/27446
#
# Machine-local layout (create once with sudo):
#   sudo mkdir -p /opt/terraform/{mirror,cache,dev/demo}
#   sudo chown -R "$USER" /opt/terraform
#
# Default: filesystem_mirror for HashiCorp providers (tried first) + direct
# registry fallback when a provider zip is missing from the mirror, plus
# plugin_cache_dir for non-mirrored installs. Do NOT point filesystem_mirror
# at /opt/terraform/cache — that tree is unpacked plugin_cache_dir layout,
# not packed .zip (terraform providers mirror). Uncomment local-namespace
# dev_overrides when actively developing a provider (Terraform warns while
# any override is set).

# --- Provider installation (mirror + local overrides) ---
# Populate mirror (must run against a Terraform config root):
#   infra setup terraform && source ~/.infra/env.sh
#   infra terraform mirror -e ENV -s STACK -p darwin_arm64 -p linux_amd64
#   # raw: cd /path/to/root && terraform providers mirror \
#   #        -platform=darwin_arm64 -platform=linux_amd64 \
#   #        /opt/terraform/mirror
#
# Local / self-built provider binary (dev_overrides):
#   Docs: https://developer.hashicorp.com/terraform/plugin/debugging
#         https://developer.hashicorp.com/terraform/cli/config/config-file
#         (# Development Overrides for Provider Developers)
#   mkdir -p /opt/terraform/dev/demo
#   cd /path/to/terraform-provider-demo
#   go build -o /opt/terraform/dev/demo/terraform-provider-demo
# In .tf: source = "example.com/local/demo"  (version ignored under override)
# Terraform warns on every command while any override entry exists — remove
# entries you are not actively developing.

provider_installation {
  # --- Local / in-development providers (MUST be first; Terraform enforces this) ---
  # Affects ONLY these addresses; mirror/direct below still apply to everyone
  # else. Add more lines for internal providers, e.g.:
  #   "example.com/acme/itsm" = "/opt/terraform/dev/itsm"
  # Path = directory that contains the provider executable
  # (terraform-provider-<NAME>). Absolute /opt/terraform/dev/… only.
  # Fork official providers only when needed (replaces that provider):
  #   "registry.terraform.io/hashicorp/aws" = "/opt/terraform/dev/aws"
  #dev_overrides {
  #  "example.com/local/demo" = "/opt/terraform/dev/demo"
  #}

  filesystem_mirror {
    path    = "/opt/terraform/mirror"
    include = ["registry.terraform.io/hashicorp/*"]
  }
  # Mirror-first, registry fallback: omit direct.exclude so a HashiCorp
  # provider missing from /opt/terraform/mirror (e.g. hashicorp/external after
  # only mirroring aws) still installs via registry.terraform.io.
  # Air-gap / no-network: uncomment exclude ONLY after the mirror holds every
  # required_providers address — otherwise init fails with
  # "not found in any of the search locations".
  #direct {
  #  exclude = ["registry.terraform.io/hashicorp/*"]
  #}
  direct {}
}

# --- Unpacked plugin cache for providers still installed via direct ---
# Complements filesystem_mirror: HashiCorp zips come from /opt/terraform/mirror;
# this cache speeds re-init of non-mirrored (direct) providers only.
# Do NOT enable plugin_cache_may_break_dependency_lock_file (breaks portable zh:).

plugin_cache_dir = "/opt/terraform/cache"

# plugin_cache_may_break_dependency_lock_file = true

# --- Provider installation: full registry mirror (air-gapped) ---
# Same populate step — then widen include/exclude:

# provider_installation {
#   # keep your dev_overrides { … } here if still needed
#   filesystem_mirror {
#     path    = "/opt/terraform/mirror"
#     include = ["registry.terraform.io/*/*"]
#   }
#   direct {
#     exclude = ["registry.terraform.io/*/*"]
#   }
# }

# --- Provider installation: local plugins dir (legacy unpacked layout) ---

# provider_installation {
#   filesystem_mirror {
#     path    = "/opt/terraform/dev/plugins"
#     include = ["registry.terraform.io/*/*"]
#   }
#   direct {
#     exclude = ["registry.terraform.io/*/*"]
#   }
# }

# --- Telemetry (uncomment to opt out of HashiCorp checkpoint) ---

# disable_checkpoint = true
# disable_checkpoint_signature = true

# --- Terraform Cloud / Enterprise ---

# credentials "app.terraform.io" {
#   token = "YOUR_TFC_TOKEN"
# }

# hosts {
#   name = "app.terraform.io"
#   services = {
#     modules = "https://app.terraform.io/api/registry/v1/modules/"
#   }
# }

# --- Network mirror (private registry proxy) ---

# provider_installation {
#   network_mirror {
#     url = "https://terraform-mirror.example.internal/"
#   }
#   direct {
#     exclude = ["registry.terraform.io/*/*"]
#   }
# }
