Skip to content

Commit

Permalink
fix(r/trigger): handle state error with equivalent query_json (#593)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

We received a bug report when using "r/trigger"'s `query_json` attribute
handling properly handling equivalent query specifications (e.g. filter
order changed, or the default "AND" filter combination was provided).

The error looked something like this:

```
Error: Provider produced inconsistent result after apply

When applying changes to honeycombio_trigger.example, provider
"provider[\"registry.terraform.io/honeycombio/honeycombio\"]" produced an
unexpected new value: .query_json: was
cty.StringVal("{\"calculations\":[{\"op\":\"COUNT\"}],\"filters\":[{\"column\":\"http.status_code\",\"op\":\"\\u003e=\",\"value\":400},{\"column\":\"trace.parent_id\",\"op\":\"does-not-exist\"}],\"filter_combination\":\"AND\",\"time_range\":900}"),
but now
cty.StringVal("{\"calculations\":[{\"op\":\"COUNT\"}],\"filters\":[{\"column\":\"http.status_code\",\"op\":\"\\u003e=\",\"value\":400},{\"column\":\"trace.parent_id\",\"op\":\"does-not-exist\"}],\"time_range\":900}").

This is a bug in the provider, which should be reported in the provider's own issue tracker.
```

The 'fix' here is to use the same approach "r/query" takes with
`query_json` and use the planned value while relying on the planmodifier
to manage unnecessary diffs.
  • Loading branch information
jharley committed Dec 20, 2024
1 parent 1aa3e54 commit 712232b
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 @@ -787,6 +787,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 712232b

Please sign in to comment.