# Nomad 多数据中心集群模块
# 支持跨地域部署：CN(dc1) + KR(dc2) + US(dc3)

terraform {
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 5.0"
    }
    huaweicloud = {
      source  = "huaweicloud/huaweicloud"
      version = "~> 1.60"
    }
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# 本地变量
locals {
  nomad_version = "1.10.5"
  
  # 通用 Nomad 配置
  nomad_encrypt_key = "NVOMDvXblgWfhtzFzOUIHnKEOrbXOkPrkIPbRGGf1YQ="
  
  # 数据中心配置
  datacenters = {
    dc1 = {
      name     = "dc1"
      region   = "cn"
      location = "China"
      provider = "existing" # 现有的 semaphore 节点
    }
    dc2 = {
      name     = "dc2" 
      region   = "kr"
      location = "Korea"
      provider = "oracle"
    }
    dc3 = {
      name     = "dc3"
      region   = "us"
      location = "US"
      provider = "huawei" # 或 aws
    }
  }
  
  # 用户数据模板
  user_data_template = templatefile("${path.module}/templates/nomad-userdata.sh", {
    nomad_version     = local.nomad_version
    nomad_encrypt_key = local.nomad_encrypt_key
  })
}

# 数据源：获取现有的 semaphore 节点信息
data "external" "semaphore_info" {
  program = ["bash", "-c", <<-EOF
    echo '{
      "ip": "100.116.158.95",
      "datacenter": "dc1",
      "status": "existing"
    }'
  EOF
  ]
}

# Oracle Cloud 韩国节点 (dc2)
module "oracle_korea_node" {
  source = "../compute"
  
  count = var.deploy_korea_node ? 1 : 0
  
  # Oracle Cloud 特定配置
  provider_type = "oracle"
  
  # 实例配置
  instance_config = {
    name          = "nomad-master-kr"
    datacenter    = "dc2"
    instance_type = "VM.Standard.E2.1.Micro" # 免费层
    image_id      = var.oracle_ubuntu_image_id
    subnet_id     = var.oracle_subnet_id
    
    # Nomad 配置
    nomad_role        = "server"
    bootstrap_expect  = 1
    bind_addr        = "auto" # 自动检测
    
    # 网络配置
    security_groups = [var.oracle_security_group_id]
    
    # 标签
    tags = merge(var.common_tags, {
      Name       = "nomad-master-kr"
      Datacenter = "dc2"
      Role       = "nomad-server"
      Provider   = "oracle"
    })
  }
  
  # 用户数据
  user_data = templatefile("${path.module}/templates/nomad-userdata.sh", {
    datacenter        = "dc2"
    nomad_version     = local.nomad_version
    nomad_encrypt_key = local.nomad_encrypt_key
    bootstrap_expect  = 1
    bind_addr        = "auto"
    server_enabled   = true
    client_enabled   = true
  })
}

# 华为云美国节点 (dc3)
module "huawei_us_node" {
  source = "../compute"
  
  count = var.deploy_us_node ? 1 : 0
  
  # 华为云特定配置
  provider_type = "huawei"
  
  # 实例配置
  instance_config = {
    name          = "nomad-ash3c-us"
    datacenter    = "dc3"
    instance_type = "s6.small.1" # 1vCPU 1GB
    image_id      = var.huawei_ubuntu_image_id
    subnet_id     = var.huawei_subnet_id
    
    # Nomad 配置
    nomad_role       = "server"
    bootstrap_expect = 1
    bind_addr       = "auto"
    
    # 网络配置
    security_groups = [var.huawei_security_group_id]
    
    # 标签
    tags = merge(var.common_tags, {
      Name       = "nomad-ash3c-us"
      Datacenter = "dc3"
      Role       = "nomad-server"
      Provider   = "huawei"
    })
  }
  
  # 用户数据
  user_data = templatefile("${path.module}/templates/nomad-userdata.sh", {
    datacenter        = "dc3"
    nomad_version     = local.nomad_version
    nomad_encrypt_key = local.nomad_encrypt_key
    bootstrap_expect  = 1
    bind_addr        = "auto"
    server_enabled   = true
    client_enabled   = true
  })
}