Skip to content

Commit

Permalink
Adding support for webhook notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
CB Hoffman committed Jan 12, 2024
1 parent 6ecbac5 commit 94e92d3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/resources/alert_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Optional:

- `email` (Block Set) The email notification. (see [below for nested schema](#nestedblock--notifications--email))
- `third_party` (Block Set) Third party notification. (see [below for nested schema](#nestedblock--notifications--third_party))
- `webhook` (Block Set) Webhook notification. (see [below for nested schema](#nestedblock--notifications--webhook))

<a id="nestedblock--notifications--email"></a>
### Nested Schema for `notifications.email`
Expand All @@ -77,4 +78,10 @@ Required:
- `integration_id` (String) The integration ID, as a string.
- `integration_type` (String) The integration type, as a string.

<a id="nestedblock--notifications--webhook"></a>
### Nested Schema for `notifications.webhook`

Required:

- `integration_id` (String) The integration ID, as a string.
- `integration_type` (String) The integration type, as a string.
19 changes: 19 additions & 0 deletions thousandeyes/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,25 @@ var schemas = map[string]*schema.Schema{
},
},
},
"webhook": {
Type: schema.TypeSet,
Description: "Webhook notification.",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"integration_id": {
Type: schema.TypeString,
Description: "The integration ID, as a string.",
Required: true,
},
"integration_type": {
Type: schema.TypeString,
Description: "The integration type, as a string.",
Required: true,
},
},
},
},
},
},
},
Expand Down
33 changes: 29 additions & 4 deletions thousandeyes/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,33 @@ func FixReadValues(m interface{}, name string) (interface{}, error) {
} else {
tp = nil
}

// webhook notifications
var w interface{}
if _, ok := m.(map[string]interface{})["webhook"]; ok {
w, err = FixReadValues(m.(map[string]interface{})["webhook"].([]interface{}), "webhook")
if err != nil {
return nil, err
}
} else {
w = nil
}

// update the notifications block if the email block is present and contains recipients, or
// the third party notifications are present. Otherwise set the whole notifications block to nil
if e == nil && tp == nil {
// the third party notifications are present, or webhook notifications are present.
// Otherwise set the whole notifications block to nil
if e == nil && tp == nil && w == nil {
m = nil
} else {
// Add the third party map to the notifications map if they are present
// Add the third party map and or webhook map to the notifications map if they are present
// if they're not configured, then the API doesn't return them at all
if tp != nil {
m.(map[string]interface{})["third_party"] = tp
}
}

if w != nil {
m.(map[string]interface{})["webhook"] = w
}

m.(map[string]interface{})["email"] = e
m = []interface{}{
Expand Down Expand Up @@ -317,6 +333,15 @@ func FixReadValues(m interface{}, name string) (interface{}, error) {
}
}

case "webhook":
for i, v := range m.([]interface{}) {
webhookNotifications := v.(map[string]interface{})
m.([]interface{})[i] = map[string]interface{}{
"integration_id": webhookNotifications["integration_id"],
"integration_type": webhookNotifications["integration_type"],
}
}

case "tests":
for i, v := range m.([]interface{}) {
test := v.(map[string]interface{})
Expand Down
24 changes: 24 additions & 0 deletions thousandeyes/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,30 @@ func TestFixReadValues(t *testing.T) {
if reflect.DeepEqual(output, thirdPartyNotificationsTarget) != true {
t.Errorf("Values not stripped correctly from third party notifications input: Received %#v Expected %#v", output, thirdPartyNotificationsTarget)
}

// webhook notifications
webhookNotificationsInput := []interface{}{
map[string]interface{}{
"integration_id": "wb-0000",
"integration_type": "WEBHOOK",
"integration_name": "TEAMS CHANNEL",
"target": "https://webhook.office.com",
},
}
webhookNotificationsTarget := []interface{}{
map[string]interface{}{
"integration_id": "wb-0000",
"integration_type": "WEBHOOK",
},
}

output, err = FixReadValues(webhookNotificationsInput, "webhook")
if err != nil {
t.Errorf("webhook notifications input returned error: %s", err.Error())
}
if reflect.DeepEqual(output, webhookNotificationsTarget) != true {
t.Errorf("Values not stripped correctly from webhook notifications input: Received %#v Expected %#v", output, webhookNotificationsTarget)
}
}

func TestResourceUpdate(t *testing.T) {
Expand Down

0 comments on commit 94e92d3

Please sign in to comment.