From 84935204c20549f0f31dbe112fa1154e84d39994 Mon Sep 17 00:00:00 2001
From: Niklas van Schrick <mc.taucher2003@gmail.com>
Date: Fri, 9 Feb 2024 23:54:33 +0100
Subject: [PATCH] Create shared module for pages domains

---
 domain/docs.tf                           | 43 ------------------
 domain/main.tf                           |  9 ++++
 modules/gitlab/pages_domain/main.tf      | 56 ++++++++++++++++++++++++
 modules/gitlab/pages_domain/variables.tf | 15 +++++++
 4 files changed, 80 insertions(+), 43 deletions(-)
 delete mode 100644 domain/docs.tf
 create mode 100644 modules/gitlab/pages_domain/main.tf
 create mode 100644 modules/gitlab/pages_domain/variables.tf

diff --git a/domain/docs.tf b/domain/docs.tf
deleted file mode 100644
index 12f1bf7..0000000
--- a/domain/docs.tf
+++ /dev/null
@@ -1,43 +0,0 @@
-//noinspection MissingProperty
-data "gitlab_project" "telescopium" {
-  path_with_namespace = "code0-tech/telescopium"
-}
-
-resource "cloudflare_record" "docs_gitlab_pages" {
-  name     = "docs"
-  type     = "CNAME"
-  zone_id  = data.cloudflare_zone.main_domain.id
-  value    = "docs-code0-tech-c91f18c0d2259c041bf05138b194e6bb082059fe38eff2e.gitlab.io"
-  proxied  = true
-  comment  = "Managed by Terraform"
-}
-
-module "pages_certificate" {
-  source = "../modules/cloudflare/certificate"
-
-  hostname = cloudflare_record.docs_gitlab_pages.hostname
-}
-
-data "cloudflare_origin_ca_root_certificate" "cloudflare_root" {
-  algorithm = "rsa"
-}
-
-resource "gitlab_pages_domain" "docs" {
-  project = data.gitlab_project.telescopium.id
-  domain  = cloudflare_record.docs_gitlab_pages.hostname
-
-  key         = module.pages_certificate.private_key
-  certificate = <<-EOF
-    ${module.pages_certificate.certificate}
-    ${data.cloudflare_origin_ca_root_certificate.cloudflare_root.cert_pem}
-  EOF
-}
-
-//noinspection HILUnresolvedReference
-resource "cloudflare_record" "docs_gitlab_pages_verification" {
-  name = "_gitlab-pages-verification-code.docs"
-  type = "TXT"
-  zone_id = data.cloudflare_zone.main_domain.id
-  value = gitlab_pages_domain.docs.verification_code
-  comment  = "Managed by Terraform"
-}
diff --git a/domain/main.tf b/domain/main.tf
index cd5b370..fd5ba70 100644
--- a/domain/main.tf
+++ b/domain/main.tf
@@ -23,3 +23,12 @@ resource "cloudflare_zone_settings_override" "main" {
     ssl = "strict"
   }
 }
+
+module "docs_pages" {
+  source = "../modules/gitlab/pages_domain"
+
+  cloudflare_domain_name = "docs"
+  cloudflare_zone_id = data.cloudflare_zone.main_domain.id
+  gitlab_project_path = "code0-tech/telescopium"
+  gitlab_unique_pages_url = "docs-code0-tech-c91f18c0d2259c041bf05138b194e6bb082059fe38eff2e.gitlab.io"
+}
diff --git a/modules/gitlab/pages_domain/main.tf b/modules/gitlab/pages_domain/main.tf
new file mode 100644
index 0000000..6880f9a
--- /dev/null
+++ b/modules/gitlab/pages_domain/main.tf
@@ -0,0 +1,56 @@
+terraform {
+  required_providers {
+    cloudflare = {
+      source  = "cloudflare/cloudflare"
+      version = "4.24.0"
+    }
+    gitlab = {
+      source = "gitlabhq/gitlab"
+      version = "16.8.1"
+    }
+  }
+}
+
+//noinspection MissingProperty
+data "gitlab_project" "this" {
+  path_with_namespace = var.gitlab_project_path
+}
+
+resource "cloudflare_record" "gitlab_pages" {
+  name     = var.cloudflare_domain_name
+  type     = "CNAME"
+  zone_id  = var.cloudflare_zone_id
+  value    = var.gitlab_unique_pages_url
+  proxied  = true
+  comment  = "Managed by Terraform"
+}
+
+module "certificate" {
+  source = "../../cloudflare/certificate"
+
+  hostname = cloudflare_record.gitlab_pages.hostname
+}
+
+data "cloudflare_origin_ca_root_certificate" "cloudflare_root" {
+  algorithm = "rsa"
+}
+
+resource "gitlab_pages_domain" "this" {
+  project = data.gitlab_project.this.id
+  domain  = cloudflare_record.gitlab_pages.hostname
+
+  key         = module.certificate.private_key
+  certificate = <<-EOF
+    ${module.certificate.certificate}
+    ${data.cloudflare_origin_ca_root_certificate.cloudflare_root.cert_pem}
+  EOF
+}
+
+//noinspection HILUnresolvedReference
+resource "cloudflare_record" "gitlab_pages_verification" {
+  name = "_gitlab-pages-verification-code.${var.cloudflare_domain_name}"
+  type = "TXT"
+  zone_id = var.cloudflare_zone_id
+  value = gitlab_pages_domain.this.verification_code
+  comment  = "Managed by Terraform"
+}
diff --git a/modules/gitlab/pages_domain/variables.tf b/modules/gitlab/pages_domain/variables.tf
new file mode 100644
index 0000000..68d186c
--- /dev/null
+++ b/modules/gitlab/pages_domain/variables.tf
@@ -0,0 +1,15 @@
+variable "cloudflare_zone_id" {
+  type = string
+}
+
+variable "gitlab_project_path" {
+  type = string
+}
+
+variable "cloudflare_domain_name" {
+  type = string
+}
+
+variable "gitlab_unique_pages_url" {
+  type = string
+}