generated from getindata/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
176 lines (167 loc) · 8.43 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
locals {
#Repo attributes that are meant to simplify configuration rather than being actual repo options
helper_options = ["allow_all_server_side_workflows", "terragrunt_atlantis_config", "infracost"]
infracost_parameters = {
github : { token_name : "gh-token", environment_variable_name : "ATLANTIS_GH_TOKEN" },
gitlab : { token_name : "gitlab-token", environment_variable_name : "ATLANTIS_GITLAB_TOKEN" },
bitbucket : { token_name : "bitbucket-token", environment_variable_name : "ATLANTIS_BITBUCKET_TOKEN" }
}
#Remove all options that are null
repos_with_non_null_values = [
for repo in var.repos : merge(
{ for k, v in var.repos_common_config : k => v if v != null },
{ for k, v in repo : k => v if v != null },
{ terragrunt_atlantis_config : merge(
var.repos_common_config.terragrunt_atlantis_config,
{ for k, v in repo.terragrunt_atlantis_config : k => v if v != null }
) }
)
]
#Apply helper variables and then remove them
repos = [
for repo in local.repos_with_non_null_values : merge(
{
for k, v in repo : k => v if contains(local.helper_options, k) == false
},
repo.allow_all_server_side_workflows ? { allowed_workflows = concat(repo.allowed_workflows, keys(local.workflows)) } : { allowed_workflows = repo.allowed_workflows },
{
pre_workflow_hooks = concat(
lookup(repo, "pre_workflow_hooks", []),
repo.terragrunt_atlantis_config.enabled ? [
{
run : join(" ", compact(
[
"terragrunt-atlantis-config",
"generate",
format("--output \"%s\"", repo.terragrunt_atlantis_config.output),
repo.terragrunt_atlantis_config.filter != null ? format("--filter \"%s\"", repo.terragrunt_atlantis_config.filter) : null,
repo.terragrunt_atlantis_config.parallel != null ? format("--parallel=%s", repo.terragrunt_atlantis_config.parallel) : null,
repo.terragrunt_atlantis_config.autoplan != null ? format("--autoplan=%s", repo.terragrunt_atlantis_config.autoplan) : null,
repo.terragrunt_atlantis_config.automerge != null ? format("--automerge=%s", repo.terragrunt_atlantis_config.automerge) : null,
repo.terragrunt_atlantis_config.cascade_dependencies != null ? format("--cascade-dependencies=%s", repo.terragrunt_atlantis_config.cascade_dependencies) : null,
repo.terragrunt_atlantis_config.use_project_markers != null ? format("--use-project-markers=%s", repo.terragrunt_atlantis_config.use_project_markers) : null,
]
))
}
] : [],
lookup(repo, "workflow", "") != "" && lookup(local._workflows, lookup(repo, "workflow", ""), "") != "" ? (
local._workflows[lookup(repo, "workflow", "")].infracost.enabled ? [
{ run : "rm -rf /tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM" },
{ run : "mkdir -p /tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM" }
] : []) : []
),
post_workflow_hooks = concat(
lookup(repo, "post_workflow_hooks", []),
lookup(repo, "workflow", "") != "" && lookup(local._workflows, lookup(repo, "workflow", ""), "") != "" ? (
local._workflows[lookup(repo, "workflow", "")].infracost.enabled ? [
{ run : join("", [
"if [ ! -d \"/tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM\" ]; then exit 0; fi",
"\n\n",
join(" ", [
"infracost",
"comment",
local._workflows[lookup(repo, "workflow", "")].infracost.platform,
"--repo $BASE_REPO_OWNER/$BASE_REPO_NAME",
"--merge-request $PULL_NUM",
"--path /tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM/'*'-infracost.json",
format("--%s $%s",
local.infracost_parameters[local._workflows[repo.workflow].infracost.platform].token_name,
coalesce(
local._workflows[repo.workflow].infracost.token_environment_variable,
local.infracost_parameters[local._workflows[repo.workflow].infracost.platform].environment_variable_name)
),
format("--behavior %s", local._workflows[lookup(repo, "workflow", "")].infracost.behavior)
]),
"\n\n",
"rm -rf /tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM"
]
)
}
] : []) : []
)
})
]
workflows_helper_options = ["asdf", "checkov", "pull_gitlab_variables", "check_gitlab_approvals", "template", "infracost"]
# tflint-ignore: terraform_naming_convention
_workflows = {
for workflow_name, workflow in var.workflows : workflow_name => {
plan = merge(
local.null_workflow.plan,
can(workflow.template) ? local.workflow_templates[workflow["template"]].plan : null,
workflow.plan)
apply = merge(
local.null_workflow.apply,
can(workflow.template) ? local.workflow_templates[workflow["template"]].apply : null,
workflow.apply),
import = merge(
local.null_workflow.import,
can(workflow.template) ? local.workflow_templates[workflow["template"]].import : null,
workflow.import)
state_rm = merge(
local.null_workflow.state_rm,
can(workflow.template) ? local.workflow_templates[workflow["template"]].state_rm : null,
workflow.state_rm),
pull_gitlab_variables = workflow.pull_gitlab_variables
asdf = workflow.asdf
checkov = workflow.checkov
check_gitlab_approvals = workflow.check_gitlab_approvals
infracost = workflow.infracost
}
}
asdf_steps = [{ run = "asdf install" }]
pull_gitlab_variables_steps = [{ multienv = "pull-gitlab-variables.sh" }]
check_gitlab_approvals_steps = [{ run = "check-gitlab-approvals.sh" }]
workflows = {
for workflow_name, workflow in local._workflows : workflow_name => {
for stage_name, stage in workflow : stage_name => { steps : concat(
workflow.asdf.enabled && stage_name == "plan" ? local.asdf_steps : [],
workflow.pull_gitlab_variables.enabled ? local.pull_gitlab_variables_steps : [],
workflow.check_gitlab_approvals.enabled && stage_name == "apply" ? local.check_gitlab_approvals_steps : [],
flatten([
for step in stage.steps : [
for name, object in step :
jsondecode(
name == "atlantis_step" ?
(object.extra_args != null ? jsonencode({ (object.command) : { extra_args : object.extra_args } }) : jsonencode(object.command)) :
jsonencode({ (name) : object })
)
if object != null
]]),
workflow.checkov.enabled && stage_name == "plan" ? [
{ run = "terragrunt show -json $PLANFILE > $SHOWFILE" },
{
run = join(" ", compact(
[
"checkov",
format("--file \"%s\"", workflow.checkov.file),
"--output github_failed_only",
workflow.checkov.soft_fail != null ? "--soft-fail" : null
]
))
}
] : [],
jsondecode(workflow.infracost.enabled && stage_name == "plan" ? jsonencode([
{ env = { name = "INFRACOST_OUTPUT", command = "echo /tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM/$WORKSPACE-`echo $REPO_REL_DIR | sed 's#/#-#g'`-infracost.json" } },
{ run = "infracost breakdown --path=$SHOWFILE --format=json --log-level=info --out-file=$INFRACOST_OUTPUT --project-name=$REPO_REL_DIR" }
]) : jsonencode([]))
) } if !contains(local.workflows_helper_options, stage_name) && lookup(stage, "steps", null) != null
}
}
repo_config = {
repos = local.repos
workflows = local.workflows
}
repo_config_json = jsonencode(local.repo_config)
repo_config_yaml = replace(
replace(yamlencode(local.repo_config), "/((?:^|\n)[\\s-]*)\"([\\w-]+)\":/", "$1$2:"),
"/((?:^|\n)[\\s-]*)\"([\\w-]+)\"/", "$1$2")
}
resource "local_file" "repo_config" {
count = var.repo_config_file.enabled ? 1 : 0
content = (var.repo_config_file.format == "json"
? local.repo_config_json
: local.repo_config_yaml
)
filename = format("%s/%s", var.repo_config_file.path, var.repo_config_file.name)
file_permission = "0644"
}