Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vincetse committed Mar 10, 2020
0 parents commit 7ecdc89
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 0 deletions.
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.3
hooks:
- id: terraform-fmt
- id: tflint
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 |
15 changes: 15 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SHELL = /bin/bash
tf = time terraform

init:
$(tf) $@

plan output:
$(tf) $@

apply destroy:
$(tf) $@ --auto-approve

create: apply

delete: destroy
18 changes: 18 additions & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
15 changes: 15 additions & 0 deletions example/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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}'"
}
9 changes: 9 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -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"
}
30 changes: 30 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 7ecdc89

Please sign in to comment.