| 1 | package pagerduty |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "regexp" |
| 7 | "strconv" |
| 8 | |
| 9 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 10 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 | ) |
| 12 | |
| 13 | // Config holds the configuration for the PagerDuty service. |
| 14 | type Config struct { |
| 15 | IntegrationKey string `desc:"The PagerDuty API integration key" url:"path"` |
| 16 | Host string `desc:"The PagerDuty API host." url:"host" default:"events.pagerduty.com"` |
| 17 | Port uint16 `desc:"The PagerDuty API port." url:"port" default:"443"` |
| 18 | Severity string `desc:"The perceived severity of the status the event (critical, error, warning, or info); required" default:"error" key:"severity"` |
| 19 | Source string `desc:"The unique location of the affected system, preferably a hostname or FQDN; required" default:"default" key:"source"` |
| 20 | Action string `desc:"The type of event (trigger, acknowledge, or resolve)" default:"trigger" key:"action"` |
| 21 | DedupKey string `desc:"A unique key used for incident deduplication" key:"dedup_key"` |
| 22 | Details string `desc:"Additional details about the incident (JSON string that will be parsed into an object)" key:"details"` |
| 23 | Contexts string `desc:"Additional context links or images" key:"contexts"` |
| 24 | Client string `desc:"The name of the monitoring client that is triggering this event" key:"client"` |
| 25 | ClientURL string `desc:"The URL of the monitoring client that is triggering this event" key:"client_url"` |
| 26 | } |
| 27 | |
| 28 | const ( |
| 29 | // Scheme is the identifying part of this service's configuration URL. |
| 30 | Scheme = "pagerduty" |
| 31 | // integrationKeyRegex validates that integration keys are 32-character hexadecimal strings. |
| 32 | integrationKeyRegex = `^[a-fA-F0-9]{32}$` |
| 33 | ) |
| 34 | |
| 35 | // Enums returns an empty map because the PagerDuty service doesn't use Enums. |
| 36 | func (c *Config) Enums() map[string]types.EnumFormatter { |
| 37 | return map[string]types.EnumFormatter{} |
| 38 | } |
| 39 | |
| 40 | // GetURL returns a URL representation of the Config's current field values. |
| 41 | func (c *Config) GetURL() *url.URL { |
| 42 | resolver := format.NewPropKeyResolver(c) |
| 43 | |
| 44 | return c.getURL(&resolver) |
| 45 | } |
| 46 | |
| 47 | // SetURL updates the Config from a URL representation of its field values. |
| 48 | func (c *Config) SetURL(serviceURL *url.URL) error { |
| 49 | resolver := format.NewPropKeyResolver(c) |
| 50 | |
| 51 | return c.setURL(&resolver, serviceURL) |
| 52 | } |
| 53 | |
| 54 | // getURL constructs a URL from the Config's fields using the provided resolver. |
| 55 | func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 56 | var host string |
| 57 | if c.Port > 0 { |
| 58 | host = fmt.Sprintf("%s:%d", c.Host, c.Port) |
| 59 | } else { |
| 60 | host = c.Host |
| 61 | } |
| 62 | |
| 63 | return &url.URL{ |
| 64 | Host: host, |
| 65 | Path: "/" + c.IntegrationKey, |
| 66 | Scheme: Scheme, |
| 67 | RawQuery: format.BuildQuery(resolver), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // setURL updates the Config from a URL using the provided resolver. |
| 72 | func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 73 | // Skip validation for dummy URLs used in docs generation |
| 74 | if serviceURL.Hostname() == "dummy.com" { |
| 75 | if len(serviceURL.Path) > 1 { |
| 76 | c.IntegrationKey = serviceURL.Path[1:] |
| 77 | } |
| 78 | |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | if len(serviceURL.Path) <= 1 { |
| 83 | return errMissingIntegrationKey |
| 84 | } |
| 85 | |
| 86 | c.IntegrationKey = serviceURL.Path[1:] |
| 87 | |
| 88 | // Validate integration key format |
| 89 | if matched, err := regexp.MatchString(integrationKeyRegex, c.IntegrationKey); err != nil { |
| 90 | return fmt.Errorf("failed to validate integration key: %w", err) |
| 91 | } else if !matched { |
| 92 | return errInvalidIntegrationKey |
| 93 | } |
| 94 | |
| 95 | if serviceURL.Hostname() != "" { |
| 96 | c.Host = serviceURL.Hostname() |
| 97 | } |
| 98 | |
| 99 | if serviceURL.Port() != "" { |
| 100 | port, err := strconv.ParseUint(serviceURL.Port(), 10, 16) |
| 101 | if err != nil { |
| 102 | return fmt.Errorf("failed to parse port: %w", err) |
| 103 | } |
| 104 | |
| 105 | c.Port = uint16(port) |
| 106 | } |
| 107 | |
| 108 | for key, vals := range serviceURL.Query() { |
| 109 | if err := resolver.Set(key, vals[0]); err != nil { |
| 110 | return fmt.Errorf("failed to set query parameter %q: %w", key, err) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return nil |
| 115 | } |