Skip to content

Commit 38ca08b

Browse files
feat: Add sub resource example and descriptors (#19)
* feat: Add example of the sub resource usage with descriptor * feat: Add CODEOWNERS * chore: Upgrade minimal version of Terraform * Update variables.tf Co-authored-by: Mariusz Wojakowski <[email protected]> * Update variables.tf Co-authored-by: Mariusz Wojakowski <[email protected]> * Update variables.tf Co-authored-by: Mariusz Wojakowski <[email protected]> * docs: update README --------- Co-authored-by: Mariusz Wojakowski <[email protected]>
1 parent ffb3e64 commit 38ca08b

File tree

12 files changed

+68
-11
lines changed

12 files changed

+68
-11
lines changed

CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# FILE PATTERN OWNER(S)
2+
* @getindata/devops-labs

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ _Additional information that should be made public, for ex. how to solve known i
6666
| <a name="input_context"></a> [context](#input\_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. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "descriptor_formats": {},<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "labels_as_tags": [<br> "unset"<br> ],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {},<br> "tenant": null<br>}</pre> | no |
6767
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
6868
| <a name="input_descriptor_formats"></a> [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.<br>Map of maps. Keys are names of descriptors. Values are maps of the form<br>`{<br> format = string<br> labels = list(string)<br>}`<br>(Type is `any` so the map values can later be enhanced to provide additional options.)<br>`format` is a Terraform format string to be passed to the `format()` function.<br>`labels` is a list of labels, in order, to pass to `format()` function.<br>Label values will be normalized before being passed to `format()` so they will be<br>identical to how they appear in `id`.<br>Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no |
69+
| <a name="input_descriptor_name"></a> [descriptor\_name](#input\_descriptor\_name) | Name of the descriptor used to form a resource name | `string` | `"resource-type"` | no |
6970
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
7071
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
71-
| <a name="input_example_var"></a> [example\_var](#input\_example\_var) | Example varible passed into the module | `string` | n/a | yes |
72+
| <a name="input_example_var"></a> [example\_var](#input\_example\_var) | Example variable passed into the module | `string` | n/a | yes |
7273
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
7374
| <a name="input_label_key_case"></a> [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.<br>Does not affect keys of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper`.<br>Default value: `title`. | `string` | `null` | no |
7475
| <a name="input_label_order"></a> [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no |
@@ -78,13 +79,15 @@ _Additional information that should be made public, for ex. how to solve known i
7879
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
7980
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br>Characters matching the regex will be removed from the ID elements.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
8081
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
82+
| <a name="input_sub_resource"></a> [sub\_resource](#input\_sub\_resource) | Some other resource that is part of stack/module | <pre>object({<br> descriptor_name = optional(string, "sub-resource-type")<br> example_var = string<br> })</pre> | n/a | yes |
8183
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
8284
| <a name="input_tenant"></a> [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no |
8385

8486
## Modules
8587

8688
| Name | Source | Version |
8789
|------|--------|---------|
90+
| <a name="module_subresource_label"></a> [subresource\_label](#module\_subresource\_label) | cloudposse/label/null | 0.25.0 |
8891
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |
8992

9093
## Outputs
@@ -103,14 +106,15 @@ _Additional information that should be made public, for ex. how to solve known i
103106

104107
| Name | Version |
105108
|------|---------|
106-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.15.0 |
109+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
107110
| <a name="requirement_null"></a> [null](#requirement\_null) | 3.1.1 |
108111

109112
## Resources
110113

111114
| Name | Type |
112115
|------|------|
113116
| [null_resource.output_input](https://registry.terraform.io/providers/hashicorp/null/3.1.1/docs/resources/resource) | resource |
117+
| [null_resource.subresource](https://registry.terraform.io/providers/hashicorp/null/3.1.1/docs/resources/resource) | resource |
114118
<!-- END_TF_DOCS -->
115119

116120
## CONTRIBUTING

examples/complete/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module "terraform_module_template" {
66
context = module.this.context
77
88
example_var = "This is a example value."
9+
sub_resource = {
10+
example_var = "This is a example value of sub resource."
11+
}
912
}
1013
```
1114

examples/complete/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ module "terraform_module_template" {
33
context = module.this.context
44

55
example_var = "This is a example value."
6+
sub_resource = {
7+
example_var = "This is a example value of sub resource."
8+
}
69
}

examples/complete/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 0.15.0"
2+
required_version = ">= 1.3.0"
33

44
required_providers {
55
null = {

examples/simple/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
```terraform
44
module "terraform_module_template" {
5-
source = "../../"
5+
source = "../../"
66
77
example_var = "This is a example value."
8+
sub_resource = {
9+
example_var = "This is a example value of sub resource."
10+
}
811
}
912
```
1013

examples/simple/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module "terraform_module_template" {
22
source = "../../"
33

44
example_var = "This is a example value."
5+
sub_resource = {
6+
example_var = "This is a example value of sub resource."
7+
}
58
}

examples/simple/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 0.15.0"
2+
required_version = ">= 1.3.0"
33

44
required_providers {
55
null = {

locals.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
locals {
22
# Get a name from the descriptor. If not available, use default naming convention.
33
# Trim and replace function are used to avoid bare delimiters on both ends of the name and situation of adjacent delimiters.
4-
name_from_descriptor = local.enabled ? trim(replace(
5-
lookup(module.this.descriptors, "module-resource-name", module.this.id), "/${module.this.delimiter}${module.this.delimiter}+/", module.this.delimiter
4+
#
5+
# todo: Build a wrapper module around context module with name from descriptor feature
6+
name_from_descriptor = module.this.enabled ? trim(replace(
7+
lookup(module.this.descriptors, var.descriptor_name, module.this.id), "/${module.this.delimiter}${module.this.delimiter}+/", module.this.delimiter
68
), module.this.delimiter) : null
79

10+
subresource_name_from_descriptor = module.subresource_label.enabled ? trim(replace(
11+
lookup(module.subresource_label.descriptors, var.sub_resource.descriptor_name, module.subresource_label.id), "/${module.subresource_label.delimiter}${module.subresource_label.delimiter}+/", module.subresource_label.delimiter
12+
), module.subresource_label.delimiter) : null
13+
814
enabled = module.this.enabled
915
}

main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,24 @@ resource "null_resource" "output_input" {
1313
command = "echo ${var.example_var} | base64"
1414
}
1515
}
16+
17+
module "subresource_label" {
18+
source = "cloudposse/label/null"
19+
version = "0.25.0"
20+
context = module.this.context
21+
22+
attributes = ["sub"]
23+
}
24+
25+
resource "null_resource" "subresource" {
26+
count = local.enabled ? 1 : 0
27+
28+
triggers = {
29+
name = local.subresource_name_from_descriptor
30+
input = var.sub_resource.example_var
31+
}
32+
33+
provisioner "local-exec" {
34+
command = "echo ${var.sub_resource.example_var} | base64"
35+
}
36+
}

variables.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
# Example, compulsory input variable
2-
31
variable "example_var" {
4-
description = "Example varible passed into the module"
2+
description = "Example variable passed into the module"
3+
type = string
4+
}
5+
6+
variable "descriptor_name" {
7+
description = "Name of the descriptor used to form a resource name"
58
type = string
9+
default = "resource-type"
10+
}
11+
12+
variable "sub_resource" {
13+
description = "Some other resource that is part of stack/module"
14+
type = object({
15+
descriptor_name = optional(string, "sub-resource-type")
16+
example_var = string
17+
})
618
}

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Example configuration of terraform providers
22

33
terraform {
4-
required_version = ">= 0.15.0"
4+
required_version = ">= 1.3.0"
55

66
required_providers {
77
null = {

0 commit comments

Comments
 (0)