Last active
October 12, 2022 23:41
-
-
Save rajathithan/c71a0e3ac22a90b3bdedab960c422d8e to your computer and use it in GitHub Desktop.
This file contains 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
# Author - Rajathithan Rajasekar | |
import base64 | |
import json | |
from datetime import datetime | |
from google.cloud import storage | |
import requests | |
def process_log_entry(data, context): | |
data_buffer = base64.b64decode(data["data"]) | |
log_entry = json.loads(data_buffer) | |
print(log_entry["textPayload"]) | |
client = storage.Client() | |
bucket = client.get_bucket("xxxxxxx") | |
blob = bucket.get_blob("cloud-router-up.txt") | |
now = datetime.now() | |
dt = now.strftime("%Y-%m-%d %H:%M:%S") | |
print(f"router is up - {dt}") | |
blob.upload_from_string(dt) | |
blob = bucket.get_blob("incident.txt") | |
inc = str(blob.download_as_string()) | |
inc_array = [x.strip() for x in inc.split(',')] | |
inc_api = inc_array[1] | |
inc_api = inc_api[:-1] | |
print(f"Incident api - {inc_api}") | |
blob = bucket.get_blob("write-to-snow.txt") | |
ciwSnow = str(blob.download_as_string()) | |
print(f"servicenow - write - status - {ciwSnow}") | |
if ciwSnow == "b'0'": | |
url = inc_api | |
auth = ("username", "password") | |
cdata = { | |
"assigned_to" : "Googlecloud", | |
"work_notes" : "Testing Google-cloud service - Cloud router is up on " + dt , | |
"close_notes" : "Closed automatically by google cloud ", | |
"state" : "7" | |
} | |
fdata = json.dumps(cdata) | |
headers = {"Content-type" : "application/json", "Accept" : "application/json"} | |
r = requests.put(url, auth = auth, headers = headers, data = fdata) | |
scode = r.status_code | |
print(f"Status Code - {r.status_code}") | |
if scode == 200: | |
rjson = r.json() | |
print(f"Result - {rjson}") | |
blob = bucket.get_blob("write-to-snow.txt") | |
nw = "1" | |
blob.upload_from_string(nw) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment