# Configure Consul provider
provider "consul" {
  address = var.consul_address
}

# Get Nomad client from Consul
data "consul_service" "nomad" {
  name = "nomad-client"
}

# Get Vault client from Consul
data "consul_service" "vault" {
  name = "vault"
  tag  = "active"
}

locals {
  # Get Nomad address from Consul
  nomad_node         = data.consul_service.nomad.service[0]
  nomad_node_address = "http://${local.nomad_node.node_address}:${local.nomad_node.port}"

  # Get Vault address from Consul
  vault_node         = data.consul_service.vault.service[0]
  vault_node_address = "http://${local.vault_node.node_address}:${local.vault_node.port}"
}

# Configure the Nomad provider
provider "nomad" {
  address   = local.nomad_node_address
  secret_id = var.nomad_secret_id
  region    = "global"
}

# Configure the Vault provider
provider "vault" {
  address = local.vault_node_address
  token   = var.vault_token
}