-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e516b62
commit 3a0a78d
Showing
7 changed files
with
200 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//go:build demo | ||
|
||
package pager | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/firehydrant/signals-migrator/console" | ||
"github.com/gosimple/slug" | ||
) | ||
|
||
func NewPager(ctx context.Context, kind string, apiKey string, appId string) (Pager, error) { | ||
switch strings.ToLower(kind) { | ||
case "pagerduty": | ||
rs := recorderServer(ctx, "PagerDuty") | ||
return NewPagerDutyWithURL(apiKey, rs.URL), nil | ||
case "opsgenie": | ||
rs := recorderServer(ctx, "Opsgenie") | ||
return NewOpsgenieWithURL(apiKey, rs.URL), nil | ||
} | ||
|
||
return nil, fmt.Errorf("%w '%s'", ErrUnknownProvider, kind) | ||
} | ||
|
||
//go:embed testdata/*/apiserver | ||
var testdata embed.FS | ||
|
||
// recorderServer is a hack mostly taken from pager_test.go to serve recorded responses. | ||
// Only use for demo purposes, see build tags. | ||
func recorderServer(ctx context.Context, provider string) *httptest.Server { | ||
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
baseDir := fmt.Sprintf("Test%s", provider) | ||
urlPath := r.URL.Path | ||
if r.URL.RawQuery != "" { | ||
urlPath += "?" + r.URL.RawQuery | ||
} | ||
filename, err := url.JoinPath("testdata", baseDir, "apiserver", slug.Make(urlPath)+".json") | ||
if err != nil { | ||
console.Warnf("[httptest] error joining path for expected response: %s\n", err) | ||
} | ||
data, err := testdata.ReadFile(filename) | ||
if err != nil { | ||
console.Warnf("[httptest] error reading file '%s': %s\n", filename, err.Error()) | ||
http.NotFound(w, r) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(data) | ||
}) | ||
s := httptest.NewServer(h) | ||
go func() { | ||
<-ctx.Done() | ||
s.Close() | ||
}() | ||
return s | ||
} |
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,22 @@ | ||
//go:build !demo | ||
|
||
package pager | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func NewPager(_ context.Context, kind string, apiKey string, appId string) (Pager, error) { | ||
switch strings.ToLower(kind) { | ||
case "pagerduty": | ||
return NewPagerDuty(apiKey), nil | ||
case "victorops": | ||
return NewVictorOps(apiKey, appId), nil | ||
case "opsgenie": | ||
return NewOpsgenie(apiKey), nil | ||
} | ||
|
||
return nil, fmt.Errorf("%w '%s'", ErrUnknownProvider, kind) | ||
} |
103 changes: 103 additions & 0 deletions
103
pager/testdata/TestPagerDuty/apiserver/escalation_policies-include-5b-5d-targets.json
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,103 @@ | ||
{ | ||
"escalation_policies": [ | ||
{ | ||
"id": "P6F7EI2", | ||
"type": "escalation_policy", | ||
"summary": "Endeavour", | ||
"self": "https://api.pagerduty.com/escalation_policies/P6F7EI2", | ||
"html_url": "https://pdt-apidocs.pagerduty.com/escalation_policies/P6F7EI2", | ||
"name": "Endeavour", | ||
"escalation_rules": [ | ||
{ | ||
"id": "PFN10S6", | ||
"escalation_delay_in_minutes": 1, | ||
"targets": [ | ||
{ | ||
"name": "Douglas Hurley", | ||
"email": "[email protected]", | ||
"time_zone": "America/Los_Angeles", | ||
"color": "teal", | ||
"avatar_url": "https://secure.gravatar.com/avatar/ab94dd438eb6d722012e6155fb46d931.png?d=mm&r=PG", | ||
"billed": true, | ||
"role": "limited_user", | ||
"description": null, | ||
"invitation_sent": true, | ||
"job_title": null, | ||
"teams": [ | ||
{ | ||
"id": "PQZPQGI", | ||
"type": "team_reference", | ||
"summary": "North American Space Agency (NASA)", | ||
"self": "https://api.pagerduty.com/teams/PQZPQGI", | ||
"html_url": "https://pdt-apidocs.pagerduty.com/teams/PQZPQGI" | ||
} | ||
], | ||
"contact_methods": [ | ||
{ | ||
"id": "P92PVVL", | ||
"type": "email_contact_method_reference", | ||
"summary": "Default", | ||
"self": "https://api.pagerduty.com/users/PRF4164/contact_methods/P92PVVL", | ||
"html_url": null | ||
}, | ||
{ | ||
"id": "PRYGTVV", | ||
"type": "phone_contact_method_reference", | ||
"summary": "Work", | ||
"self": "https://api.pagerduty.com/users/PRF4164/contact_methods/PRYGTVV", | ||
"html_url": null | ||
}, | ||
{ | ||
"id": "PNMVSG5", | ||
"type": "sms_contact_method_reference", | ||
"summary": "Mobile", | ||
"self": "https://api.pagerduty.com/users/PRF4164/contact_methods/PNMVSG5", | ||
"html_url": null | ||
} | ||
], | ||
"notification_rules": [ | ||
{ | ||
"id": "PNFV1JA", | ||
"type": "assignment_notification_rule_reference", | ||
"summary": "0 minutes: channel P92PVVL", | ||
"self": "https://api.pagerduty.com/users/PRF4164/notification_rules/PNFV1JA", | ||
"html_url": null | ||
}, | ||
{ | ||
"id": "PRRG2Z0", | ||
"type": "assignment_notification_rule_reference", | ||
"summary": "0 minutes: channel P92PVVL", | ||
"self": "https://api.pagerduty.com/users/PRF4164/notification_rules/PRRG2Z0", | ||
"html_url": null | ||
}, | ||
{ | ||
"id": "P4FC8CY", | ||
"type": "assignment_notification_rule_reference", | ||
"summary": "0 minutes: channel PRYGTVV", | ||
"self": "https://api.pagerduty.com/users/PRF4164/notification_rules/P4FC8CY", | ||
"html_url": null | ||
}, | ||
{ | ||
"id": "P16R808", | ||
"type": "assignment_notification_rule_reference", | ||
"summary": "0 minutes: channel PNMVSG5", | ||
"self": "https://api.pagerduty.com/users/PRF4164/notification_rules/P16R808", | ||
"html_url": null | ||
} | ||
], | ||
"id": "P4CMCAU", | ||
"type": "user", | ||
"summary": "Douglas Hurley", | ||
"self": "https://api.pagerduty.com/users/PRF4164", | ||
"html_url": "https://pdt-apidocs.pagerduty.com/users/PRF4164" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"limit": 25, | ||
"offset": 0, | ||
"more": false, | ||
"total": null | ||
} |
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