Skip to content

Commit

Permalink
Merge pull request #479 from splunk-terraform/extend_webhook_integration
Browse files Browse the repository at this point in the history
Extend Webhook integration with fields: method, payloadTemplate
  • Loading branch information
atoulme authored May 22, 2024
2 parents 6ce6d99 + 3690f6a commit f3bf0d7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0
github.com/mitchellh/go-homedir v1.1.0
github.com/signalfx/signalfx-go v1.34.0
github.com/signalfx/signalfx-go v1.35.0
github.com/stretchr/testify v1.8.2
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/signalfx/signalfx-go v1.34.0 h1:OQ6tyMY4efWB57EPIQqrpWrAfcSdyfa+bLtmAe7GLfE=
github.com/signalfx/signalfx-go v1.34.0/go.mod h1:IpGZLPvCKNFyspAXoS480jB02mocTpo0KYd8jbl6/T8=
github.com/signalfx/signalfx-go v1.35.0 h1:/IA4XuvxUOulipT71oawyRInk08JTdHPlA4ETZ20Ae4=
github.com/signalfx/signalfx-go v1.35.0/go.mod h1:IpGZLPvCKNFyspAXoS480jB02mocTpo0KYd8jbl6/T8=
github.com/skeema/knownhosts v1.2.0 h1:h9r9cf0+u7wSE+M183ZtMGgOJKiL96brpaz5ekfJCpM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
26 changes: 22 additions & 4 deletions signalfx/resource_signalfx_webhook_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func integrationWebhookResource() *schema.Resource {
},
},
},
"method": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "HTTP method used for the webhook request, such as 'GET', 'POST' and 'PUT'",
},
"payload_template": {
Type: schema.TypeString,
Optional: true,
Description: "Template for the payload to be sent with the webhook request in JSON format",
},
},

Create: integrationWebhookCreate,
Expand Down Expand Up @@ -81,10 +91,12 @@ func integrationWebhookExists(d *schema.ResourceData, meta interface{}) (bool, e

func getWebhookPayloadIntegration(d *schema.ResourceData) *integration.WebhookIntegration {
webhook := &integration.WebhookIntegration{
Type: "Webhook",
Name: d.Get("name").(string),
Enabled: d.Get("enabled").(bool),
Url: d.Get("url").(string),
Type: "Webhook",
Name: d.Get("name").(string),
Enabled: d.Get("enabled").(bool),
Url: d.Get("url").(string),
Method: d.Get("method").(string),
PayloadTemplate: d.Get("payload_template").(string),
}

if val, ok := d.GetOk("shared_secret"); ok {
Expand Down Expand Up @@ -133,6 +145,12 @@ func webhookIntegrationAPIToTF(d *schema.ResourceData, og *integration.WebhookIn
if err := d.Set("shared_secret", og.SharedSecret); err != nil {
return err
}
if err := d.Set("method", og.Method); err != nil {
return err
}
if err := d.Set("payload_template", og.PayloadTemplate); err != nil {
return err
}
if len(og.Headers) > 0 {
headers := make([]map[string]interface{}, len(og.Headers))
count := 0
Expand Down
35 changes: 28 additions & 7 deletions signalfx/resource_signalfx_webhook_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

const payloadTemplate = `{
"incidentId": "{{{incidentId}}}"
}
`

const newIntegrationWebhookConfig = `
resource "signalfx_webhook_integration" "webhook_myteamXX" {
name = "Webhook - My Team"
enabled = true
url = "https://www.example.com"
enabled = false
url = "https://www.example.com/v1"
method = "GET"
payload_template = <<-EOF
%s EOF
headers {
header_key = "foo"
Expand All @@ -26,11 +34,14 @@ const updatedIntegrationWebhookConfig = `
resource "signalfx_webhook_integration" "webhook_myteamXX" {
name = "Webhook - My Team NEW"
enabled = true
url = "https://www.example.com"
url = "https://www.example.com/v2"
method = "POST"
payload_template = <<-EOF
%s EOF
headers {
header_key = "foo"
header_value = "bar"
header_value = "foobar"
}
}
`
Expand All @@ -43,11 +54,16 @@ func TestAccCreateUpdateIntegrationWebhook(t *testing.T) {
Steps: []resource.TestStep{
// Create It
{
Config: newIntegrationWebhookConfig,
Config: fmt.Sprintf(newIntegrationWebhookConfig, payloadTemplate),
Check: resource.ComposeTestCheckFunc(
testAccCheckIntegrationWebhookResourceExists,
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "name", "Webhook - My Team"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "enabled", "true"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "enabled", "false"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "url", "https://www.example.com/v1"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "method", "GET"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "payload_template", payloadTemplate),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "headers.0.header_key", "foo"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "headers.0.header_value", "bar"),
),
},
{
Expand All @@ -58,11 +74,16 @@ func TestAccCreateUpdateIntegrationWebhook(t *testing.T) {
},
// Update It
{
Config: updatedIntegrationWebhookConfig,
Config: fmt.Sprintf(updatedIntegrationWebhookConfig, payloadTemplate),
Check: resource.ComposeTestCheckFunc(
testAccCheckIntegrationWebhookResourceExists,
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "name", "Webhook - My Team NEW"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "enabled", "true"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "url", "https://www.example.com/v2"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "method", "POST"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "payload_template", payloadTemplate),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "headers.0.header_key", "foo"),
resource.TestCheckResourceAttr("signalfx_webhook_integration.webhook_myteamXX", "headers.0.header_value", "foobar"),
),
},
},
Expand Down
16 changes: 12 additions & 4 deletions website/docs/r/webhook_integration.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ Splunk Observability Cloud webhook integration.

```tf
resource "signalfx_webhook_integration" "webhook_myteam" {
name = "Webhook - My Team"
enabled = true
url = "https://www.example.com"
shared_secret = "abc1234"
name = "Webhook - My Team"
enabled = true
url = "https://www.example.com"
shared_secret = "abc1234"
method = "POST"
payload_template = <<-EOF
{
"incidentId": "{{{incidentId}}}"
}
EOF
headers {
header_key = "some_header"
Expand All @@ -34,6 +40,8 @@ resource "signalfx_webhook_integration" "webhook_myteam" {
* `enabled` - (Required) Whether the integration is enabled.
* `url` - (Required) The URL to request
* `shared_secret` - (Optional)
* `method` - (Optional) HTTP method used for the webhook request, such as 'GET', 'POST' and 'PUT'
* `payload_template` - (Optional) Template for the payload to be sent with the webhook request in JSON format
* `headers` - (Optional) A header to send with the request
* `header_key` - (Required) The key of the header to send
* `header_value` - (Required) The value of the header to send
Expand Down

0 comments on commit f3bf0d7

Please sign in to comment.