Skip to content

Commit 7628ede

Browse files
toppercodesampagent
andcommitted
notifier: preserve pagerduty credentials when API redacts values
Amp-Thread-ID: https://ampcode.com/threads/T-019cb11d-d0b4-724b-938e-a9eb7f44c002 Co-authored-by: Amp <amp@ampcode.com>
1 parent c923456 commit 7628ede

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

axiom/resource_notifier.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (r *NotifierResource) Create(ctx context.Context, req resource.CreateReques
346346
return
347347
}
348348

349-
resp.Diagnostics.Append(resp.State.Set(ctx, flattenNotifier(*notifier))...)
349+
resp.Diagnostics.Append(resp.State.Set(ctx, mergeNotifierState(flattenNotifier(*notifier), plan))...)
350350
}
351351

352352
func (r *NotifierResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
@@ -373,7 +373,7 @@ func (r *NotifierResource) Read(ctx context.Context, req resource.ReadRequest, r
373373
return
374374
}
375375

376-
resp.Diagnostics.Append(resp.State.Set(ctx, flattenNotifier(*notifier))...)
376+
resp.Diagnostics.Append(resp.State.Set(ctx, mergeNotifierState(flattenNotifier(*notifier), plan))...)
377377
}
378378

379379
func (r *NotifierResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
@@ -397,7 +397,7 @@ func (r *NotifierResource) Update(ctx context.Context, req resource.UpdateReques
397397
return
398398
}
399399

400-
resp.Diagnostics.Append(resp.State.Set(ctx, flattenNotifier(*notifier))...)
400+
resp.Diagnostics.Append(resp.State.Set(ctx, mergeNotifierState(flattenNotifier(*notifier), plan))...)
401401
}
402402

403403
func (r *NotifierResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
@@ -488,6 +488,26 @@ func flattenNotifier(notifier axiom.Notifier) NotifierResourceModel {
488488
}
489489
}
490490

491+
func mergeNotifierState(remote NotifierResourceModel, source NotifierResourceModel) NotifierResourceModel {
492+
if remote.Properties == nil || source.Properties == nil {
493+
return remote
494+
}
495+
496+
if remote.Properties.Pagerduty == nil || source.Properties.Pagerduty == nil {
497+
return remote
498+
}
499+
500+
if remote.Properties.Pagerduty.RoutingKey.ValueString() == "" {
501+
remote.Properties.Pagerduty.RoutingKey = source.Properties.Pagerduty.RoutingKey
502+
}
503+
504+
if remote.Properties.Pagerduty.Token.ValueString() == "" {
505+
remote.Properties.Pagerduty.Token = source.Properties.Pagerduty.Token
506+
}
507+
508+
return remote
509+
}
510+
491511
func buildNotifierProperties(properties axiom.NotifierProperties) *NotifierProperties {
492512
var notifierProperties NotifierProperties
493513
if properties.Discord != nil {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package axiom
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func TestMergeNotifierStatePagerDutyFallback(t *testing.T) {
10+
source := NotifierResourceModel{
11+
Properties: &NotifierProperties{
12+
Pagerduty: &PagerDutyConfig{
13+
RoutingKey: types.StringValue("plan-routing-key"),
14+
Token: types.StringValue("plan-token"),
15+
},
16+
},
17+
}
18+
19+
remote := NotifierResourceModel{
20+
Properties: &NotifierProperties{
21+
Pagerduty: &PagerDutyConfig{
22+
RoutingKey: types.StringValue(""),
23+
Token: types.StringValue(""),
24+
},
25+
},
26+
}
27+
28+
merged := mergeNotifierState(remote, source)
29+
30+
if got := merged.Properties.Pagerduty.RoutingKey.ValueString(); got != "plan-routing-key" {
31+
t.Fatalf("RoutingKey = %q, expected plan-routing-key", got)
32+
}
33+
34+
if got := merged.Properties.Pagerduty.Token.ValueString(); got != "plan-token" {
35+
t.Fatalf("Token = %q, expected plan-token", got)
36+
}
37+
}
38+
39+
func TestMergeNotifierStatePagerDutyKeepsRemoteValues(t *testing.T) {
40+
source := NotifierResourceModel{
41+
Properties: &NotifierProperties{
42+
Pagerduty: &PagerDutyConfig{
43+
RoutingKey: types.StringValue("plan-routing-key"),
44+
Token: types.StringValue("plan-token"),
45+
},
46+
},
47+
}
48+
49+
remote := NotifierResourceModel{
50+
Properties: &NotifierProperties{
51+
Pagerduty: &PagerDutyConfig{
52+
RoutingKey: types.StringValue("api-routing-key"),
53+
Token: types.StringValue("api-token"),
54+
},
55+
},
56+
}
57+
58+
merged := mergeNotifierState(remote, source)
59+
60+
if got := merged.Properties.Pagerduty.RoutingKey.ValueString(); got != "api-routing-key" {
61+
t.Fatalf("RoutingKey = %q, expected api-routing-key", got)
62+
}
63+
64+
if got := merged.Properties.Pagerduty.Token.ValueString(); got != "api-token" {
65+
t.Fatalf("Token = %q, expected api-token", got)
66+
}
67+
}

0 commit comments

Comments
 (0)