Skip to content

Commit

Permalink
fix(trigger): handle state error with equiv. query specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jharley committed Dec 19, 2024
1 parent f349ad5 commit d87c31f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
28 changes: 6 additions & 22 deletions internal/provider/trigger_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Expand Down Expand Up @@ -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)...)
Expand Down
37 changes: 37 additions & 0 deletions internal/provider/trigger_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d87c31f

Please sign in to comment.