module "vpc_info_x8" {
  source = "git@github.com:theorchard/terraform-vpc-info.git//?ref=3.2.0"

  environment = var.environment
}

module "neo4j_debian_13_environment_x8g" {
  source = "git@github.com:theorchard/terraform-neo4j-cluster.git//debian?ref=1.1.0"

  environment             = var.environment
  service_name            = "${var.service_name}-debian-13-standalone-testing_x8"
  vpc_id                  = module.vpc_info_x8.vpc_id
  vpc_private_subnet_ids  = module.vpc_info_x8.default_private_subnet_ids
  data_volume_device_name = "/dev/sdb"
  data_volume_device_type = "gp3"
  data_volume_iops        = 12000
  data_volume_size        = 6000
  data_volume_throughput  = 1250
  # it can't go any faster than that due to instance type limitations
  ssh_keypair_name                  = "orchard_admin"
  route53_zone_id                   = var.route53_zone_id
  data_volume_delete_on_termination = true
  aws_instance_type                 = "x8g.8xlarge"
  user_data                         = module.chef_bootstrap_neo4j_debian_13_x8.user_data_output

  ami_id = data.aws_ami.debian_13_arm64.id
  # Capacity will be gradually increased during cutover and backfilled after completion.
  desired_capacity = 3
  min_size         = 3
  max_size         = 3
}



resource "aws_security_group_rule" "allow_shared_jenkins_nodes_ssh_debian_13_x8" {
  type      = "ingress"
  from_port = 22
  to_port   = 22
  protocol  = "TCP"
  prefix_list_ids = [
    data.aws_ec2_managed_prefix_list.shared_account_prefix_list.id,
    data.aws_ec2_managed_prefix_list.vpn_sme_internal_primary.id,
    data.aws_ec2_managed_prefix_list.jump-box-private.id,
  ]
  security_group_id = module.neo4j_debian_13_environment_x8g.instance_security_group_id_output
}

resource "aws_security_group_rule" "allow_shared_jenkins_nodes_neo4j_debian_13_x8" {
  type      = "ingress"
  from_port = 7687
  to_port   = 7687
  protocol  = "TCP"
  prefix_list_ids = [
    data.aws_ec2_managed_prefix_list.shared_account_prefix_list.id,
    data.aws_ec2_managed_prefix_list.vpn_sme_internal_primary.id
  ]
  security_group_id = module.neo4j_debian_13_environment_x8g.instance_security_group_id_output
}

module "chef_bootstrap_neo4j_debian_13_x8" {
  source = "git@github.com:theorchard/terraform-chef-bootstrap.git//?ref=4.0.1"

  chef_role                    = "qa_neo4j_debian_13_standalone_testing"
  chef_environment             = var.environment
  aws_iam_role_id              = module.neo4j_debian_13_environment_x8g.instance_iam_role_name_output
  aws_instance_name            = "${var.environment}-${var.service_name}-debian-13-standalone-testing"
  ebs_data_mount_point         = "/var/lib/neo4j"
  block_device_filesystem_type = "xfs"
}

resource "aws_route53_record" "standalone_testing_discovery" {
  provider = aws.networking

  name    = "qa-neo4j-standalone-testing-discovery"
  zone_id = data.aws_route53_zone.networking_route53_zone.zone_id
  type    = "A"
  ttl     = 60
  records = [
    "10.10.10.10",
  ]

  # Allow DNS record updates via lifecycle hooks without Terraform trying to change things.
  lifecycle {
    ignore_changes = [
      records
    ]
  }
}

resource "aws_route53_record" "standalone_testing" {
  provider = aws.networking

  name    = "qa-neo4j-standalone-testing"
  zone_id = data.aws_route53_zone.networking_route53_zone.zone_id
  type    = "A"
  ttl     = 60
  # Record value itself is a placeholder
  records = [
    "10.10.10.10",
  ]

  # Allow DNS record updates via lifecycle hooks without Terraform trying to change things.
  lifecycle {
    ignore_changes = [
      records
    ]
  }
}

resource "aws_iam_role_policy_attachment" "qa_neo4j_debian_assume_role_policy_st_x8" {
  role       = module.neo4j_debian_13_environment_x8g.instance_iam_role_name_output
  policy_arn = aws_iam_policy.assume_cross_account_neo4j_dns_update_role_policy.arn
}

resource "aws_iam_role_policy_attachment" "prod_kms_backup_policy_attachment_deb_st_x8" {
  role       = module.neo4j_debian_13_environment_x8g.instance_iam_role_name_output
  policy_arn = data.aws_iam_policy.prod_neo4j_backup_kms_policy.arn
}

resource "aws_iam_role_policy_attachment" "prod_s3_backup_readonly_policy_attachment_deb_st_x8" {
  role       = module.neo4j_debian_13_environment_x8g.instance_iam_role_name_output
  policy_arn = data.aws_iam_policy.prod_neo4j_backup_s3_readonly_policy.arn
}

resource "aws_iam_role_policy_attachment" "qa_restore_policy_attachment_deb_st_x8" {
  role       = module.neo4j_debian_13_environment_x8g.instance_iam_role_name_output
  policy_arn = data.aws_iam_policy.secrets_manager_restore_policy.arn
}

module "datadog_neo4j_dashboards_and_monitors_testing" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/neo4j?ref=6.16.1"

  environment                = var.environment
  service_name               = "neo4j-standalone-testing"
  neo4j_major_version_number = 5
  neo4j_monitors_enabled     = false
  notification_endpoints     = ""
}
