From c9e250dab160952d103117b9463779575a9d9436 Mon Sep 17 00:00:00 2001 From: Sean Houghton Date: Tue, 2 Oct 2018 01:45:53 -0700 Subject: [PATCH] Add support for images and links in the PagerDuty notification config (#1559) * Add support for images and links in the PagerDuty notification config This only works when using the v2 API. It supports template values for all fields. Signed-off-by: Sean Houghton --- config/notifiers.go | 15 +++++++++++++++ notify/impl.go | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/config/notifiers.go b/config/notifiers.go index 0a37493b..9b87c18a 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -203,12 +203,27 @@ type PagerdutyConfig struct { ClientURL string `yaml:"client_url,omitempty" json:"client_url,omitempty"` Description string `yaml:"description,omitempty" json:"description,omitempty"` Details map[string]string `yaml:"details,omitempty" json:"details,omitempty"` + Images []PagerdutyImage `yaml:"images,omitempty" json:"images,omitempty"` + Links []PagerdutyLink `yaml:"links,omitempty" json:"links,omitempty"` Severity string `yaml:"severity,omitempty" json:"severity,omitempty"` Class string `yaml:"class,omitempty" json:"class,omitempty"` Component string `yaml:"component,omitempty" json:"component,omitempty"` Group string `yaml:"group,omitempty" json:"group,omitempty"` } +// PagerdutyLink is a link +type PagerdutyLink struct { + HRef string `yaml:"href,omitempty" json:"href,omitempty"` + Text string `yaml:"text,omitempty" json:"text,omitempty"` +} + +// PagerdutyImage is an image +type PagerdutyImage struct { + Src string `yaml:"src,omitempty" json:"src,omitempty"` + Alt string `yaml:"alt,omitempty" json:"alt,omitempty"` + Text string `yaml:"text,omitempty" json:"text,omitempty"` +} + // UnmarshalYAML implements the yaml.Unmarshaler interface. func (c *PagerdutyConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { *c = DefaultPagerdutyConfig diff --git a/notify/impl.go b/notify/impl.go index 912d3e96..9dd076c0 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -465,6 +465,19 @@ type pagerDutyMessage struct { Client string `json:"client,omitempty"` ClientURL string `json:"client_url,omitempty"` Details map[string]string `json:"details,omitempty"` + Images []pagerDutyImage `json:"images,omitempty"` + Links []pagerDutyLink `json:"links,omitempty"` +} + +type pagerDutyLink struct { + HRef string `json:"href"` + Text string `json:"text"` +} + +type pagerDutyImage struct { + Src string `json:"src"` + Alt string `json:"alt"` + Text string `json:"text"` } type pagerDutyPayload struct { @@ -547,6 +560,8 @@ func (n *PagerDuty) notifyV2( RoutingKey: tmpl(string(n.conf.RoutingKey)), EventAction: eventType, DedupKey: hashKey(key), + Images: make([]pagerDutyImage, len(n.conf.Images)), + Links: make([]pagerDutyLink, len(n.conf.Links)), Payload: &pagerDutyPayload{ Summary: tmpl(n.conf.Description), Source: tmpl(n.conf.Client), @@ -558,6 +573,17 @@ func (n *PagerDuty) notifyV2( }, } + for index, item := range n.conf.Images { + msg.Images[index].Src = tmpl(item.Src) + msg.Images[index].Alt = tmpl(item.Alt) + msg.Images[index].Text = tmpl(item.Text) + } + + for index, item := range n.conf.Links { + msg.Links[index].HRef = tmpl(item.HRef) + msg.Links[index].Text = tmpl(item.Text) + } + if tmplErr != nil { return false, fmt.Errorf("failed to template PagerDuty v2 message: %v", tmplErr) }