Skip to content

Commit 9e03699

Browse files
authored
fix(r/trigger): updating env-wide trigger causes apply error (#709)
Updating an env-wide trigger was not handling the `dataset` attribute correctly resulting in an apply-time error. Also noticed that Trigger's didn't get the same update as SLOs to support importing environment-wide triggers by 'bare ID' (e.g. no need to use `__all__`), so fixed that here and updated the test. - Closes #702
1 parent 3e71b80 commit 9e03699

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

docs/resources/slo.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ In addition to all arguments above, the following attributes are exported:
9393

9494
## Import
9595

96-
SLOs can be imported using a combination of the dataset name and their ID, e.g.
97-
98-
For multi-dataset SLOs, just pass in their ID.
99-
100-
SLOs can be imported using by using their ID combined with their dataset.
96+
Single-dataset SLOs can be imported using by using their ID combined with their dataset.
10197
If the SLO is a multi-dataset (MD) SLO, the dataset is not provided.
10298

10399
### SLO

docs/resources/trigger.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,22 @@ In addition to all arguments above, the following attributes are exported:
330330

331331
* `id` - ID of the trigger.
332332

333+
333334
## Import
334335

335-
Triggers can be imported using a combination of the dataset name and their ID, e.g.
336+
Single-dataset Triggers can be imported using by using their ID combined with their dataset.
337+
If the Trigger is an environment-wide trigger, the dataset is not provided.
338+
339+
### Trigger
340+
341+
```
342+
$ terraform import honeycombio_trigger.my_trigger my-dataset/bj9BwOb1uKz
343+
```
344+
345+
### Environment-wide Trigger
336346

337347
```
338-
$ terraform import honeycombio_trigger.my_trigger my-dataset/AeZzSoWws9G
348+
$ terraform import honeycombio_trigger.my_trigger bj9BwOb1uJz
339349
```
340350

341351
You can find the ID in the URL bar when visiting the trigger from the UI.

internal/provider/trigger_resource.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ func (r *triggerResource) Create(ctx context.Context, req resource.CreateRequest
317317
}
318318

319319
var state models.TriggerResourceModel
320-
state.Dataset = plan.Dataset
321320
state.ID = types.StringValue(trigger.ID)
321+
state.Dataset = plan.Dataset
322322
state.Name = types.StringValue(trigger.Name)
323323
state.Description = types.StringValue(trigger.Description)
324324
state.Disabled = types.BoolValue(trigger.Disabled)
@@ -489,8 +489,8 @@ func (r *triggerResource) Update(ctx context.Context, req resource.UpdateRequest
489489
}
490490

491491
var state models.TriggerResourceModel
492-
state.Dataset = dataset
493492
state.ID = types.StringValue(trigger.ID)
493+
state.Dataset = plan.Dataset
494494
state.Name = types.StringValue(trigger.Name)
495495
state.Description = types.StringValue(trigger.Description)
496496
state.Disabled = types.BoolValue(trigger.Disabled)
@@ -552,17 +552,20 @@ func (r *triggerResource) Delete(ctx context.Context, req resource.DeleteRequest
552552
}
553553

554554
func (r *triggerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
555-
// import ID is of the format <dataset>/<trigger ID>
556555
dataset, id, found := strings.Cut(req.ID, "/")
556+
557+
// if dataset separator not found, we will assume its the bare id
558+
// if thats the case, we need to reassign values since strings.Cut would return (id, "", false)
559+
dsValue := types.StringNull()
560+
idValue := id
557561
if !found {
558-
resp.Diagnostics.AddError(
559-
"Invalid Import ID",
560-
"The supplied ID must be wrtten as <dataset>/<trigger ID>.",
561-
)
562-
return
562+
idValue = dataset
563+
} else {
564+
dsValue = types.StringValue(dataset)
563565
}
564-
req.ID = id
565-
resp.State.SetAttribute(ctx, path.Root("dataset"), dataset)
566+
567+
req.ID = idValue
568+
resp.State.SetAttribute(ctx, path.Root("dataset"), dsValue)
566569
resp.State.SetAttribute(ctx, path.Root("query_id"), types.StringNull()) // favor query_json on import
567570

568571
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)

internal/provider/trigger_resource_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,23 @@ resource "honeycombio_trigger" "test" {
439439
resource.TestCheckResourceAttr("honeycombio_trigger.test", "threshold.0.value", "1000"),
440440
),
441441
},
442+
{ // update the trigger's name
443+
Config: testAccConfigEnvironmentWideTrigger(name + "0"),
444+
Check: resource.ComposeAggregateTestCheckFunc(
445+
testAccEnsureTriggerExists(t, "honeycombio_trigger.test"),
446+
resource.TestCheckResourceAttr("honeycombio_trigger.test", "name", name+"0"),
447+
resource.TestCheckNoResourceAttr("honeycombio_trigger.test", "dataset"),
448+
resource.TestCheckResourceAttr("honeycombio_trigger.test", "description", "Environment-wide trigger"),
449+
resource.TestCheckResourceAttr("honeycombio_trigger.test", "frequency", "1800"),
450+
resource.TestCheckResourceAttr("honeycombio_trigger.test", "threshold.0.op", ">"),
451+
resource.TestCheckResourceAttr("honeycombio_trigger.test", "threshold.0.value", "1000"),
452+
),
453+
},
442454
{
443-
ResourceName: "honeycombio_trigger.test",
444-
ImportStateIdPrefix: "__all__/",
445-
ImportState: true,
455+
ResourceName: "honeycombio_trigger.test",
456+
ImportState: true,
457+
ImportStateVerify: true,
458+
ImportStateVerifyIgnore: []string{"recipient"},
446459
},
447460
},
448461
})

0 commit comments

Comments
 (0)