variable env {}

variable CONFIG_KEYPAIR {}

variable LOGGER_DSN {}
variable LOGGER_LEVEL {}

variable SENTRY_DSN {}

variable DD_MYSQL_DATABASE {}
variable DD_MYSQL_HOST {}
variable DD_MYSQL_PASSWORD {}
variable DD_MYSQL_PORT {}
variable DD_MYSQL_USER {}

variable DDB_ORDERS_TABLE {}
variable DDB_SEARCH_REQUESTS_TABLE {}

variable ELASTICSEARCH_HOST {}
variable ELASTICSEARCH_PORT {}
variable ELASTICSEARCH_USE_SSL {}
variable ELASTICSEARCH_USE_AUTH {}
variable ELASTICSEARCH_DEFAULT_TIMEOUT {}

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

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket         = "orcd-terraform-state"
    key            = "prod/ows-vectororder/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = "true"
    dynamodb_table = "terraform-prod-ows-vectororder"
  }
}

module "ows_vectororder_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//"

  environment_name = "${var.env}"
  service_name     = "ows-vectororder"
}

module "ows_vectororder_service_environment" {
  source = "git@github.com:theorchard/terraform-elasticbeanstalk.git//"

  environment                        = "${var.env}"
  service_name                       = "ows-vectororder"
  update_max_batch                   = "1"
  launchconfiguration_keypair        = "${var.CONFIG_KEYPAIR}"
  load_balancer_listener_port        = "443"
  load_balancer_listener_protocol    = "HTTPS"
  notification_endpoints             = "vector.chapter@theorchard.com "
  process_port                       = "80"
  process_protocol                   = "HTTP"
  process_instance_health_check_path = "/hello/"


  # AWS account specific settings:
  vpc_elb_subnets                    = "subnet-cb4dbae0,subnet-5366ef24"
  vpc_subnets                        = "subnet-cb4dbae0,subnet-5366ef24"
  vpc_id                             = "vpc-7f0e841a"
  iam_managed_policy_attachments     = [
    "${module.ows_vectororder_owsrequest.policy_arn_output}"
  ]
  elb_v2_access_logs_s3_bucket       = "aws-logs-437795906767-us-east-1"
  load_balancer_ssl_certificate_arn  = "arn:aws:acm:us-east-1:437795906767:certificate/a76e4883-28aa-4062-a4cc-c06b8012ae28"
  aws_route53_zone_id                = "Z2RSKZ9M7TJ9Z"


  # Environment specific settings:
  launchconfiguration_instance_type  = "t2.medium"
  update_min_in_service              = "1"


  environment_variables = [
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "Environment"
      value = "${var.env}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "LOGGER_DSN"
      value = "${var.LOGGER_DSN}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "SENTRY_DSN"
      value = "${var.SENTRY_DSN}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "DD_MYSQL_DATABASE"
      value = "${var.DD_MYSQL_DATABASE}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "DD_MYSQL_HOST"
      value = "${var.DD_MYSQL_HOST}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "DD_MYSQL_PORT"
      value = "${var.DD_MYSQL_PORT}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "DD_MYSQL_USER"
      value = "${var.DD_MYSQL_USER}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "DDB_ORDERS_TABLE"
      value = "${var.DDB_ORDERS_TABLE}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "DDB_SEARCH_REQUESTS_TABLE"
      value = "${var.DDB_SEARCH_REQUESTS_TABLE}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "ELASTICSEARCH_HOST"
      value = "${var.ELASTICSEARCH_HOST}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "ELASTICSEARCH_PORT"
      value = "${var.ELASTICSEARCH_PORT}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "ELASTICSEARCH_USE_SSL"
      value = "${var.ELASTICSEARCH_USE_SSL}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "ELASTICSEARCH_USE_AUTH"
      value = "${var.ELASTICSEARCH_USE_AUTH}"
    },
    {
      namespace = "aws:elasticbeanstalk:application:environment"
      name = "ELASTICSEARCH_DEFAULT_TIMEOUT"
      value = "${var.ELASTICSEARCH_DEFAULT_TIMEOUT}"
    },
  ]
}
