diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7db64a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.tfstate +*.tfstate.backup +.terraform diff --git a/README.md b/README.md new file mode 100644 index 0000000..be80bef --- /dev/null +++ b/README.md @@ -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}" +} +``` diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..1880a9c --- /dev/null +++ b/main.tf @@ -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}" +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..c649261 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,5 @@ + +output "value" { + description = "1 if input was tested `true`, 0 otherwise" + value = "${local.value}" +} diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..f8bc9aa --- /dev/null +++ b/test/README.md @@ -0,0 +1,2 @@ +Test cases +=== diff --git a/test/main.tf b/test/main.tf new file mode 100644 index 0000000..6ab11ed --- /dev/null +++ b/test/main.tf @@ -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" +} diff --git a/test/outputs.tf b/test/outputs.tf new file mode 100644 index 0000000..90d4ae4 --- /dev/null +++ b/test/outputs.tf @@ -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}" +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..1407e3a --- /dev/null +++ b/variables.tf @@ -0,0 +1,4 @@ +variable "value" { + description = "Value to test for truth" + type = "string" +}