Skip to content

Commit f139b6b

Browse files
Jamie-BitFlightaknysh
authored andcommitted
Label order (#36)
* Added missing environment output * Added the ability to provide a list that defines the order and values in the id output and Name tag * Add absolute URLs to examples * Simplify TF code * Update README * Update README
1 parent 05cad64 commit f139b6b

File tree

13 files changed

+229
-95
lines changed

13 files changed

+229
-95
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export README_DEPS ?= docs/targets.md docs/terraform.md
77

88
## Lint terraform code
99
lint:
10-
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate
10+
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate

README.md

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ module "eg_prod_bastion_label" {
5252
}
5353
```
5454

55-
This will create an `id` with the value of `eg-prod-bastion-public`.
55+
This will create an `id` with the value of `eg-prod-bastion-public` because when generating `id`, the default order is `namespace`, `environment`, `stage`, `name`, `attributes`
56+
(you can override it by using the `label_order` variable, see [Advanced Example 3](#advanced-example-3)).
5657

5758
Now reference the label when creating an instance:
5859

@@ -228,28 +229,34 @@ resource "aws_autoscaling_group" "default" {
228229
tags = ["${module.label.tags_as_list_of_maps}"]
229230
}
230231
```
231-
### Advanced Example 3 as per the [complete example](./examples/complete)
232-
This example shows how you can pass the `context` output of one label module to the next label_module.
233-
Allowing you to create one label that has the base set of values, and then creating every extra label
232+
233+
### Advanced Example 3
234+
235+
See [complete example](./examples/complete)
236+
237+
This example shows how you can pass the `context` output of one label module to the next label_module,
238+
allowing you to create one label that has the base set of values, and then creating every extra label
234239
as a derivative of that.
235240

236241
```hcl
237242
module "label1" {
238-
source = "../../"
243+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
239244
namespace = "CloudPosse"
240245
environment = "UAT"
241246
stage = "build"
242247
name = "Winston Churchroom"
243248
attributes = ["fire", "water", "earth", "air"]
244249
250+
label_order = ["name", "environment", "stage", "attributes"]
251+
245252
tags = {
246253
"City" = "Dublin"
247254
"Environment" = "Private"
248255
}
249256
}
250257
251258
module "label2" {
252-
source = "../../"
259+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
253260
context = "${module.label1.context}"
254261
name = "Charlie"
255262
stage = "test"
@@ -261,7 +268,7 @@ module "label2" {
261268
}
262269
263270
module "label3" {
264-
source = "../../"
271+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
265272
name = "Starfish"
266273
stage = "release"
267274
@@ -272,50 +279,83 @@ module "label3" {
272279
}
273280
```
274281

275-
This creates label outputs like this.
282+
This creates label outputs like this:
283+
276284
```hcl
277-
Outputs:
278285
label1 = {
279286
attributes = fire-water-earth-air
280-
id = cloudposse-uat-build-winstonchurchroom-fire-water-earth-air
287+
id = winstonchurchroom-uat-build-fire-water-earth-air
281288
name = winstonchurchroom
282289
namespace = cloudposse
283290
stage = build
284291
}
292+
label1_context = {
293+
attributes = [fire-water-earth-air]
294+
delimiter = [-]
295+
environment = [uat]
296+
label_order = [name environment stage attributes]
297+
name = [winstonchurchroom]
298+
namespace = [cloudposse]
299+
stage = [build]
300+
tags_keys = [City Environment Name Namespace Stage]
301+
tags_values = [Dublin Private winstonchurchroom-uat-build-fire-water-earth-air cloudposse build]
302+
}
285303
label1_tags = {
286304
City = Dublin
287305
Environment = Private
288-
Name = cloudposse-uat-build-winstonchurchroom-fire-water-earth-air
306+
Name = winstonchurchroom-uat-build-fire-water-earth-air
289307
Namespace = cloudposse
290308
Stage = build
291309
}
292310
label2 = {
293311
attributes = fire-water-earth-air
294-
id = cloudposse-uat-build-charlie-fire-water-earth-air
312+
id = charlie-uat-test-fire-water-earth-air
295313
name = charlie
296314
namespace = cloudposse
297-
stage = build
315+
stage = test
316+
}
317+
label2_context = {
318+
attributes = [fire-water-earth-air]
319+
delimiter = [-]
320+
environment = [uat]
321+
label_order = [name environment stage attributes]
322+
name = [charlie]
323+
namespace = [cloudposse]
324+
stage = [test]
325+
tags_keys = [City Environment Name Namespace Stage]
326+
tags_values = [London Public charlie-uat-test-fire-water-earth-air cloudposse test]
298327
}
299328
label2_tags = {
300329
City = London
301330
Environment = Public
302-
Name = cloudposse-uat-build-charlie-fire-water-earth-air
331+
Name = charlie-uat-test-fire-water-earth-air
303332
Namespace = cloudposse
304-
Stage = build
333+
Stage = test
305334
}
306335
label3 = {
307-
attributes =
336+
attributes =
308337
id = release-starfish
309338
name = starfish
310-
namespace =
339+
namespace =
311340
stage = release
312341
}
342+
label3_context = {
343+
attributes = []
344+
delimiter = [-]
345+
environment = []
346+
label_order = [namespace environment stage name attributes]
347+
name = [starfish]
348+
namespace = []
349+
stage = [release]
350+
tags_keys = [Animal Eat Environment Name Namespace Stage]
351+
tags_values = [Rabbit Carrot release-starfish release]
352+
}
313353
label3_tags = {
314354
Animal = Rabbit
315355
Eat = Carrot
316-
Environment =
356+
Environment =
317357
Name = release-starfish
318-
Namespace =
358+
Namespace =
319359
Stage = release
320360
}
321361
```
@@ -329,8 +369,9 @@ label3_tags = {
329369
```
330370
Available targets:
331371
332-
help This help screen
372+
help Help screen
333373
help/all Display help for all targets
374+
help/short This help short screen
334375
lint Lint terraform code
335376
336377
```
@@ -339,14 +380,15 @@ Available targets:
339380

340381
| Name | Description | Type | Default | Required |
341382
|------|-------------|:----:|:-----:|:-----:|
342-
| additional_tag_map | Additional tags for appending to each tag map. | map | `<map>` | no |
343-
| attributes | Additional attributes (e.g. `policy` or `role`) | list | `<list>` | no |
383+
| additional_tag_map | Additional tags for appending to each tag map | map | `<map>` | no |
384+
| attributes | Additional attributes (e.g. `1`) | list | `<list>` | no |
344385
| context | Default context to use for passing state between label invocations | map | `<map>` | no |
345386
| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no |
346387
| enabled | Set to false to prevent the module from creating any resources | string | `true` | no |
347388
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | string | `` | no |
389+
| label_order | The naming order of the id output and Name tag | list | `<list>` | no |
348390
| name | Solution name, e.g. 'app' or 'jenkins' | string | `` | no |
349-
| namespace | Namespace, which could be your organization name, e.g. 'cp' or 'cloudposse' | string | `` | no |
391+
| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | string | `` | no |
350392
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no |
351393
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map | `<map>` | no |
352394

@@ -359,6 +401,7 @@ Available targets:
359401
| delimiter | Delimiter used in label ID |
360402
| environment | Normalized environment |
361403
| id | Disambiguated ID |
404+
| label_order | The naming order of the id output and Name tag |
362405
| name | Normalized name |
363406
| namespace | Normalized namespace |
364407
| stage | Normalized stage |

README.yaml

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ usage: |-
6565
}
6666
```
6767
68-
This will create an `id` with the value of `eg-prod-bastion-public`.
68+
This will create an `id` with the value of `eg-prod-bastion-public` because when generating `id`, the default order is `namespace`, `environment`, `stage`, `name`, `attributes`
69+
(you can override it by using the `label_order` variable, see [Advanced Example 3](#advanced-example-3)).
6970
7071
Now reference the label when creating an instance:
7172
@@ -241,28 +242,34 @@ usage: |-
241242
tags = ["${module.label.tags_as_list_of_maps}"]
242243
}
243244
```
244-
### Advanced Example 3 as per the [complete example](./examples/complete)
245-
This example shows how you can pass the `context` output of one label module to the next label_module.
246-
Allowing you to create one label that has the base set of values, and then creating every extra label
245+
246+
### Advanced Example 3
247+
248+
See [complete example](./examples/complete)
249+
250+
This example shows how you can pass the `context` output of one label module to the next label_module,
251+
allowing you to create one label that has the base set of values, and then creating every extra label
247252
as a derivative of that.
248253
249254
```hcl
250255
module "label1" {
251-
source = "../../"
256+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
252257
namespace = "CloudPosse"
253258
environment = "UAT"
254259
stage = "build"
255260
name = "Winston Churchroom"
256261
attributes = ["fire", "water", "earth", "air"]
257262
263+
label_order = ["name", "environment", "stage", "attributes"]
264+
258265
tags = {
259266
"City" = "Dublin"
260267
"Environment" = "Private"
261268
}
262269
}
263270
264271
module "label2" {
265-
source = "../../"
272+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
266273
context = "${module.label1.context}"
267274
name = "Charlie"
268275
stage = "test"
@@ -274,7 +281,7 @@ usage: |-
274281
}
275282
276283
module "label3" {
277-
source = "../../"
284+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
278285
name = "Starfish"
279286
stage = "release"
280287
@@ -284,56 +291,88 @@ usage: |-
284291
}
285292
}
286293
```
287-
288-
This creates label outputs like this.
294+
295+
This creates label outputs like this:
296+
289297
```hcl
290-
Outputs:
291298
label1 = {
292299
attributes = fire-water-earth-air
293-
id = cloudposse-uat-build-winstonchurchroom-fire-water-earth-air
300+
id = winstonchurchroom-uat-build-fire-water-earth-air
294301
name = winstonchurchroom
295302
namespace = cloudposse
296303
stage = build
297304
}
305+
label1_context = {
306+
attributes = [fire-water-earth-air]
307+
delimiter = [-]
308+
environment = [uat]
309+
label_order = [name environment stage attributes]
310+
name = [winstonchurchroom]
311+
namespace = [cloudposse]
312+
stage = [build]
313+
tags_keys = [City Environment Name Namespace Stage]
314+
tags_values = [Dublin Private winstonchurchroom-uat-build-fire-water-earth-air cloudposse build]
315+
}
298316
label1_tags = {
299317
City = Dublin
300318
Environment = Private
301-
Name = cloudposse-uat-build-winstonchurchroom-fire-water-earth-air
319+
Name = winstonchurchroom-uat-build-fire-water-earth-air
302320
Namespace = cloudposse
303321
Stage = build
304322
}
305323
label2 = {
306324
attributes = fire-water-earth-air
307-
id = cloudposse-uat-build-charlie-fire-water-earth-air
325+
id = charlie-uat-test-fire-water-earth-air
308326
name = charlie
309327
namespace = cloudposse
310-
stage = build
328+
stage = test
329+
}
330+
label2_context = {
331+
attributes = [fire-water-earth-air]
332+
delimiter = [-]
333+
environment = [uat]
334+
label_order = [name environment stage attributes]
335+
name = [charlie]
336+
namespace = [cloudposse]
337+
stage = [test]
338+
tags_keys = [City Environment Name Namespace Stage]
339+
tags_values = [London Public charlie-uat-test-fire-water-earth-air cloudposse test]
311340
}
312341
label2_tags = {
313342
City = London
314343
Environment = Public
315-
Name = cloudposse-uat-build-charlie-fire-water-earth-air
344+
Name = charlie-uat-test-fire-water-earth-air
316345
Namespace = cloudposse
317-
Stage = build
346+
Stage = test
318347
}
319348
label3 = {
320-
attributes =
349+
attributes =
321350
id = release-starfish
322351
name = starfish
323-
namespace =
352+
namespace =
324353
stage = release
325354
}
355+
label3_context = {
356+
attributes = []
357+
delimiter = [-]
358+
environment = []
359+
label_order = [namespace environment stage name attributes]
360+
name = [starfish]
361+
namespace = []
362+
stage = [release]
363+
tags_keys = [Animal Eat Environment Name Namespace Stage]
364+
tags_values = [Rabbit Carrot release-starfish release]
365+
}
326366
label3_tags = {
327367
Animal = Rabbit
328368
Eat = Carrot
329-
Environment =
369+
Environment =
330370
Name = release-starfish
331-
Namespace =
371+
Namespace =
332372
Stage = release
333373
}
334374
```
335375
336-
337376
include:
338377
- "docs/targets.md"
339378
- "docs/terraform.md"
@@ -355,4 +394,4 @@ contributors:
355394
- name: "Vladimir"
356395
github: "SweetOps"
357396
- name: "Daren Desjardins"
358-
github: "darend"
397+
github: "darend"

docs/targets.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
```
33
Available targets:
44
5-
help This help screen
5+
help Help screen
66
help/all Display help for all targets
7+
help/short This help short screen
78
lint Lint terraform code
89
910
```

0 commit comments

Comments
 (0)