variable "environment" {}
variable "service_name" {}

variable "backup_port" {}

provider "aws" {
  region = "us-east-1"
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/neo4j-cluster/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "neo4j_environment" {
  source = "../modules"

  environment  = var.environment
  service_name = var.service_name
  vpc_id       = "vpc-7f0e841a"
  vpc_private_subnet_ids = [
    "subnet-067a6b45b079d7296",
    "subnet-043d235081c966332",
  ]
  data_volume_device_name           = "/dev/sdb"
  ssh_keypair_name                  = "orchard_admin"
  route53_zone_id                   = "Z2RSKZ9M7TJ9Z"
  dbms_backup_s3_bucket             = "qa-orcdbucket"
  data_volume_delete_on_termination = true
  aws_instance_type                 = "r5.xlarge"
  user_data                         = module.chef_bootstrap_neo4j.user_data_output
  backup_user_data                  = module.chef_bootstrap_neo4j_backup.user_data_output
  backup_port                       = var.backup_port
  bolt_ingress_cidr_blocks = [
    "10.10.40.0/22",
    "10.30.0.0/22",
    "10.40.0.0/22",
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
  ]
}

module "chef_bootstrap_neo4j" {
  source = "git@github.com:theorchard/terraform-chef-bootstrap.git"

  chef_version      = "14.13.11"
  chef_server       = "chef04.qaorch.com"
  chef_server_url   = "https://chef04.qaorch.com/organizations/orchard"
  s3_chef_bucket    = "orcd-chef-validator-keys"
  chef_environment  = "_default"
  chef_role         = "qa_neo4j_cluster"
  log_file          = "/var/log/chef/bootstrap.log"
  aws_iam_role_id   = module.neo4j_environment.instance_iam_role_name_output
  aws_instance_name = "${var.environment}-${var.service_name}"
}

module "chef_bootstrap_neo4j_backup" {
  source = "git@github.com:theorchard/terraform-chef-bootstrap.git"

  chef_version      = "14.13.11"
  chef_server       = "chef04.qaorch.com"
  chef_server_url   = "https://chef04.qaorch.com/organizations/orchard"
  s3_chef_bucket    = "orcd-chef-validator-keys"
  chef_environment  = "_default"
  chef_role         = "qa_neo4j_cluster_backup"
  log_file          = "/var/log/chef/bootstrap.log"
  aws_iam_role_id   = module.neo4j_environment.backup_instance_iam_role_name_output
  aws_instance_name = "${var.environment}-${var.service_name}-backup"
}

# Datadog monitors and dashboards
module "datadog_neo4j_dashboards_and_monitors" {
  source                                         = "git@github.com:theorchard/terraform-datadog.git//modules/neo4j"
  environment                                    = var.environment
  service_name                                   = var.service_name
  neo4j_cluster_role_status_lambda_function_name = "neo4j-cluster-role-check-watchdog-${var.environment}"
}
