-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service): PagerDuty service integration (#933)
Co-authored-by: Niko Köser <[email protected]>
- Loading branch information
1 parent
1df6960
commit be564db
Showing
9 changed files
with
652 additions
and
0 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,70 @@ | ||
# PagerDuty Notifications | ||
|
||
## Prerequisites | ||
|
||
Ensure you have a valid PagerDuty API token to authenticate requests. | ||
|
||
### Compatibility | ||
|
||
This service is compatible with the PagerDuty API for creating incidents. | ||
|
||
## Usage | ||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/nikoksr/notify" | ||
"github.com/nikoksr/notify/service/pagerduty" | ||
) | ||
|
||
func main() { | ||
// Create a new PagerDuty service. Replace 'your_pagerduty_api_token' with your actual PagerDuty API token. | ||
pagerDutyService, err := pagerduty.New("your_pagerduty_api_token") | ||
if err != nil { | ||
log.Fatalf("failed to create pagerduty service: %s", err) | ||
} | ||
|
||
// Set the sender address and add receivers. (required) | ||
pagerDutyService.SetFromAddress("[email protected]") | ||
pagerDutyService.AddReceivers("ServiceDirectory1", "ServiceDirectory2") | ||
|
||
// Set the urgency, priority ID, and notification type. (optional) | ||
pagerDutyService.SetUrgency("high") | ||
pagerDutyService.SetPriorityID("P123456") | ||
pagerDutyService.SetNotificationType("incident") | ||
|
||
// Create a notifier instance and register the PagerDuty service to it. | ||
notifier := notify.New() | ||
notifier.UseServices(pagerDutyService) | ||
|
||
// Send a notification. | ||
err = notifier.Send(context.Background(), "Test Alert", "This is a test alert from PagerDuty service.") | ||
if err != nil { | ||
log.Fatalf("failed to send notification: %s", err) | ||
} | ||
|
||
log.Println("Notification sent successfully") | ||
} | ||
``` | ||
|
||
## Configuration | ||
|
||
### Required Properties | ||
- **API Token**: Your PagerDuty API token. | ||
- **From Address**: The email address of the sender. The author of the incident. | ||
- **Receivers**: List of PagerDuty service directories to send the incident to. | ||
|
||
### Optional Properties | ||
- **Urgency**: The urgency of the incident (e.g., "high", "low"). | ||
- **PriorityID**: The ID of the priority level assigned to the incident. | ||
- **NotificationType**: Type of notification (default is "incident"). | ||
|
||
These properties can be set using the respective setter methods provided by the `Config` struct: | ||
- `SetFromAddress(string)` | ||
- `AddReceivers(...string)` | ||
- `SetUrgency(string)` | ||
- `SetPriorityID(string)` | ||
- `SetNotificationType(string)` |
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,96 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/mail" | ||
|
||
"github.com/PagerDuty/go-pagerduty" | ||
) | ||
|
||
const ( | ||
APIReferenceType = "service_reference" | ||
APIPriorityReference = "priority_reference" | ||
DefaultNotificationType = "incident" | ||
) | ||
|
||
// Config contains the configuration for the PagerDuty service. | ||
type Config struct { | ||
FromAddress string | ||
Receivers []string | ||
NotificationType string | ||
Urgency string | ||
PriorityID string | ||
} | ||
|
||
func NewConfig() *Config { | ||
return &Config{ | ||
NotificationType: DefaultNotificationType, | ||
Receivers: make([]string, 0, 1), | ||
} | ||
} | ||
|
||
// OK checks if the configuration is valid. | ||
// It returns an error if the configuration is invalid. | ||
func (c *Config) OK() error { | ||
if c.FromAddress == "" { | ||
return errors.New("from address is required") | ||
} | ||
|
||
_, err := mail.ParseAddress(c.FromAddress) | ||
if err != nil { | ||
return fmt.Errorf("from address is invalid: %w", err) | ||
} | ||
|
||
if len(c.Receivers) == 0 { | ||
return errors.New("at least one receiver is required") | ||
} | ||
|
||
if c.NotificationType == "" { | ||
return errors.New("notification type is required") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// PriorityReference returns the PriorityID reference if it is set, otherwise it returns nil. | ||
func (c *Config) PriorityReference() *pagerduty.APIReference { | ||
if c.PriorityID == "" { | ||
return nil | ||
} | ||
|
||
return &pagerduty.APIReference{ | ||
ID: c.PriorityID, | ||
Type: APIPriorityReference, | ||
} | ||
} | ||
|
||
// SetFromAddress sets the from address in the configuration. | ||
func (c *Config) SetFromAddress(fromAddress string) { | ||
c.FromAddress = fromAddress | ||
} | ||
|
||
// AddReceivers appends the receivers to the configuration. | ||
func (c *Config) AddReceivers(receivers ...string) { | ||
c.Receivers = append(c.Receivers, receivers...) | ||
} | ||
|
||
// SetPriorityID sets the PriorityID in the configuration. | ||
func (c *Config) SetPriorityID(priorityID string) { | ||
c.PriorityID = priorityID | ||
} | ||
|
||
// SetUrgency sets the urgency in the configuration. | ||
func (c *Config) SetUrgency(urgency string) { | ||
c.Urgency = urgency | ||
} | ||
|
||
// SetNotificationType sets the notification type in the configuration. | ||
// If the notification type is empty, it will be set to the default value "incident". | ||
func (c *Config) SetNotificationType(notificationType string) { | ||
if notificationType == "" { | ||
notificationType = DefaultNotificationType | ||
} | ||
|
||
c.NotificationType = notificationType | ||
} |
Oops, something went wrong.