diff --git a/.github/workflows/tf-docs.yaml b/.github/workflows/tf-docs.yaml new file mode 100644 index 0000000..73ee4c5 --- /dev/null +++ b/.github/workflows/tf-docs.yaml @@ -0,0 +1,27 @@ +name: Generate terraform docs +on: + pull_request: + branches: + - main + + workflow_dispatch: + +jobs: + docs: + runs-on: ubuntu-latest + + permissions: + contents: 'write' + + 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: + working-dir: . + 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..b201270 100644 --- a/README.md +++ b/README.md @@ -1 +1,35 @@ -# database-as-code \ No newline at end of file +# 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/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/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 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 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