-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_locals_imports
executable file
·195 lines (173 loc) · 6.25 KB
/
generate_locals_imports
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/sh
#
# generate_locals_imports - Automatically generate `locals` to be used to import Terraform resources.
#
# This script queries the HCP Terraform API to retrieve various
# organization-specific IDs and generates a Terraform `locals` block
# containing these values. The `locals` block is output to a file in the root
# of this repository named `locals_imports.tf` so it can be used to import
# resources.
#
# Usage:
# Ensure `TF_TOKEN_app_terraform_io` is set before running:
# $ export TF_TOKEN_app_terraform_io="your-token-here"
#
# Then execute the script from the root of this repository:
# $ .local/bin/generate_locals_imports
#
# Dependencies:
# - jq (for JSON parsing)
# - terraform (for formatting output)
# - curl (for querying the API)
set -eu
# Check if the required utilities are installed.
for utility in jq terraform curl; do
command -v "${utility}" >/dev/null || {
printf '%s\n' "Error: ${utility} is not installed." >&2
exit 1
}
done
# Check if the required `TF_TOKEN_app_terraform_io` environment variable is set.
tf_token="${TF_TOKEN_app_terraform_io:?'parameter is null or not set. Use export TF_TOKEN_app_terraform_io="" to set this parameter.'}"
# Define important paths.
script_path="$(cd "$(dirname "$0")" && pwd)"
tfvars_path="${script_path}/../../terraform.tfvars"
locals_imports_path="${script_path}/../../locals_imports.tf"
[ -f "${tfvars_path}" ] || {
printf '%s\n' "Error: ${tfvars_path} not found, unable to get the organization name." >&2
exit 1
}
# Get the HCP Terraform organization name from the required `hcp_terraform_organization_name` terraform variable.
organization_name="$(grep '^hcp_terraform_organization_name' "${tfvars_path}" | cut -d'=' -f2 | tr -d ' "')"
# Set the `curl` options once so we can just write `curl "$@"` everywhere.
set -- --silent --header "Authorization: Bearer ${tf_token}" --header "Content-Type: application/vnd.api+json"
# Clear the `locals_imports.tf` file.
: >"${locals_imports_path}"
# The rest of this script will generate a `locals` block by querying the
# HCP Terraform API for the relevant ID using the `TF_TOKEN_app_terraform_io`
# environment variable to authenticate.
#
# The general flow is:
# - Use `printf` to populate the HCL surrounding the IDs to query.
# - Use `curl` to query the relevant API endpoint.
# - Use `jq` to filter for the relevant resources / IDs.
# - Use a `while read -r variable_name; do` loop to iterate over
# queries that require nested `curl` commands to get the necessary
# IDs.
# - Use `printf` to populate the HCL block with the results from the
# queries and filters.
#
# The generated file will resemble the following structure, with the appropriate
# IDs populated based on the HCP Terraform organization being managed:
#
# # This file is generated automatically using: `.local/bin/generate_locals_imports`.
# locals {
# imports = {
# team_ids = {
# owners = "id"
# }
# organization_membership_ids = {
# owners = {
# "email" = "id"
# }
# }
# project_ids = {
# "project" = "id"
# }
# variable_set_ids = {
# "variable_set" = "id"
# }
# workspace_ids = {
# "workspace" = "id"
# }
# }
# }
#
# Finally, `terraform fmt` is used to automatically format the output, ensuring
# consistent spacing and alignment. This allows us to focus on generating the
# content without manually adjusting spacing in the `printf` commands.
{
# shellcheck disable=SC2016
printf '# This file is generated automatically using: `.local/bin/generate_locals_imports`.\n'
printf '%s\n' "locals {"
printf '%s\n' "imports = {"
} >>"${locals_imports_path}"
owners_team_id="$(
curl "$@" https://app.terraform.io/api/v2/organizations/"${organization_name}"/teams |
jq -r '.data[] | select(.attributes.name == "owners") | .id'
)"
{
printf '%s\n' "team_ids = {"
printf '%s = "%s"\n' "owners" "${owners_team_id}"
printf '%s\n' "}"
} >>"${locals_imports_path}"
owners_team_organization_membership_ids="$(
curl "$@" https://app.terraform.io/api/v2/teams/"${owners_team_id}" |
jq -r '.data.relationships."organization-memberships".data[].id' |
while read -r organization_membership_id; do
email="$(
curl "$@" https://app.terraform.io/api/v2/organization-memberships/"${organization_membership_id}" |
jq -r '.data.attributes.email'
)"
printf '"%s" = "%s"\n' "${email}" "${organization_membership_id}"
done
)"
{
printf '%s\n' "organization_membership_ids = {"
printf '%s\n' "owners = {"
printf '%s\n' "${owners_team_organization_membership_ids}"
printf '%s\n%s\n' "}" "}"
} >>"${locals_imports_path}"
project_ids="$(
curl "$@" https://app.terraform.io/api/v2/organizations/"${organization_name}"/projects |
jq -r '.data[].id' |
while read -r project_id; do
project_name="$(
curl "$@" https://app.terraform.io/api/v2/projects/"${project_id}" |
jq -r '.data.attributes.name'
)"
printf '"%s" = "%s"\n' "${project_name}" "${project_id}"
done
)"
{
printf '%s\n' "project_ids = {"
printf '%s\n' "${project_ids}"
printf '%s\n' "}"
} >>"${locals_imports_path}"
variable_set_ids="$(
curl "$@" https://app.terraform.io/api/v2/organizations/"${organization_name}"/varsets |
jq -r '.data[].id' |
while read -r variable_set_id; do
variable_set_name="$(
curl "$@" https://app.terraform.io/api/v2/varsets/"${variable_set_id}" |
jq -r '.data.attributes.name'
)"
printf '"%s" = "%s"\n' "${variable_set_name}" "${variable_set_id}"
done
)"
{
printf '%s\n' "variable_set_ids = {"
printf '%s\n' "${variable_set_ids}"
printf '%s\n' "}"
} >>"${locals_imports_path}"
workspace_ids="$(
curl "$@" https://app.terraform.io/api/v2/organizations/"${organization_name}"/workspaces |
jq -r '.data[].id' |
while read -r workspace_id; do
workspace_name="$(
curl "$@" https://app.terraform.io/api/v2/workspaces/"${workspace_id}" |
jq -r '.data.attributes.name'
)"
printf '%s = "%s"\n' "${workspace_name}" "${workspace_id}"
done
)"
{
printf '%s\n' "workspace_ids = {"
printf '%s\n' "${workspace_ids}"
printf '%s\n' "}"
} >>"${locals_imports_path}"
{
printf '%s\n%s\n' "}" "}"
} >>"${locals_imports_path}"
# Format the `locals` block.
terraform fmt "${locals_imports_path}"