provider "aws" {
  region = var.aws_region
}

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

module "github_repositories" {
  source = "git@github.com:theorchard/terraform-github.git//?ref=2.1.8"
  github_repository_names = {
    python-owsclient = {
      branch_protection_enabled        = true
      branch_protection_enforce_admins = false
      repo_auto_init                   = true
      archived_repository              = false
      github_code_owner_reviews        = false
      visibility                       = "private"
      description                      = "Python library for OWS RestAPI requests."
      has_downloads                    = false,
      homepage_url                     = "https://github.com/theorchard/${var.service_name}"
    }
  }
}


provider "github" {
  token = var.github_token != "" ? var.github_token : null
  owner = var.github_organization
}

data "github_repository" "python_owsclient" {
  full_name = "${var.github_organization}/${var.service_name}"
}

data "aws_secretsmanager_secret" "python_pdp_sdk_public_key" {
  name = "${var.environment}/python-pdp-sdk/DEPLOY_KEY_PUBLIC"
}

data "aws_secretsmanager_secret_version" "python_pdp_sdk_public_key" {
  secret_id = data.aws_secretsmanager_secret.python_pdp_sdk_public_key.id
}

resource "github_repository_deploy_key" "python_pdp_sdk_public_key" {
  title      = "Allow python-pdp-sdk to pull python-owsclient from Github Actions"
  repository = data.github_repository.python_owsclient.name
  key        = data.aws_secretsmanager_secret_version.python_pdp_sdk_public_key.secret_string
  read_only  = "true"
}
