data "aws_ami" "ubuntu_pro" {
  # Ubuntu Pro 18.04 Product ID: fc15dd7b-2aa0-47e5-bb05-94c75950b5de

  most_recent = true
  owners = [ var.aws_marketplace_ubuntu_owner_id ]

  # 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-0a4050943619c0460" ]
  }

  filter {
    name   = "root-device-type"
    values = ["ebs"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}

module "vmray-server" {
  source                 = "terraform-aws-modules/ec2-instance/aws"
  version                = "~> 2.0"

  name                   = "vmray-server"
  instance_count         = 1
  disable_api_termination = var.instance_termination_protection

  ami                    = data.aws_ami.ubuntu_pro.image_id
  instance_type          = var.vmray_server_instance_type
  key_name               = var.vmray_key_name
  vpc_security_group_ids = [ aws_security_group.vmray_sg.id ]
  subnet_id              = data.terraform_remote_state.standard_vpc.outputs.public_subnets[0]
  tags = merge(var.standard_tags, var.tags)

  ebs_optimized          = true
  monitoring             = false # Do we use this?

  user_data_base64 = data.template_cloudinit_config.cloud-init-vmray-server.rendered
}

#
module "vmray-worker" {
  source                 = "terraform-aws-modules/ec2-instance/aws"
  version                = "~> 2.0"

  name                   = "vmray-worker"
  instance_count         = var.vmray_worker_instance_count
  disable_api_termination = var.instance_termination_protection

  ami                    = data.aws_ami.ubuntu_pro.image_id
  instance_type          = var.vmray_worker_instance_type
  key_name               = var.vmray_key_name
  vpc_security_group_ids = [ aws_security_group.vmray_sg.id ]
  subnet_ids             = data.terraform_remote_state.standard_vpc.outputs.public_subnets
  tags = merge(var.standard_tags, var.tags)

  ebs_optimized          = true
  monitoring             = false

  #user_data_base64 = data.template_cloudinit_config.vmray-worker.*.rendered
}


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 = "vmrayserver.TODO.makemeavariable"
    environment = var.environment
  }
}

# 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"
  #}
}
