# 查看Oracle云实例状态脚本
# 用于查看美国区和韩国区的实例状态

# 韩国区配置 - 使用默认provider
# 美国区配置 - 使用us alias

# 获取韩国区的所有实例
data "oci_core_instances" "korea_instances" {
  compartment_id = data.consul_keys.oracle_config.var.tenancy_ocid
  
  filter {
    name   = "lifecycle_state"
    values = ["RUNNING", "STOPPED", "STOPPING", "STARTING"]
  }
}

# 获取美国区的所有实例
data "oci_core_instances" "us_instances" {
  provider       = oci.us
  compartment_id = data.consul_keys.oracle_config_us.var.tenancy_ocid
  
  filter {
    name   = "lifecycle_state"
    values = ["RUNNING", "STOPPED", "STOPPING", "STARTING"]
  }
}

# 获取韩国区实例的详细信息
data "oci_core_instance" "korea_instance_details" {
  count       = length(data.oci_core_instances.korea_instances.instances)
  instance_id = data.oci_core_instances.korea_instances.instances[count.index].id
}

# 获取美国区实例的详细信息
data "oci_core_instance" "us_instance_details" {
  provider    = oci.us
  count       = length(data.oci_core_instances.us_instances.instances)
  instance_id = data.oci_core_instances.us_instances.instances[count.index].id
}

# 输出韩国区实例信息
output "korea_instances" {
  description = "韩国区实例状态"
  value = {
    count = length(data.oci_core_instances.korea_instances.instances)
    instances = [
      for instance in data.oci_core_instance.korea_instance_details : {
        id           = instance.id
        name         = instance.display_name
        state        = instance.state
        shape        = instance.shape
        region       = "ap-chuncheon-1"
        ad           = instance.availability_domain
        public_ip    = instance.public_ip
        private_ip   = instance.private_ip
        time_created = instance.time_created
      }
    ]
  }
}

# 输出美国区实例信息
output "us_instances" {
  description = "美国区实例状态"
  value = {
    count = length(data.oci_core_instances.us_instances.instances)
    instances = [
      for instance in data.oci_core_instance.us_instance_details : {
        id           = instance.id
        name         = instance.display_name
        state        = instance.state
        shape        = instance.shape
        region       = "us-ashburn-1"
        ad           = instance.availability_domain
        public_ip    = instance.public_ip
        private_ip   = instance.private_ip
        time_created = instance.time_created
      }
    ]
  }
}

# 输出总计信息
output "summary" {
  description = "实例总计信息"
  value = {
    total_instances = length(data.oci_core_instances.korea_instances.instances) + length(data.oci_core_instances.us_instances.instances)
    korea_count    = length(data.oci_core_instances.korea_instances.instances)
    us_count       = length(data.oci_core_instances.us_instances.instances)
  }
}