Skip to content

Instantly share code, notes, and snippets.

@foamdino
Created November 20, 2018 05:23
Show Gist options
  • Select an option

  • Save foamdino/9d24716101415e81d2f7eefc4dc42fcf to your computer and use it in GitHub Desktop.

Select an option

Save foamdino/9d24716101415e81d2f7eefc4dc42fcf to your computer and use it in GitHub Desktop.
lambstatus api
import requests
import json
def get_api_headers(config):
headers = {
"x-api-key": config.get("apiKey"),
"Content-Type": "application/json"
}
return headers
def get_api_endpoint(config):
return config.get("apiEndPoint")
def resolve_component_id (config, component_name):
headers = get_api_headers(config)
endpoint = get_api_endpoint(config)
r = requests.get('{}/api/v0/components'.format(endpoint), headers=headers)
components = json.loads(r.content)
for component in components:
if component["name"] == component_name:
print ("Component ID for Component {} is {}".format(component_name, component["componentID"]))
return component["componentID"]
return None
def post_to_status_page(config, incident, new_msg):
headers = get_api_headers(config)
endpoint = get_api_endpoint(config)
outboundMsg = {}
outboundMsg["name"] = incident["name"]
outboundMsg["status"] = incident["status"]
outboundMsg["message"] = new_msg
r = requests.post('{}/api/v0/incidents'.format(endpoint), data = json.dumps(outboundMsg), headers=headers)
return json.loads(r.content)
def update_to_status_page(config, incident, new_status, msg):
headers = get_api_headers(config)
endpoint = get_api_endpoint(config)
print ("Updating {}".format(incident))
outboundMsg = {}
outboundMsg["name"] = incident["name"]
outboundMsg["status"] = new_status
outboundMsg["message"] = msg
# PATCH https://<your_api_endpoint>/api/v0/incidents/{incidentid}
r = requests.patch('{}/api/v0/incidents/{}'.format(endpoint, incident["statuspage_id"]),
data = json.dumps(outboundMsg), headers=headers)
return json.loads(r.content)
def update_component_status (config, component,status):
if component is not None:
componentId = resolve_component_id(config, component)
print ("Updating component {} with id {} to status {}".format(component, status, componentId))
headers = get_api_headers(config)
endpoint = get_api_endpoint(config)
outboundMsg = {}
outboundMsg["status"] = status
r = requests.patch('{}/api/v0/components/{}'.format(endpoint, componentId),
data = json.dumps(outboundMsg), headers=headers)
return json.loads(r.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment