Skip to content

Commit 0861dd8

Browse files
okgoloveactions-botaknysh
authored
Use fresh null label and context (#76)
* Use fresh null label and context * Updated README.md * Do not use extra label * Fix wrong label usage * Apply suggestions from code review Co-authored-by: Andriy Knysh <aknysh@users.noreply.github.com> * Change expected resources name in terratest Co-authored-by: actions-bot <58130806+actions-bot@users.noreply.github.com> Co-authored-by: Andriy Knysh <aknysh@users.noreply.github.com>
1 parent c3de8d6 commit 0861dd8

File tree

7 files changed

+201
-135
lines changed

7 files changed

+201
-135
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,34 +236,36 @@ Available targets:
236236
| Name | Description | Type | Default | Required |
237237
|------|-------------|------|---------|:--------:|
238238
| acl | The canned ACL to apply to the S3 bucket | `string` | `"private"` | no |
239-
| additional\_tag\_map | Additional tags for appending to each tag map | `map(string)` | `{}` | no |
239+
| additional\_tag\_map | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no |
240240
| arn\_format | ARN format to be used. May be changed to support deployment in GovCloud/China regions. | `string` | `"arn:aws"` | no |
241-
| attributes | Additional attributes (e.g. `state`) | `list(string)` | <pre>[<br> "state"<br>]</pre> | no |
241+
| attributes | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no |
242242
| billing\_mode | DynamoDB billing mode | `string` | `"PROVISIONED"` | no |
243243
| block\_public\_acls | Whether Amazon S3 should block public ACLs for this bucket | `bool` | `true` | no |
244244
| block\_public\_policy | Whether Amazon S3 should block public bucket policies for this bucket | `bool` | `true` | no |
245-
| context | Default context to use for passing state between label invocations | <pre>object({<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> enabled = bool<br> delimiter = string<br> attributes = list(string)<br> label_order = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": "",<br> "enabled": true,<br> "environment": "",<br> "label_order": [],<br> "name": "",<br> "namespace": "",<br> "regex_replace_chars": "",<br> "stage": "",<br> "tags": {}<br>}</pre> | no |
246-
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
245+
| context | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | <pre>object({<br> enabled = bool<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> delimiter = string<br> attributes = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> label_order = list(string)<br> id_length_limit = number<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_order": [],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
246+
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
247247
| enable\_point\_in\_time\_recovery | Enable DynamoDB point-in-time recovery | `bool` | `false` | no |
248248
| enable\_public\_access\_block | Enable Bucket Public Access Block | `bool` | `true` | no |
249249
| enable\_server\_side\_encryption | Enable DynamoDB server-side encryption | `bool` | `true` | no |
250-
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
250+
| enabled | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
251+
| environment | Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
251252
| force\_destroy | A boolean that indicates the S3 bucket can be destroyed even if it contains objects. These objects are not recoverable | `bool` | `false` | no |
253+
| id\_length\_limit | Limit `id` to this many characters.<br>Set to `0` for unlimited length.<br>Set to `null` for default, which is `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
252254
| ignore\_public\_acls | Whether Amazon S3 should ignore public ACLs for this bucket | `bool` | `true` | no |
253-
| label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | no |
255+
| label\_order | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |
254256
| mfa\_delete | A boolean that indicates that versions of S3 objects can only be deleted with MFA. ( Terraform cannot apply changes of this value; https://github.com/terraform-providers/terraform-provider-aws/issues/629 ) | `bool` | `false` | no |
255-
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `"terraform"` | no |
256-
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `""` | no |
257+
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `null` | no |
258+
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `null` | no |
257259
| prevent\_unencrypted\_uploads | Prevent uploads of unencrypted objects to S3 | `bool` | `true` | no |
258260
| profile | AWS profile name as set in the shared credentials file | `string` | `""` | no |
259261
| read\_capacity | DynamoDB read capacity units | `number` | `5` | no |
260-
| regex\_replace\_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. By default only hyphens, letters and digits are allowed, all other chars are removed | `string` | `"/[^a-zA-Z0-9-]/"` | no |
262+
| regex\_replace\_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
261263
| restrict\_public\_buckets | Whether Amazon S3 should restrict public bucket policies for this bucket | `bool` | `true` | no |
262264
| role\_arn | The role to be assumed | `string` | `""` | no |
263265
| s3\_bucket\_name | S3 bucket name. If not provided, the name will be generated by the label module in the format namespace-stage-name | `string` | `""` | no |
264266
| s3\_replica\_bucket\_arn | The ARN of the S3 replica bucket (destination) | `string` | `""` | no |
265267
| s3\_replication\_enabled | Set this to true and specify `s3_replica_bucket_arn` to enable replication | `bool` | `false` | no |
266-
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `""` | no |
268+
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
267269
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
268270
| terraform\_backend\_config\_file\_name | Name of terraform backend config file | `string` | `"terraform.tf"` | no |
269271
| terraform\_backend\_config\_file\_path | Directory for the terraform backend config file, usually `.`. The default is to create no file. | `string` | `""` | no |

context.tf

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#
2+
# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label
3+
# All other instances of this file should be a copy of that one
4+
#
5+
#
6+
# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf
7+
# and then place it in your Terraform module to automatically get
8+
# Cloud Posse's standard configuration inputs suitable for passing
9+
# to Cloud Posse modules.
10+
#
11+
# Modules should access the whole context as `module.this.context`
12+
# to get the input variables with nulls for defaults,
13+
# for example `context = module.this.context`,
14+
# and access individual variables as `module.this.<var>`,
15+
# with final values filled in.
16+
#
17+
# For example, when using defaults, `module.this.context.delimiter`
18+
# will be null, and `module.this.delimiter` will be `-` (hyphen).
19+
#
20+
21+
module "this" {
22+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.21.0"
23+
24+
enabled = var.enabled
25+
namespace = var.namespace
26+
environment = var.environment
27+
stage = var.stage
28+
name = var.name
29+
delimiter = var.delimiter
30+
attributes = var.attributes
31+
tags = var.tags
32+
additional_tag_map = var.additional_tag_map
33+
label_order = var.label_order
34+
regex_replace_chars = var.regex_replace_chars
35+
id_length_limit = var.id_length_limit
36+
37+
context = var.context
38+
}
39+
40+
# Copy contents of cloudposse/terraform-null-label/variables.tf here
41+
42+
variable "context" {
43+
type = object({
44+
enabled = bool
45+
namespace = string
46+
environment = string
47+
stage = string
48+
name = string
49+
delimiter = string
50+
attributes = list(string)
51+
tags = map(string)
52+
additional_tag_map = map(string)
53+
regex_replace_chars = string
54+
label_order = list(string)
55+
id_length_limit = number
56+
})
57+
default = {
58+
enabled = true
59+
namespace = null
60+
environment = null
61+
stage = null
62+
name = null
63+
delimiter = null
64+
attributes = []
65+
tags = {}
66+
additional_tag_map = {}
67+
regex_replace_chars = null
68+
label_order = []
69+
id_length_limit = null
70+
}
71+
description = <<-EOT
72+
Single object for setting entire context at once.
73+
See description of individual variables for details.
74+
Leave string and numeric variables as `null` to use default value.
75+
Individual variable settings (non-null) override settings in context object,
76+
except for attributes, tags, and additional_tag_map, which are merged.
77+
EOT
78+
}
79+
80+
variable "enabled" {
81+
type = bool
82+
default = null
83+
description = "Set to false to prevent the module from creating any resources"
84+
}
85+
86+
variable "namespace" {
87+
type = string
88+
default = null
89+
description = "Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp'"
90+
}
91+
92+
variable "environment" {
93+
type = string
94+
default = null
95+
description = "Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT'"
96+
}
97+
98+
variable "stage" {
99+
type = string
100+
default = null
101+
description = "Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release'"
102+
}
103+
104+
variable "name" {
105+
type = string
106+
default = null
107+
description = "Solution name, e.g. 'app' or 'jenkins'"
108+
}
109+
110+
variable "delimiter" {
111+
type = string
112+
default = null
113+
description = <<-EOT
114+
Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.
115+
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all.
116+
EOT
117+
}
118+
119+
variable "attributes" {
120+
type = list(string)
121+
default = []
122+
description = "Additional attributes (e.g. `1`)"
123+
}
124+
125+
variable "tags" {
126+
type = map(string)
127+
default = {}
128+
description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`"
129+
}
130+
131+
variable "additional_tag_map" {
132+
type = map(string)
133+
default = {}
134+
description = "Additional tags for appending to tags_as_list_of_maps. Not added to `tags`."
135+
}
136+
137+
variable "label_order" {
138+
type = list(string)
139+
default = null
140+
description = <<-EOT
141+
The naming order of the id output and Name tag.
142+
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
143+
You can omit any of the 5 elements, but at least one must be present.
144+
EOT
145+
}
146+
147+
variable "regex_replace_chars" {
148+
type = string
149+
default = null
150+
description = <<-EOT
151+
Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`.
152+
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits.
153+
EOT
154+
}
155+
156+
variable "id_length_limit" {
157+
type = number
158+
default = null
159+
description = <<-EOT
160+
Limit `id` to this many characters.
161+
Set to `0` for unlimited length.
162+
Set to `null` for default, which is `0`.
163+
Does not affect `id_full`.
164+
EOT
165+
}
166+
167+
#### End of copy of cloudposse/terraform-null-label/variables.tf

docs/terraform.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,36 @@
2222
| Name | Description | Type | Default | Required |
2323
|------|-------------|------|---------|:--------:|
2424
| acl | The canned ACL to apply to the S3 bucket | `string` | `"private"` | no |
25-
| additional\_tag\_map | Additional tags for appending to each tag map | `map(string)` | `{}` | no |
25+
| additional\_tag\_map | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no |
2626
| arn\_format | ARN format to be used. May be changed to support deployment in GovCloud/China regions. | `string` | `"arn:aws"` | no |
27-
| attributes | Additional attributes (e.g. `state`) | `list(string)` | <pre>[<br> "state"<br>]</pre> | no |
27+
| attributes | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no |
2828
| billing\_mode | DynamoDB billing mode | `string` | `"PROVISIONED"` | no |
2929
| block\_public\_acls | Whether Amazon S3 should block public ACLs for this bucket | `bool` | `true` | no |
3030
| block\_public\_policy | Whether Amazon S3 should block public bucket policies for this bucket | `bool` | `true` | no |
31-
| context | Default context to use for passing state between label invocations | <pre>object({<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> enabled = bool<br> delimiter = string<br> attributes = list(string)<br> label_order = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": "",<br> "enabled": true,<br> "environment": "",<br> "label_order": [],<br> "name": "",<br> "namespace": "",<br> "regex_replace_chars": "",<br> "stage": "",<br> "tags": {}<br>}</pre> | no |
32-
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
31+
| context | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | <pre>object({<br> enabled = bool<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> delimiter = string<br> attributes = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> label_order = list(string)<br> id_length_limit = number<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_order": [],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
32+
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
3333
| enable\_point\_in\_time\_recovery | Enable DynamoDB point-in-time recovery | `bool` | `false` | no |
3434
| enable\_public\_access\_block | Enable Bucket Public Access Block | `bool` | `true` | no |
3535
| enable\_server\_side\_encryption | Enable DynamoDB server-side encryption | `bool` | `true` | no |
36-
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
36+
| enabled | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
37+
| environment | Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
3738
| force\_destroy | A boolean that indicates the S3 bucket can be destroyed even if it contains objects. These objects are not recoverable | `bool` | `false` | no |
39+
| id\_length\_limit | Limit `id` to this many characters.<br>Set to `0` for unlimited length.<br>Set to `null` for default, which is `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
3840
| ignore\_public\_acls | Whether Amazon S3 should ignore public ACLs for this bucket | `bool` | `true` | no |
39-
| label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | no |
41+
| label\_order | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |
4042
| mfa\_delete | A boolean that indicates that versions of S3 objects can only be deleted with MFA. ( Terraform cannot apply changes of this value; https://github.com/terraform-providers/terraform-provider-aws/issues/629 ) | `bool` | `false` | no |
41-
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `"terraform"` | no |
42-
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `""` | no |
43+
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `null` | no |
44+
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `null` | no |
4345
| prevent\_unencrypted\_uploads | Prevent uploads of unencrypted objects to S3 | `bool` | `true` | no |
4446
| profile | AWS profile name as set in the shared credentials file | `string` | `""` | no |
4547
| read\_capacity | DynamoDB read capacity units | `number` | `5` | no |
46-
| regex\_replace\_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. By default only hyphens, letters and digits are allowed, all other chars are removed | `string` | `"/[^a-zA-Z0-9-]/"` | no |
48+
| regex\_replace\_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
4749
| restrict\_public\_buckets | Whether Amazon S3 should restrict public bucket policies for this bucket | `bool` | `true` | no |
4850
| role\_arn | The role to be assumed | `string` | `""` | no |
4951
| s3\_bucket\_name | S3 bucket name. If not provided, the name will be generated by the label module in the format namespace-stage-name | `string` | `""` | no |
5052
| s3\_replica\_bucket\_arn | The ARN of the S3 replica bucket (destination) | `string` | `""` | no |
5153
| s3\_replication\_enabled | Set this to true and specify `s3_replica_bucket_arn` to enable replication | `bool` | `false` | no |
52-
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `""` | no |
54+
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
5355
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
5456
| terraform\_backend\_config\_file\_name | Name of terraform backend config file | `string` | `"terraform.tf"` | no |
5557
| terraform\_backend\_config\_file\_path | Directory for the terraform backend config file, usually `.`. The default is to create no file. | `string` | `""` | no |

0 commit comments

Comments
 (0)