Skip to content

Commit

Permalink
Merge pull request #57 from netdata/feat/add-notification-repeat
Browse files Browse the repository at this point in the history
feat: add notification repeat option
  • Loading branch information
witalisoft authored Jan 2, 2025
2 parents 1987cd7 + 7f7c219 commit e5e9294
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 164 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.2.2

FEATURES:

- option to specify notification repeat interval `repeat_notification_min` for the paid notification channels

## 0.2.1

BUGFIXES:
Expand Down
14 changes: 8 additions & 6 deletions docs/resources/notification_discord_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Resource for managing centralized notifications for Discord. Available only in p
resource "netdata_notification_discord_channel" "test" {
name = "discord notifications"
enabled = true
space_id = "<space_id>"
rooms_id = ["<room_id>"]
webhook_url = "https://discord.com/api/webhooks/0000000000000/XXXXXXXXXXXXXXXXXXXXXXXX"
alarms = "ALARMS_SETTING_ALL"
channel_type = "text"
enabled = true
space_id = "<space_id>"
rooms_id = ["<room_id>"]
repeat_notification_min = 30
webhook_url = "https://discord.com/api/webhooks/0000000000000/XXXXXXXXXXXXXXXXXXXXXXXX"
alarms = "ALARMS_SETTING_ALL"
channel_type = "text"
}
```

Expand All @@ -40,6 +41,7 @@ resource "netdata_notification_discord_channel" "test" {
### Optional

- `channel_thread` (String) Discord channel thread name required if channel type is `forum`
- `repeat_notification_min` (Number) The time interval for the Discord notification to be repeated. The interval is presented in minutes and should be between 30 and 1440, or 0 to avoid repetition, which is the default.
- `rooms_id` (List of String) The list of room IDs to set the Discord notification. If the rooms list is null, the Discord notification will be applied to `All rooms`

### Read-Only
Expand Down
14 changes: 8 additions & 6 deletions docs/resources/notification_pagerduty_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Resource for managing centralized notifications for Pagerduty. Available only in
resource "netdata_notification_pagerduty_channel" "test" {
name = "pagerduty notifications"
enabled = true
space_id = netdata_space.test.id
rooms_id = ["<room_id>"]
alarms = "ALARMS_SETTING_ALL"
alert_events_url = "https://events.pagerduty.com/v2/enqueue"
integration_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
enabled = true
space_id = netdata_space.test.id
rooms_id = ["<room_id>"]
alarms = "ALARMS_SETTING_ALL"
repeat_notification_min = 30
alert_events_url = "https://events.pagerduty.com/v2/enqueue"
integration_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
```

Expand All @@ -39,6 +40,7 @@ resource "netdata_notification_pagerduty_channel" "test" {

### Optional

- `repeat_notification_min` (Number) The time interval for the Pagerduty notification to be repeated. The interval is presented in minutes and should be between 30 and 1440, or 0 to avoid repetition, which is the default.
- `rooms_id` (List of String) The list of room IDs to set the Pagerduty notification. If the rooms list is null, the Pagerduty notification will be applied to `All rooms`

### Read-Only
Expand Down
12 changes: 7 additions & 5 deletions docs/resources/notification_slack_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ Resource for managing centralized notifications for Slack. Available only in pai
resource "netdata_notification_slack_channel" "test" {
name = "slack notifications"
enabled = true
space_id = "<space_id>"
rooms_id = ["<room_id>"]
webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
alarms = "ALARMS_SETTING_ALL"
enabled = true
space_id = "<space_id>"
rooms_id = ["<room_id>"]
repeat_notification_min = 30
webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
alarms = "ALARMS_SETTING_ALL"
}
```

Expand All @@ -37,6 +38,7 @@ resource "netdata_notification_slack_channel" "test" {

### Optional

- `repeat_notification_min` (Number) The time interval for the Slack notification to be repeated. The interval is presented in minutes and should be between 30 and 1440, or 0 to avoid repetition, which is the default.
- `rooms_id` (List of String) The list of room IDs to set the Slack notification. If the rooms list is null, the Slack notification will be applied to `All rooms`

### Read-Only
Expand Down
11 changes: 6 additions & 5 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ resource "netdata_node_room_member" "test" {
resource "netdata_notification_slack_channel" "test" {
name = "slack"

enabled = true
space_id = netdata_space.test.id
rooms_id = [netdata_room.test.id]
alarms = "ALARMS_SETTING_ALL"
webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
enabled = true
space_id = netdata_space.test.id
rooms_id = [netdata_room.test.id]
alarms = "ALARMS_SETTING_ALL"
repeat_notification_min = 60
webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
}

resource "netdata_notification_discord_channel" "test" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
resource "netdata_notification_discord_channel" "test" {
name = "discord notifications"

enabled = true
space_id = "<space_id>"
rooms_id = ["<room_id>"]
webhook_url = "https://discord.com/api/webhooks/0000000000000/XXXXXXXXXXXXXXXXXXXXXXXX"
alarms = "ALARMS_SETTING_ALL"
channel_type = "text"
enabled = true
space_id = "<space_id>"
rooms_id = ["<room_id>"]
repeat_notification_min = 30
webhook_url = "https://discord.com/api/webhooks/0000000000000/XXXXXXXXXXXXXXXXXXXXXXXX"
alarms = "ALARMS_SETTING_ALL"
channel_type = "text"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
resource "netdata_notification_pagerduty_channel" "test" {
name = "pagerduty notifications"

enabled = true
space_id = netdata_space.test.id
rooms_id = ["<room_id>"]
alarms = "ALARMS_SETTING_ALL"
alert_events_url = "https://events.pagerduty.com/v2/enqueue"
integration_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
enabled = true
space_id = netdata_space.test.id
rooms_id = ["<room_id>"]
alarms = "ALARMS_SETTING_ALL"
repeat_notification_min = 30
alert_events_url = "https://events.pagerduty.com/v2/enqueue"
integration_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
resource "netdata_notification_slack_channel" "test" {
name = "slack notifications"

enabled = true
space_id = "<space_id>"
rooms_id = ["<room_id>"]
webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
alarms = "ALARMS_SETTING_ALL"
enabled = true
space_id = "<space_id>"
rooms_id = ["<room_id>"]
repeat_notification_min = 30
webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
alarms = "ALARMS_SETTING_ALL"
}
26 changes: 14 additions & 12 deletions internal/client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ type NotificationIntegrations struct {
}

type NotificationChannel struct {
ID string `json:"id"`
Enabled bool `json:"enabled"`
Name string `json:"name"`
Integration NotificationIntegration `json:"integration"`
Alarms string `json:"alarms"`
Rooms []string `json:"rooms"`
Secrets json.RawMessage `json:"secrets"`
ID string `json:"id"`
Enabled bool `json:"enabled"`
Name string `json:"name"`
Integration NotificationIntegration `json:"integration"`
Alarms string `json:"alarms"`
Rooms []string `json:"rooms"`
Secrets json.RawMessage `json:"secrets"`
RepeatNotificationMinute int64 `json:"repeat_notification_min,omitempty"`
}

type NotificationIntegration struct {
Expand All @@ -61,11 +62,12 @@ type NotificationPagerdutyChannel struct {
}

type notificationRequestPayload struct {
Name string `json:"name"`
IntegrationID string `json:"integrationID"`
Alarms string `json:"alarms"`
Rooms []string `json:"rooms"`
Secrets json.RawMessage `json:"secrets"`
Name string `json:"name"`
IntegrationID string `json:"integrationID"`
Alarms string `json:"alarms"`
Rooms []string `json:"rooms"`
Secrets json.RawMessage `json:"secrets"`
RepeatNotificationMinute int64 `json:"repeat_notification_min,omitempty"`
}

type Invitation struct {
Expand Down
16 changes: 9 additions & 7 deletions internal/client/notification_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ func (c *Client) CreateDiscordChannel(spaceID string, commonParams NotificationC
}

reqBody := notificationRequestPayload{
Name: commonParams.Name,
IntegrationID: commonParams.Integration.ID,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
Name: commonParams.Name,
IntegrationID: commonParams.Integration.ID,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
RepeatNotificationMinute: commonParams.RepeatNotificationMinute,
}

secretsJson, err := json.Marshal(discordParams)
Expand Down Expand Up @@ -70,9 +71,10 @@ func (c *Client) UpdateDiscordChannelByID(spaceID string, commonParams Notificat
}

reqBody := notificationRequestPayload{
Name: commonParams.Name,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
Name: commonParams.Name,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
RepeatNotificationMinute: commonParams.RepeatNotificationMinute,
}

secretsJson, err := json.Marshal(discordParams)
Expand Down
16 changes: 9 additions & 7 deletions internal/client/notification_pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ func (c *Client) CreatePagerdutyChannel(spaceID string, commonParams Notificatio
}

reqBody := notificationRequestPayload{
Name: commonParams.Name,
IntegrationID: commonParams.Integration.ID,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
Name: commonParams.Name,
IntegrationID: commonParams.Integration.ID,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
RepeatNotificationMinute: commonParams.RepeatNotificationMinute,
}

secretsJson, err := json.Marshal(pagerdutyParams)
Expand Down Expand Up @@ -69,9 +70,10 @@ func (c *Client) UpdatePagerdutyChannelByID(spaceID string, commonParams Notific
}

reqBody := notificationRequestPayload{
Name: commonParams.Name,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
Name: commonParams.Name,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
RepeatNotificationMinute: commonParams.RepeatNotificationMinute,
}

secretsJson, err := json.Marshal(pagerdutyParams)
Expand Down
16 changes: 9 additions & 7 deletions internal/client/notification_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ func (c *Client) CreateSlackChannel(spaceID string, commonParams NotificationCha
}

reqBody := notificationRequestPayload{
Name: commonParams.Name,
IntegrationID: commonParams.Integration.ID,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
Name: commonParams.Name,
IntegrationID: commonParams.Integration.ID,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
RepeatNotificationMinute: commonParams.RepeatNotificationMinute,
}

secretsJson, err := json.Marshal(slackParams)
Expand Down Expand Up @@ -68,9 +69,10 @@ func (c *Client) UpdateSlackChannelByID(spaceID string, commonParams Notificatio
}

reqBody := notificationRequestPayload{
Name: commonParams.Name,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
Name: commonParams.Name,
Rooms: commonParams.Rooms,
Alarms: commonParams.Alarms,
RepeatNotificationMinute: commonParams.RepeatNotificationMinute,
}
secretsJson, err := json.Marshal(slackParams)
if err != nil {
Expand Down
44 changes: 25 additions & 19 deletions internal/provider/notification_discord_channel_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ type discordChannelResource struct {
}

type discordChannelResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
SpaceID types.String `tfsdk:"space_id"`
RoomsID types.List `tfsdk:"rooms_id"`
Alarms types.String `tfsdk:"alarms"`
WebhookURL types.String `tfsdk:"webhook_url"`
ChannelType types.String `tfsdk:"channel_type"`
ChannelThread types.String `tfsdk:"channel_thread"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
SpaceID types.String `tfsdk:"space_id"`
RoomsID types.List `tfsdk:"rooms_id"`
Alarms types.String `tfsdk:"alarms"`
RepeatNotificationMinute types.Int64 `tfsdk:"repeat_notification_min"`
WebhookURL types.String `tfsdk:"webhook_url"`
ChannelType types.String `tfsdk:"channel_type"`
ChannelThread types.String `tfsdk:"channel_thread"`
}

func (s *discordChannelResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -118,11 +119,12 @@ func (s *discordChannelResource) Create(ctx context.Context, req resource.Create
plan.RoomsID.ElementsAs(ctx, &roomsID, false)

commonParams := client.NotificationChannel{
Name: plan.Name.ValueString(),
Integration: *notificationIntegration,
Rooms: roomsID,
Alarms: plan.Alarms.ValueString(),
Enabled: plan.Enabled.ValueBool(),
Name: plan.Name.ValueString(),
Integration: *notificationIntegration,
Rooms: roomsID,
Alarms: plan.Alarms.ValueString(),
Enabled: plan.Enabled.ValueBool(),
RepeatNotificationMinute: plan.RepeatNotificationMinute.ValueInt64(),
}

discordParams := client.NotificationDiscordChannel{
Expand Down Expand Up @@ -151,6 +153,7 @@ func (s *discordChannelResource) Create(ctx context.Context, req resource.Create
plan.Enabled = types.BoolValue(notificationChannel.Enabled)
plan.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms)
plan.Alarms = types.StringValue(notificationChannel.Alarms)
plan.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute)

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -194,6 +197,7 @@ func (s *discordChannelResource) Read(ctx context.Context, req resource.ReadRequ
state.Enabled = types.BoolValue(notificationChannel.Enabled)
state.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms)
state.Alarms = types.StringValue(notificationChannel.Alarms)
state.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute)
state.WebhookURL = types.StringValue(notificationSecrets.URL)
state.ChannelType = types.StringValue(notificationSecrets.ChannelParams.Selection)
if notificationSecrets.ChannelParams.Selection == "forum" {
Expand Down Expand Up @@ -228,11 +232,12 @@ func (s *discordChannelResource) Update(ctx context.Context, req resource.Update
plan.RoomsID.ElementsAs(ctx, &roomsID, false)

commonParams := client.NotificationChannel{
ID: plan.ID.ValueString(),
Name: plan.Name.ValueString(),
Rooms: roomsID,
Alarms: plan.Alarms.ValueString(),
Enabled: plan.Enabled.ValueBool(),
ID: plan.ID.ValueString(),
Name: plan.Name.ValueString(),
Rooms: roomsID,
Alarms: plan.Alarms.ValueString(),
Enabled: plan.Enabled.ValueBool(),
RepeatNotificationMinute: plan.RepeatNotificationMinute.ValueInt64(),
}

discordParams := client.NotificationDiscordChannel{
Expand Down Expand Up @@ -261,6 +266,7 @@ func (s *discordChannelResource) Update(ctx context.Context, req resource.Update
plan.Enabled = types.BoolValue(notificationChannel.Enabled)
plan.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms)
plan.Alarms = types.StringValue(notificationChannel.Alarms)
plan.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute)

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
Expand Down
Loading

0 comments on commit e5e9294

Please sign in to comment.