Skip to content

Commit cf1fcb3

Browse files
committed
Validate & cache IW2 url instance at daemon start up
1 parent d723a64 commit cf1fcb3

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

internal/daemon/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/icinga/icinga-go-library/logging"
99
"github.com/icinga/icinga-go-library/utils"
1010
"github.com/icinga/icinga-notifications/internal"
11+
"fmt"
12+
"net/url"
1113
"os"
1214
"time"
1315
)
@@ -25,6 +27,8 @@ type ConfigFile struct {
2527
Icingaweb2URL string `yaml:"icingaweb2-url"`
2628
Database database.Config `yaml:"database"`
2729
Logging logging.Config `yaml:"logging"`
30+
31+
IcingaWeb2UrlParsed *url.URL // Parsed version of Icingaweb2URL
2832
}
2933

3034
// SetDefaults implements the defaults.Setter interface.
@@ -44,6 +48,16 @@ func (c *ConfigFile) Validate() error {
4448
return err
4549
}
4650

51+
if c.Icingaweb2URL == "" {
52+
return errors.New("icingaweb2-url must be set")
53+
}
54+
55+
parsedUrl, err := url.Parse(c.Icingaweb2URL)
56+
if err != nil {
57+
return fmt.Errorf("invalid icingaweb2-url: %s", err)
58+
}
59+
c.IcingaWeb2UrlParsed = parsedUrl
60+
4761
return nil
4862
}
4963

internal/incident/incident.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"net/url"
87
"strconv"
98
"sync"
109
"time"
@@ -271,12 +270,7 @@ func (i *Incident) RetriggerEscalations(ev *event.Event) {
271270
// The URL of the incident is constructed using the Icinga Web 2 URL configured in the daemon configuration,
272271
// and panics if it fails to parse that URL.
273272
func (i *Incident) MakeNotificationRequest(ev *event.Event) *plugin.NotificationRequest {
274-
baseUrl, err := url.Parse(daemon.Config().Icingaweb2URL)
275-
if err != nil {
276-
i.Logger.Panicw("Failed to parse Icinga Web 2 URL", zap.String("url", daemon.Config().Icingaweb2URL), zap.Error(err))
277-
}
278-
279-
incidentUrl := baseUrl.JoinPath("/notifications/incident")
273+
incidentUrl := daemon.Config().IcingaWeb2UrlParsed.JoinPath("/notifications/incident")
280274
incidentUrl.RawQuery = fmt.Sprintf("id=%d", i.ID)
281275

282276
return &plugin.NotificationRequest{

0 commit comments

Comments
 (0)