locals {
  lambda_name_update_dim_tables = "${var.service_name}-update-dim-tables"
}

module "lambda_update_dim_tables" {
  source = "git@github.com:theorchard/terraform-lambda.git//?ref=5.3.0"

  environment             = var.environment
  application_family      = var.application_family
  lambda_name             = local.lambda_name_update_dim_tables
  lambda_description      = "Updates Snowflake dimension tables after a product transfer."
  lambda_function_timeout = "900"
  use_container_image     = true
  vpc_id                  = module.vpc_info.vpc_id
  vpc_subnet_ids          = module.vpc_info.default_private_subnet_ids
  lambda_function_environment_variables = {
    ENVIRONMENT         = var.environment
    SENTRY_DSN          = module.sentry_update_dim_tables.sentry_key_dsn_public_output
    SNOWFLAKE_USER_NAME = var.snowflake_user_name
    SNOWFLAKE_ACCOUNT   = var.snowflake_account
    SNOWFLAKE_ROLE      = var.snowflake_role
    SNOWFLAKE_SCHEMA    = var.snowflake_schema
  }
}

module "datadog_lambda_update_dim_tables" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.18.2"

  environment                       = var.environment
  service_name                      = local.lambda_name_update_dim_tables
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints
}

module "sentry_update_dim_tables" {
  source = "git@github.com:theorchard/terraform-sentry.git//?ref=5.0.0"

  environment        = var.environment
  service_name       = local.lambda_name_update_dim_tables
  application_family = var.application_family
  teams              = [var.environment]
}

module "m2m_secrets_update_dim_tables" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//modules/auth0m2m?ref=1.6.1"

  environment        = var.environment
  service_name       = local.lambda_name_update_dim_tables
  application_family = var.application_family
}

module "secrets_update_dim_tables" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secret_names)

  environment                    = var.environment
  service_name                   = local.lambda_name_update_dim_tables
  secret_name                    = each.value
  application_family             = var.application_family
  secret_recovery_window_in_days = 7
}
