provider "aws" {
  region  = var.aws_region
  profile = "gdb-apollo-prod"

  default_tags {
    tags = {
      platform           = "Apollo"
      environment        = "Production"
      platform_with_env  = "Apollo Production"
      managed_by         = "Terraform"
      application_family = "apollo"
    }
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "gdb-apollo-prod-tfstate"
    key     = "prod/apollo-charts-monitoring-scheduled-job/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
    profile = "gdb-apollo-prod"
  }
}

data "aws_kms_key" "secrets_general_key" {
  key_id = "alias/${var.environment}/${var.application_family}/secrets_general"
}

resource "aws_secretsmanager_secret" "apollo_secret" {
  for_each = toset(var.apollo_scheduled_job_secrets_manager_secret_names)

  name       = "${var.application_family}/${var.environment}/apollo_charts_monitoring/${each.value}"
  kms_key_id = data.aws_kms_key.secrets_general_key.id

  tags = {
    project                  = "Infrastructure",
    service                  = "Secrets Manager",
    plat_env_project_service = "APL_PROD_INFRA_SCRT"
  }
}

# Create a sentry project.
resource "sentry_project" "apollo_sentry_project" {
  organization = var.sentry_organization
  teams        = var.sentry_teams
  name         = local.project_name
  slug         = local.project_slug
  platform     = var.sentry_platform
}

# Create a new key with a rate limit
# Sentry has it's own spike detection system but still
resource "sentry_key" "apollo_sentry_key" {
  organization = var.sentry_organization
  project      = sentry_project.apollo_sentry_project.slug

  name = "RateLimited"

  rate_limit_count  = "100"
  rate_limit_window = 3600 // 1 hour
}

resource "aws_secretsmanager_secret_version" "apollo_sentry_dsn_secret" {
  secret_id     = aws_secretsmanager_secret.apollo_secret["SENTRY_DSN"].id
  secret_string = sentry_key.apollo_sentry_key.dsn_public
}

# Retrieve a Slack integration
data "sentry_organization_integration" "slack_integration" {
  organization = var.sentry_organization

  provider_key = "slack"
  name         = var.slack_organization_name
}

resource "sentry_issue_alert" "apollo_sentry_issue_alert" {
  organization = var.sentry_organization
  project      = sentry_project.apollo_sentry_project.slug
  name         = "Send a notification for new and repeated issues"

  action_match = "any"
  filter_match = "any"
  frequency    = 1440

  conditions = [
    # A new issue is created
    {
      id = "sentry.rules.conditions.first_seen_event.FirstSeenEventCondition"
    },
    # The issue is seen more than 10 times in one hour
    {
      id             = "sentry.rules.conditions.event_frequency.EventFrequencyCondition"
      value          = 10
      comparisonType = "count"
      interval       = "1h"
    },
    # The issue is seen more than 100 times in one day
    {
      id             = "sentry.rules.conditions.event_frequency.EventFrequencyCondition"
      value          = 100
      comparisonType = "count"
      interval       = "1d"
    },
    # The issue is seen more than 1000 times in one week
    {
      id             = "sentry.rules.conditions.event_frequency.EventFrequencyCondition"
      value          = 1000
      comparisonType = "count"
      interval       = "1w"
    },
  ]

  actions = [
    # Send a notification to the Slack workspace to #general
    {
      id         = "sentry.integrations.slack.notify_action.SlackNotifyServiceAction"
      channel    = var.slack_channel_name
      channel_id = var.slack_channel_id
      tags       = join(",", var.notification_tags)
      workspace  = data.sentry_organization_integration.slack_integration.internal_id
    },
  ]
}
