Skip to content

Commit 9a0a0af

Browse files
committed
Finish refactoring to use locals instead of null
1 parent 0f1e7ff commit 9a0a0af

File tree

4 files changed

+28
-129
lines changed

4 files changed

+28
-129
lines changed

main.tf

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,24 @@
33
#
44
# TODO:
55
# Add attributes to name if not empty
6-
# return name as lowercase
7-
# return id and id_20, id_32 for combined name
86
# Refactor to use local and output the locals. Test - test - test
97

108
locals {
119
attr = "${lower(format("%s", join(var.delimiter, compact(var.attributes))))}"
1210
env = "${lower(format("%s", var.environment))}"
1311
name_low = "${lower(format("%s", var.name))}"
1412
org = "${lower(format("%s", var.organization))}"
15-
name_env = "${var.namespace-env ? join(var.delimiter, list(local.env, local.name_low)) : local.name_low}"
16-
name_org = "${var.namespace-org ? join(var.delimiter, list(local.org, local.name_env)) : local.name_env}"
17-
name_20 = "${substr(replace(local.name_org,"_","-"),0,19 <= length(local.name_org) ? 19 : length(local.name_org))}"
18-
name_32 = "${substr(replace(local.name_org,"_","-"),0,31 <= length(local.name_org) ? 31 : length(local.name_org))}"
13+
id_env = "${var.namespace-env ? join(var.delimiter, list(local.env, local.name_low)) : local.name_low}"
14+
id_org = "${var.namespace-org ? join(var.delimiter, list(local.org, local.id_env)) : local.id_env}"
15+
id = "${local.id_org}"
16+
id_20 = "${substr(replace(local.id,"_","-"),0,19 <= length(local.id) ? 19 : length(local.id))}"
17+
id_32 = "${substr(replace(local.id,"_","-"),0,31 <= length(local.id) ? 31 : length(local.id))}"
1918
tags = "${ merge(
20-
var.tags,
21-
map(
22-
"Name", "${local.name_org}",
23-
"Environment", "${local.env}",
24-
"Organization", "${local.org}",
25-
"Terraform", "true"
26-
))}"
19+
var.tags,
20+
map(
21+
"Name", "${local.id}",
22+
"Environment", "${local.env}",
23+
"Organization", "${local.org}",
24+
"Terraform", "true"
25+
))}"
2726
}
28-
/*
29-
resource "null_resource" "pre1" {
30-
count = "${var.enabled ? 1 : 0}"
31-
triggers = {
32-
attributes = "${lower(format("%s", join(var.delimiter, compact(var.attributes))))}"
33-
environment = "${lower(format("%s", var.environment))}"
34-
name = "${lower(format("%s", var.name))}"
35-
organization = "${lower(format("%s", var.organization))}"
36-
}
37-
}
38-
resource "null_resource" "pre2" {
39-
count = "${var.enabled ? 1 : 0}"
40-
triggers = {
41-
name_env = "${var.namespace-env ? join(var.delimiter, list(null_resource.pre1.triggers.environment, null_resource.pre1.triggers.name)) : null_resource.pre1.triggers.name}"
42-
}
43-
}
44-
resource "null_resource" "pre3" {
45-
count = "${var.enabled ? 1 : 0}"
46-
triggers = {
47-
name_org = "${var.namespace-org ? join(var.delimiter, list(null_resource.pre1.triggers.organization, null_resource.pre2.triggers.name_env)) : null_resource.pre2.triggers.name_env}"
48-
}
49-
}
50-
resource "null_resource" "this" {
51-
count = "${var.enabled ? 1 : 0}"
52-
triggers = {
53-
attributes = "${null_resource.pre1.triggers.attributes}"
54-
environment = "${null_resource.pre1.triggers.environment}"
55-
id = "${local.name_org}"
56-
organization = "${null_resource.pre1.triggers.organization}"
57-
name = "${null_resource.pre3.triggers.name_org}"
58-
name_20 = "${substr(replace(null_resource.pre3.triggers.name_org,"_","-"),0,19 <= length(null_resource.pre3.triggers.name_org) ? 19 : length(null_resource.pre3.triggers.name_org))}"
59-
name_32 = "${substr(replace(null_resource.pre3.triggers.name_org,"_","-"),0,31 <= length(null_resource.pre3.triggers.name_org) ? 31 : length(null_resource.pre3.triggers.name_org))}"
60-
/* FIX: wants only strings. Moved to outputs
61-
tags = "${ merge(
62-
var.tags,
63-
map("Name", var.name),
64-
map("Environment", var.environment),
65-
map("Terraform", "true") )}"*/
66-
/* }
67-
}
68-
*/

outputs.tf

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,25 @@ output "environment" {
88
value = "${local.env}"
99
}
1010
output "id" {
11-
description = "Full formatted name ID"
12-
#value = "${null_resource.this.triggers.id}"
13-
value = "${local.name_org}"
11+
description = "Fully formatted name ID"
12+
value = "${local.id}"
1413
}
15-
output "name" {
16-
description = "Name"
17-
value = "${local.name_low}"
18-
}
19-
output "name_20" {
20-
description = "Name truncated to 20 characters"
21-
value = "${local.name_20}"
22-
}
23-
output "name_32" {
24-
description = "Name truncated to 32 characters"
25-
value = "${local.name_32}"
26-
}
27-
output "organization" {
28-
description = "Organization name lowercase"
29-
value = "${local.org}"
30-
}
31-
32-
/*
33-
output "attributes" {
34-
description = "Attribute string lowercase"
35-
value = "${null_resource.this.triggers.attributes}"
36-
}
37-
output "environment" {
38-
description = "Environment name lowercase"
39-
value = "${null_resource.this.triggers.environment}"
14+
output "id_20" {
15+
description = "ID truncated to 20 characters"
16+
value = "${local.id_20}"
4017
}
41-
output "id" {
42-
description = "Full formatted name ID"
43-
#value = "${null_resource.this.triggers.id}"
44-
value = "${local.name_org}=new"
18+
output "id_32" {
19+
description = "ID truncated to 32 characters"
20+
value = "${local.id_32}"
4521
}
4622
output "name" {
47-
description = "Name"
48-
value = "${null_resource.this.triggers.name}"
49-
}
50-
output "name_20" {
51-
description = "Name truncated to 20 characters"
52-
value = "${null_resource.this.triggers.name_20}"
53-
}
54-
output "name_32" {
55-
description = "Name truncated to 32 characters"
56-
value = "${null_resource.this.triggers.name_32}"
23+
description = "Name lowercase"
24+
value = "${local.name_low}"
5725
}
5826
output "organization" {
5927
description = "Organization name lowercase"
60-
value = "${null_resource.this.triggers.organization}"
28+
value = "${local.org}"
6129
}
62-
*/
6330
output "tags" {
6431
value = "${local.tags}"
6532
}

test/outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ output "name" {
1414
description = "Name"
1515
value = "${module.labels.name}"
1616
}
17-
output "name_20" {
17+
output "id_20" {
1818
description = "Name truncated to 20 characters"
19-
value = "${module.labels.name_20}"
19+
value = "${module.labels.id_20}"
2020
}
21-
output "name_32" {
21+
output "id_32" {
2222
description = "Name truncated to 32 characters"
23-
value = "${module.labels.name_32}"
23+
value = "${module.labels.id_32}"
2424
}
2525
output "organization" {
2626
description = "Organization name lowercase"

variables.tf

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Standard Variables
21
variable "attributes" {
32
description = "Suffix name with additional attributes (policy, role, etc.)"
43
type = "list"
@@ -8,15 +7,11 @@ variable "delimiter" {
87
description = "Delimiter to be used between `name`, `namespaces`, `attributes`, etc."
98
default = "-"
109
}
11-
variable "enabled" {
12-
description = "Enable use of module. Set to false to not create resources"
13-
default = true
14-
}
1510
variable "environment" {
1611
description = "Environment (ex: dev, qa, stage, prod)"
1712
}
1813
variable "name" {
19-
description = "Name for the EFS Filesystem"
14+
description = "Base name for resource"
2015
}
2116
variable "namespace-env" {
2217
description = "Prefix name with the environment"
@@ -35,24 +30,3 @@ variable "tags" {
3530
type = "map"
3631
default = {}
3732
}
38-
39-
// Internal Variables
40-
variable "id" {
41-
default = ""
42-
}
43-
variable "name_lower" {
44-
description = "Internal"
45-
default = ""
46-
}
47-
variable "name_env" {
48-
description = "Internal"
49-
default = ""
50-
}
51-
variable "name_org" {
52-
description = "Internal"
53-
default = ""
54-
}
55-
variable "nameo" {
56-
description = "Internal"
57-
default = ""
58-
}

0 commit comments

Comments
 (0)