provider "aws" {
  region = var.aws_region
}

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

module "switchboard_kinesis_stream" {
  source = "git@github.com:theorchard/terraform-kinesis.git//?ref=1.0.0"

  environment      = var.environment
  service_name     = "switchboard-consumer"
  shard_count      = 2
  retention_period = 168
}

data "aws_secretsmanager_secret_version" "datadog_api" {
  secret_id = "${var.environment}/datadog/DD_API_KEY"
}

data "aws_secretsmanager_secret_version" "datadog_app_key" {
  secret_id = "${var.environment}/datadog/DD_APP_KEY"
}

# Datadog provider
provider "datadog" {
  api_key = data.aws_secretsmanager_secret_version.datadog_api.secret_string
  app_key = data.aws_secretsmanager_secret_version.datadog_app_key.secret_string
}

resource "datadog_monitor" "maxwells_daemon_monitor" {
  name               = "qa-maxwells-switchboard01 maxwell's daemon monitor"
  type               = "process alert"
  message            = "Please make sure that qa-maxwells-switchboard01 is runnning, qa.db.qaorch.com is online, and that the mysql user has the appropriate permissions @agushchin@theorchard.com @owright@theorchard.com @amclarty@theorchard.com @tmoin@theorchard.com @it@theorchard.com @rdemoose@theorchard.com @mstace@theorchard.com @jcarrion@theorchard.com @slack-monitoring"
  escalation_message = "Please make sure that qa-maxwells-switchboard01 is runnning, qa.db.qaorch.com is online, and that the mysql user has the appropriate permissions @agushchin@theorchard.com @owright@theorchard.com @amclarty@theorchard.com @tmoin@theorchard.com @it@theorchard.com @rdemoose@theorchard.com @mstace@theorchard.com @jcarrion@theorchard.com @slack-monitoring"

  query = "processes('com.zendesk.maxwell').over('environment:${var.environment},service_name:maxwell_switchboard').rollup('count').last('5m') < 1"

  thresholds = {
    warning  = "0.1"
    critical = "0"
  }

  include_tags        = true
  locked              = true
  notify_no_data      = false
  notify_audit        = false
  renotify_interval   = 60
  timeout_h           = 0
  require_full_window = false

  tags = [
    "environment:${var.environment}",
    "service_name:maxwell_switchboard",
  ]
}

module "daemon_switchboard_consumer_dashboard" {
  source                      = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=1.0.0"
  environment                 = var.environment
  environment_type            = "fargate"
  service_name                = var.service_name
  service_5xx_monitor_enabled = false
  service_4xx_monitor_enabled = false
}

module "lambda_switchboard_producer_dashboard" {
  source                                = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=1.0.0"
  environment                           = var.environment
  service_name                          = "switchboard-producer"
  notification_endpoints                = "@rvstiphout@theorchard.com @evlastos@theorchard.com @owright@theorchard.com @aprice@theorchard.com @amclarty@theorchard.com"
  escalation_notification_endpoints     = "@rvstiphout@theorchard.com @evlastos@theorchard.com @owright@theorchard.com @aprice@theorchard.com @amclarty@theorchard.com"
  lambda_error_monitor_enabled          = true
  lambda_error_critical_number          = 2
  lambda_error_warning_number           = 1
  lambda_error_critical_recovery_number = 0
  lambda_error_warning_recovery_number  = 0
}
