# Handy tool to find AMIs:
#   https://cloud-images.ubuntu.com/locator/ec2/
#data "aws_ami" "ubuntu_pro" {
#  # Ubuntu Pro 18.04 Product ID: fc15dd7b-2aa0-47e5-bb05-94c75950b5de
#
#  most_recent = true
#  owners = [ "513442679011" ] # This seems like it might change sometimes?
#
#  # sadly, it looks like the image isn't named correctly in govcloud. I've given
#  # canonical feedback on this, but for now we'll use the ami id directly.
#  #filter {
#  #  name   = "name"
#  #  values = ["ubuntu-pro/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*", ]
#  #}
#
#  filter {
#    name   = "image-id"
#    values = [ "ami-0fe6338c47e61cd5d" ]
#  }
#
#  filter {
#    name   = "root-device-type"
#    values = ["ebs"]
#  }
#
#  filter {
#    name   = "virtualization-type"
#    values = ["hvm"]
#  }
#}

data "aws_security_group" "typical-host" {
  name   = "typical-host"
  vpc_id = var.vpc_id
}

# Use the default EBS key
data "aws_kms_key" "ebs-key" {
  key_id = "alias/ebs_root_encrypt_decrypt"
}

resource "aws_network_interface" "vmray-server-interface" {
  subnet_id = var.private_subnets[0]
  security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.vmray_server_sg.id ]
  description = "vmray-server"
  tags = merge(var.standard_tags, var.tags, { Name = "vmray-server" })
}

resource "aws_instance" "vmray-server-instance" {
  tenancy = "default"
  ebs_optimized = true
  disable_api_termination = var.instance_termination_protection
  instance_initiated_shutdown_behavior = "stop"
  instance_type          = var.instance_types["vmray-server"]
  key_name = "msoc-build"
  monitoring = false
  iam_instance_profile = "msoc-default-instance-profile"

  ami = data.aws_ami.ubuntu2004.image_id
  # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  # that could be removed.
  lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }

  root_block_device {
      volume_type = "gp3"
      volume_size = "60"
      delete_on_termination = true
      encrypted = true
      kms_key_id = data.aws_kms_key.ebs-key.arn
  }

  network_interface {
    device_index = 0
    network_interface_id = aws_network_interface.vmray-server-interface.id
  }

  user_data = data.template_cloudinit_config.cloud-init-vmray-server.rendered
  tags = merge( var.standard_tags, var.tags, { Name = "vmray-server" })
  volume_tags = merge( var.standard_tags, var.tags, { Name = "vmray-server" })
}

data "template_file" "cloud-init-vmray-server" {
  # Should these be in a common directory? I suspect they'd be reusable
  template = file("${path.module}/cloud-init/cloud-init.tpl")

  vars = {
    hostname = "vmray-server"
    fqdn = "vmray-server.${var.dns_info["private"]["zone"]}"
    environment = var.environment
    salt_master  = var.salt_master
    proxy = var.proxy
    aws_partition = var.aws_partition
    aws_partition_alias = var.aws_partition_alias
    aws_region = var.aws_region
  }
}

# Render a multi-part cloud-init config making use of the part
# above, and other source files
data "template_cloudinit_config" "cloud-init-vmray-server" {
  gzip          = true
  base64_encode = true

  # Main cloud-config configuration file.
  part {
    filename     = "init.cfg"
    content_type = "text/cloud-config"
    content      = data.template_file.cloud-init-vmray-server.rendered
  }

  # Additional parts as needed
  #part {
  #  content_type = "text/x-shellscript"
  #  content      = "ffbaz"
  #}
}

module "private_dns_record_vmray_server" {
  source = "../../submodules/dns/private_A_record"

  name = "vmray-server"
  ip_addresses = [ aws_instance.vmray-server-instance.private_ip ]
  dns_info = var.dns_info
  reverse_enabled = true

  providers = {
    aws.c2 = aws.c2
  }
}
