-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from kvrhdn/trigger_recipient
Add honeycombio_trigger_recipient data source
- Loading branch information
Showing
6 changed files
with
215 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Data Source: honeycombio_trigger_recipient | ||
|
||
Search the triggers of a dataset for a trigger recipient. The ID of the already existing trigger recipient can be used when creating new triggers. Specifying a trigger recipient by ID is necessary when creating Slack recipients using the API. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "dataset" { | ||
type = string | ||
} | ||
# search for a trigger recipient of type "slack" and target "honeycomb-triggers" in the given dataaset | ||
data "honeycombio_trigger_recipient" "slack" { | ||
dataset = var.dataset | ||
type = "slack" | ||
target = "honeycomb-triggers" | ||
} | ||
data "honeycombio_query" "example" { | ||
calculation { | ||
op = "AVG" | ||
column = "duration_ms" | ||
} | ||
} | ||
resource "honeycombio_trigger" "example" { | ||
name = "Requests are slower than usuals" | ||
query_json = data.honeycombio_query.example.json | ||
dataset = var.dataset | ||
frequency = 600 // in seconds, 10 minutes | ||
threshold { | ||
op = ">" | ||
value = 1000 | ||
} | ||
recipient { | ||
type = "email" | ||
target = "[email protected]" | ||
} | ||
# add an already existing recipient | ||
recipient { | ||
id = data.honeycombio_trigger_recipient.slack | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `dataset` - (Required) Search through all triggers linked to this dataset. | ||
* `type` - (Required) The type of recipient, allowed types are `email`, `marker`, `pagerduty` and `slack`. | ||
* `target` - (Optional) Target of the trigger, this has another meaning depending on the type of recipient (see the table below). | ||
|
||
Type | Target | ||
----------|------------------------- | ||
email | an email address | ||
marker | name of the marker | ||
pagerduty | _N/A_ | ||
slack | name of the channel | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - ID of the trigger recipient. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package honeycombio | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
honeycombio "github.com/kvrhdn/go-honeycombio" | ||
) | ||
|
||
func dataSourceHoneycombioSlackRecipient() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceHoneycombioSlackRecipientRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"dataset": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice(validTriggerRecipientTypes, false), | ||
}, | ||
"target": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceHoneycombioSlackRecipientRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*honeycombio.Client) | ||
dataset := d.Get("dataset").(string) | ||
|
||
triggers, err := client.Triggers.List(dataset) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
searchType := honeycombio.TriggerRecipientType(d.Get("type").(string)) | ||
searchTarget := d.Get("target").(string) | ||
|
||
for _, t := range triggers { | ||
for _, r := range t.Recipients { | ||
if r.Type == searchType && r.Target == searchTarget { | ||
d.SetId(r.ID) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return diag.Errorf("could not find a trigger recipient in \"%s\" with type = \"%s\" and target = \"%s\"", dataset, searchType, searchTarget) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package honeycombio | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
honeycombio "github.com/kvrhdn/go-honeycombio" | ||
) | ||
|
||
func TestAccDataSourceHoneycombioTriggerRecipient_basic(t *testing.T) { | ||
c := testAccProvider.Meta().(*honeycombio.Client) | ||
dataset := testAccDataset() | ||
|
||
trigger := testAccTriggerRecipientCreateTrigger(t, c, dataset) | ||
defer testAccTriggerRecipientDeleteTrigger(t, c, dataset, trigger) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTriggerRecipient(dataset, "email", "[email protected]"), | ||
}, | ||
{ | ||
Config: testAccTriggerRecipient(dataset, "email", "[email protected]"), | ||
ExpectError: regexp.MustCompile("could not find a trigger recipient in "), | ||
}, | ||
{ | ||
Config: testAccTriggerRecipient(dataset, "slack", "[email protected]"), | ||
ExpectError: regexp.MustCompile("could not find a trigger recipient in "), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccTriggerRecipient(dataset, recipientType, target string) string { | ||
return fmt.Sprintf(` | ||
data "honeycombio_trigger_recipient" "test" { | ||
dataset = "%s" | ||
type = "%s" | ||
target = "%s" | ||
}`, dataset, recipientType, target) | ||
} | ||
|
||
func testAccTriggerRecipientCreateTrigger(t *testing.T, c *honeycombio.Client, dataset string) *honeycombio.Trigger { | ||
trigger := &honeycombio.Trigger{ | ||
Name: "Terraform provider - acc test trigger recipient", | ||
Query: &honeycombio.QuerySpec{ | ||
Calculations: []honeycombio.CalculationSpec{ | ||
{ | ||
Op: honeycombio.CalculateOpCount, | ||
}, | ||
}, | ||
}, | ||
Threshold: &honeycombio.TriggerThreshold{ | ||
Op: honeycombio.TriggerThresholdOpGreaterThan, | ||
Value: &[]float64{100}[0], | ||
}, | ||
Recipients: []honeycombio.TriggerRecipient{ | ||
{ | ||
Type: honeycombio.TriggerRecipientTypeEmail, | ||
Target: "[email protected]", | ||
}, | ||
}, | ||
} | ||
trigger, err := c.Triggers.Create(dataset, trigger) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
return trigger | ||
} | ||
|
||
func testAccTriggerRecipientDeleteTrigger(t *testing.T, c *honeycombio.Client, dataset string, trigger *honeycombio.Trigger) { | ||
err := c.Triggers.Delete(dataset, trigger.ID) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters