module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team_name
}

provider "aws" {
  region = var.region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/distribution/distro-scripts/mr-snowflake-backfill/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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

  environment = var.environment
}

data "aws_route53_zone" "zone" {
  name         = "theorchard.io."
  private_zone = false
}

module "mr-snowflake-backfill-cache" {
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=3.1.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                      = var.environment
  service_name                     = var.service_name
  cache_engine                     = "memcached"
  memcached_engine_version         = "1.6.6"
  cache_subnet_group_name          = "${var.environment}-elasticache-subnet-group"
  cache_node_count                 = 1
  cache_node_type                  = "cache.t4g.micro"
  cache_parameter_group_name       = "default.memcached1.6"
  application_family               = var.application_family
  route53_zone_id                  = data.aws_route53_zone.zone.zone_id
  vpc_id                           = module.vpc_info.vpc_id
  cache_transit_encryption_enabled = false
}

# Custom monitor exported from DD UI
resource "datadog_monitor" "No_new_runs_of_mr-snowflake-backfill-pipeline_in_the_last_7_hour" {
  name                   = var.monitor_name
  type                   = "event-v2 alert"
  query                  = <<EOT
    events("source:jenkins job:mr-snowflake-backfill-pipeline result:success").rollup("count").last("12h") <= 0
    EOT
  message                = <<EOT
    {{#is_alert}}
    Found no successful runs of https://scheduler.theorchard.io/job/mr-snowflake-backfill-pipeline/ in the last 7 hours!
    Double-check it's still running! Head to [this Notion doc](https://www.notion.so/Outages-Error-Alerts-Runbook-1e797177520f80b798a9c3f5f51bff6a?source=copy_link#1e797177520f806e86f8c87932ec4ba9) for more information on outages related to this job. 
    Notify: ${var.escalation_notification_endpoints}
    {{/is_alert}}

    {{#is_warning}}
    Found a failed run of https://scheduler.theorchard.io/job/mr-snowflake-backfill-pipeline/ - check to see if the next run is successful!
    Notify: ${var.notification_endpoints}  
    {{/is_warning}}

    {{#is_recovery}}
    https://scheduler.theorchard.io/job/mr-snowflake-backfill-pipeline/ is back to running successfully! 
    Notify: ${var.notification_endpoints} 
    {{/is_recovery}}
    EOT
  draft_status           = "published"
  escalation_message     = <<EOT
    Still no runs detected in https://scheduler.theorchard.io/job/mr-snowflake-backfill-pipeline/ - this needs to be resolved ASAP!

    ${var.notification_endpoints} 
    EOT
  groupby_simple_monitor = false
  include_tags           = false
  on_missing_data        = "default"
  renotify_interval      = 60
  renotify_statuses      = ["alert"]
  require_full_window    = false
  monitor_thresholds {
    critical = 0
    warning  = 1
  }

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