Debian Codegraph @

pkg/services/incident/opsgenie/opsgenie_config.go

raw
1 package opsgenie
2
3 import (
4 "errors"
5 "fmt"
6 "net/url"
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 OpsGenie service.
14 type Config struct {
15 APIKey string `desc:"The OpsGenie API key" url:"path"`
16 Host string `desc:"The OpsGenie API host. Use 'api.eu.opsgenie.com' for EU instances" url:"host" default:"api.opsgenie.com"`
17 Port uint16 `desc:"The OpsGenie API port." url:"port" default:"443"`
18 Alias string `desc:"Client-defined identifier of the alert" key:"alias" optional:"true"`
19 Description string `desc:"Description field of the alert" key:"description" optional:"true"`
20 Responders []Entity `desc:"Teams, users, escalations and schedules that the alert will be routed to send notifications" key:"responders" optional:"true"`
21 VisibleTo []Entity `desc:"Teams and users that the alert will become visible to without sending any notification" key:"visibleTo" optional:"true"`
22 Actions []string `desc:"Custom actions that will be available for the alert" key:"actions" optional:"true"`
23 Tags []string `desc:"Tags of the alert" key:"tags" optional:"true"`
24 Details map[string]string `desc:"Map of key-value pairs to use as custom properties of the alert" key:"details" optional:"true"`
25 Entity string `desc:"Entity field of the alert that is generally used to specify which domain the Source field of the alert" key:"entity" optional:"true"`
26 Source string `desc:"Source field of the alert" key:"source" optional:"true"`
27 Priority string `desc:"Priority level of the alert. Possible values are P1, P2, P3, P4 and P5" key:"priority" optional:"true"`
28 Note string `desc:"Additional note that will be added while creating the alert" key:"note" optional:"true"`
29 User string `desc:"Display name of the request owner" key:"user" optional:"true"`
30 Title string `desc:"notification title, optionally set by the sender" default:"" key:"title"`
31 }
32
33 const (
34 defaultPort = 443 // defaultPort is the default port for OpsGenie API connections.
35 Scheme = "opsgenie" // Scheme is the identifying part of this service's configuration URL.
36 )
37
38 // ErrAPIKeyMissing indicates that the API key is missing from the config URL path.
39 var ErrAPIKeyMissing = errors.New("API key missing from config URL path")
40
41 // Enums returns an empty map because the OpsGenie service doesn't use Enums.
42 func (c *Config) Enums() map[string]types.EnumFormatter {
43 return map[string]types.EnumFormatter{}
44 }
45
46 // GetURL returns a URL representation of the Config's current field values.
47 func (c *Config) GetURL() *url.URL {
48 resolver := format.NewPropKeyResolver(c)
49
50 return c.getURL(&resolver)
51 }
52
53 // SetURL updates the Config from a URL representation of its field values.
54 func (c *Config) SetURL(serviceURL *url.URL) error {
55 resolver := format.NewPropKeyResolver(c)
56
57 return c.setURL(&resolver, serviceURL)
58 }
59
60 // getURL constructs a URL from the Config's fields using the provided resolver.
61 func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
62 var host string
63 if c.Port > 0 {
64 host = fmt.Sprintf("%s:%d", c.Host, c.Port)
65 } else {
66 host = c.Host
67 }
68
69 result := &url.URL{
70 Host: host,
71 Path: "/" + c.APIKey,
72 Scheme: Scheme,
73 RawQuery: format.BuildQuery(resolver),
74 }
75
76 return result
77 }
78
79 // setURL updates the Config from a URL using the provided resolver.
80 func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
81 c.Host = serviceURL.Hostname()
82
83 if serviceURL.String() != "opsgenie://[email protected]" {
84 if serviceURL.Path != "" {
85 c.APIKey = serviceURL.Path[1:]
86 } else {
87 return ErrAPIKeyMissing
88 }
89 }
90
91 if serviceURL.Port() != "" {
92 port, err := strconv.ParseUint(
93 serviceURL.Port(),
94 10,
95 16,
96 )
97 if err != nil {
98 return fmt.Errorf("parsing port %q: %w", serviceURL.Port(), err)
99 }
100
101 c.Port = uint16(port)
102 } else {
103 c.Port = defaultPort
104 }
105
106 for key, vals := range serviceURL.Query() {
107 if err := resolver.Set(key, vals[0]); err != nil {
108 return fmt.Errorf(
109 "setting query parameter %q to %q: %w",
110 key,
111 vals[0],
112 err,
113 )
114 }
115 }
116
117 return nil
118 }