|
6 | 6 | import "tfconfig"
|
7 | 7 | import "strings"
|
8 | 8 |
|
9 |
| -##### Global Variables ##### |
10 |
| -# Define the address of the TFE server |
11 |
| -address = "app.terraform.io" |
| 9 | +#####Functions##### |
12 | 10 |
|
13 |
| -# Define organization variable |
14 |
| -organization = "Cloud-Operations" |
| 11 | +#Prevent resources in root module |
| 12 | +prevent_resources_in_root_module = func() { |
15 | 13 |
|
16 |
| -##### Rules ##### |
17 |
| -# Don't allow resources in root module |
18 |
| -no_resources_in_root_module = rule { |
19 |
| - length(tfconfig.resources) is 0 or not |
20 |
| - (print("Resources not allowed in root module") and |
21 |
| - print("Your root module has", length(tfconfig.resources), "resources.")) |
| 14 | + validated = true |
| 15 | + |
| 16 | + if length(tfconfig.resources) != 0 { |
| 17 | + print("Resources are not allowed in the root module.") |
| 18 | + print("Your root module has", length(tfconfig.resources), "type(s) of resources.") |
| 19 | + validated = false |
| 20 | + } |
22 | 21 |
|
| 22 | + return validated |
23 | 23 | }
|
24 | 24 |
|
25 | 25 | # Require all modules directly under root module to come from PMR
|
26 |
| -require_modules_from_pmr = rule { |
27 |
| - all tfconfig.modules as name, m { |
28 |
| - strings.has_prefix(m.source, address + "/" + organization) or not |
29 |
| - (print("All modules must from the private module registry", |
30 |
| - address + "/" + organization) and |
31 |
| - print("You included module", name, "with source", m.source)) |
| 26 | +require_modules_from_pmr = func(address, organization) { |
| 27 | + |
| 28 | + validated = true |
| 29 | + |
| 30 | + for tfconfig.modules as name, m { |
| 31 | + if not strings.has_prefix(m.source, address + "/" + organization) { |
| 32 | + print("All non-root modules must come from the private module registry", |
| 33 | + address + "/" + organization) |
| 34 | + print("You included module,", name, ", with source,", m.source) |
| 35 | + validated = false |
| 36 | + } |
32 | 37 | }
|
| 38 | + |
| 39 | + return validated |
33 | 40 | }
|
34 | 41 |
|
| 42 | +##### Global Variables ##### |
| 43 | +# Define the address of the TFE server |
| 44 | +address = "app.terraform.io" |
| 45 | + |
| 46 | +# Define organization variable |
| 47 | +organization = "Cloud-Operations" |
| 48 | + |
| 49 | +##### Rules ##### |
| 50 | + |
35 | 51 | # Main rule that requires other rules to be true
|
| 52 | +no_resources_in_root_module = prevent_resources_in_root_module() |
| 53 | +all_non_root_modules_from_pmr = require_modules_from_pmr(address, organization) |
36 | 54 | main = rule {
|
37 |
| - print("modules", tfconfig.modules) and |
38 |
| - no_resources_in_root_module and |
39 |
| - require_modules_from_pmr |
| 55 | + no_resources_in_root_module and all_non_root_modules_from_pmr |
40 | 56 | }
|
0 commit comments