Skip to content

Commit

Permalink
bug: tfrender now properly associates escalation policy step to oncal…
Browse files Browse the repository at this point in the history
…l schedule (#33)
  • Loading branch information
wilsonehusin authored May 6, 2024
1 parent 6567015 commit 69dbcd5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 15 deletions.
41 changes: 41 additions & 0 deletions pager/pagerduty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,45 @@ func TestPagerDuty(t *testing.T) {
t.Logf("found %d schedules", len(s))
assertJSON(t, s)
})

t.Run("LoadEscalationPolicies", func(t *testing.T) {
t.Parallel()
ctx, pd := setup(t)
data := map[string]any{}

if err := pd.LoadSchedules(ctx); err != nil {
t.Fatalf("error loading schedules: %s", err)
}

if err := pd.LoadEscalationPolicies(ctx); err != nil {
t.Fatalf("error loading escalation policies: %s", err)
}

ep, err := store.UseQueries(ctx).ListExtEscalationPolicies(ctx)
if err != nil {
t.Fatalf("error loading escalation policies: %s", err)
}
data["escalation_policies"] = ep

data["escalation_policy_steps"] = []any{}
data["escalation_policy_step_targets"] = []any{}
for _, p := range ep {
steps, err := store.UseQueries(ctx).ListExtEscalationPolicySteps(ctx, p.ID)
if err != nil {
t.Fatalf("error loading escalation policy steps: %s", err)
}
for _, s := range steps {
data["escalation_policy_steps"] = append(data["escalation_policy_steps"].([]any), s)
targets, err := store.UseQueries(ctx).ListExtEscalationPolicyStepTargets(ctx, s.ID)
if err != nil {
t.Fatalf("error loading escalation policy step targets for '%s': %s", s.ID, err)
}
for _, t := range targets {
data["escalation_policy_step_targets"] = append(data["escalation_policy_step_targets"].([]any), t)
}
}
}

assertJSON(t, data)
})
}
36 changes: 36 additions & 0 deletions pager/testdata/TestPagerDuty/LoadEscalationPolicies.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"escalation_policies": [
{
"id": "P6F7EI2",
"name": "Endeavour",
"description": "",
"team_id": {
"String": "",
"Valid": false
},
"repeat_limit": 0,
"repeat_interval": {
"String": "",
"Valid": false
},
"handoff_target_type": "",
"handoff_target_id": "",
"to_import": 0
}
],
"escalation_policy_step_targets": [
{
"escalation_policy_step_id": "PFN10S6",
"target_type": "User",
"target_id": "P4CMCAU"
}
],
"escalation_policy_steps": [
{
"id": "PFN10S6",
"escalation_policy_id": "P6F7EI2",
"position": 0,
"timeout": "PT1M"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ INSERT INTO ext_escalation_policy_steps VALUES('PKQDFZH','P2D2WR1',0,'PT30M');
INSERT INTO ext_escalation_policy_steps VALUES('P08T67P','PS6ITO0',0,'PT30M');

INSERT INTO ext_escalation_policy_step_targets VALUES('P08T67P','User','PXI6XNI');
INSERT INTO ext_escalation_policy_step_targets VALUES('PKQDFZH','OnCallSchedule','PGR96WL-PR3J6XJ');
INSERT INTO ext_escalation_policy_step_targets VALUES('PKQDFZH','OnCallSchedule','PGR96WL');

COMMIT;
30 changes: 16 additions & 14 deletions tfrender/tfrender.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (r *TFRender) ResourceFireHydrantEscalationPolicy(ctx context.Context) erro
}

for _, t := range targets {
var idTraversal *hcl.Traversal
idTraversals := []hcl.Traversal{}

switch t.TargetType {
case store.TARGET_TYPE_USER:
Expand All @@ -168,37 +168,39 @@ func (r *TFRender) ResourceFireHydrantEscalationPolicy(ctx context.Context) erro
console.Errorf("querying user '%s' for step %d of %s: %s\n", t.TargetID, s.Position, p.Name, err.Error())
continue
}
idTraversal = &hcl.Traversal{ //nolint:staticcheck // See "safeguard" below
idTraversals = append(idTraversals, hcl.Traversal{ //nolint:staticcheck // See "safeguard" below
hcl.TraverseRoot{Name: "data"},
hcl.TraverseAttr{Name: "firehydrant_user"},
hcl.TraverseAttr{Name: u.TFSlug()},
hcl.TraverseAttr{Name: "id"},
}
})
case store.TARGET_TYPE_SCHEDULE:
schedule, err := store.UseQueries(ctx).GetExtSchedule(ctx, t.TargetID)
schedules, err := store.UseQueries(ctx).ListExtSchedulesLikeID(ctx, fmt.Sprintf(`%s%%`, t.TargetID))
if err != nil {
console.Errorf("querying schedule '%s' for step %d of %s: %s\n", t.TargetID, s.Position, p.Name, err.Error())
continue
}
slug := schedule.TFSlug()
if currentTeam != nil {
slug = fmt.Sprintf("%s_%s", currentTeam.TFSlug(), slug)
}
idTraversal = &hcl.Traversal{ //nolint:staticcheck // See "safeguard" below
hcl.TraverseRoot{Name: "firehydrant_on_call_schedule"},
hcl.TraverseAttr{Name: slug},
hcl.TraverseAttr{Name: "id"},
for _, schedule := range schedules {
slug := schedule.TFSlug()
if currentTeam != nil {
slug = fmt.Sprintf("%s_%s", currentTeam.TFSlug(), slug)
}
idTraversals = append(idTraversals, hcl.Traversal{ //nolint:staticcheck // See "safeguard" below
hcl.TraverseRoot{Name: "firehydrant_on_call_schedule"},
hcl.TraverseAttr{Name: slug},
hcl.TraverseAttr{Name: "id"},
})
}
default:
console.Errorf("unknown target type '%s' for step %d of %s\n", t.TargetType, s.Position, p.Name)
continue
}

if idTraversal != nil { //nolint:staticcheck // Safeguard in case the switch-case above is changed.
for _, targetTraversal := range idTraversals { //nolint:staticcheck // Safeguard in case the switch-case above is changed.
step.AppendNewline()
target := step.AppendNewBlock("targets", nil).Body()
target.SetAttributeValue("type", cty.StringVal(t.TargetType))
target.SetAttributeTraversal("id", *idTraversal)
target.SetAttributeTraversal("id", targetTraversal)
}
}
}
Expand Down

0 comments on commit 69dbcd5

Please sign in to comment.