Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
snemetz committed Dec 16, 2017
0 parents commit e4b00a3
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.tfstate
*.tfstate.backup
.terraform
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Terraform module to simplify and expand boolean use
===

Since Terraform currently doesn't have a boolean variable type, this provides a consistent handling. The list of true values is also expanded.

Designed to simplify the use of booleans (especially where 1 variable is tested many times) and the using count to enable/disable resources.

* Will handle any capitalization of the input value.
* Will return 1 for any true value, 0 for anything else.
* Current true values: true, t, 1, on, enable

Example: If
```hcl
module "boolean" {
source = "devops-workflow/boolean/local"
value = "${var.boolean}"
}
var = "${module.boolean.value ? "true setting" : "false setting"}"
```

Example: count for enabling/disabling a resource
```hcl
module enabled" {
source = "devops-workflow/boolean/local"
value = "${var.enabled}"
}
resource "resource_type" "resource_name" {
count = "${module.enabled.value}"
}
```
13 changes: 13 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Terraform module to simplify and expand boolean use
#

locals {
l = "${lower(var.value)}"
v1 = "${local.l == "true" ? 1 : 0}"
v2 = "${local.v1 ? 1 : local.l == "t" ? 1 : 0}"
v3 = "${local.v2 ? 1 : local.l == "1" ? 1 : 0}"
v4 = "${local.v3 ? 1 : local.l == "on" ? 1 : 0}"
v5 = "${local.v4 ? 1 : local.l == "enable" ? 1 : 0}"
value = "${local.v5}"
}
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

output "value" {
description = "1 if input was tested `true`, 0 otherwise"
value = "${local.value}"
}
2 changes: 2 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test cases
===
25 changes: 25 additions & 0 deletions test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

module "true" {
source = "../"
value = "true"
}
module "t" {
source = "../"
value = "t"
}
module "one" {
source = "../"
value = "1"
}
module "on" {
source = "../"
value = "on"
}
module "enable" {
source = "../"
value = "enable"
}
module "false" {
source = "../"
value = "false"
}
19 changes: 19 additions & 0 deletions test/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

output "true" {
value = "${module.true.value}"
}
output "t" {
value = "${module.t.value}"
}
output "one" {
value = "${module.one.value}"
}
output "on" {
value = "${module.on.value}"
}
output "enable" {
value = "${module.enable.value}"
}
output "false" {
value = "${module.false.value}"
}
4 changes: 4 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "value" {
description = "Value to test for truth"
type = "string"
}

0 comments on commit e4b00a3

Please sign in to comment.