provider "aws" {
  region = var.aws_region
}

provider "github" {
  owner = "theorchard"
}

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

module "github_repositories" {
  source = "git@github.com:theorchard/terraform-github.git//?ref=2.1.7"

  github_push_teams = [
    "data-analytics-eng",
    "mergers",
  ]

  github_repository_names = {
    looker-pde-content = {
      branch_protection_enabled        = true
      branch_protection_enforce_admins = true
      repo_auto_init                   = true
      archived_repository              = false
      github_code_owner_reviews        = false
      visibility                       = "private"
      description                      = "Looker PDE Content"
      homepage_url                     = "https://github.com/theorchard/looker-pde-content"
      has_downloads                    = false
    }
  }

  # Looker does not allow rebase or squash merges
  allow_merge_commit = true
  allow_rebase_merge = false
  allow_squash_merge = false
}

resource "github_repository_webhook" "looker" {
  depends_on = [module.github_repositories]

  repository = "looker-pde-content"
  active     = true

  configuration {
    url          = "https://theorchard.looker.com/webhooks/projects/looker-pde-content/deploy"
    content_type = "form"
    insecure_ssl = false
  }

  events = ["push"]
}

resource "github_repository_deploy_key" "deploy_key" {
  title      = "looker_deploy_key"
  repository = "looker-pde-content"
  key        = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC29lvovBxWyohKvSgot4zL+KNCQifc4RZWPJvZvxhA0HsSh9oAsNqt0R6WH5kQRRNmWRQ9VQLWxOiiZkE7kkTMKYJxpMHxasB8brj0RrDb7Ae4ySbnASkA966is8A47tyGjacdbvvE+8oXcWuB/woyRqO6qnTDYy6re7qDVX25x0AvYcJWCzERKeuFEk9Z/3/3blZFDuYQcQU1Zr5GEV0PMW7Q1Jkh6j/JC9YpIyBdlkgqCdA8OXvRIM4LB1gI97uV2bnkEk8f9puVmpLiZ3KdOlgCo9z5VO5F+tMy1qdnKs939o25OEJ1LeYp30riPLilurPAhkMCpAOP2VaSMLda8l1bypYs0Z1cnb/BSrC5oiR+wdUBmZyeXdl+6soeXBQJOGOY94dO+elHJwvx+wwmkaonFx0dCUoZ0hugm4vT00jI+Z/JpUNASpBVVeFGxWaNuD7NgBbQxZmiHWwIx9yVjqg1y4coKF+vZyVNqV+7/p7EvQprfL4tUmCoct5GfaLyzzySN8zrwfNTYiHMilKWV2A7gADE2IVXM7B3KzKZxyavVbo9MRUILOWquNGcGJGzbZefyGzQH8GLkAd5ATI34V3jkPlR3mrGTDEUs5EVjoPm9e4bjBTpHz7qil0pbhgLpgnPQ4AD45WmcMjy4nX+G083rlevi1OL2ffp+N4Ogw== Looker deploy_keys/looker-pde-content"
  read_only  = "false"
  lifecycle {
    ignore_changes = [key]
  }
}

