Skip to content

Commit 72e54ae

Browse files
authored
Merge pull request #10 from TerraHubCorp/dev
Transform HCL into TerraHub config with template
2 parents 9247ee0 + 954e404 commit 72e54ae

File tree

12 files changed

+219
-265
lines changed

12 files changed

+219
-265
lines changed

.gitignore

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
# Local .terraform directories
1+
# Local hidden directories
22
**/.terraform/*
3+
**/.backup/*
34

4-
# .tfstate files
5-
**/*.tf*
5+
# Local .tfstate files
6+
*.tfstate
7+
*.tfstate.*
8+
9+
# Zipped google function
10+
google_function/demo.*

.terrahub.yml

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
## project config
21
project:
32
name: demo-terraform-automation-google
4-
code: f2754a99
5-
include: ['.']
6-
exclude: ['**/.terraform/*', '**/node_modules/*']
7-
8-
## template config
3+
code: 7b3c5d2c
4+
include:
5+
- .
6+
exclude:
7+
- '**/.terraform/**'
8+
- '**/node_modules/**'
9+
- '**/.git/**'
10+
terraform:
11+
varFile:
12+
- default.tfvars
13+
version: 0.11.11
914
template:
1015
provider:
1116
google: {}
1217
locals:
13-
google_project_id: project-123456789012
14-
google_region: us-central1
15-
google_org_id: 123456789012
16-
google_service_account_name: demo-team
18+
google_project_id: terrahub-123456
1719
google_billing_account: 123456-ABCDEF-ZYXWVU
18-
google_location_id: us-central
19-
20-
## terraform config
21-
terraform:
22-
varFile: ['default.tfvars']
23-
version: 0.11.7

README.md

Lines changed: 91 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,11 @@ Manual Setup (set values in double quotes and run the following command in termi
1616
```shell
1717
export GOOGLE_CLOUD_PROJECT="" ## e.g. terrahub-123456
1818
export GOOGLE_APPLICATION_CREDENTIALS="" ## e.g. ${HOME}/.config/gcloud/terraform.json
19-
export ORG_ID="" ## e.g. 123456789012
2019
export BILLING_ID="" ## e.g. 123456-ABCDEF-ZYXWVU
2120
export PROJECT_NAME="" ## e.g. TerraHub
2221
export IAM_NAME="" ## e.g. terraform
2322
export IAM_DESC="" ## e.g. terraform service account
2423
```
25-
26-
### Setup ORG_ID Programmatically
27-
28-
Automated Setup (run the following command in terminal):
29-
```shell
30-
export ORG_ID="$(gcloud organizations list --format=json | jq '.[0].name[14:]')"
31-
```
32-
33-
> NOTE: If you don't have JQ CLI, check out this
34-
[installation guide](https://stedolan.github.io/jq/download/)
35-
3624
### Setup BILLING_ID Programmatically
3725

3826
Automated Setup (run the following command in terminal):
@@ -49,18 +37,15 @@ Run the following command in terminal:
4937
```shell
5038
gcloud projects create ${GOOGLE_CLOUD_PROJECT} \
5139
--name="${PROJECT_NAME}" \
52-
--organization="${ORG_ID}" \
5340
--set-as-default
5441

55-
gcloud config set project ${GOOGLE_CLOUD_PROJECT}
42+
gcloud beta billing projects link ${GOOGLE_CLOUD_PROJECT} \
43+
--billing-account="${BILLING_ID}"
5644

5745
gcloud services enable cloudresourcemanager.googleapis.com
5846
gcloud services enable cloudbilling.googleapis.com
5947
gcloud services enable iam.googleapis.com
60-
gcloud services enable compute.googleapis.com
61-
62-
gcloud beta billing projects link ${GOOGLE_CLOUD_PROJECT} \
63-
--billing-account="${BILLING_ID}"
48+
gcloud services enable cloudfunctions.googleapis.com
6449
```
6550

6651
Your output should be similar to the one below:
@@ -95,23 +80,6 @@ Your output should be similar to the one below:
9580
```
9681
```
9782

98-
## Add IAM Policy Binding to Google Cloud Organization
99-
100-
Run the following command in terminal:
101-
```shell
102-
gcloud organizations add-iam-policy-binding ${ORG_ID} \
103-
--member="serviceAccount:${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
104-
--role="roles/resourcemanager.projectCreator"
105-
106-
gcloud organizations add-iam-policy-binding ${ORG_ID} \
107-
--member="serviceAccount:${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
108-
--role="roles/billing.user"
109-
```
110-
111-
Your output should be similar to the one below:
112-
```
113-
```
114-
11583
## Create Terraform Configurations Using TerraHub
11684

11785
Run the following commands in terminal:
@@ -123,80 +91,144 @@ Your output should be similar to the one below:
12391
```
12492
Usage: terrahub [command] [options]
12593
126-
terrahub@0.1.28 (built: 2019-02-08T17:17:41.912Z)
94+
terrahub@0.0.1 (built: 2018-04-07T19:15:39.787Z)
12795
```
12896

12997
> NOTE: If you don't have TerraHub CLI, check out this
13098
[installation guide](https://www.npmjs.com/package/terrahub)
13199

132100
Run the following command in terminal:
133101
```shell
134-
mkdir demo-terraform-google
135-
cd demo-terraform-google
136-
terrahub project -n demo-terraform-google
102+
mkdir demo-terraform-automation-google
103+
cd demo-terraform-automation-google
104+
terrahub project -n demo-terraform-automation-google
137105
```
138106

139107
Your output should be similar to the one below:
140108
```
141109
✅ Project successfully initialized
142110
```
143111

144-
## Create TerraHub Components
112+
## Create TerraHub Components from Templates
145113

146114
Run the following command in terminal:
147115
```shell
148-
terrahub component -t google_project -n project
149-
terrahub component -t google_service_account -n service_account -o ../project
150-
terrahub component -t google_service_account_key -n service_account_key -o ../service_account
151-
terrahub component -t google_project_iam_member -n project_iam_member -o ../project
152-
terrahub component -t google_project_iam_binding -n project_iam_policy_binding_storage_admin -o ../project_iam_member
153-
terrahub component -t google_project_iam_binding -n project_iam_policy_binding_compute_admin -o ../project_iam_member
116+
terrahub component -t google_storage_bucket -n google_storage \
117+
&& terrahub component -t google_cloudfunctions_function -n google_function -o ../google_storage
154118
```
155119

156120
Your output should be similar to the one below:
157121
```
158122
✅ Done
159123
```
160124

161-
## Visualize TerraHub Components
125+
## Update Project Config
162126

163127
Run the following command in terminal:
164128
```shell
165-
terrahub graph
129+
terrahub configure -c terraform.version=0.11.11
130+
terrahub configure -c template.provider.google={}
131+
terrahub configure -c template.locals.google_project_id="${GOOGLE_CLOUD_PROJECT}"
132+
terrahub configure -c template.locals.google_billing_account="${BILLING_ID}"
166133
```
167134

168135
Your output should be similar to the one below:
169136
```
170-
Project: demo-terraform-google
171-
└─ project [path: ./project]
172-
├─ project_iam_member [path: ./project_iam_member]
173-
│ ├─ project_iam_binding_compute_admin [path: ./project_iam_binding_compute_admin]
174-
│ └─ project_iam_binding_storage_admin [path: ./project_iam_binding_storage_admin]
175-
└─ service_account [path: ./service_account]
176-
└─ service_account_key [path: ./service_account_key]
137+
✅ Done
177138
```
178139

179-
## Update Project Config
140+
## Customize TerraHub Component for Storage Bucket
180141

181142
Run the following command in terminal:
182143
```shell
183-
terrahub configure -c template.locals.google_org_id="${ORG_ID}"
184-
terrahub configure -c template.locals.google_billing_account="${BILLING_ID}"
185-
terrahub configure -c template.locals.google_project_id="${GOOGLE_CLOUD_PROJECT}"
144+
terrahub configure -i google_storage -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/google_storage/terraform.tfstate'
145+
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.name='demo_terraform_automation_${local.project["code"]}'
146+
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.location='US'
147+
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.project='${local.google_project_id}'
148+
terrahub configure -i google_storage -c component.template.variable -D -y
186149
```
187150

188151
Your output should be similar to the one below:
189152
```
190153
✅ Done
191154
```
192155

156+
## Customize TerraHub Component for Google Cloud Function
157+
158+
Run the following command in terminal:
159+
```shell
160+
terrahub configure -i google_function -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/google_function/terraform.tfstate'
161+
terrahub configure -i google_function -c component.template.data.terraform_remote_state.storage.backend='local'
162+
terrahub configure -i google_function -c component.template.data.terraform_remote_state.storage.config.path='/tmp/.terrahub/local_backend/google_storage/terraform.tfstate'
163+
terrahub configure -i google_function -c component.template.resource.google_storage_bucket_object.google_storage_object.name='demo.zip'
164+
terrahub configure -i google_function -c component.template.resource.google_storage_bucket_object.google_storage_object.bucket='${data.terraform_remote_state.storage.thub_id}'
165+
terrahub configure -i google_function -c component.template.resource.google_storage_bucket_object.google_storage_object.source='./demo.zip'
166+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.depends_on[0]='google_storage_bucket_object.google_storage_object'
167+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.name='demofunction${local.project["code"]}'
168+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.region='us-central1'
169+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.runtime='nodejs8'
170+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.description='My demo function'
171+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.available_memory_mb=128
172+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.source_archive_bucket='${data.terraform_remote_state.storage.thub_id}'
173+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.source_archive_object='${google_storage_bucket_object.google_storage_object.name}'
174+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.trigger_http=true
175+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.timeout=60
176+
terrahub configure -i google_function -c component.template.resource.google_cloudfunctions_function.google_function.entry_point='helloGET'
177+
terrahub configure -i google_function -c component.template.variable -D -y
178+
terrahub configure -i google_function -c component.template.output -D -y
179+
terrahub configure -i google_function -c component.template.output.id.value='${google_cloudfunctions_function.google_function.id}'
180+
terrahub configure -i google_function -c component.template.output.trigger_url.value='${google_cloudfunctions_function.google_function.https_trigger_url}'
181+
terrahub configure -i google_function -c build.env.variables.THUB_FUNCTION_ZIP='demo.zip'
182+
terrahub configure -i google_function -c build.env.variables.THUB_BUILD_PATH='..'
183+
terrahub configure -i google_function -c build.env.variables.THUB_FUNCTION_TXT='demo.txt'
184+
terrahub configure -i google_function -c build.env.variables.THUB_BUILD_OK='true'
185+
terrahub configure -i google_function -c build.phases.pre_build.commands[0]='echo "BUILD: Running pre_build step"'
186+
terrahub configure -i google_function -c build.phases.pre_build.commands[1]='if [ ! -e "$THUB_FUNCTION_TXT" ]; then touch "$THUB_FUNCTION_TXT"; fi'
187+
terrahub configure -i google_function -c build.phases.pre_build.finally[0]='echo "BUILD: pre_build step successful"'
188+
terrahub configure -i google_function -c build.phases.build.commands[0]='echo "BUILD: Running build step"'
189+
terrahub configure -i google_function -c build.phases.build.commands[1]='if [ "$(head -n 1 "$THUB_FUNCTION_TXT")" != "$(stat -c %y "${THUB_BUILD_PATH}/index.js")" ]; then zip -j ${THUB_FUNCTION_ZIP} ${THUB_BUILD_PATH}/index.js; fi'
190+
terrahub configure -i google_function -c build.phases.build.commands[2]='if [ "$(head -n 1 "$THUB_FUNCTION_TXT")" != "$(stat -c %y "${THUB_BUILD_PATH}/index.js")" ]; then terrahub configure -i google_function -c component.template.resource.google_storage_bucket_object.google_storage_object.name=$(date +%s).zip; fi'
191+
terrahub configure -i google_function -c build.phases.build.finally[0]='echo "BUILD: build step successful"'
192+
terrahub configure -i google_function -c build.phases.post_build.commands[0]='echo "BUILD: Running post_build step"'
193+
terrahub configure -i google_function -c build.phases.post_build.commands[1]='echo $(stat -c %y "${THUB_BUILD_PATH}/index.js") > "$THUB_FUNCTION_TXT"'
194+
terrahub configure -i google_function -c build.phases.post_build.finally[0]='echo "BUILD: post_build step successful"'
195+
```
196+
197+
Your output should be similar to the one below:
198+
```
199+
✅ Done
200+
```
201+
202+
## Visualize TerraHub Components
203+
204+
Run the following command in terminal:
205+
```shell
206+
terrahub graph
207+
```
208+
209+
Your output should be similar to the one below:
210+
```
211+
Project: demo-terraform-automation-google
212+
└─ google_storage [path: ./google_storage]
213+
└─ google_function [path: ./google_function]
214+
```
215+
216+
193217
## Run TerraHub Automation
194218

195219
Run the following command in terminal:
196220
```shell
221+
terrahub build -i google_function
197222
terrahub run -a -y
198223
```
199224

200225
Your output should be similar to the one below:
201226
```
202227
```
228+
229+
## Run Test Command
230+
231+
Run the following command in terminal:
232+
```
233+
curl https://us-central1-terrahub-123456.cloudfunctions.net/demofunctionxxxxxxxx
234+
```

google_function/.terrahub.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
component:
2+
name: google_function
3+
mapping:
4+
- .
5+
dependsOn:
6+
- ../google_storage
7+
template:
8+
resource:
9+
google_cloudfunctions_function:
10+
google_function:
11+
name: 'demofunction${local.project["code"]}'
12+
depends_on:
13+
- google_storage_bucket_object.google_storage_object
14+
region: us-central1
15+
runtime: nodejs8
16+
description: My demo function
17+
available_memory_mb: 128
18+
source_archive_bucket: '${data.terraform_remote_state.storage.thub_id}'
19+
source_archive_object: '${google_storage_bucket_object.google_storage_object.name}'
20+
trigger_http: true
21+
timeout: 60
22+
entry_point: helloGET
23+
google_storage_bucket_object:
24+
google_storage_object:
25+
name: demo.zip
26+
bucket: '${data.terraform_remote_state.storage.thub_id}'
27+
source: ./demo.zip
28+
terraform:
29+
backend:
30+
local:
31+
path: /tmp/.terrahub/local_backend/google_function/terraform.tfstate
32+
data:
33+
terraform_remote_state:
34+
storage:
35+
backend: local
36+
config:
37+
path: /tmp/.terrahub/local_backend/google_storage/terraform.tfstate
38+
output:
39+
id:
40+
value: '${google_cloudfunctions_function.google_function.id}'
41+
trigger_url:
42+
value: '${google_cloudfunctions_function.google_function.https_trigger_url}'
43+
build:
44+
env:
45+
variables:
46+
THUB_FUNCTION_ZIP: demo.zip
47+
THUB_BUILD_PATH: ..
48+
THUB_FUNCTION_TXT: demo.txt
49+
THUB_BUILD_OK: true
50+
phases:
51+
pre_build:
52+
commands:
53+
- 'echo "BUILD: Running pre_build step"'
54+
- 'if [ ! -e "$THUB_FUNCTION_TXT" ]; then touch "$THUB_FUNCTION_TXT"; fi'
55+
finally:
56+
- 'echo "BUILD: pre_build step successful"'
57+
build:
58+
commands:
59+
- 'echo "BUILD: Running build step"'
60+
- >-
61+
if [ "$(head -n 1 "$THUB_FUNCTION_TXT")" != "$(stat -c %y
62+
"${THUB_BUILD_PATH}/index.js")" ]; then zip -j ${THUB_FUNCTION_ZIP}
63+
${THUB_BUILD_PATH}/index.js; fi
64+
- >-
65+
if [ "$(head -n 1 "$THUB_FUNCTION_TXT")" != "$(stat -c %y
66+
"${THUB_BUILD_PATH}/index.js")" ]; then terrahub configure -i
67+
google_function -c
68+
component.template.resource.google_storage_bucket_object.google_storage_object.name=$(date
69+
+%s).zip; fi
70+
finally:
71+
- 'echo "BUILD: build step successful"'
72+
post_build:
73+
commands:
74+
- 'echo "BUILD: Running post_build step"'
75+
- >-
76+
echo $(stat -c %y "${THUB_BUILD_PATH}/index.js") >
77+
"$THUB_FUNCTION_TXT"
78+
finally:
79+
- 'echo "BUILD: post_build step successful"'

google_storage/.terrahub.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
component:
2+
name: google_storage
3+
mapping:
4+
- .
5+
template:
6+
resource:
7+
google_storage_bucket:
8+
google_storage:
9+
name: 'demo_terraform_automation_${local.project["code"]}'
10+
location: US
11+
project: '${local.google_project_id}'
12+
output:
13+
id:
14+
value: '${google_storage_bucket.google_storage.id}'
15+
thub_id:
16+
value: '${google_storage_bucket.google_storage.id}'
17+
self_link:
18+
value: '${google_storage_bucket.google_storage.self_link}'
19+
project:
20+
value: '${google_storage_bucket.google_storage.project}'
21+
url:
22+
value: '${google_storage_bucket.google_storage.url}'
23+
terraform:
24+
backend:
25+
local:
26+
path: /tmp/.terrahub/local_backend/google_storage/terraform.tfstate

0 commit comments

Comments
 (0)