From 7ecdc8973d9ca8cc7ec48292f1e2d69a6b889610 Mon Sep 17 00:00:00 2001 From: Vince Tse Date: Tue, 10 Mar 2020 03:22:19 +0000 Subject: [PATCH] Initial commit --- .gitignore | 104 ++++++++++++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 6 +++ README.md | 42 ++++++++++++++++ example/Makefile | 15 ++++++ example/main.tf | 18 +++++++ example/outputs.tf | 15 ++++++ main.tf | 10 ++++ outputs.tf | 9 ++++ variables.tf | 30 ++++++++++++ 9 files changed, 249 insertions(+) create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 README.md create mode 100644 example/Makefile create mode 100644 example/main.tf create mode 100644 example/outputs.tf create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e7ae16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,104 @@ +# Created by https://www.gitignore.io/api/vim,linux,macos,terraform +# Edit at https://www.gitignore.io/?templates=vim,linux,macos,terraform + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Terraform ### +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log + +# .tfvars files +*.tfvars + +# Ignore any .tfvars files that are generated automatically for each Terraform run. Most +# .tfvars files are managed as part of configuration and so should be included in +# version control. +# +# example.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist + +# Auto-generated tag files +tags + +# Persistent undo +[._]*.un~ + +# Coc configuration directory +.vim + +# End of https://www.gitignore.io/api/vim,linux,macos,terraform diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..4daad35 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,6 @@ +repos: + - repo: https://github.com/gruntwork-io/pre-commit + rev: v0.1.3 + hooks: + - id: terraform-fmt + - id: tflint diff --git a/README.md b/README.md new file mode 100644 index 0000000..4a38569 --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# Terraform Database URL Generation Module + +Are you ever tired of figuring out how to generate a [database URL](https://devcenter.heroku.com/changelog-items/438) or figuring out what the CLI command format is? This module is for you. + +## Usage + +``` +module "db1" { + source = "./.." + database_type = "mysql" + username = "foobar" + password = "pa55w0rd" + hostname = "db-host.example.com" + database_name = "db1" +} + +output "db1_url" { + value = module.db1.database_url +} + +output "db1_cli" { + value = module.db1.cli_command +} +``` + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:-----:| +| database\_name | Database name | `string` | n/a | yes | +| database\_type | Database type, e.g. mysql, postgresql, etc | `string` | n/a | yes | +| hostname | Database hostname | `string` | n/a | yes | +| password | Database user password | `string` | n/a | yes | +| port | Database port number | `number` | `null` | yes | +| username | Database username | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| cli\_command | CLI command line for associated database type | +| database\_url | The database URL | diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..d587116 --- /dev/null +++ b/example/Makefile @@ -0,0 +1,15 @@ +SHELL = /bin/bash +tf = time terraform + +init: + $(tf) $@ + +plan output: + $(tf) $@ + +apply destroy: + $(tf) $@ --auto-approve + +create: apply + +delete: destroy diff --git a/example/main.tf b/example/main.tf new file mode 100644 index 0000000..d4d37a9 --- /dev/null +++ b/example/main.tf @@ -0,0 +1,18 @@ +module "db1" { + source = "./.." + database_type = "mysql" + username = "foobar" + password = "pa55w0rd" + hostname = "db-host.example.com" + database_name = "db1" +} + +module "db2" { + source = "./.." + database_type = "postgresql" + username = "foobar" + password = "pa55w0rd" + hostname = "db-host.example.com" + port = 5432 + database_name = "db1" +} diff --git a/example/outputs.tf b/example/outputs.tf new file mode 100644 index 0000000..fc3cad6 --- /dev/null +++ b/example/outputs.tf @@ -0,0 +1,15 @@ +output "db1_url" { + value = module.db1.database_url +} + +output "db1_cli" { + value = module.db1.cli_command +} + +output "db2_url" { + value = module.db2.database_url +} + +output "db2_cli" { + value = module.db2.cli_command +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..81a48d1 --- /dev/null +++ b/main.tf @@ -0,0 +1,10 @@ +locals { + port = var.port == null ? "" : ":${var.port}" + db_url = "${var.database_type}://${var.username}:${var.password}@${var.hostname}${local.port}/${var.database_name}" + + pg_port = var.port == null ? "5432" : var.port + pg_cmd = "PGUSER='${var.username}' PGPASSWORD='${var.password}' PGHOST='${var.hostname}' PGPORT='${local.pg_port}' PGDATABASE='${var.database_name}' psql" + + mysql_port = var.port == null ? "3306" : var.port + mysql_cmd = "MYSQL_PWD='${var.password}' mysql --host='${var.hostname}' --port='${local.mysql_port}' --database='${var.database_name}' --user='${var.username}'" +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..9a068f1 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,9 @@ +output "database_url" { + value = local.db_url + description = "The database URL" +} + +output "cli_command" { + value = (var.database_type == "mysql") ? local.mysql_cmd : ((var.database_type == "postgresql" ? local.pg_cmd : "")) + description = "CLI command line for associated database type" +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..1598cf0 --- /dev/null +++ b/variables.tf @@ -0,0 +1,30 @@ +variable "database_type" { + type = string + description = "Database type, e.g. mysql, postgresql, etc" +} + +variable "username" { + type = string + description = "Database username" +} + +variable "password" { + type = string + description = "Database user password" +} + +variable "hostname" { + type = string + description = "Database hostname" +} + +variable "port" { + type = number + description = "Database port number" + default = null +} + +variable "database_name" { + type = string + description = "Database name" +}