Skip to content

Commit

Permalink
Add importer to honeycombio_trigger (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenraad Verheyden committed Oct 16, 2020
1 parent a046b57 commit c5b6ba4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/resources/trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ webhook | name of the webhook
In addition to all arguments above, the following attributes are exported:

* `id` - ID of the trigger.

## Import

Triggers can be imported using a combination of the dataset name and their ID, e.g.

```
$ terraform import honeycombio_board.my_board my-dataaset/AeZzSoWws9G
```

You can find the ID in the URL bar when visiting the trigger from the UI.
22 changes: 22 additions & 0 deletions honeycombio/resource_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package honeycombio
import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -16,6 +18,9 @@ func newTrigger() *schema.Resource {
ReadContext: resourceTriggerRead,
UpdateContext: resourceTriggerUpdate,
DeleteContext: resourceTriggerDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceTriggerImport,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -103,6 +108,23 @@ func newTrigger() *schema.Resource {
}
}

func resourceTriggerImport(ctx context.Context, d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
// import ID is of the format <dataset>/<trigger ID>
// note that the dataset name can also contain '/'
idSegments := strings.Split(d.Id(), "/")
if len(idSegments) < 2 {
return nil, fmt.Errorf("invalid import ID, supplied ID must be written as <dataset>/<trigger ID>")
}

dataset := strings.Join(idSegments[0:len(idSegments)-1], "/")
id := idSegments[len(idSegments)-1]

d.Set("dataset", dataset)
d.SetId(id)

return []*schema.ResourceData{d}, nil
}

func resourceTriggerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*honeycombio.Client)

Expand Down
13 changes: 13 additions & 0 deletions honeycombio/resource_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func TestAccHoneycombioTrigger_basic(t *testing.T) {
testAccCheckTriggerExists(t, "honeycombio_trigger.test", &triggerAfter),
),
},
{
ResourceName: "honeycombio_trigger.test",
ImportStateIdPrefix: fmt.Sprintf("%v/", dataset),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -156,6 +162,13 @@ func TestAccHoneycombioTrigger_validationErrors(t *testing.T) {
}`),
ExpectError: regexp.MustCompile("limit is not allowed in a trigger query"),
},
{
ResourceName: "honeycombio_trigger.test",
ImportStateId: "someId",
ImportState: true,
ImportStateVerify: true,
ExpectError: regexp.MustCompile("invalid import ID, supplied ID must be written as <dataset>/<trigger ID>"),
},
},
})
}
Expand Down

0 comments on commit c5b6ba4

Please sign in to comment.