Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/data-sources/notification_vk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "uptimekuma_notification_vk Data Source - uptimekuma"
subcategory: ""
description: |-
Get VK (VKontakte) notification information by ID or name
---

# uptimekuma_notification_vk (Data Source)

Get VK (VKontakte) notification information by ID or name

## Example Usage

```terraform
# Look up an existing VK notification by name
data "uptimekuma_notification_vk" "alerts" {
name = "VK Alerts"
}

# Look up by ID
data "uptimekuma_notification_vk" "by_id" {
id = 1
}

# Use with a monitor resource
resource "uptimekuma_monitor_http" "api" {
name = "API Monitor"
url = "https://api.example.com/health"
notification_ids = [data.uptimekuma_notification_vk.alerts.id]
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `id` (Number) Notification identifier
- `name` (String) Notification name
54 changes: 54 additions & 0 deletions docs/resources/notification_vk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "uptimekuma_notification_vk Resource - uptimekuma"
subcategory: ""
description: |-
VK (VKontakte) notification resource
---

# uptimekuma_notification_vk (Resource)

VK (VKontakte) notification resource

## Example Usage

```terraform
resource "uptimekuma_notification_vk" "example" {
name = "VK Notifications"
access_token = "vk1.a.YOUR_ACCESS_TOKEN"
peer_id = "12345"
is_active = true
is_default = false
}

resource "uptimekuma_notification_vk" "custom" {
name = "VK Notifications (custom API version)"
access_token = "vk1.a.YOUR_ACCESS_TOKEN"
peer_id = "2000000001"
api_version = "5.131"
dont_parse_links = true
is_active = true
is_default = false
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `access_token` (String, Sensitive) The VK API user or service access token.
- `name` (String) Notification name
- `peer_id` (String) The recipient. Must be a numeric string: positive for user IDs, negative for community IDs, or 2000000000+chat_id for group chats.

### Optional

- `api_version` (String) The VK API version to use.
- `apply_existing` (Boolean)
- `dont_parse_links` (Boolean) If true, disables link previews in VK notifications.
- `is_active` (Boolean)
- `is_default` (Boolean)

### Read-Only

- `id` (Number) Notification identifier
16 changes: 16 additions & 0 deletions examples/data-sources/uptimekuma_notification_vk/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Look up an existing VK notification by name
data "uptimekuma_notification_vk" "alerts" {
name = "VK Alerts"
}

# Look up by ID
data "uptimekuma_notification_vk" "by_id" {
id = 1
}

# Use with a monitor resource
resource "uptimekuma_monitor_http" "api" {
name = "API Monitor"
url = "https://api.example.com/health"
notification_ids = [data.uptimekuma_notification_vk.alerts.id]
}
17 changes: 17 additions & 0 deletions examples/resources/uptimekuma_notification_vk/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "uptimekuma_notification_vk" "example" {
name = "VK Notifications"
access_token = "vk1.a.YOUR_ACCESS_TOKEN"
peer_id = "12345"
is_active = true
is_default = false
}

resource "uptimekuma_notification_vk" "custom" {
name = "VK Notifications (custom API version)"
access_token = "vk1.a.YOUR_ACCESS_TOKEN"
peer_id = "2000000001"
api_version = "5.131"
dont_parse_links = true
is_active = true
is_default = false
}
131 changes: 131 additions & 0 deletions internal/provider/data_source_notification_vk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

kuma "github.com/breml/go-uptime-kuma-client"
)

var _ datasource.DataSource = &NotificationVKDataSource{}

// NewNotificationVKDataSource returns a new instance of the VK notification data source.
func NewNotificationVKDataSource() datasource.DataSource {
return &NotificationVKDataSource{}
}

// NotificationVKDataSource manages VK notification data source operations.
type NotificationVKDataSource struct {
client *kuma.Client
}

// NotificationVKDataSourceModel describes the data model for VK notification data source.
type NotificationVKDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}

// Metadata returns the metadata for the data source.
func (*NotificationVKDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_notification_vk"
}

// Schema returns the schema for the data source.
func (*NotificationVKDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
MarkdownDescription: "Get VK (VKontakte) notification information by ID or name",
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "Notification identifier",
Optional: true,
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Notification name",
Optional: true,
Computed: true,
},
},
}
}

// Configure configures the data source with the API client.
func (d *NotificationVKDataSource) Configure(
_ context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
d.client = configureClient(req.ProviderData, &resp.Diagnostics)
}

// Read reads the current state of the data source.
func (d *NotificationVKDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var data NotificationVKDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

if !validateNotificationDataSourceInput(resp, data.ID, data.Name) {
return
}

// Attempt to read by ID if provided.
if !data.ID.IsNull() && !data.ID.IsUnknown() {
d.readByID(ctx, &data, resp)
return
}

// Attempt to read by name if ID not provided.
d.readByName(ctx, &data, resp)
}

func (d *NotificationVKDataSource) readByID(
ctx context.Context,
data *NotificationVKDataSourceModel,
resp *datasource.ReadResponse,
) {
notification, err := d.client.GetNotification(ctx, data.ID.ValueInt64())
if err != nil {
resp.Diagnostics.AddError("failed to read notification", err.Error())
return
}

if notification.Type() != "VK" {
resp.Diagnostics.AddError("Incorrect notification type", "Notification is not a VK notification")
return
}

data.Name = types.StringValue(notification.Name)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (d *NotificationVKDataSource) readByName(
ctx context.Context,
data *NotificationVKDataSourceModel,
resp *datasource.ReadResponse,
) {
id, ok := findNotificationByName(ctx, d.client, data.Name.ValueString(), "VK", &resp.Diagnostics)
if !ok {
return
}

data.ID = types.Int64Value(id)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
73 changes: 73 additions & 0 deletions internal/provider/data_source_notification_vk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

func TestAccNotificationVKDataSource(t *testing.T) {
name := acctest.RandomWithPrefix("NotificationVK")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccNotificationVKDataSourceConfig(name),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"data.uptimekuma_notification_vk.test",
tfjsonpath.New("name"),
knownvalue.StringExact(name),
),
},
},
{
Config: testAccNotificationVKDataSourceConfigByID(name),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"data.uptimekuma_notification_vk.test",
tfjsonpath.New("name"),
knownvalue.StringExact(name),
),
},
},
},
})
}

func testAccNotificationVKDataSourceConfig(name string) string {
return providerConfig() + fmt.Sprintf(`
resource "uptimekuma_notification_vk" "test" {
name = %[1]q
is_active = true
access_token = "vk1.a.abcdefghijklmnopqrstuvwxyz"
peer_id = "12345"
}

data "uptimekuma_notification_vk" "test" {
name = uptimekuma_notification_vk.test.name
}
`, name)
}

func testAccNotificationVKDataSourceConfigByID(name string) string {
return providerConfig() + fmt.Sprintf(`
resource "uptimekuma_notification_vk" "test" {
name = %[1]q
is_active = true
access_token = "vk1.a.abcdefghijklmnopqrstuvwxyz"
peer_id = "12345"
}

data "uptimekuma_notification_vk" "test" {
id = uptimekuma_notification_vk.test.id
}
`, name)
}
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ func notificationResources() []func() resource.Resource {
NewNotificationTeamsResource,
NewNotificationTelegramResource,
NewNotificationTwilioResource,
NewNotificationVKResource,
NewNotificationWAHAResource,
NewNotificationWhapiResource,
NewNotificationWPushResource,
Expand Down Expand Up @@ -577,6 +578,7 @@ func notificationDataSources() []func() datasource.DataSource {
NewNotificationTeamsDataSource,
NewNotificationTelegramDataSource,
NewNotificationTwilioDataSource,
NewNotificationVKDataSource,
NewNotificationWAHADataSource,
NewNotificationWhapiDataSource,
NewNotificationWPushDataSource,
Expand Down
Loading
Loading