Opsgenie Python Incident API
Incidents
To use the Incident functionalities of Opsgenie's API via the Python SDK you will first have to import the SDK library and (configure)(doc:opsgenie-python-api-v2-1#section-client-initialization) it with the API Key that you procured from your Opsgenie Integrations. For additional configurations, you may refer to the Python SDK Configurations page present in the Opsgenie documentation.
# Importing relevant Opsgenie SDK libraries including the Alert API client
import opsgenie_sdk
class Example:
def __init__(self, opsgenie_api_key):
self.conf = self.conf = opsgenie_sdk.configuration.Configuration()
self.conf.api_key['Authorization'] = '<Your-API-Key>'
self.api_client = opsgenie_sdk.api_client.ApiClient(configuration=self.conf)
self.incident_api = opsgenie_sdk.IncidentApi(api_client=self.api_client)
Get Request Status
Incident creation, deletion, and action requests are processed asynchronously to provide higher availability and scalability, therefore valid requests for those endpoints are responded to with HTTP status 202 - Accepted. The Get Request Status endpoint is used to track the status and incident details (if any) of the request whose identifier is given.
You can get the request status using the Incident client that is initialized prior to performing any Opsgenie API functionality.
def get_request_status(self):
try:
get_response = self.alert_api.get_request_status(request_id=self._request_id)
print(get_response)
return get_response
except ApiException as err:
print("Exception when calling AlertApi->get request status: %s\n" % err)
Create Incident
You can create a new incident using the Incident client that is initialized prior to performing any Opsgenie API functionality.
In creating the new incident, please ensure you pass the required and right values in as a body object to the create_incident
function via the create_incident_payload
parameter. To know which values are required and can be used, please visit the Create Incident section in the Opsgenie REST API documentation here.
def create_incident(self):
body = opsgenie_client.CreateIncidentPayload(
message="Example Incident",
description="Creating example incident",
service_id=self.serviceId,
priority='P5')
try:
create_response = self.incident_api.create_incident(create_incident_payload=body)
self._request_id = self.incident_api.api_client.last_response.getheader('X-Request-ID')
return create_response
except ApiException as err:
print("Exception when calling AlertApi->create: %s\n" % err)
Get Incident
You can get an incident using the Incident client that is initialized prior to performing any Opsgenie API functionality.
To get an incident, please ensure you pass the required identifier, the incident id, to the get_incident
function. To know which identifiers are required and can be used, please visit the Get Incident section in the Opsgenie REST API documentation here.
def get_incident(self, incident_id):
try:
get_response = self.incident_api.get_incident(incident_id)
print(get_response)
except ApiException as err:
print("Exception when calling AlertApi->create: %s\n" % err)
Delete Incident
You can delete an incident using the Incident client that is initialized prior to performing any Opsgenie API functionality.
To delete an incident, please ensure you pass the required identifier, the incident id, to the delete_incident
function. To know which identifiers are required and can be used, please visit the Delete Incident section in the Opsgenie REST API documentation here.
def delete_incident(self, incident_id):
try:
delete_response = self.incident_api.delete_incident(incident_id)
print(delete_response)
except ApiException as err:
print("Exception when calling AlertApi->create: %s\n" % err)
List Incidents
You can list your incidents using the Incident client that is initialized prior to performing any Opsgenie API functionality.
To list incidents, please ensure you pass the required query and right parameters to the list_incidents
function. To know which query values and parameters are required, please visit the List Incidents section in the Opsgenie REST API documentation here.
def list_incidents(self):
query = 'status=open'
try:
list_response = self.incident_api.list_incidents(query, limit=5)
print(list_response)
except ApiException as err:
print("Exception when calling AlertApi->create: %s\n" % err)
Close Incident
You can close incidents using the Incident client that is initialized prior to performing any Opsgenie API functionality.
To close an incident, please ensure you pass the required identifier, the incident id, to the close_incident
function via the close_incident_payload
parameter. You may also pass in a note
value. To know exactly how to do so, please visit the Close Incident section in the Opsgenie REST API documentation here.
def close_incident(self, incident_id):
body = opsgenie_client.CreateIncidentPayload(
note="Close example incident")
try:
close_response = self.incident_api.close_incident(incident_id, close_incident_payload=body)
print(close_response)
except ApiException as err:
print("Exception when calling AlertApi->create: %s\n" % err)
Updated over 3 years ago