Skip to content

Commit

Permalink
docs(component): validate good component
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingandyyy committed Jun 24, 2024
1 parent 9c81fe9 commit 0ce4e0b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/modules/component.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Module: Component

## Define component

Developer **MUST** use turo component module to define a component, and the resource name **MUST** prefix with `component_`.

```tf
module "component_good_foo_bar" {
source = "app.terraform.io/turo/component-metadata/null"
version = "3.1.2"
name = "good-foo-bar"
system_metadata = var.metadata_module.parent_system_metadata
}
```

## Locate the component

### Simple Component

The component definition block **MUST** sit in a terraform file named `<component-name>.tf`

For the above good-foo-bar example, the file name **MUST** be `good-foo-bar.tf`

### Component module

For complex component which has its own component module/folder, the component definition block **MUST** sit in the `main.tf` of the component module/folder.

For the above good-foo-bar example, the file **MUST** be `<component-name>-component/main.tf`.
1 change: 1 addition & 0 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ We use modules for a few reasons:
See:

- [breaking-changes](breaking-changes.md)
- [component](component.md)
- [input-variables](input-variables.md)
- [output-variables](output-variables.md)
7 changes: 7 additions & 0 deletions hooks/domain-component-valididate/check
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/bin/bash -e

# This script check if the component usage is valid:

# allow case: <component>.tf
# allow case: <component>-component/main.tf

# Function to check if module names follow the 'component_<name>' convention
check_component_module_id() {
local module_name="$1"
Expand All @@ -25,6 +30,8 @@ check_component_file_name() {
parent_directory=$(dirname "$file")
parent_directory_name=$(basename "$parent_directory")

# allow <component>.tf
# allow <component>-component/main.tf
if [[ "$filename" != "$expected_file" && ("$filename" != "main.tf" || ! "$parent_directory_name" =~ -component$) ]]; then
echo "ERROR: Module '$module_name' with name '$component_name' should be in '$expected_file' or a 'main.tf' within a directory ending with '-component', but found in '$filename'."
return 1
Expand Down

0 comments on commit 0ce4e0b

Please sign in to comment.