This is not an officially supported Google product.
This repository provides examples of how a Google Cloud user can forward notifications to third-party integrations not officially supported as notification options. The two provided examples forward notifications to a Philips Hue smart bulb and a Jira server. Each example accomplishes this through the use of a Flask server running on Cloud Run which recieves monitoring notifications through Cloud Pub/Sub push messages and then parses and delivers them to a third party service.
The sample code in this repository is referenced in this Cloud Community tutorial.
.
├── .github/workflows
├── docs
├── environments # Terraform configurations for each environment
│ ├── prod
│ └── dev
├── jira_integration_example # Jira Integration
│ ├── utilities
│ ├── tests # Unit and integration tests
│ ...
├── modules # Terraform modules
├── philips_hue_integration_example # Philips Hue Integration
│ ├── utilities
│ ├── tests # Unit tests
│ ...
├── scripts # Scripts for testing and authentication
.
.
.
└── cloudbuild.yaml # Build configuration file
-
Create a new Google Cloud Platform project from the Cloud Console or use an existing one.
-
Click the "Open in Cloud Shell" button below to clone and open this repository on Cloud Shell.
- Set the Cloud Platform project in Cloud Shell. Replace
[PROJECT_ID]
with your Cloud Platform project id:
gcloud config set project [PROJECT_ID]
- (Optional) In order to successfully run unit tests and linter in the section below, setup a virtualenv and install the required dependencies:
virtualenv env
source env/bin/activate
pip3 install -r philips_hue_integration_example/requirements.txt
pip3 install -r jira_integration_example/requirements.txt
pip3 install -r scripts/requirements.txt
To deploy either the Philips Hue integration or Jira integration for the first time manually, complete the following steps. Make sure to first complete the integration specific deployment steps (part 1), then complete the deployment steps for all integrations (part 2). However if you are redeploying, complete the steps in the redeploying section.
- Store your Philips Hue bridge IP address as
philips_ip
and username asphilips_username
in Secret Manager. - Checkout the desired GitHub environment branch (
dev
orprod
). - Edit the
cloudbuild.yaml
configuration file to build a Philips Hue Docker image. Make sure the following line is set in thebuild docker image
step:
args: ['build', '--build-arg', 'PROJECT_ID=$PROJECT_ID', '--tag', 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}', './philips_hue_integration_example']
- Store your Jira Server URL as
jira_url
and Jira project asjira_project
in Secret Manager. - Setup Jira OAuth to be used to authenticate the Jira client in the Cloud Run service. Replace
[JIRA_URL]
with your Jira Server URL:
python3 jira_oauth_setup_script.py --gcp_project_id=$PROJECT_ID [JIRA_URL]
(Note, this script prompts you to complete some steps manually)
- Checkout the desired GitHub environment branch (
dev
orprod
). - Edit the
cloudbuild.yaml
configuration file to build a Jira Docker image. Make sure the following line is set in thebuild docker image
step:
args: ['build', '--build-arg', 'PROJECT_ID=$PROJECT_ID', '--tag', 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}', './jira_hue_integration_example']
- Create Cloud Storage bucket:
PROJECT_ID=$(gcloud config get-value project)
gsutil mb gs://${PROJECT_ID}-tfstate
- You may optionally enable Object Versioning to keep the history of your deployments:
gsutil versioning set on gs://${PROJECT_ID}-tfstate
- Retrieve the email for your project's Cloud Build service account:
CLOUDBUILD_SA="$(gcloud projects describe $PROJECT_ID --format 'value(projectNumber)')@cloudbuild.gserviceaccount.com"
- Grant the required access to your Cloud Build service account:
gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$CLOUDBUILD_SA --role roles/iam.securityAdmin
gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$CLOUDBUILD_SA --role roles/run.admin
gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$CLOUDBUILD_SA --role roles/storage.admin
gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$CLOUDBUILD_SA --role roles/editor
- To allow the Cloud Run service to access secrets in Secret Manager, grant the Compute Engine default service account the Secret Manager Secret Accessor role. Replace
[PROJECT_NUMBER]
with the Cloud project number:
gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:[PROJECT_NUMBER][email protected] --role roles/secretmanager.secretAccessor
- Trigger a build and deploy to Cloud Run. Replace
[BRANCH]
with the current environment branch:
cd ~/cloud-monitoring-notification-delivery-integration-sample-code
gcloud builds submit . --config cloudbuild.yaml --substitutions BRANCH_NAME=[BRANCH]
Note that this step uses Terraform to automatically create necessary resources in the Google Cloud Platform project. For more info on what resources are created and managed, refer to the Terraform section below.
- Create a Pub/Sub notification channel that uses the topic
tf-topic
(which was created by Terraform in the previous step). - Add the Pub/Sub channel to an alerting policy by selecting Pub/Sub as the channel type and the channel created in the prior step as the notification channel.
- Congratulations! Your service is now successfully deployed to Cloud Run and alerts will be forwarded to either the Philips Hue light bulb or Jira server.
If you've already deployed once manually and want to build and redeploy a new version, do the following:
-
Checkout the desired GitHub environment branch (dev or prod).
-
Trigger a build and deploy to Cloud Run. Replace
[BRANCH]
with the current environment branch:
cd ~/cloud-monitoring-notification-delivery-integration-sample-code
gcloud builds submit . --config cloudbuild.yaml --substitutions BRANCH_NAME=[BRANCH]
Refer to this solutions guide for instructions on how to setup continuous deployment: TBD
In order to successfully run these tests, make sure you have successfully setup virtualenv and installed the required dependencies as specified in the "Setup" section above.
To run unit tests for Philips Hue and Jira integrations:
bash ./scripts/run_tests.sh
To lint project source code with pylint:
bash ./scripts/run_linter.sh
Terraform is a HashiCorp open source tool that enables you to predictably create, change, and improve your cloud infrastructure by using code. In this project, Terraform is used to automatically create and manage necessary resources in Google Cloud Platform.
Terraform will create the following resources in your cloud project:
- A Cloud Run service called
cloud-run-pubsub-service
to deploy the Flask application - A Pub/Sub topic called
tf-topic
- A Pub/Sub push subscription called
alert-push-subscription
with a push endpoint tocloud-run-pubsub-service
- A service account with ID
cloud-run-pubsub-invoker
to represent the Pub/Sub subscription identity
In addition, Terraform configures the following authentication policies:
- Enabling Pub/Sub to create authentication tokens in your gcloud project
- Giving the
cloud-run-pubsub-invoker
service account permission to invokecloud-run-pubsub-service
- Adding authentication for
alert-push-subscription
using thecloud-run-pubsub-invoker
service account
These configurations will be applied automatically on source code changes after connecting Cloud Build with GitHub and when deploying manually.
Deployment with Terraform will be automated through source code changes in GitHub. To manually see and apply the changes Terraform makes to your Cloud project resources, do the following:
Navigate to the desired environment folder (environments/dev
or environments/prod
) and run the following:
Initialize a working directory containing Terraform configuration files:
terraform init -backend-config "bucket=$PROJECT_ID-tfstate"
Refresh the current Terraform state:
terraform refresh -var="project=$PROJECT_ID"
To see what changes will be made without applying them yet:
terraform plan -var="project=$PROJECT_ID"
Apply configuration changes:
terraform apply -var="project=$PROJECT_ID"
When prompted, type yes
to confirm changes. Once finished, information about the created resources should appear in the output.
See also the list of contributors who participated in this project.
Every file containing source code must include copyright and license information. This includes any JS/CSS files that you might be serving out to browsers. (This is to help well-intentioned people avoid accidental copying that doesn't comply with the license.)
Apache header:
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
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.