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 25, 2024
1 parent 9c81fe9 commit 5d316a0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 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)
20 changes: 12 additions & 8 deletions hooks/domain-component-valididate/check
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/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 @@ -17,19 +21,19 @@ check_component_file_name() {
local file="$3"
local filename
local expected_file
local parent_directory
local parent_directory_name

filename=$(basename "$file")
expected_file="${component_name//_/-}.tf"
parent_directory=$(dirname "$file")
parent_directory_name=$(basename "$parent_directory")

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
# allow <component>.tf
# allow components/<component>/main.tf
if [[ "$filename" = "$expected_file" || "$file" =~ components/${component_name}/main.tf ]]; then
return 0
fi
return 0

# otherwise, it will return an error
echo "ERROR: Module '$module_name' should be in '$expected_file' or 'components/$component_name/main.tf', but found in '$filename'."
return 1
}

check_component_files() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module "component_good_foo_bar" {
module "component_foo_bar" {
source = "app.terraform.io/turo/component-metadata/null"
version = "3.1.2"

name = "good-foo-bar"
name = "foo-bar"
system_metadata = var.metadata_module.parent_system_metadata
}
4 changes: 2 additions & 2 deletions hooks/domain-component-valididate/test
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ echo "testing: $script_directory"
echo "testing: check $script_directory/fixtures/good-foo-bar.tf"
"$script_directory/check" "$script_directory/fixtures/good-foo-bar.tf"

echo "testing: check $script_directory/fixtures/good-component/main.tf"
"$script_directory/check" "$script_directory/fixtures/good-component/main.tf"
echo "testing: check $script_directory/fixtures/components/foo-bar/main.tf"
"$script_directory/check" "$script_directory/fixtures/components/foo-bar/main.tf"

echo "testing: check $script_directory/fixtures/bad-folder/main.tf"
echo " expecting error"
Expand Down

0 comments on commit 5d316a0

Please sign in to comment.