From c997f5bf078c3f8e2338cc92c12dd765d545ff64 Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Thu, 24 Oct 2024 10:29:35 -0500 Subject: [PATCH] check: merge each field of Alert.PagerDuty config --- internal/check/checks.go | 24 +++++++++++++++++++++-- internal/check/checks_test.go | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 internal/check/checks_test.go diff --git a/internal/check/checks.go b/internal/check/checks.go index 570c77b..9f9fb43 100644 --- a/internal/check/checks.go +++ b/internal/check/checks.go @@ -27,7 +27,7 @@ func Setup(ctx context.Context, logger log.Logger, conf *config.Config) (*Instan "check_name": log.String(check.Name), }) - client, err := provider.NewClient(checkLogger, cmp.Or(check.Alert, conf.Alert)) + client, err := provider.NewClient(checkLogger, mergeAlertConfigs(check.Alert, conf.Alert)) if err != nil { return nil, fmt.Errorf("setting up check[%d] provider: %w", idx, err) } @@ -67,7 +67,7 @@ func (xs *Instances) CheckIn(ctx context.Context, logger log.Logger, checkID str }) // Grab the provider client for the check - client, err := provider.NewClient(logger, cmp.Or(found.Alert, xs.conf.Alert)) + client, err := provider.NewClient(logger, mergeAlertConfigs(found.Alert, xs.conf.Alert)) if err != nil { return nil, fmt.Errorf("problem getting client for check-in: %w", err) } @@ -82,3 +82,23 @@ func (xs *Instances) CheckIn(ctx context.Context, logger log.Logger, checkID str NextExpectedCheckIn: checkInExpected, }, nil } + +func mergeAlertConfigs(local, global config.Alert) config.Alert { + var out config.Alert + + // PagerDuty config merging + out.PagerDuty = cmp.Or(local.PagerDuty, global.PagerDuty) + + if local.PagerDuty != nil && global.PagerDuty != nil { + // Prefer the local config over the global config + out.PagerDuty = &config.PagerDuty{ + ApiKey: cmp.Or(local.PagerDuty.ApiKey, global.PagerDuty.ApiKey), + EscalationPolicy: cmp.Or(local.PagerDuty.EscalationPolicy, global.PagerDuty.EscalationPolicy), + From: cmp.Or(local.PagerDuty.From, global.PagerDuty.From), + RoutingKey: cmp.Or(local.PagerDuty.RoutingKey, global.PagerDuty.RoutingKey), + Urgency: cmp.Or(local.PagerDuty.Urgency, global.PagerDuty.Urgency), + } + } + + return out +} diff --git a/internal/check/checks_test.go b/internal/check/checks_test.go new file mode 100644 index 0000000..47ec67c --- /dev/null +++ b/internal/check/checks_test.go @@ -0,0 +1,36 @@ +package check + +import ( + "testing" + + "github.com/adamdecaf/deadcheck/internal/config" + + "github.com/stretchr/testify/require" +) + +func TestMergeAlertConfigs(t *testing.T) { + t.Run("pagerduty", func(t *testing.T) { + var local config.Alert + global := config.Alert{ + PagerDuty: &config.PagerDuty{ + ApiKey: "api-key", + }, + } + got := mergeAlertConfigs(local, global) + require.Equal(t, "api-key", got.PagerDuty.ApiKey) + + local.PagerDuty = &config.PagerDuty{ + ApiKey: "other-key", + } + got = mergeAlertConfigs(local, global) + require.Equal(t, "other-key", got.PagerDuty.ApiKey) + + global.PagerDuty.Urgency = "high" + got = mergeAlertConfigs(local, global) + require.Equal(t, "high", got.PagerDuty.Urgency) + + local.PagerDuty.Urgency = "low" + got = mergeAlertConfigs(local, global) + require.Equal(t, "low", got.PagerDuty.Urgency) + }) +}