0

Create Service

by
Published 4 days ago

Create a service (a system or application that generates incidents) attached to an escalation policy. Set auto_resolve_timeout to 0 to disable auto-resolve and acknowledgement_timeout to 0 to disable re-triggering after acknowledgment.

Script pagerduty Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
/**
4
 * Create Service
5
 * Create a service (a system or application that generates incidents) attached to an escalation policy. Set auto_resolve_timeout to 0 to disable auto-resolve and acknowledgement_timeout to 0 to disable re-triggering after acknowledgment.
6
 */
7
export async function main(
8
  auth: RT.Pagerduty,
9
  name: string,
10
  escalation_policy_id: string,
11
  description: string | undefined,
12
  auto_resolve_timeout: number | undefined,
13
  acknowledgement_timeout: number | undefined,
14
) {
15
  const service: { [key: string]: any } = {
16
    type: "service",
17
    name,
18
    escalation_policy: {
19
      id: escalation_policy_id,
20
      type: "escalation_policy_reference",
21
    },
22
  }
23
  if (description !== undefined && description !== "")
24
    service.description = description
25
  if (auto_resolve_timeout !== undefined)
26
    service.auto_resolve_timeout = auto_resolve_timeout
27
  if (acknowledgement_timeout !== undefined)
28
    service.acknowledgement_timeout = acknowledgement_timeout
29

30
  const response = await fetch("https://api.pagerduty.com/services", {
31
    method: "POST",
32
    headers: {
33
      Authorization: `Token token=${auth.token}`,
34
      "Content-Type": "application/json",
35
      Accept: "application/vnd.pagerduty+json;version=2",
36
    },
37
    body: JSON.stringify({ service }),
38
  })
39

40
  if (!response.ok) {
41
    throw new Error(`${response.status} ${await response.text()}`)
42
  }
43

44
  return await response.json()
45
}
46