# Some instance variables
locals {
  ami_selection       = "minion" # master, minion, ...
}

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

resource "aws_instance" "ghe-backup-instance" {
  tenancy = "default"
  ebs_optimized = true
  disable_api_termination = var.instance_termination_protection
  instance_initiated_shutdown_behavior = "stop"
  instance_type = var.backup_instance_type
  key_name = "msoc-build"
  monitoring = false
  iam_instance_profile = "msoc-default-instance-profile"

  ami = local.ami_map[local.ami_selection]
  # 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 ] }
  #lifecycle { ignore_changes = [ ami, key_name, user_data ] }

  # These device definitions are optional, but added for clarity.
  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
  }

  ebs_block_device {
    # swap
    device_name = "/dev/xvdm"
    volume_type = "gp3"
    volume_size = 8
    delete_on_termination = true
    encrypted = true
    kms_key_id = data.aws_kms_key.ebs-key.arn
    # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
    # This may prompt replacement when the AMI is updated.
    # See:
    #   https://github.com/hashicorp/terraform/issues/19958
    #   https://github.com/terraform-providers/terraform-provider-aws/issues/13118
    snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  }
  ebs_block_device {
    # /home
    device_name = "/dev/xvdn"
    volume_type = "gp3"
    # volume_size = xx
    delete_on_termination = true
    encrypted = true
    kms_key_id = data.aws_kms_key.ebs-key.arn
    snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id

  }
  ebs_block_device {
    # /var
    device_name = "/dev/xvdo"
    volume_type = "gp3"
    # volume_size = xx
    delete_on_termination = true
    encrypted = true
    kms_key_id = data.aws_kms_key.ebs-key.arn
    snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  }
  ebs_block_device {
    # /var/tmp
    device_name = "/dev/xvdp"
    volume_type = "gp3"
    # volume_size = xx
    delete_on_termination = true
    encrypted = true
    kms_key_id = data.aws_kms_key.ebs-key.arn
    snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  }
  ebs_block_device {
    # /var/log
    device_name = "/dev/xvdq"
    volume_type = "gp3"
    # volume_size = xx
    delete_on_termination = true
    encrypted = true
    kms_key_id = data.aws_kms_key.ebs-key.arn
    snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  }
  ebs_block_device {
    # /var/log/audit
    device_name = "/dev/xvdr"
    volume_type = "gp3"
    # volume_size = xx
    delete_on_termination = true
    encrypted = true
    kms_key_id = data.aws_kms_key.ebs-key.arn
    snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  }
  ebs_block_device {
    # /tmp
    device_name = "/dev/xvds"
    volume_type = "gp3"
    # volume_size = xx
    delete_on_termination = true
    encrypted = true
    kms_key_id = data.aws_kms_key.ebs-key.arn
    snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  }

  network_interface {
    device_index = 0
    network_interface_id = aws_network_interface.ghe-backup-interface.id
  }

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

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

  vars = {
    hostname = "ghe-backup"
    fqdn = "ghe-backup.${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" {
  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.rendered
  }

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

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

  name = "ghe-backup"
  ip_addresses = [ aws_instance.ghe-backup-instance.private_ip ]
  dns_info = var.dns_info
  reverse_enabled = var.reverse_enabled

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