Commit 64d0c291 authored by BH18294's avatar BH18294

Added Trigger Eamil Cloud function

parent 4721e0ef
Component Working:
Configure Send Grid to create API key for the validation from Cloud function to Send Grid
Configure Sender email in Send Grid and validate it to use it in the Cloud Function
Create Pub/Sub Topic in GCP
Create Cloud Function and configure the trigger to Cloud Pub/Sub to get it triggered every time when a message publishes to previously created Pub/Sub topic.
Choose Runtime Python 3.10 in Cloud Function
Add SendGrid as a requirement package in the Cloud Function Code
Add Python Code in the main.py which contains the code to send email
Deploy the code in the cloud function
Now whenever the message is published on the topic from App Script the Cloud function will execute and as per the Environment (QA,DEV,PERF,PROD) email will be received by the relevant approver.
From the same email the approver can go to the Artifact approval form to get it approved.
Pre-Requisites:
Enable SendGrid Email API from Google Marketplace
Service Account needs following permissions
roles/pubsub.publisher
roles/pubsub.subscriber
roles/cloudfunctions.developer
roles/logging.viewer
Deployment steps:
Create an API key:
Sign in to SendGrid and go to Settings > API Keys.
Create an API key.
Select the permissions for the key. At a minimum, the key must have Mail send permissions to send email.
Click Save to create the key.
SendGrid generates a new key. This is the only copy of the key, so make sure that you copy the key and save it for later.
- Setting up a Pub/Sub topic:
Specify your Google Cloud project in an environment variable.
export PROJECT_ID=PROJECT_ID
Specify your Google Cloud organization in an environment variable.
export ORG_ID=ORG_ID
Set the project ID for gcloud commands.
gcloud config set project PROJECT_ID
Create the Pub/Sub topic where notifications are published.
gcloud pubsub topics create trigger-email
Specify the topic in an environment variable.
export TOPIC=projects/$PROJECT_ID/topics/trigger-email
Create the subscription that notifies Cloud Functions to send an email or chat message when messages are published to the topic.
gcloud pubsub subscriptions create gcf-trigger-email-asia-east2-trigger-email-sub --topic trigger-email
- Create SendGrid Email API account
In this section, you create a SendGrid Email API account and obtain an API key. If you already have SendGrid enabled, skip to Obtain a Sendgrid Email API Key and ensure your existing API key has adequate permissions. Go to the Google Cloud console
In the search box at the top of the page, search for SendGrid Email API. SendGrid Search
On the next page, select the plan that fits your needs.
Review the terms and if you're comfortable, click Subscribe.
Activate the SendGrid service by clicking Register with SendGrid.
On the registration screen, enter a username, password, and email address. Accept the terms of service and click Continue.
At the confirmation dialog, click Return to Google.
- Obtain a SendGrid Email API key
Click Manage API keys on SendGrid website. A new tab opens for the SendGrid website. Manage API keys
Complete the form or sign in, if prompted. Then, click Get Started!
In the menu panel, expand Settings and click API Keys.
On the next screen, click the Create API Key button.
Under API Key Name, enter "Email Notifications," select Full Access, and then click the Create & View button.[Text Wrapping Break][Text Wrapping Break]API key name
You are shown the API key. Record the value. You need it in the next section.
Click Done. You are shown the current set of API keys. Close the tab and return to the Google Cloud console.
Next, you deploy a Cloud Function to send notifications to an email address.
- Create the SendGrid Cloud Function
In this section, you deploy a function that sends notifications to your email account.
Ensure that you're using the same PROJECT_ID you used to create the Pub/Sub topic.
Click Create Function. SendGrid function
Set Function name to send-high-and-critical-finding-email-notification and the Trigger type to Pub/Sub.
Select the Pub/Sub topic that you created in Setting up a Pub/Sub topic.
Click Save, and then click Next.
On the next page, set Runtime to Python 3.10.
In the file list, click requirements.txt and add the following to the text field: sendgrid.[Text Wrapping Break][Text Wrapping Break]SendGrid requirements
Click main.py and replace the contents with your code snippet.
Navigate to the Entry point field and enter the name of the function in the code snippet (send_email_notification, in this example).
Click Deploy. You are returned to the Cloud Functions list where you should see your new function. When a green check mark appears next to the function name, it has been successfully deployed. The process could take a few minutes.
\ No newline at end of file
This diff is collapsed.
import base64
import json
from google.cloud import firestore
def hello_pubsub(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
print(pubsub_message)
message_json = json.loads(pubsub_message)
Timestamp = message_json['timestamp']
IntendID = message_json['intendID']
ProjectName = message_json['projectName']
ArtifactName = message_json['artifactName']
ArtifactVersion = message_json['artifactVersion']
Environment = message_json['environment']
RequesterEmail = message_json['requesterEmail']
requestartifact = {
'timestamp' : Timestamp,
'intendID' : IntendID,
'projectName' : ProjectName,
'artifactName' : ArtifactName,
'artifactVersion' : ArtifactVersion,
'environment' : Environment,
'requesterEmail' : RequesterEmail
}
db = firestore.Client()
update_time, request_artifact = db.collection(u'requestArtifactDeployment').add(requestartifact)
print(f'Added document with id {request_artifact.id}')
google-cloud-core
google-cloud-firestore
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment