From 47779d6b1572602f4730c12fc9b73867a64bacae Mon Sep 17 00:00:00 2001 From: iamgp21 Date: Thu, 14 Aug 2025 21:53:24 +0530 Subject: [PATCH 1/6] First Commit --- .github/workflows/tf-docs.yaml | 23 +++++++++++++++++++++++ .gitignore | 2 +- .terraform.lock.hcl | 24 ++++++++++++++++++++++++ README.md | 6 +++++- backends/wsl.hcl | 1 + entrypoint.tf | 7 +++++++ modules/database/main.tf | 6 ++++++ modules/database/terraform.tf | 8 ++++++++ modules/database/variables.tf | 8 ++++++++ provider.tf | 9 +++++++++ tfvars/wsl.tfvars | 6 ++++++ variables.tf | 16 ++++++++++++++++ versions.tf | 11 +++++++++++ 13 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/tf-docs.yaml create mode 100644 .terraform.lock.hcl create mode 100644 backends/wsl.hcl create mode 100644 entrypoint.tf create mode 100644 modules/database/main.tf create mode 100644 modules/database/terraform.tf create mode 100644 modules/database/variables.tf create mode 100644 provider.tf create mode 100644 tfvars/wsl.tfvars create mode 100644 variables.tf create mode 100644 versions.tf diff --git a/.github/workflows/tf-docs.yaml b/.github/workflows/tf-docs.yaml new file mode 100644 index 0000000..728c213 --- /dev/null +++ b/.github/workflows/tf-docs.yaml @@ -0,0 +1,23 @@ +name: Generate terraform docs +on: + pull_request: + branches: + - main + + workflow_dispatch: + +jobs: + docs: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.head.ref }} # Use the PR branch for checkout + + - name: Render terraform docs inside the README.md and push changes back to PR branch + uses: terraform-docs/gh-actions@v1.4.1 + with: + find-dir: modules/ + output-file: README.md + output-method: inject + git-push: "true" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6349e36..789aae7 100644 --- a/.gitignore +++ b/.gitignore @@ -13,7 +13,7 @@ crash.*.log # password, private keys, and other secrets. These should not be part of version # control as they are data points which are potentially sensitive and subject # to change depending on the environment. -*.tfvars +# *.tfvars *.tfvars.json # Ignore override files as they are usually used to override resources locally and so diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..f003474 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,24 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/cyrilgdn/postgresql" { + version = "1.25.0" + constraints = "1.25.0" + hashes = [ + "h1:4Hlupc8gYrPnFKisesXs9lypK6LXslU4L4tjBZOhmiE=", + "zh:0f9db6e1274603d642e96b58eaf6cc4223f7118f2d7ce909dc4812d332cc002a", + "zh:1819470f0304c6a60b2b51817cb43f6ff59a49e08cc9e50644b86b3a76c91601", + "zh:27bfb544983cac101a7c7c2e4cb9939a712dffcdd7ddcab83c2f8afc334e33c5", + "zh:46166f6f05771b0495df18459fdf3a63fae8b38e95a1b2754f03d006e17ea33d", + "zh:64d53afc52f26e8214990acc3e07f3b47bef628aa6b317595a8faec05b252209", + "zh:944d7ded418c022dd3ee513246677d601376fa38d76c9c4aecff2c2eefcaa35b", + "zh:9819551b61542a6d322d6a323bbb552ce02e769ce2222fd9bb1935473c7c4b3c", + "zh:c38bd73e208fe216efab48d099c85b8ad1e51ff102b3892443febc9778e7236e", + "zh:c73de133274dcc7a03e95f598550facc59315538f355e57e14b36e222b298826", + "zh:c7af02f5338bfe7f1976e01d3fcf82e05b3551893e732539a84c568d25571a84", + "zh:d1aa3d7432c7de883873f8f70e9a6207c7b536d874486d37aee0ca8c8853a890", + "zh:e17e9809fc7cc2d6f89078b8bfe6308930117b2270be8081820da40029b04828", + "zh:e1b21b7b7022e0d468d72f4534d226d57a7bfd8c96a4c7dc2c2fa0bb0b99298d", + "zh:f24b73645d8bc225f692bdf9c035411099ef57138569f45f3605ec79ac872e3b", + ] +} diff --git a/README.md b/README.md index dd22280..ea72f9a 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# database-as-code \ No newline at end of file +# database-as-code + + + + \ No newline at end of file diff --git a/backends/wsl.hcl b/backends/wsl.hcl new file mode 100644 index 0000000..afa4ab0 --- /dev/null +++ b/backends/wsl.hcl @@ -0,0 +1 @@ +path = "/mnt/c/Users/gaura/OneDrive/GITHUB/iamgp21/TF_STATE/wsl2_postgresql.tfstate" \ No newline at end of file diff --git a/entrypoint.tf b/entrypoint.tf new file mode 100644 index 0000000..76e8504 --- /dev/null +++ b/entrypoint.tf @@ -0,0 +1,7 @@ +module "database" { + source = "./modules/database" + postgresql_databases = var.postgresql_databases + providers = { + postgresql.wsl = postgresql.wsl + } +} \ No newline at end of file diff --git a/modules/database/main.tf b/modules/database/main.tf new file mode 100644 index 0000000..3109f04 --- /dev/null +++ b/modules/database/main.tf @@ -0,0 +1,6 @@ +resource "postgresql_database" "example_db" { + provider = postgresql.wsl + for_each = var.postgresql_databases + name = each.value.name + owner = each.value.db_owner +} \ No newline at end of file diff --git a/modules/database/terraform.tf b/modules/database/terraform.tf new file mode 100644 index 0000000..b85789f --- /dev/null +++ b/modules/database/terraform.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + postgresql = { + source = "cyrilgdn/postgresql" + configuration_aliases = [postgresql.wsl] + } + } +} \ No newline at end of file diff --git a/modules/database/variables.tf b/modules/database/variables.tf new file mode 100644 index 0000000..1d8e85d --- /dev/null +++ b/modules/database/variables.tf @@ -0,0 +1,8 @@ +variable "postgresql_databases" { + description = "A map of PostgreSQL databases to create" + type = map(object({ + name = string + db_owner = string + })) + default = {} +} \ No newline at end of file diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..faa2d5f --- /dev/null +++ b/provider.tf @@ -0,0 +1,9 @@ +provider "postgresql" { + alias = "wsl" + host = "172.31.161.14" + port = 5432 + database = "postgres" # This is the default database + username = "postgres" # Default PostgreSQL user(superuser) + password = var.postgresql_default_password + sslmode = "require" +} \ No newline at end of file diff --git a/tfvars/wsl.tfvars b/tfvars/wsl.tfvars new file mode 100644 index 0000000..2aed855 --- /dev/null +++ b/tfvars/wsl.tfvars @@ -0,0 +1,6 @@ +postgresql_databases = { + example_db = { + name = "example_db" + db_owner = "postgres" + } +} \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..50806ea --- /dev/null +++ b/variables.tf @@ -0,0 +1,16 @@ +variable "postgresql_default_password" { + description = "The password for the PostgreSQL user" + type = string + sensitive = true + default = "" + +} + +variable "postgresql_databases" { + description = "A map of PostgreSQL databases to create" + type = map(object({ + name = string + db_owner = string + })) + default = {} +} \ No newline at end of file diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..4d0fb83 --- /dev/null +++ b/versions.tf @@ -0,0 +1,11 @@ +terraform { + required_version = "~>1.9.0" + required_providers { + postgresql = { + source = "cyrilgdn/postgresql" + version = "1.25.0" + } + } + backend "local" { + } +} \ No newline at end of file From 85d64a628d9216bb06be2c541a2c0185b2c05f39 Mon Sep 17 00:00:00 2001 From: iamgp21 Date: Thu, 14 Aug 2025 21:58:10 +0530 Subject: [PATCH 2/6] Added permissions --- .github/workflows/tf-docs.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/tf-docs.yaml b/.github/workflows/tf-docs.yaml index 728c213..9b24ac8 100644 --- a/.github/workflows/tf-docs.yaml +++ b/.github/workflows/tf-docs.yaml @@ -9,6 +9,10 @@ on: jobs: docs: runs-on: ubuntu-latest + + permissions: + contents: 'write' + steps: - uses: actions/checkout@v3 with: From 9bf525aabff330f9e3650b82c06b2fac640a1437 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 14 Aug 2025 16:28:25 +0000 Subject: [PATCH 3/6] terraform-docs: automated action --- modules/database/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 modules/database/README.md diff --git a/modules/database/README.md b/modules/database/README.md new file mode 100644 index 0000000..a097575 --- /dev/null +++ b/modules/database/README.md @@ -0,0 +1,31 @@ + +## Requirements + +No requirements. + +## Providers + +| Name | Version | +|------|---------| +| [postgresql.wsl](#provider\_postgresql.wsl) | n/a | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [postgresql_database.example_db](https://registry.terraform.io/providers/cyrilgdn/postgresql/latest/docs/resources/database) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [postgresql\_databases](#input\_postgresql\_databases) | A map of PostgreSQL databases to create |
map(object({
name = string
db_owner = string
}))
| `{}` | no | + +## Outputs + +No outputs. + \ No newline at end of file From 121afb876b1d5a76fe5de26ca2023c92ff32c6ea Mon Sep 17 00:00:00 2001 From: iamgp21 Date: Thu, 14 Aug 2025 22:05:18 +0530 Subject: [PATCH 4/6] Updated github workflow --- .github/workflows/tf-docs.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tf-docs.yaml b/.github/workflows/tf-docs.yaml index 9b24ac8..b4db1c3 100644 --- a/.github/workflows/tf-docs.yaml +++ b/.github/workflows/tf-docs.yaml @@ -21,7 +21,7 @@ jobs: - name: Render terraform docs inside the README.md and push changes back to PR branch uses: terraform-docs/gh-actions@v1.4.1 with: - find-dir: modules/ + working-dir: .,modules/ output-file: README.md output-method: inject git-push: "true" \ No newline at end of file From bc56b57844fb06fd1b3a04733c88b53d0af80e0f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 14 Aug 2025 16:38:37 +0000 Subject: [PATCH 5/6] terraform-docs: automated action --- README.md | 30 ++++++++++++++++++++++++++++++ modules/README.md | 25 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 modules/README.md diff --git a/README.md b/README.md index ea72f9a..b201270 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,35 @@ # database-as-code +## Requirements +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | ~>1.9.0 | +| [postgresql](#requirement\_postgresql) | 1.25.0 | + +## Providers + +No providers. + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [database](#module\_database) | ./modules/database | n/a | + +## Resources + +No resources. + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [postgresql\_databases](#input\_postgresql\_databases) | A map of PostgreSQL databases to create |
map(object({
name = string
db_owner = string
}))
| `{}` | no | +| [postgresql\_default\_password](#input\_postgresql\_default\_password) | The password for the PostgreSQL user | `string` | `""` | no | + +## Outputs + +No outputs. \ No newline at end of file diff --git a/modules/README.md b/modules/README.md new file mode 100644 index 0000000..ef2fa69 --- /dev/null +++ b/modules/README.md @@ -0,0 +1,25 @@ + +## Requirements + +No requirements. + +## Providers + +No providers. + +## Modules + +No modules. + +## Resources + +No resources. + +## Inputs + +No inputs. + +## Outputs + +No outputs. + \ No newline at end of file From 0fc04ee9b05eb88debf4c3a6b5cae7d65a1205ae Mon Sep 17 00:00:00 2001 From: iamgp21 Date: Thu, 14 Aug 2025 22:10:20 +0530 Subject: [PATCH 6/6] Updated actions --- .github/workflows/tf-docs.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tf-docs.yaml b/.github/workflows/tf-docs.yaml index b4db1c3..73ee4c5 100644 --- a/.github/workflows/tf-docs.yaml +++ b/.github/workflows/tf-docs.yaml @@ -21,7 +21,7 @@ jobs: - name: Render terraform docs inside the README.md and push changes back to PR branch uses: terraform-docs/gh-actions@v1.4.1 with: - working-dir: .,modules/ + working-dir: . output-file: README.md output-method: inject git-push: "true" \ No newline at end of file