-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pd: setup more helper methods we'll use
- Loading branch information
Showing
8 changed files
with
225 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package pd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/PagerDuty/go-pagerduty" | ||
) | ||
|
||
type escalationPolicySetup struct { | ||
id string | ||
name string | ||
|
||
userIDs []string | ||
} | ||
|
||
func (c *client) findEscalationPolicy(ctx context.Context, setup escalationPolicySetup) (*pagerduty.EscalationPolicy, error) { | ||
opts := pagerduty.ListEscalationPoliciesOptions{ | ||
Limit: 100, | ||
} | ||
eps, err := c.underlying.ListEscalationPoliciesWithContext(ctx, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("listing escalation policies: %w", err) | ||
} | ||
for _, ep := range eps.EscalationPolicies { | ||
if ep.ID == setup.id { | ||
return &ep, nil | ||
} | ||
if strings.EqualFold(ep.Name, setup.name) { | ||
return &ep, nil | ||
} | ||
} | ||
|
||
// Can't find it so create one | ||
req := pagerduty.EscalationPolicy{ | ||
Name: setup.name, | ||
Description: "managed by deadcheck, DO NOT MODIFY", | ||
} | ||
// Add an escalation rule | ||
for _, userID := range setup.userIDs { | ||
rule := pagerduty.EscalationRule{ | ||
Delay: 1, | ||
Targets: []pagerduty.APIObject{ | ||
{ | ||
Type: "user_reference", | ||
ID: userID, | ||
}, | ||
}, | ||
} | ||
req.EscalationRules = append(req.EscalationRules, rule) | ||
} | ||
|
||
ep, err := c.underlying.CreateEscalationPolicyWithContext(ctx, req) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating escalation policy: %w", err) | ||
} | ||
return ep, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package pd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/PagerDuty/go-pagerduty" | ||
) | ||
|
||
func (c *client) setupInitialIncident(ctx context.Context, service *pagerduty.Service, ep *pagerduty.EscalationPolicy) (*pagerduty.Incident, error) { | ||
req := &pagerduty.CreateIncidentOptions{ | ||
Title: fmt.Sprintf("Creating ongoing incdient for %s", service.Name), | ||
Body: &pagerduty.APIDetails{ | ||
Details: "This incident will be active and used by deadcheck to alert you when check-ins do not occur as expected. Deadcheck will update this incident to reflect the current status of check-in.", | ||
}, | ||
Urgency: "low", | ||
EscalationPolicy: &pagerduty.APIReference{ | ||
ID: ep.ID, | ||
Type: "escalation_policy", | ||
}, | ||
Service: &pagerduty.APIReference{ | ||
ID: service.ID, | ||
Type: "service", | ||
}, | ||
} | ||
inc, err := c.underlying.CreateIncidentWithContext(ctx, c.pdConfig.From, req) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating incident: %w", err) | ||
} | ||
return inc, nil | ||
} | ||
|
||
func (c *client) snoozeIncident(ctx context.Context, inc *pagerduty.Incident, service *pagerduty.Service, snooze time.Duration) error { | ||
// Ack the incident | ||
update := []pagerduty.ManageIncidentsOptions{ | ||
{ | ||
ID: inc.ID, | ||
Status: "acknowledged", | ||
}, | ||
} | ||
_, err := c.underlying.ManageIncidentsWithContext(ctx, c.pdConfig.From, update) | ||
if err != nil { | ||
return fmt.Errorf("incident acknowledged: %w", err) | ||
} | ||
|
||
// Snooze the incident | ||
inc, err = c.underlying.SnoozeIncidentWithContext(ctx, inc.ID, c.pdConfig.From, uint(snooze.Seconds())) | ||
if err != nil { | ||
return fmt.Errorf("snoozing incident: %w", err) | ||
} | ||
|
||
// Update the incident details for humans to read | ||
expectedCheckin := time.Now().In(time.UTC).Add(snooze).Format("2006-01-02 15:04 UTC") | ||
update = []pagerduty.ManageIncidentsOptions{ | ||
{ | ||
ID: inc.ID, | ||
Title: fmt.Sprintf("%s did not check-in, expected check-in at %v", service.Name, expectedCheckin), | ||
Urgency: "high", | ||
}, | ||
} | ||
_, err = c.underlying.ManageIncidentsWithContext(ctx, c.pdConfig.From, update) | ||
if err != nil { | ||
return fmt.Errorf("updating incidnet for after snooze: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *client) resolveIncident(ctx context.Context, inc *pagerduty.Incident) error { | ||
update := []pagerduty.ManageIncidentsOptions{ | ||
{ | ||
ID: inc.ID, | ||
Status: "resolved", | ||
}, | ||
} | ||
_, err := c.underlying.ManageIncidentsWithContext(ctx, c.pdConfig.From, update) | ||
if err != nil { | ||
return fmt.Errorf("resolving incident: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters