From 557bf441b4d864ed87579754b16dbe61c7bdece9 Mon Sep 17 00:00:00 2001 From: Witold Duranek Date: Thu, 6 Feb 2025 15:35:36 +0100 Subject: [PATCH] fix: rename attribute for notification channels --- CHANGELOG.md | 8 ++++ docs/resources/node_room_member.md | 2 +- .../resources/notification_discord_channel.md | 2 +- .../notification_pagerduty_channel.md | 2 +- docs/resources/notification_slack_channel.md | 2 +- internal/client/models.go | 4 +- internal/client/notification_discord.go | 4 +- internal/client/notification_pagerduty.go | 4 +- internal/client/notification_slack.go | 4 +- internal/provider/node_room_member.go | 2 +- .../notification_discord_channel_resource.go | 18 ++++--- ...ification_discord_channel_resource_test.go | 17 ++++--- ...notification_pagerduty_channel_resource.go | 18 ++++--- ...ication_pagerduty_channel_resource_test.go | 17 ++++--- .../notification_slack_channel_resource.go | 18 ++++--- ...otification_slack_channel_resource_test.go | 47 ++++++++++--------- internal/provider/notifications.go | 15 +++--- 17 files changed, 112 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed08e0b..759b736 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.4.0 + +BREAKING CHANGES: + +- resource/netdata_notification_discord_channel: renamed attribute from `alarms` to `notifications` with new values +- resource/netdata_notification_pagerduty_channel: renamed attribute from `alarms` to `notifications` with new values +- resource/netdata_notification_slack_channel: renamed attribute from `alarms` to `notifications` with new values + ## 0.3.0 FEATURES: diff --git a/docs/resources/node_room_member.md b/docs/resources/node_room_member.md index 23f7643..d91ed8c 100644 --- a/docs/resources/node_room_member.md +++ b/docs/resources/node_room_member.md @@ -62,7 +62,7 @@ resource "netdata_node_room_member" "test" { Required: -- `action` (String) Determines whether matching nodes will be included or excluded from the room. EXCLUDE action always takes precedence against INCLUDE. Valid values: INCLUDE or EXCLUDE. +- `action` (String) Determines whether matching nodes will be included or excluded from the room. Valid values: INCLUDE or EXCLUDE. EXCLUDE action always takes precedence against INCLUDE. Optional: diff --git a/docs/resources/notification_discord_channel.md b/docs/resources/notification_discord_channel.md index c22a216..1295b7a 100644 --- a/docs/resources/notification_discord_channel.md +++ b/docs/resources/notification_discord_channel.md @@ -31,10 +31,10 @@ resource "netdata_notification_discord_channel" "test" { ### Required -- `alarms` (String) The alarms setting to set the Discord notification. Valid values are: `ALARMS_SETTING_ALL`, `ALARMS_SETTING_CRITICAL`, `ALARMS_SETTING_ALL_BUT_UNREACHABLE`, `ALARMS_SETTING_UNREACHABLE` - `channel_type` (String) Discord channel type. Valid values are: `text`, `forum` - `enabled` (Boolean) The enabled status of the Discord notification - `name` (String) The name of the Discord notification +- `notifications` (List of String) The notification options for the Discord. Valid values are: `CRITICAL`, `WARNING`, `CLEAR`, `REACHABLE`, `UNREACHABLE` - `space_id` (String) The ID of the space for the Discord notification - `webhook_url` (String, Sensitive) Discord webhook URL diff --git a/docs/resources/notification_pagerduty_channel.md b/docs/resources/notification_pagerduty_channel.md index 47e9043..6d7e6ef 100644 --- a/docs/resources/notification_pagerduty_channel.md +++ b/docs/resources/notification_pagerduty_channel.md @@ -31,11 +31,11 @@ resource "netdata_notification_pagerduty_channel" "test" { ### Required -- `alarms` (String) The alarms setting to set the Pagerduty notification. Valid values are: `ALARMS_SETTING_ALL`, `ALARMS_SETTING_CRITICAL`, `ALARMS_SETTING_ALL_BUT_UNREACHABLE`, `ALARMS_SETTING_UNREACHABLE` - `alert_events_url` (String) URL for alert events - `enabled` (Boolean) The enabled status of the Pagerduty notification - `integration_key` (String, Sensitive) Integration key - `name` (String) The name of the Pagerduty notification +- `notifications` (List of String) The notification options for the Pagerduty. Valid values are: `CRITICAL`, `WARNING`, `CLEAR`, `REACHABLE`, `UNREACHABLE` - `space_id` (String) The ID of the space for the Pagerduty notification ### Optional diff --git a/docs/resources/notification_slack_channel.md b/docs/resources/notification_slack_channel.md index bc9aeb2..6a9e30e 100644 --- a/docs/resources/notification_slack_channel.md +++ b/docs/resources/notification_slack_channel.md @@ -30,9 +30,9 @@ resource "netdata_notification_slack_channel" "test" { ### Required -- `alarms` (String) The alarms setting to set the Slack notification. Valid values are: `ALARMS_SETTING_ALL`, `ALARMS_SETTING_CRITICAL`, `ALARMS_SETTING_ALL_BUT_UNREACHABLE`, `ALARMS_SETTING_UNREACHABLE` - `enabled` (Boolean) The enabled status of the Slack notification - `name` (String) The name of the Slack notification +- `notifications` (List of String) The notification options for the Slack. Valid values are: `CRITICAL`, `WARNING`, `CLEAR`, `REACHABLE`, `UNREACHABLE` - `space_id` (String) The ID of the space for the Slack notification - `webhook_url` (String, Sensitive) Slack webhook URL diff --git a/internal/client/models.go b/internal/client/models.go index 69210f4..6d18871 100644 --- a/internal/client/models.go +++ b/internal/client/models.go @@ -37,7 +37,7 @@ type NotificationChannel struct { Enabled bool `json:"enabled"` Name string `json:"name"` Integration NotificationIntegration `json:"integration"` - Alarms string `json:"alarms"` + NotificationOptions []string `json:"notification_options"` Rooms []string `json:"rooms"` Secrets json.RawMessage `json:"secrets"` RepeatNotificationMinute int64 `json:"repeat_notification_min,omitempty"` @@ -68,7 +68,7 @@ type NotificationPagerdutyChannel struct { type notificationRequestPayload struct { Name string `json:"name"` IntegrationID string `json:"integrationID"` - Alarms string `json:"alarms"` + NotificationOptions []string `json:"notification_options"` Rooms []string `json:"rooms"` Secrets json.RawMessage `json:"secrets"` RepeatNotificationMinute int64 `json:"repeat_notification_min,omitempty"` diff --git a/internal/client/notification_discord.go b/internal/client/notification_discord.go index e48bcc8..ee99e0d 100644 --- a/internal/client/notification_discord.go +++ b/internal/client/notification_discord.go @@ -17,7 +17,7 @@ func (c *Client) CreateDiscordChannel(spaceID string, commonParams NotificationC Name: commonParams.Name, IntegrationID: commonParams.Integration.ID, Rooms: commonParams.Rooms, - Alarms: commonParams.Alarms, + NotificationOptions: commonParams.NotificationOptions, RepeatNotificationMinute: commonParams.RepeatNotificationMinute, } @@ -73,7 +73,7 @@ func (c *Client) UpdateDiscordChannelByID(spaceID string, commonParams Notificat reqBody := notificationRequestPayload{ Name: commonParams.Name, Rooms: commonParams.Rooms, - Alarms: commonParams.Alarms, + NotificationOptions: commonParams.NotificationOptions, RepeatNotificationMinute: commonParams.RepeatNotificationMinute, } diff --git a/internal/client/notification_pagerduty.go b/internal/client/notification_pagerduty.go index 08f54c1..de176b8 100644 --- a/internal/client/notification_pagerduty.go +++ b/internal/client/notification_pagerduty.go @@ -17,7 +17,7 @@ func (c *Client) CreatePagerdutyChannel(spaceID string, commonParams Notificatio Name: commonParams.Name, IntegrationID: commonParams.Integration.ID, Rooms: commonParams.Rooms, - Alarms: commonParams.Alarms, + NotificationOptions: commonParams.NotificationOptions, RepeatNotificationMinute: commonParams.RepeatNotificationMinute, } @@ -72,7 +72,7 @@ func (c *Client) UpdatePagerdutyChannelByID(spaceID string, commonParams Notific reqBody := notificationRequestPayload{ Name: commonParams.Name, Rooms: commonParams.Rooms, - Alarms: commonParams.Alarms, + NotificationOptions: commonParams.NotificationOptions, RepeatNotificationMinute: commonParams.RepeatNotificationMinute, } diff --git a/internal/client/notification_slack.go b/internal/client/notification_slack.go index 392b4f9..800fccc 100644 --- a/internal/client/notification_slack.go +++ b/internal/client/notification_slack.go @@ -17,7 +17,7 @@ func (c *Client) CreateSlackChannel(spaceID string, commonParams NotificationCha Name: commonParams.Name, IntegrationID: commonParams.Integration.ID, Rooms: commonParams.Rooms, - Alarms: commonParams.Alarms, + NotificationOptions: commonParams.NotificationOptions, RepeatNotificationMinute: commonParams.RepeatNotificationMinute, } @@ -71,7 +71,7 @@ func (c *Client) UpdateSlackChannelByID(spaceID string, commonParams Notificatio reqBody := notificationRequestPayload{ Name: commonParams.Name, Rooms: commonParams.Rooms, - Alarms: commonParams.Alarms, + NotificationOptions: commonParams.NotificationOptions, RepeatNotificationMinute: commonParams.RepeatNotificationMinute, } secretsJson, err := json.Marshal(slackParams) diff --git a/internal/provider/node_room_member.go b/internal/provider/node_room_member.go index 01fc983..4ede573 100644 --- a/internal/provider/node_room_member.go +++ b/internal/provider/node_room_member.go @@ -103,7 +103,7 @@ There are two options to add nodes to the room: }, }, "action": schema.StringAttribute{ - Description: "Determines whether matching nodes will be included or excluded from the room. EXCLUDE action always takes precedence against INCLUDE. Valid values: INCLUDE or EXCLUDE.", + Description: "Determines whether matching nodes will be included or excluded from the room. Valid values: INCLUDE or EXCLUDE. EXCLUDE action always takes precedence against INCLUDE.", Required: true, Validators: []validator.String{ stringvalidator.OneOf([]string{"INCLUDE", "EXCLUDE"}...), diff --git a/internal/provider/notification_discord_channel_resource.go b/internal/provider/notification_discord_channel_resource.go index 98ebc0d..72fe9de 100644 --- a/internal/provider/notification_discord_channel_resource.go +++ b/internal/provider/notification_discord_channel_resource.go @@ -36,7 +36,7 @@ type discordChannelResourceModel struct { Enabled types.Bool `tfsdk:"enabled"` SpaceID types.String `tfsdk:"space_id"` RoomsID types.List `tfsdk:"rooms_id"` - Alarms types.String `tfsdk:"alarms"` + NotificationOptions types.List `tfsdk:"notifications"` RepeatNotificationMinute types.Int64 `tfsdk:"repeat_notification_min"` WebhookURL types.String `tfsdk:"webhook_url"` ChannelType types.String `tfsdk:"channel_type"` @@ -118,11 +118,14 @@ func (s *discordChannelResource) Create(ctx context.Context, req resource.Create var roomsID []string plan.RoomsID.ElementsAs(ctx, &roomsID, false) + var notificationOptions []string + plan.NotificationOptions.ElementsAs(ctx, ¬ificationOptions, false) + commonParams := client.NotificationChannel{ Name: plan.Name.ValueString(), Integration: *notificationIntegration, Rooms: roomsID, - Alarms: plan.Alarms.ValueString(), + NotificationOptions: notificationOptions, Enabled: plan.Enabled.ValueBool(), RepeatNotificationMinute: plan.RepeatNotificationMinute.ValueInt64(), } @@ -152,7 +155,7 @@ func (s *discordChannelResource) Create(ctx context.Context, req resource.Create plan.Name = types.StringValue(notificationChannel.Name) plan.Enabled = types.BoolValue(notificationChannel.Enabled) plan.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - plan.Alarms = types.StringValue(notificationChannel.Alarms) + plan.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) plan.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) diags = resp.State.Set(ctx, plan) @@ -196,7 +199,7 @@ func (s *discordChannelResource) Read(ctx context.Context, req resource.ReadRequ state.Name = types.StringValue(notificationChannel.Name) state.Enabled = types.BoolValue(notificationChannel.Enabled) state.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - state.Alarms = types.StringValue(notificationChannel.Alarms) + state.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) state.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) state.WebhookURL = types.StringValue(notificationSecrets.URL) state.ChannelType = types.StringValue(notificationSecrets.ChannelParams.Selection) @@ -231,11 +234,14 @@ func (s *discordChannelResource) Update(ctx context.Context, req resource.Update var roomsID []string plan.RoomsID.ElementsAs(ctx, &roomsID, false) + var notificationOptions []string + plan.NotificationOptions.ElementsAs(ctx, ¬ificationOptions, false) + commonParams := client.NotificationChannel{ ID: plan.ID.ValueString(), Name: plan.Name.ValueString(), Rooms: roomsID, - Alarms: plan.Alarms.ValueString(), + NotificationOptions: notificationOptions, Enabled: plan.Enabled.ValueBool(), RepeatNotificationMinute: plan.RepeatNotificationMinute.ValueInt64(), } @@ -265,7 +271,7 @@ func (s *discordChannelResource) Update(ctx context.Context, req resource.Update plan.Name = types.StringValue(notificationChannel.Name) plan.Enabled = types.BoolValue(notificationChannel.Enabled) plan.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - plan.Alarms = types.StringValue(notificationChannel.Alarms) + plan.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) plan.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) diags = resp.State.Set(ctx, plan) diff --git a/internal/provider/notification_discord_channel_resource_test.go b/internal/provider/notification_discord_channel_resource_test.go index becc715..a3b3bf2 100644 --- a/internal/provider/notification_discord_channel_resource_test.go +++ b/internal/provider/notification_discord_channel_resource_test.go @@ -24,7 +24,7 @@ func TestAccDiscordNotificationResource(t *testing.T) { space_id = "%s" rooms_id = [netdata_room.test.id] webhook_url = "https://discord.com/api/webhooks/0000000000000/XXXXXXXXXXXXXXXXXXXXXXXX" - alarms = "ALARMS_SETTING_ALL" + notifications = ["CRITICAL","WARNING","CLEAR"] channel_type = "text" repeat_notification_min = 30 } @@ -35,7 +35,9 @@ func TestAccDiscordNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "enabled", "true"), resource.TestCheckResourceAttrSet("netdata_notification_discord_channel.test", "space_id"), resource.TestCheckResourceAttrSet("netdata_notification_discord_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "notifications.0", "CRITICAL"), + resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "notifications.1", "WARNING"), + resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "notifications.2", "CLEAR"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "repeat_notification_min", "30"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "webhook_url", "https://discord.com/api/webhooks/0000000000000/XXXXXXXXXXXXXXXXXXXXXXXX"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "channel_type", "text"), @@ -53,7 +55,7 @@ func TestAccDiscordNotificationResource(t *testing.T) { space_id = "%s" rooms_id = [netdata_room.test.id] webhook_url = "https://discord.com/api/webhooks/0000000000000/XXXXXXXXXXXXXXXXXXXXXXXX" - alarms = "ALARMS_SETTING_ALL" + notifications = ["CLEAR","CRITICAL"] channel_type = "text" repeat_notification_min = 60 } @@ -64,7 +66,8 @@ func TestAccDiscordNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "enabled", "true"), resource.TestCheckResourceAttrSet("netdata_notification_discord_channel.test", "space_id"), resource.TestCheckResourceAttrSet("netdata_notification_discord_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "notifications.0", "CLEAR"), + resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "notifications.1", "CRITICAL"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "repeat_notification_min", "60"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "webhook_url", "https://discord.com/api/webhooks/0000000000000/XXXXXXXXXXXXXXXXXXXXXXXX"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "channel_type", "text"), @@ -82,7 +85,7 @@ func TestAccDiscordNotificationResource(t *testing.T) { space_id = "%s" rooms_id = null webhook_url = "https://discord.com/api/webhooks/1000000000000/XXXXXXXXXXXXXXXXXXXXXXXX" - alarms = "ALARMS_SETTING_ALL" + notifications = ["CRITICAL","WARNING","CLEAR"] channel_type = "forum" channel_thread = "thread" } @@ -93,7 +96,9 @@ func TestAccDiscordNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "enabled", "false"), resource.TestCheckResourceAttrSet("netdata_notification_discord_channel.test", "space_id"), resource.TestCheckNoResourceAttr("netdata_notification_discord_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "notifications.0", "CRITICAL"), + resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "notifications.1", "WARNING"), + resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "notifications.2", "CLEAR"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "repeat_notification_min", "0"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "webhook_url", "https://discord.com/api/webhooks/1000000000000/XXXXXXXXXXXXXXXXXXXXXXXX"), resource.TestCheckResourceAttr("netdata_notification_discord_channel.test", "channel_type", "forum"), diff --git a/internal/provider/notification_pagerduty_channel_resource.go b/internal/provider/notification_pagerduty_channel_resource.go index c597731..86f1c10 100644 --- a/internal/provider/notification_pagerduty_channel_resource.go +++ b/internal/provider/notification_pagerduty_channel_resource.go @@ -33,7 +33,7 @@ type pagerdutyChannelResourceModel struct { Enabled types.Bool `tfsdk:"enabled"` SpaceID types.String `tfsdk:"space_id"` RoomsID types.List `tfsdk:"rooms_id"` - Alarms types.String `tfsdk:"alarms"` + NotificationOptions types.List `tfsdk:"notifications"` RepeatNotificationMinute types.Int64 `tfsdk:"repeat_notification_min"` AlertEventsURL types.String `tfsdk:"alert_events_url"` IntegrationKey types.String `tfsdk:"integration_key"` @@ -97,11 +97,14 @@ func (s *pagerdutyChannelResource) Create(ctx context.Context, req resource.Crea var roomsID []string plan.RoomsID.ElementsAs(ctx, &roomsID, false) + var notificationOptions []string + plan.NotificationOptions.ElementsAs(ctx, ¬ificationOptions, false) + commonParams := client.NotificationChannel{ Name: plan.Name.ValueString(), Integration: *notificationIntegration, Rooms: roomsID, - Alarms: plan.Alarms.ValueString(), + NotificationOptions: notificationOptions, Enabled: plan.Enabled.ValueBool(), RepeatNotificationMinute: plan.RepeatNotificationMinute.ValueInt64(), } @@ -124,7 +127,7 @@ func (s *pagerdutyChannelResource) Create(ctx context.Context, req resource.Crea plan.Name = types.StringValue(notificationChannel.Name) plan.Enabled = types.BoolValue(notificationChannel.Enabled) plan.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - plan.Alarms = types.StringValue(notificationChannel.Alarms) + plan.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) plan.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) diags = resp.State.Set(ctx, plan) @@ -168,7 +171,7 @@ func (s *pagerdutyChannelResource) Read(ctx context.Context, req resource.ReadRe state.Name = types.StringValue(notificationChannel.Name) state.Enabled = types.BoolValue(notificationChannel.Enabled) state.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - state.Alarms = types.StringValue(notificationChannel.Alarms) + state.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) state.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) state.AlertEventsURL = types.StringValue(notificationSecrets.AlertEventsURL) state.IntegrationKey = types.StringValue(notificationSecrets.IntegrationKey) @@ -191,11 +194,14 @@ func (s *pagerdutyChannelResource) Update(ctx context.Context, req resource.Upda var roomsID []string plan.RoomsID.ElementsAs(ctx, &roomsID, false) + var notificationOptions []string + plan.NotificationOptions.ElementsAs(ctx, ¬ificationOptions, false) + commonParams := client.NotificationChannel{ ID: plan.ID.ValueString(), Name: plan.Name.ValueString(), Rooms: roomsID, - Alarms: plan.Alarms.ValueString(), + NotificationOptions: notificationOptions, Enabled: plan.Enabled.ValueBool(), RepeatNotificationMinute: plan.RepeatNotificationMinute.ValueInt64(), } @@ -219,7 +225,7 @@ func (s *pagerdutyChannelResource) Update(ctx context.Context, req resource.Upda plan.Name = types.StringValue(notificationChannel.Name) plan.Enabled = types.BoolValue(notificationChannel.Enabled) plan.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - plan.Alarms = types.StringValue(notificationChannel.Alarms) + plan.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) plan.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) diags = resp.State.Set(ctx, plan) diff --git a/internal/provider/notification_pagerduty_channel_resource_test.go b/internal/provider/notification_pagerduty_channel_resource_test.go index bdbcfbc..cf2ca6f 100644 --- a/internal/provider/notification_pagerduty_channel_resource_test.go +++ b/internal/provider/notification_pagerduty_channel_resource_test.go @@ -23,7 +23,7 @@ func TestAccPagerdutyNotificationResource(t *testing.T) { enabled = true space_id = "%s" rooms_id = [netdata_room.test.id] - alarms = "ALARMS_SETTING_ALL" + notifications = ["CRITICAL","WARNING","CLEAR"] alert_events_url = "https://events.pagerduty.com/v2/enqueue" integration_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" repeat_notification_min = 30 @@ -35,7 +35,9 @@ func TestAccPagerdutyNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "enabled", "true"), resource.TestCheckResourceAttrSet("netdata_notification_pagerduty_channel.test", "space_id"), resource.TestCheckResourceAttrSet("netdata_notification_pagerduty_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "notifications.0", "CRITICAL"), + resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "notifications.1", "WARNING"), + resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "notifications.2", "CLEAR"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "repeat_notification_min", "30"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "alert_events_url", "https://events.pagerduty.com/v2/enqueue"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "integration_key", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), @@ -52,7 +54,7 @@ func TestAccPagerdutyNotificationResource(t *testing.T) { enabled = true space_id = "%s" rooms_id = [netdata_room.test.id] - alarms = "ALARMS_SETTING_ALL" + notifications = ["CLEAR","CRITICAL"] alert_events_url = "https://events.pagerduty.com/v2/enqueue" integration_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" repeat_notification_min = 60 @@ -64,7 +66,8 @@ func TestAccPagerdutyNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "enabled", "true"), resource.TestCheckResourceAttrSet("netdata_notification_pagerduty_channel.test", "space_id"), resource.TestCheckResourceAttrSet("netdata_notification_pagerduty_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "notifications.0", "CLEAR"), + resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "notifications.1", "CRITICAL"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "repeat_notification_min", "60"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "alert_events_url", "https://events.pagerduty.com/v2/enqueue"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "integration_key", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), @@ -81,7 +84,7 @@ func TestAccPagerdutyNotificationResource(t *testing.T) { enabled = false space_id = "%s" rooms_id = null - alarms = "ALARMS_SETTING_ALL" + notifications = ["CRITICAL","WARNING","CLEAR"] alert_events_url = "https://events.pagerduty.com/v2/enqueue" integration_key = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" } @@ -92,7 +95,9 @@ func TestAccPagerdutyNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "enabled", "false"), resource.TestCheckResourceAttrSet("netdata_notification_pagerduty_channel.test", "space_id"), resource.TestCheckNoResourceAttr("netdata_notification_pagerduty_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "notifications.0", "CRITICAL"), + resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "notifications.1", "WARNING"), + resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "notifications.2", "CLEAR"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "repeat_notification_min", "0"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "alert_events_url", "https://events.pagerduty.com/v2/enqueue"), resource.TestCheckResourceAttr("netdata_notification_pagerduty_channel.test", "integration_key", "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"), diff --git a/internal/provider/notification_slack_channel_resource.go b/internal/provider/notification_slack_channel_resource.go index d27a755..72158a3 100644 --- a/internal/provider/notification_slack_channel_resource.go +++ b/internal/provider/notification_slack_channel_resource.go @@ -33,7 +33,7 @@ type slackChannelResourceModel struct { Enabled types.Bool `tfsdk:"enabled"` SpaceID types.String `tfsdk:"space_id"` RoomsID types.List `tfsdk:"rooms_id"` - Alarms types.String `tfsdk:"alarms"` + NotificationOptions types.List `tfsdk:"notifications"` RepeatNotificationMinute types.Int64 `tfsdk:"repeat_notification_min"` WebhookURL types.String `tfsdk:"webhook_url"` } @@ -91,11 +91,14 @@ func (s *slackChannelResource) Create(ctx context.Context, req resource.CreateRe var roomsID []string plan.RoomsID.ElementsAs(ctx, &roomsID, false) + var notificationOptions []string + plan.NotificationOptions.ElementsAs(ctx, ¬ificationOptions, false) + commonParams := client.NotificationChannel{ Name: plan.Name.ValueString(), Integration: *notificationIntegration, Rooms: roomsID, - Alarms: plan.Alarms.ValueString(), + NotificationOptions: notificationOptions, Enabled: plan.Enabled.ValueBool(), RepeatNotificationMinute: plan.RepeatNotificationMinute.ValueInt64(), } @@ -117,7 +120,7 @@ func (s *slackChannelResource) Create(ctx context.Context, req resource.CreateRe plan.Name = types.StringValue(notificationChannel.Name) plan.Enabled = types.BoolValue(notificationChannel.Enabled) plan.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - plan.Alarms = types.StringValue(notificationChannel.Alarms) + plan.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) plan.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) diags = resp.State.Set(ctx, plan) @@ -161,7 +164,7 @@ func (s *slackChannelResource) Read(ctx context.Context, req resource.ReadReques state.Name = types.StringValue(notificationChannel.Name) state.Enabled = types.BoolValue(notificationChannel.Enabled) state.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - state.Alarms = types.StringValue(notificationChannel.Alarms) + state.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) state.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) state.WebhookURL = types.StringValue(notificationSecrets.URL) diags = resp.State.Set(ctx, &state) @@ -183,11 +186,14 @@ func (s *slackChannelResource) Update(ctx context.Context, req resource.UpdateRe var roomsID []string plan.RoomsID.ElementsAs(ctx, &roomsID, false) + var notificationOptions []string + plan.NotificationOptions.ElementsAs(ctx, ¬ificationOptions, false) + commonParams := client.NotificationChannel{ ID: plan.ID.ValueString(), Name: plan.Name.ValueString(), Rooms: roomsID, - Alarms: plan.Alarms.ValueString(), + NotificationOptions: notificationOptions, Enabled: plan.Enabled.ValueBool(), RepeatNotificationMinute: plan.RepeatNotificationMinute.ValueInt64(), } @@ -209,7 +215,7 @@ func (s *slackChannelResource) Update(ctx context.Context, req resource.UpdateRe plan.Name = types.StringValue(notificationChannel.Name) plan.Enabled = types.BoolValue(notificationChannel.Enabled) plan.RoomsID, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.Rooms) - plan.Alarms = types.StringValue(notificationChannel.Alarms) + plan.NotificationOptions, _ = types.ListValueFrom(ctx, types.StringType, notificationChannel.NotificationOptions) plan.RepeatNotificationMinute = types.Int64Value(notificationChannel.RepeatNotificationMinute) diags = resp.State.Set(ctx, plan) diff --git a/internal/provider/notification_slack_channel_resource_test.go b/internal/provider/notification_slack_channel_resource_test.go index bf349c3..52b8ed1 100644 --- a/internal/provider/notification_slack_channel_resource_test.go +++ b/internal/provider/notification_slack_channel_resource_test.go @@ -19,12 +19,12 @@ func TestAccSlackNotificationResource(t *testing.T) { name = "testAcc" } resource "netdata_notification_slack_channel" "test" { - name = "slack" - enabled = true - space_id = "%s" - rooms_id = [netdata_room.test.id] - webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" - alarms = "ALARMS_SETTING_ALL" + name = "slack" + enabled = true + space_id = "%s" + rooms_id = [netdata_room.test.id] + webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" + notifications = ["CRITICAL","WARNING","CLEAR"] repeat_notification_min = 30 } `, getNonCommunitySpaceIDEnv(), getNonCommunitySpaceIDEnv()), @@ -34,7 +34,9 @@ func TestAccSlackNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "enabled", "true"), resource.TestCheckResourceAttrSet("netdata_notification_slack_channel.test", "space_id"), resource.TestCheckResourceAttrSet("netdata_notification_slack_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "notifications.0", "CRITICAL"), + resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "notifications.1", "WARNING"), + resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "notifications.2", "CLEAR"), resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "repeat_notification_min", "30"), resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "webhook_url", "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"), ), @@ -46,12 +48,12 @@ func TestAccSlackNotificationResource(t *testing.T) { name = "testAcc" } resource "netdata_notification_slack_channel" "test" { - name = "slack" - enabled = true - space_id = "%s" - rooms_id = [netdata_room.test.id] - webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" - alarms = "ALARMS_SETTING_ALL" + name = "slack" + enabled = true + space_id = "%s" + rooms_id = [netdata_room.test.id] + webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" + notifications = ["CLEAR","CRITICAL"] repeat_notification_min = 60 } `, getNonCommunitySpaceIDEnv(), getNonCommunitySpaceIDEnv()), @@ -61,7 +63,8 @@ func TestAccSlackNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "enabled", "true"), resource.TestCheckResourceAttrSet("netdata_notification_slack_channel.test", "space_id"), resource.TestCheckResourceAttrSet("netdata_notification_slack_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "notifications.0", "CLEAR"), + resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "notifications.1", "CRITICAL"), resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "repeat_notification_min", "60"), resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "webhook_url", "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"), ), @@ -73,12 +76,12 @@ func TestAccSlackNotificationResource(t *testing.T) { name = "testAcc" } resource "netdata_notification_slack_channel" "test" { - name = "slack" - enabled = false - space_id = "%s" - rooms_id = null - webhook_url = "https://hooks.slack.com/services/T10000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" - alarms = "ALARMS_SETTING_ALL" + name = "slack" + enabled = false + space_id = "%s" + rooms_id = null + webhook_url = "https://hooks.slack.com/services/T10000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" + notifications = ["CRITICAL","WARNING","CLEAR"] } `, getNonCommunitySpaceIDEnv(), getNonCommunitySpaceIDEnv()), Check: resource.ComposeAggregateTestCheckFunc( @@ -87,7 +90,9 @@ func TestAccSlackNotificationResource(t *testing.T) { resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "enabled", "false"), resource.TestCheckResourceAttrSet("netdata_notification_slack_channel.test", "space_id"), resource.TestCheckNoResourceAttr("netdata_notification_slack_channel.test", "rooms_id.0"), - resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "alarms", "ALARMS_SETTING_ALL"), + resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "notifications.0", "CRITICAL"), + resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "notifications.1", "WARNING"), + resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "notifications.2", "CLEAR"), resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "repeat_notification_min", "0"), resource.TestCheckResourceAttr("netdata_notification_slack_channel.test", "webhook_url", "https://hooks.slack.com/services/T10000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"), ), diff --git a/internal/provider/notifications.go b/internal/provider/notifications.go index fc136a2..2e0ac38 100644 --- a/internal/provider/notifications.go +++ b/internal/provider/notifications.go @@ -2,7 +2,6 @@ package provider import ( "fmt" - "regexp" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" @@ -61,14 +60,14 @@ func commonNotificationSchema(notificationType string) schema.Schema { ), }, }, - "alarms": schema.StringAttribute{ - Description: fmt.Sprintf("The alarms setting to set the %s notification. Valid values are: `ALARMS_SETTING_ALL`, `ALARMS_SETTING_CRITICAL`, `ALARMS_SETTING_ALL_BUT_UNREACHABLE`, `ALARMS_SETTING_UNREACHABLE`", notificationType), + "notifications": schema.ListAttribute{ + Description: fmt.Sprintf("The notification options for the %s. Valid values are: `CRITICAL`, `WARNING`, `CLEAR`, `REACHABLE`, `UNREACHABLE`", notificationType), + ElementType: types.StringType, Required: true, - Validators: []validator.String{ - stringvalidator.RegexMatches( - regexp.MustCompile(`^(ALARMS_SETTING_ALL|ALARMS_SETTING_CRITICAL|ALARMS_SETTING_ALL_BUT_UNREACHABLE|ALARMS_SETTING_UNREACHABLE)$`), - "Invalid alarms setting", - ), + Validators: []validator.List{ + listvalidator.SizeAtLeast(1), + listvalidator.UniqueValues(), + listvalidator.ValueStringsAre(stringvalidator.OneOf([]string{"CRITICAL", "WARNING", "CLEAR", "REACHABLE", "UNREACHABLE"}...)), }, }, },