0

When through the documentation of pagerduty was but still not able to understand what parameters to send in the request body and also facing trouble in understanding how to make the api request.If any one can share the sample code on making a pagerduty schedule that would help me alot.

2 Answers2

0

Below is the sample code to create schedules in PagerDuty.

Each list can have multiple items (to add more users / layers)

import requests


url = "https://api.pagerduty.com/schedules?overflow=false"

payload={
    "schedule": {
        "schedule_layers": [
            {
                "start": "<dateTime>", # Start Time of layer |  "start": "2021-01-01T00:00:00+05:30", 
                "users": [
                    {
                        "user": {
                            "id": "<string>",        # ID of user to add in layer  
                            "summary": "<string>",   
                            "type": "<string>",      # "type": "user"
                            "self": "<url>",
                            "html_url": "<url>"
                        }
                    }
                ],
                "rotation_virtual_start": "<dateTime>",   # Start of layer  | "rotation_virtual_start": "2021-01-01T00:00:00+05:30",
                "rotation_turn_length_seconds": "<integer>", # Layer rotation, for multiple user switching | "rotation_turn_length_seconds": <seconds>,
                "id": "<string>",      # Auto-generated. Only needed if you want update and existing Schedule Layer
                "end": "<dateTime>",   # End Time of layer |  "end": "2021-01-01T00:00:00+05:30",
                "restrictions": [
                    {
                        "type": "<string>", # To restrict shift to certain timings Weekly daily etc | "type": "daily_restriction",
                        "duration_seconds": "<integer>", # Duration of layer | "duration_seconds": "300"
                        "start_time_of_day": "<partial-time>", #Start time of layer | "start_time_of_day": "00:00:00",
                        "start_day_of_week": "<integer>" 
                    }
                ],
                "name": "<string>", # Name to give Layer 
            }
        ]
        "time_zone": "<activesupport-time-zone>",  # Timezone to set for layer and its timings | "time_zone": "Asia/Kolkata",
        "type": "schedule",
        "name": "<string>", # Name to give Schedule 
        "description": "<string>",# Description to give Schedule 
        "id": "<string>",      # Auto-generated. Only needed if you want update and existing Schedule Layer
    }
}

headers = {
  'Authorization': 'Token token=<Your token here>',
  'Accept': 'application/vnd.pagerduty+json;version=2',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, json=payload)

print(response.text)

Best way to do this is to get the postman collection for PagerDuty and edit the request as per your liking. Once you get a successful response, convert that into code using the inbuilt feature of postman.

0

Using PagerDuty API for scheduling is not easy. Creating new schedule is okaish, but if you decide to update schedule - it is definitely not trivial. You'll probably occur bunch of limitation: number of restriction per layer, must reuse current layers, etc.

As option you can use a python library pdscheduling https://github.com/skrypka/pdscheduling

Roman
  • 1