Skip to content

Commit

Permalink
feat: Add sub resource example and descriptors (#19)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
jakubigla and mariusz89016 authored Oct 13, 2023
1 parent ffb3e64 commit 38ca08b
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# FILE PATTERN OWNER(S)
* @getindata/devops-labs
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ _Additional information that should be made public, for ex. how to solve known i
| <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 |
| <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 |
| <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 |
| <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 |
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
| <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 |
| <a name="input_example_var"></a> [example\_var](#input\_example\_var) | Example varible passed into the module | `string` | n/a | yes |
| <a name="input_example_var"></a> [example\_var](#input\_example\_var) | Example variable passed into the module | `string` | n/a | yes |
| <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 |
| <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 |
| <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 |
Expand All @@ -78,13 +79,15 @@ _Additional information that should be made public, for ex. how to solve known i
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |

## Modules

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

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

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.15.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | 3.1.1 |

## Resources

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

## CONTRIBUTING
Expand Down
3 changes: 3 additions & 0 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module "terraform_module_template" {
context = module.this.context
example_var = "This is a example value."
sub_resource = {
example_var = "This is a example value of sub resource."
}
}
```

Expand Down
3 changes: 3 additions & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ module "terraform_module_template" {
context = module.this.context

example_var = "This is a example value."
sub_resource = {
example_var = "This is a example value of sub resource."
}
}
2 changes: 1 addition & 1 deletion examples/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.15.0"
required_version = ">= 1.3.0"

required_providers {
null = {
Expand Down
5 changes: 4 additions & 1 deletion examples/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

```terraform
module "terraform_module_template" {
source = "../../"
source = "../../"
example_var = "This is a example value."
sub_resource = {
example_var = "This is a example value of sub resource."
}
}
```

Expand Down
3 changes: 3 additions & 0 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module "terraform_module_template" {
source = "../../"

example_var = "This is a example value."
sub_resource = {
example_var = "This is a example value of sub resource."
}
}
2 changes: 1 addition & 1 deletion examples/simple/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.15.0"
required_version = ">= 1.3.0"

required_providers {
null = {
Expand Down
10 changes: 8 additions & 2 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
locals {
# Get a name from the descriptor. If not available, use default naming convention.
# Trim and replace function are used to avoid bare delimiters on both ends of the name and situation of adjacent delimiters.
name_from_descriptor = local.enabled ? trim(replace(
lookup(module.this.descriptors, "module-resource-name", module.this.id), "/${module.this.delimiter}${module.this.delimiter}+/", module.this.delimiter
#
# todo: Build a wrapper module around context module with name from descriptor feature
name_from_descriptor = module.this.enabled ? trim(replace(
lookup(module.this.descriptors, var.descriptor_name, module.this.id), "/${module.this.delimiter}${module.this.delimiter}+/", module.this.delimiter
), module.this.delimiter) : null

subresource_name_from_descriptor = module.subresource_label.enabled ? trim(replace(
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
), module.subresource_label.delimiter) : null

enabled = module.this.enabled
}
21 changes: 21 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,24 @@ resource "null_resource" "output_input" {
command = "echo ${var.example_var} | base64"
}
}

module "subresource_label" {
source = "cloudposse/label/null"
version = "0.25.0"
context = module.this.context

attributes = ["sub"]
}

resource "null_resource" "subresource" {
count = local.enabled ? 1 : 0

triggers = {
name = local.subresource_name_from_descriptor
input = var.sub_resource.example_var
}

provisioner "local-exec" {
command = "echo ${var.sub_resource.example_var} | base64"
}
}
18 changes: 15 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Example, compulsory input variable

variable "example_var" {
description = "Example varible passed into the module"
description = "Example variable passed into the module"
type = string
}

variable "descriptor_name" {
description = "Name of the descriptor used to form a resource name"
type = string
default = "resource-type"
}

variable "sub_resource" {
description = "Some other resource that is part of stack/module"
type = object({
descriptor_name = optional(string, "sub-resource-type")
example_var = string
})
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Example configuration of terraform providers

terraform {
required_version = ">= 0.15.0"
required_version = ">= 1.3.0"

required_providers {
null = {
Expand Down

0 comments on commit 38ca08b

Please sign in to comment.