Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Initial investigation of Terraform in Kokoro integration tests #8555

Merged
merged 173 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
173 commits
Select commit Hold shift + click to select a range
1d3f47c
ci: Initial Terraform configurations (Work in Progress)
burkedavison Sep 21, 2022
fb6b240
ci: Add Terraform scripts and configurations for java-accessapproval,…
burkedavison Sep 21, 2022
fe40718
fix: Remove hardcoded project prefix, and replace with environment va…
burkedavison Sep 21, 2022
65ff848
fix: Ensure 'set-quota-project' is set to the created GCP project. De…
burkedavison Sep 21, 2022
6f89ea6
fix: Workaround set-quota-project not working when previous quota pro…
burkedavison Sep 22, 2022
dad488e
chore: Add 'auto_create_subnetworks' to container network configuration.
burkedavison Sep 22, 2022
3959735
fix: Add predestroy stage. Remove container network from tfstate to a…
burkedavison Sep 22, 2022
3fbf756
chore: Refactor script to split project and module provisioning. Supp…
burkedavison Sep 22, 2022
061b666
chore: Add java-compute terraform config
burkedavison Sep 22, 2022
9eaac54
chore: Add java-containeranalysis terraform config
burkedavison Sep 22, 2022
1ddad88
chore: Add java-datacatalog terraform config
burkedavison Sep 22, 2022
638566e
chore: Add java-datalabeling terraform config
burkedavison Sep 22, 2022
45dfae5
chore: Add java-errorreporting terraform config
burkedavison Sep 22, 2022
1aee4e1
chore: Add java-game-servers terraform config
burkedavison Sep 22, 2022
57a1ef3
chore: Add java-iot terraform config
burkedavison Sep 22, 2022
52b4884
chore: Add java-kms terraform config
burkedavison Sep 22, 2022
fdf8198
chore: Add java-monitoring terraform config
burkedavison Sep 22, 2022
4c93bcb
chore: Add java-resourcemanager terraform config
burkedavison Sep 22, 2022
d8d5727
chore: Add java-secretmanager terraform config
burkedavison Sep 22, 2022
cbe9b98
chore: Add java-speech terraform config
burkedavison Sep 22, 2022
cf3c219
chore: Add java-trace terraform config
burkedavison Sep 22, 2022
f7543ae
chore: Add java-translate terraform config
burkedavison Sep 22, 2022
d5b11ca
chore: Add java-video-intelligence terraform config
burkedavison Sep 22, 2022
57aebd8
chore: Add java-vision terraform config
burkedavison Sep 22, 2022
5e8a943
chore: Refactor solution to use single generated Terraform root modul…
burkedavison Sep 26, 2022
3a817cf
chore: Add java-asset
burkedavison Sep 26, 2022
85d4ace
chore: Add java-iam-admin
burkedavison Sep 26, 2022
e13a6e9
chore: Add java-notebooks
burkedavison Sep 26, 2022
72a0d60
chore: Add java-texttospeech
burkedavison Sep 26, 2022
8594ff7
chore: Add java-dataproc
burkedavison Sep 26, 2022
51f1fd8
chore: Add java-tasks
burkedavison Sep 26, 2022
8f27d2a
fix: Remove unintentional .kokoro/build.sh modification.
burkedavison Sep 26, 2022
7efd5d4
fix: Ensure compute API is enabled prior to requesting compute defaul…
burkedavison Sep 27, 2022
cacbcec
chore: Create and Impersonate a Service Account on the Terraform-crea…
burkedavison Sep 27, 2022
13c2f1b
chore: Add java-dns terraform configuration
burkedavison Sep 27, 2022
329748c
chore: Add java-dialogflow and java-dialogflow-cx terraform configura…
burkedavison Sep 27, 2022
c75f5c8
fix: Ensure environment variables are correctly added when performing…
burkedavison Sep 28, 2022
41f1325
fix: Fail fast if terraform apply fails.
burkedavison Sep 28, 2022
8cecc8d
chore: Rework module output and env.sh design to eliminate need for f…
burkedavison Sep 28, 2022
ecfdd45
chore: Add java-notification support
burkedavison Sep 28, 2022
327c613
fix: Ensure environment variables are set with script if not already …
burkedavison Sep 28, 2022
977ce54
fix: Add time delay to allow role/permissions to apply to service acc…
burkedavison Sep 28, 2022
6c25de7
fix: Relocate invocation of generated-env.sh to ensure consistent app…
burkedavison Sep 28, 2022
3d4cc93
chore: Support 'y' argument to destroy.sh to destroy the project non-…
burkedavison Sep 28, 2022
5b71c3f
fix: Destroy modules before project to ensure clean starting point fo…
burkedavison Sep 28, 2022
36f146d
chore: Add test-individually.sh, which iterates through each terrafor…
burkedavison Sep 28, 2022
1f86837
fix: Apply predestroy.sh to <root>/.terraform state.
burkedavison Sep 28, 2022
cd1b082
chore: Add java-scheduler, although not yet reliable.
burkedavison Sep 29, 2022
03ef514
chore: Add java-oslogin. Not currently working.
burkedavison Sep 29, 2022
9ae6b1a
fix: Rework of Terraform logic to use single root module. Project and…
burkedavison Sep 30, 2022
74a6728
fix: cleanup
burkedavison Sep 30, 2022
040d9a3
fix: Ensure cloudresourcemanager.googleapis.com is enabled prior to s…
burkedavison Oct 3, 2022
be9b07c
Merge branch 'main' into terraform
burkedavison Oct 3, 2022
a50e5fd
fix: Workaround for 'Resource Exhausted' error given first time runni…
burkedavison Oct 3, 2022
01b0570
fix: Re-enable job and topic deletions during Scheduler IT clean up.
burkedavison Oct 3, 2022
5e05e29
fix: Re-enable application-default login and add delay after enabling…
burkedavison Oct 3, 2022
8d3ded0
fix: Apply retry to v1beta1 Scheduler IT
burkedavison Oct 3, 2022
42e677d
fix: Sleep immediately after gcloud project creation to allow default…
burkedavison Oct 3, 2022
30da4a1
fix: Add 10s delay after creating service account and before assignin…
burkedavison Oct 3, 2022
6a8b09a
fix: Replace compound assertion statement with fine-grain assertions …
burkedavison Oct 3, 2022
2529c10
Merge remote-tracking branch 'origin/terraform' into terraform
burkedavison Oct 3, 2022
7ee1e2a
fix: Add delay to allow service account permissions to settle.
burkedavison Oct 3, 2022
70ce173
chore: Handle generated-outputs.template.tf same as generated-main+va…
burkedavison Oct 3, 2022
e1f2f03
fix: Assign gcloud_account local variable before using it. Comment im…
burkedavison Oct 3, 2022
eb9a2f1
fix: Synchronize environment using common script when entering indivi…
burkedavison Oct 3, 2022
68e6e14
fix: Fail ITHeadersTest if server does not respond to request, rather…
burkedavison Oct 3, 2022
854dac7
fix: Sleep syntax, and no longer sync environment before apply.sh
burkedavison Oct 3, 2022
c800cdc
fix: DRY and remove unnecessary resources from template.
burkedavison Oct 3, 2022
5c204bd
Revert "fix: Fail ITHeadersTest if server does not respond to request…
burkedavison Oct 4, 2022
82de4ef
chore: Explicit error when headers is null.
burkedavison Oct 4, 2022
3b8c01b
chore: Make API enabling and disabling behaviors variables, following…
burkedavison Oct 4, 2022
ff29a16
fix: Increase retries and retry delay in Scheduler IT to ensure enoug…
burkedavison Oct 4, 2022
7eb3309
fix: Add region to submodule input object. Fix typo.
burkedavison Oct 4, 2022
dd7a68e
Merge branch 'main' into terraform
burkedavison Oct 4, 2022
acc1c28
fix: When running ITs with terraform, ignore os-login, recommender, a…
burkedavison Oct 4, 2022
77fec0e
chore: DRY friendly output module name, and module output value parsing.
burkedavison Oct 4, 2022
41bc320
fix: Randomize Dialogflow Agent name.
burkedavison Oct 4, 2022
8629ecd
fix: Randomize Container Network name.
burkedavison Oct 4, 2022
cacb6ed
fix: Attempt to refresh Terraform's state if no project ID is in its …
burkedavison Oct 4, 2022
69a760d
chore: Add java-recommender Terraform configuration, and adjust IT to…
burkedavison Oct 4, 2022
75c031d
chore: Improve ./.terraform/README.md
burkedavison Oct 4, 2022
92fa513
fix: Only invoke predestroy.sh for active modules.
burkedavison Oct 5, 2022
d6a936f
chore: Refactor all Terraform project setup steps into setup.sh
burkedavison Oct 5, 2022
fb6b69a
chore: Add java-talent Terraform configuration
burkedavison Oct 5, 2022
09cf5cb
chore: Simplify
burkedavison Oct 6, 2022
1a06ff9
fix: Consistent application of variable rename
burkedavison Oct 6, 2022
8611e97
fix: Wait 60s after service account creation to mitigate errors due t…
burkedavison Oct 6, 2022
3f18991
chore: Slightly lengthen initialization waiting time for service acco…
burkedavison Oct 6, 2022
b891f4d
fix: Additional delay and comment around scheduler retry.
burkedavison Oct 6, 2022
5778d9b
chore: Remove ./.terraform/README.md while offline review continues.
burkedavison Oct 6, 2022
8604095
Merge branch 'main' into terraform
burkedavison Oct 6, 2022
0c917af
fix: Don't prepend "serviceAccount:" when using member "allAuthentica…
burkedavison Oct 7, 2022
f479750
Merge branch 'main' into terraform
burkedavison Oct 7, 2022
f8277b8
chore: Change module delimiter from ':' to ',' for consistency with .…
burkedavison Oct 7, 2022
843c440
Merge branch 'main' into terraform
burkedavison Oct 7, 2022
3f3216e
ci: Initial investigation of Terraform in Kokoro integration tests
burkedavison Oct 7, 2022
36eb8b6
chore: Use Terraform during Kokoro integration test.
burkedavison Oct 7, 2022
0fef18c
chore: Split 'gcloud' steps from 'terraform' steps in preparation for…
burkedavison Oct 7, 2022
2d24c78
Merge branch 'terraform' into terraform-and-kokoro
burkedavison Oct 7, 2022
483dea6
fix: Return to previous working directory after plan.sh
burkedavison Oct 7, 2022
0d1ffe3
Merge branch 'terraform' into terraform-and-kokoro
burkedavison Oct 7, 2022
5d66a9a
chore: Use set -eo pipefail rather than explicit "|| exit"
burkedavison Oct 10, 2022
c5264f9
chore: Use set -eo pipefail rather than explicit "|| exit"
burkedavison Oct 10, 2022
42b099b
Merge branch 'main' into terraform
burkedavison Oct 11, 2022
70527ff
chore: Remove 'test-individually.sh'
burkedavison Oct 11, 2022
bc34548
fix: Ignore 'java-recommender' IT rather than modifying logic.
burkedavison Oct 11, 2022
56abe0c
Merge branch 'main' into terraform
burkedavison Oct 14, 2022
ac8ed50
Merge branch 'terraform' into terraform-and-kokoro
burkedavison Oct 14, 2022
0db282c
chore: release main (#8610)
release-please[bot] Oct 14, 2022
6420712
build: have release-please tag releases (#8615)
chingor13 Oct 17, 2022
023793d
Merge branch 'main' into terraform
burkedavison Oct 17, 2022
de6b4a2
Merge branch 'terraform' into terraform-and-kokoro
burkedavison Oct 17, 2022
774cd3d
ci: Change project from gcloud-devel to cloud-java-ci-tests
burkedavison Oct 17, 2022
9e890a8
ci: Update kokoro credential to cloud-java-ci-it-service-account
burkedavison Oct 19, 2022
ddae556
Merge branch 'main' into terraform
burkedavison Oct 19, 2022
fb0b704
Merge branch 'terraform' into terraform-and-kokoro
burkedavison Oct 19, 2022
3f9b482
fix: Revert ITHeadersTest changes to same as main
burkedavison Oct 19, 2022
ccdc776
Merge branch 'terraform' into terraform-and-kokoro
burkedavison Oct 19, 2022
5ac7c07
fix: Remove unused import
burkedavison Oct 19, 2022
f5fa481
Merge branch 'terraform' into terraform-and-kokoro
burkedavison Oct 19, 2022
a6dfc72
fix: Apply mvn com.coveo:fmt-maven-plugin:format
burkedavison Oct 19, 2022
09abe52
ci: Terraform now used in Kokoro integration tests
burkedavison Oct 24, 2022
23c4a91
ci: Temporarily disable Terraform in integration tests
burkedavison Oct 24, 2022
9106537
Merge branch 'main' into terraform
burkedavison Oct 25, 2022
b641cb2
ci: Echo shell commands to console for debugging.
burkedavison Oct 25, 2022
a2263a0
fix: Remove random suffix from Dialogflow Agent label, as only one ag…
burkedavison Oct 25, 2022
9916565
Merge branch 'terraform' into terraform-and-kokoro
burkedavison Oct 25, 2022
8a00678
Merge branch 'main' into terraform-and-kokoro
burkedavison Oct 26, 2022
6b3eebd
chore: Convert Terraform configs from using project-services module t…
burkedavison Oct 26, 2022
1ceaed8
fix: Correct input variable syntax
burkedavison Oct 26, 2022
20480e9
fix: Unset IFS prior to command invocation
burkedavison Oct 26, 2022
48063da
chore: Disable -x flag to reduce scripting noise.
burkedavison Oct 26, 2022
630f51e
fix: Use 'trap' to ensure Terraform destroys provisioned resources on…
burkedavison Oct 26, 2022
76d4172
Merge branch 'main' into terraform-and-kokoro
burkedavison Oct 27, 2022
37ba287
chore: Disable predestroy scripts for java-container and java-redis t…
burkedavison Oct 27, 2022
6535e08
chore: Measure Terraform setup and teardown time
burkedavison Oct 27, 2022
a7862d4
Revert "chore: Disable predestroy scripts for java-container and java…
burkedavison Oct 27, 2022
d8133d5
Merge branch 'main' into terraform-and-kokoro
burkedavison Oct 28, 2022
65834bd
chore: mvn install all modules
lqiu96 Oct 28, 2022
13499df
Merge remote-tracking branch 'origin/main-fix_ITs' into terraform-and…
burkedavison Oct 28, 2022
bc5de69
fix: Don't suppress exception in ITKmsTest
burkedavison Oct 28, 2022
9a6952c
fix: Disable random suffix on Redix and Container integration test ne…
burkedavison Oct 31, 2022
9d759ba
fix: Add variable to optionally create networks for ITs. Default is f…
burkedavison Oct 31, 2022
a96f763
fix: Automatically create new network or use existing network for red…
burkedavison Nov 1, 2022
8a871b4
chore: Temporary log output for Kokoro debugging.
burkedavison Nov 1, 2022
d336e21
fix: Ensure 'destroy' occurs if failure during 'apply'.
burkedavison Nov 1, 2022
2c2ae1d
fix: Set gcloud project in Kokoro before performing init.
burkedavison Nov 1, 2022
4d064a2
fix: Revert destroy invocation change
burkedavison Nov 1, 2022
30457b7
fix: Add java-redis preplan.sh
burkedavison Nov 1, 2022
c522d0a
chore: @Ignore java-notification integration test
burkedavison Nov 1, 2022
fc9f0d4
Merge branch 'main' into terraform-and-kokoro
burkedavison Nov 1, 2022
1d05349
fix: Run formatter
burkedavison Nov 1, 2022
9efffae
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Nov 1, 2022
86e6dda
fix: Only forget resource if Terraform knows about it.
burkedavison Nov 2, 2022
a5b7744
Merge remote-tracking branch 'origin/terraform-and-kokoro' into terra…
burkedavison Nov 2, 2022
7b21318
fix: Retry on empty landmark result from Vision API
burkedavison Nov 2, 2022
216b165
fix: Lint formatting
burkedavison Nov 2, 2022
68b75b1
Merge branch 'main' into terraform-and-kokoro
burkedavison Nov 2, 2022
1640f3b
chore: Simplify java-container.ITSystemTest.getContainerNetworkName()
burkedavison Nov 3, 2022
a283bb8
fix: Shell script case-statement indentation
burkedavison Nov 3, 2022
210e931
fix: Don't let Terraform destroy the Dialogflow Agent.
burkedavison Nov 3, 2022
6bf2c34
fix: Don't let Terraform unassign permissions from default service ag…
burkedavison Nov 3, 2022
0abaeba
fix: Re-enable notification system test with lesser permissions on pu…
burkedavison Nov 3, 2022
ade1d92
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Nov 3, 2022
0943d9b
fix: Disable notification system test
burkedavison Nov 3, 2022
22f8009
Merge remote-tracking branch 'origin/terraform-and-kokoro' into terra…
burkedavison Nov 3, 2022
f6c3315
ci: Add terraform-integration Kokoro build config. Maintain gcloud-de…
burkedavison Nov 3, 2022
2be9a24
fix: Rename .terraform folders to .cloud
burkedavison Nov 4, 2022
e105238
chore: Add .cloud/README.md
burkedavison Nov 4, 2022
991f49e
fix: Correct name of run_integration_tests
burkedavison Nov 4, 2022
0675faf
fix: Additional .terraform rename locations.
burkedavison Nov 4, 2022
b651fc2
fix: Add .terraform.lock.hcl file
burkedavison Nov 7, 2022
94923fd
Merge branch 'main' into terraform-and-kokoro
burkedavison Nov 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .cloud/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
generated.tfplan
generated.tfplan.json
generated.auto.tfvars
generated-env.sh
generated-main.tf
generated-outputs.tf
generated-variables.tf
40 changes: 40 additions & 0 deletions .cloud/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 149 additions & 0 deletions .cloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Introduction

When you experience integration test failures, they may be due to concurrent tests accessing the
same GCP project resources, required resources not existing on the GCP project, quota limits being
exceeded due to overreliance on a single project, incorrect permissions, disabled APIs, and/or
service account impersonation requirements.

[Terraform](https://www.terraform.io/) is an "Infrastructure as Code" (IaC) tool that allows cloud
and on-prem resources to be defined in configuration files to be versioned, reused, and shared. By
using Terraform to provision our GCP projects prior to running integration tests we:

* Ensure infrastructure provisioning consistency,
* Prevent conflicts between concurrent integration test invocations using the same GCP project by
randomizing resource names,
* Provide a managed lifecycle including the ability to destroy short-lived testing environments,
* Version the infrastructure requirements with the test source.

# Running `google-cloud-java` Integration Tests

When you need to troubleshoot integration tests in the google-cloud-java repository, follow one of
the supported workflows below to provision a GCP project with the required configuration.

## Installation

1. Download and install the latest version of Terraform: https://www.terraform.io/downloads
2. *(Optional)* Update your default shell environment variables. (For Mac users,
modify `~/.bash_profile`.
For Linux users, modify `~/.bashrc`.)
* See
also: [About Cloud Billing accounts](https://cloud.google.com/billing/docs/how-to/manage-billing-account)
* See
also: [Creating and managing Folders](https://cloud.google.com/resource-manager/docs/creating-managing-folders)

```shell
export GOOGLE_CLOUD_FOLDER_ID="000000000000"
export GOOGLE_CLOUD_BILLING_ACCOUNT="000000-000000-000000"
export GOOGLE_CLOUD_PROJECT_PREFIX="my-project"
```

## Supported Workflows

During these workflows, you may be asked to authenticate one or two times with gcloud as it
optionally creates a GCP project, and then sets up the
[application-default credentials](https://cloud.google.com/docs/authentication/application-default-credentials)
to be used by Terraform and the client libraries.

A service account will be created, assigned `roles/owner`[^1], and used for all client library
integration tests. Your user credentials must have permission in the designated GCP project to
self-assign `roles/iam.serviceAccountTokenCreator` to impersonate the service account.

[^1]: Basic roles like `roles/owner` are
[not recommended in production environments](https://cloud.google.com/iam/docs/understanding-roles#basic)
.

### Workflow: Set up a GCP Project for **One or More** Client Libraries, then Test

1. Invoke `setup.sh` with the directory names of the selected client libraries:
```shell
$ ./.cloud/setup.sh [library][,library ...]
```

Examples:
* `./.cloud/setup.sh java-accessapproval`
* `./.cloud/setup.sh java-asset,java-compute,java-container`

2. When ready to begin testing, invoke `verify.sh`:
```shell
$ ./.cloud/verify.sh [library][,library ...]
```

Examples:
* `./.cloud/verify.sh java-accessapproval`
* `./.cloud/verify.sh java-asset,java-compute,java-container`

Note: Invoking `verify.sh` without arguments will begin performing integration testing on all client
libraries. In this workflow, the GCP project will only have been configured the project for a subset
of client libraries, this command is unlikely to succeed unless the GCP project has been previously
resourced and configured manually. If you wish to perform integration testing on all client
libraries, use the below workflow to first provision the project for all client libraries.

### Workflow: Set up a GCP Project for **All** Client Libraries, then Test

1. Invoke `setup.sh` without arguments:
```shell
$ ./.cloud/setup.sh
```
Every client library with a `.cloud` subdirectory will be included in the generated Terraform
root module, and the GCP project will be prepared to test all client libraries.
2. When ready to begin testing, invoke `verify.sh` with or without arguments:
```shell
$ ./.cloud/verify.sh
```
The above will perform integration tests on all client libraries.
```shell
$ ./.cloud/verify.sh [library][,library ...]
```
The above will only perform integration testing on the specified client libraries.

Examples:
* `./.cloud/verify.sh`
* `./.cloud/verify.sh java-accessapproval`
* `./.cloud/verify.sh java-asset,java-compute,java-container`

### Workflow: Set up and Test with One Command

Use `test.sh` to perform a combination of both `setup.sh` and `verify.sh`.

```shell
$ ./.cloud/test.sh [library][,library ...]
```

Examples:

* `./.cloud/test.sh`
* `./.cloud/test.sh java-accessapproval`
* `./.cloud/test.sh java-asset,java-compute,java-container`

### Workflow: Clean up GCP Project of Test Resources

To clean-up (or “destroy”) all provisioned resources under Terraform’s management,
invoke `cleanup.sh`:

```shell
$ ./.cloud/cleanup.sh
```

### Workflow: Add / Remove Client Libraries in an Existing Test Configuration

Terraform will automatically manage the resources for a GCP project when regenerating the root
module to include different client libraries. No additional commands are necessary to support this
use-case.

Example:

```shell
$ ./.cloud/setup.sh java-bigqueryconnection
# Terraform provisions the project for java-bigqueryconnection's requirements.

$ ./.cloud/setup.sh java-accessapproval
# Terraform simultaneously destroys java-bigqueryconnection's resources that
# are no longer being used, and provisions the project for java-accessapproval's
# requirements.
# Warning: This 'implicit removal' is prone to errors. If an error occurs, an
# explicit invocation of 'cleanup.sh' may be required.

$ ./.cloud/setup.sh java-accessapproval,java-texttospeech
# Terraform maintains the java-accessapproval resources while also provisioning
# the project for java-texttospeech's requirements.
```
31 changes: 31 additions & 0 deletions .cloud/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -eo pipefail

scriptDir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
pushd "$scriptDir" >/dev/null

# Ensure GCP project environment variables are initialized.
if [[ $(terraform state list) == "" ]]; then
echo "Nothing to destroy."
exit
fi

source ./helpers/gcloud-sync-env.sh
source ./helpers/destroy.sh
source ./helpers/gcloud-delete-project.sh

popd >/dev/null
21 changes: 21 additions & 0 deletions .cloud/helpers/apply.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -eo pipefail

helperDir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
pushd "$helperDir/.." >/dev/null || exit
terraform apply "generated.tfplan" || exit
popd >/dev/null || exit
71 changes: 71 additions & 0 deletions .cloud/helpers/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -eo pipefail

# Find all directories starting with 'java-', sort them, then join
# with ',' as the delimiter.
function listAllModules() {
# Ensure current directory is repo root.
helperDir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
pushd "$helperDir/../.." >/dev/null

ls -1 -d java-* | sort | paste -s -d, -

popd >/dev/null
}

# Replaces '-' with '_' to get a Terraform output-friendly label
function getFriendlyOutputName() {
echo "$1" | tr '-' _
}

# Get the output object in JSON format for the given module.
function getOutput() {
friendlyName=$(getFriendlyOutputName "$1")
terraform output -json "$friendlyName"
}

# Parse stdin and get the value associated with the given key.
function parseJson() {
python3 -c "import sys, json; print(json.load(sys.stdin)['$1'])"
}

# Example use: getModuleOutput java-redis redis_network
function getModuleOutput() {
getOutput "$1" | parseJson "$2"
}

# @returns exit code 0 if list $1 contains entry $2.
function contains() {
echo "$1" | grep -w -q "$2"
}

# @returns a "new line"-delimited list of active terraform modules
function getActiveTerraformModules() {
terraform state list | awk -F'[/.]' '{print $2}' | uniq
}

function getTerraformServiceAccountName() {
echo "terraform-service-account"
}

function getTerraformServiceAccountEmail() {
if [ -z "${GOOGLE_CLOUD_PROJECT}" ]; then
echo "GOOGLE_CLOUD_PROJECT must be defined."
exit 1
fi
echo "$(getTerraformServiceAccountName)@$GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com"
}
39 changes: 39 additions & 0 deletions .cloud/helpers/destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -eo pipefail

helperDir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
pushd "$helperDir/.." >/dev/null

# Execute 'predestroy.sh' scripts for any active modules
source ./helpers/common.sh
allModules=$(listAllModules)
activeModules=$(getActiveTerraformModules)
IFS=','
for module in $allModules; do
friendlyName=$(getFriendlyOutputName "$module")
if ! contains "$activeModules" "$friendlyName"; then
continue # Skip unless active.
fi

if [[ -f "../$module/.cloud/predestroy.sh" ]]; then
# shellcheck disable=SC1090
source "../$module/.cloud/predestroy.sh"
fi
done

terraform destroy -auto-approve
popd >/dev/null
Loading