-
Notifications
You must be signed in to change notification settings - Fork 30
Add module to wait for data fusion instance to become healthy #31
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
Merged
morgante
merged 4 commits into
terraform-google-modules:master
from
umairidris:wait_healthy
Apr 18, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Data Fusion Wait Healthy | ||
|
||
This module provides a way to wait for a Data Fusion Instance to become healthy. | ||
|
||
Data Fusion Instances currently load their services in an asynchronous | ||
manner. Thus, if one tries to create a pipeline or resource immediately after | ||
the Data Fusion Instance has been created, they will get an error. | ||
|
||
Rather than waiting for an arbitrary amount of time, this module will ping the | ||
Data Fusion Instance until it has loaded the google-cloud plugin, which usually | ||
indicates a healthy instance. | ||
|
||
NOTE: In the future this module may be removed if Data Fusion allows users to | ||
wait for specific services during instance creation. | ||
|
||
## Usage | ||
|
||
Basic usage of this module is as follows: | ||
|
||
```hcl | ||
data "google_client_config" "current" {} | ||
|
||
resource "google_data_fusion_instance" "instance" { | ||
provider = google-beta | ||
name = "example-instance" | ||
project = "example-project" | ||
region = "us-central1" | ||
type = "BASIC" | ||
} | ||
|
||
module "wait_healthy" { | ||
source = "terraform-google-modules/data-fusion/google//modules/wait_healthy" | ||
version = "~> 0.1" | ||
|
||
service_endpoint = google_data_fusion_instance.instance.service_endpoint | ||
access_token = data.google_client_config.current.access_token | ||
} | ||
|
||
provider "cdap" { | ||
host = "${module.wait_healthy.service_endpoint}/api/" | ||
token = data.google_client_config.current.access_token | ||
} | ||
|
||
resource "cdap_application" "app" { | ||
... | ||
} | ||
``` | ||
|
||
Functional examples are included in the | ||
[examples](./examples/) directory. | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|:----:|:-----:|:-----:| | ||
| access\_token | Token for authenticating requests to the CDF instance. | string | n/a | yes | | ||
| service\_endpoint | Endpoint of the the CDF instance to check for health. | string | n/a | yes | | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| service\_endpoint | The input service_endpoint. Useful for creating a dependency on this module. | | ||
|
||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
|
||
## Requirements | ||
|
||
These sections describe requirements for using this module. | ||
|
||
### Software | ||
|
||
The following dependencies must be available: | ||
|
||
- [Terraform][terraform] v0.12 | ||
- [Terraform Provider for GCP][terraform-provider-gcp] plugin v2.0 | ||
|
||
### Service Account | ||
|
||
A service account with the following roles must be used to provision | ||
the resources of this module: | ||
|
||
- Data Fusion Admin: `roles/datafusion.admin` | ||
|
||
The [Project Factory module][project-factory-module] and the | ||
[IAM module][iam-module] may be used in combination to provision a | ||
service account with the necessary roles applied. | ||
|
||
### APIs | ||
|
||
A project with the following APIs enabled must be used to host the | ||
resources of this module: | ||
|
||
- Google Cloud Data Fusion API: `datafusion.googleapis.com` | ||
|
||
The [Project Factory module][project-factory-module] can be used to | ||
provision a project with the necessary APIs enabled. | ||
|
||
## Contributing | ||
|
||
Refer to the [contribution guidelines](./CONTRIBUTING.md) for | ||
information on contributing to this module. | ||
|
||
[iam-module]: https://registry.terraform.io/modules/terraform-google-modules/iam/google | ||
[project-factory-module]: https://registry.terraform.io/modules/terraform-google-modules/project-factory/google | ||
[terraform-provider-gcp]: https://www.terraform.io/docs/providers/google/index.html | ||
[terraform]: https://www.terraform.io/downloads.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright 2020 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
resource "null_resource" "wait_healthy" { | ||
provisioner "local-exec" { | ||
command = "${path.module}/wait_healthy.sh -e ${var.service_endpoint} -t ${var.access_token}" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright 2020 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
output "service_endpoint" { | ||
description = "The input service_endpoint. Useful for creating a dependency on this module." | ||
value = var.service_endpoint | ||
depends_on = [ | ||
null_resource.wait_healthy, | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright 2020 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
variable "service_endpoint" { | ||
description = "Endpoint of the the CDF instance to check for health." | ||
type = string | ||
} | ||
|
||
variable "access_token" { | ||
description = "Token for authenticating requests to the CDF instance." | ||
type = string | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/bin/bash | ||
# Copyright 2018 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 | ||
# | ||
# http://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 -e | ||
|
||
ENDPOINT="" | ||
ACCESS_TOKEN="" | ||
|
||
while getopts 'e:t:' c | ||
do | ||
case $c in | ||
e) ENDPOINT=${OPTARG} ;; | ||
t) ACCESS_TOKEN=${OPTARG} ;; | ||
*) | ||
echo "Invalid flag ${OPTARG}" | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
if [[ -z ${ENDPOINT} ]]; then | ||
echo "-e must be set" | ||
exit 1 | ||
fi | ||
|
||
if [[ -z ${ACCESS_TOKEN} ]]; then | ||
echo "-t must be set" | ||
exit 1 | ||
fi | ||
|
||
|
||
for ((i=1;i<=50;i++)); do | ||
sleep 10 | ||
resp=$(curl -s -w "\n%{http_code}" -H "Authorization: Bearer ${ACCESS_TOKEN}" "${ENDPOINT}/api/v3/namespaces/default/artifacts") | ||
|
||
code=$(tail -n 1 <<< "${resp}") | ||
body=$(head -n 1 <<< "${resp}") | ||
|
||
if [[ "${code}" == 200 ]]; then | ||
name=$(jq -r '.[] | select(.name=="google-cloud") | .name' <<< "${body}") | ||
if [[ "${name}" == "google-cloud" ]]; then | ||
echo "Artifact google-cloud loaded, instance healthy" | ||
exit 0 | ||
fi | ||
|
||
echo "Artifact google-cloud not loaded (${name}), instance unhealthy, checking again in 10 seconds" | ||
|
||
elif [[ "${code}" =~ ^(400|502|504)$ ]]; then | ||
echo "Instance unhealthy: ${code}, checking again in 10 seconds" | ||
|
||
else | ||
echo "Unexpected code: ${code}" | ||
echo "${body}" | ||
exit 1 | ||
fi | ||
done | ||
|
||
echo "Failed to wait for instance to become healthy in allocated time" | ||
exit 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
license in all files.