From d87c31fd8b95146998019f15cbbd719adcda5e6a Mon Sep 17 00:00:00 2001 From: Jason Harley Date: Thu, 19 Dec 2024 17:26:33 -0500 Subject: [PATCH] fix(trigger): handle state error with equiv. query specifications --- internal/provider/trigger_resource.go | 28 ++++------------ internal/provider/trigger_resource_test.go | 37 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/internal/provider/trigger_resource.go b/internal/provider/trigger_resource.go index f7e76aab..5f1093d2 100644 --- a/internal/provider/trigger_resource.go +++ b/internal/provider/trigger_resource.go @@ -297,17 +297,9 @@ func (r *triggerResource) Create(ctx context.Context, req resource.CreateRequest state.QueryJson = types.StringNull() } else { state.QueryID = types.StringNull() - - json, err := trigger.Query.Encode() - if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("query_json"), - "failed to encode query_json", - err.Error(), - ) - } else { - state.QueryJson = types.StringValue(json) - } + // store the plan's query JSON in state so it matches the config and rely on the plan modifier + // to handle the rest when we read it back + state.QueryJson = plan.QueryJson } resp.Diagnostics.Append(resp.State.Set(ctx, state)...) @@ -450,17 +442,9 @@ func (r *triggerResource) Update(ctx context.Context, req resource.UpdateRequest state.QueryJson = types.StringNull() } else { state.QueryID = types.StringNull() - - json, err := trigger.Query.Encode() - if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("query_json"), - "failed to encode query_json", - err.Error(), - ) - } else { - state.QueryJson = types.StringValue(json) - } + // store the plan's query JSON in state so it matches the config and rely on the plan modifier + // to handle the rest when we read it back + state.QueryJson = plan.QueryJson } resp.Diagnostics.Append(resp.State.Set(ctx, state)...) diff --git a/internal/provider/trigger_resource_test.go b/internal/provider/trigger_resource_test.go index ae7c072f..d7b9ba0f 100644 --- a/internal/provider/trigger_resource_test.go +++ b/internal/provider/trigger_resource_test.go @@ -785,6 +785,43 @@ resource "honeycombio_trigger" "test" { }) } +func TestAcc_TriggerResource_QueryJSONHandlesEquivQuerySpecs(t *testing.T) { + dataset := testAccDataset() + + resource.Test(t, resource.TestCase{ + PreCheck: testAccPreCheck(t), + ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` +data "honeycombio_query_specification" "test" { + calculation { + op = "COUNT" + } + + filter_combination = "AND" + + time_range = 1200 +} + +resource honeycombio_trigger "test" { + name = "test trigger" + dataset = "%s" + + threshold { + op = ">" + value = 100 + } + + frequency = data.honeycombio_query_specification.test.time_range / 2 + + query_json = data.honeycombio_query_specification.test.json +}`, dataset), + }, + }, + }) +} + func testAccConfigBasicTriggerTest(dataset, name, pdseverity string) string { email := test.RandomEmail() pdKey := test.RandomString(32)