Skip to content

Instantly share code, notes, and snippets.

@rajathithan
Last active May 18, 2020 16:58
Show Gist options
  • Save rajathithan/7a4d44710101b7e877df978897ca1d7b to your computer and use it in GitHub Desktop.
Save rajathithan/7a4d44710101b7e877df978897ca1d7b to your computer and use it in GitHub Desktop.
router up
# 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