From cfde353169c78ea8c9a7443f283d010ff4d1336e Mon Sep 17 00:00:00 2001 From: Amichay Oren Date: Wed, 19 Apr 2023 11:04:56 +0300 Subject: [PATCH] adding TF w/ Slack Notifier --- functions/notifier/__init__.py | 0 functions/notifier/main.py | 26 ++++++++++ functions/notifier/requirements.txt | 1 + main.tf | 73 +++++++++++++++++++++++++++++ tests/test_link.py | 2 +- 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 functions/notifier/__init__.py create mode 100644 functions/notifier/main.py create mode 100644 functions/notifier/requirements.txt create mode 100644 main.tf diff --git a/functions/notifier/__init__.py b/functions/notifier/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/functions/notifier/main.py b/functions/notifier/main.py new file mode 100644 index 0000000..4c1f5c6 --- /dev/null +++ b/functions/notifier/main.py @@ -0,0 +1,26 @@ +import json + +import requests + +SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/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" + + +def slackNotifier(request): + def send_slack_notification(message): + headers = {"Content-type": "application/json"} + payload = {"text": message} + + response = requests.post( + SLACK_WEBHOOK_URL, headers=headers, data=json.dumps(payload) + ) + + if response.status_code == 200: + print("Notification sent successfully") + else: + print("Failed to send notification") + + message = ( + "Slack Message Sent Directly from GCP following Terraform Deployment" + ) + send_slack_notification(message=message) + return message diff --git a/functions/notifier/requirements.txt b/functions/notifier/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/functions/notifier/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..9634f29 --- /dev/null +++ b/main.tf @@ -0,0 +1,73 @@ +provider "google" { + project = var.project_id + region = var.region +} + +terraform { + backend "gcs" { + bucket = "tf-state-bucket" + prefix = "" + } +} + +variable "slack_notification_url" { + default = "https://hooks.slack.com/services/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" +} + +variable "env" { + default = "dev" +} + +variable "project_id" { + default = "development-380917" +} + +variable "region" { + default = "us-east4" +} + +locals { + resource_prefix = var.env == "prod" ? "" : "${var.env}-" +} + + + +# -------------------------- +# -- Function Storage Bucket +# -------------------------- +resource "google_storage_bucket" "serverless_function_bucket" { + name = "${local.resource_prefix}serverless-function-bucket" + location = "US" +} + + + +# -------------------------- +# -- Slack Notifier Function +# -------------------------- +data "archive_file" "slack_notifier" { + type = "zip" + output_path = "/tmp/slack_notifier.zip" + source_dir = "functions/notifier" +} + +resource "google_storage_bucket_object" "slack_notifier_zip" { + name = "${local.resource_prefix}slack-notifier.zip" + bucket = google_storage_bucket.serverless_function_bucket.name + content_type = "application/zip" + source = data.archive_file.slack_notifier.output_path + depends_on = [ + google_storage_bucket.serverless_function_bucket + ] +} + +resource "google_cloudfunctions_function" "slack_notifier" { + name = "${local.resource_prefix}slack-notifier" + description = "Slack Notifier Function" + runtime = "python311" + source_archive_bucket = google_storage_bucket.serverless_function_bucket.name + source_archive_object = google_storage_bucket_object.slack_notifier_zip.name + trigger_http = true + entry_point = "slackNotifier" + available_memory_mb = 128 +} diff --git a/tests/test_link.py b/tests/test_link.py index 16d6f4c..75f42e7 100644 --- a/tests/test_link.py +++ b/tests/test_link.py @@ -1,6 +1,6 @@ import requests -base_url: str = "https://api.nine30.com/link" +base_url: str = "https://api.nine30.com/bank/link" def no_test_link_missing_parameter() -> None: