-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdata_source_notification_telnyx.go
More file actions
139 lines (119 loc) · 3.69 KB
/
Copy pathdata_source_notification_telnyx.go
File metadata and controls
139 lines (119 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package provider
import (
"context"
"fmt"
"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 = &NotificationTelnyxDataSource{}
// NewNotificationTelnyxDataSource returns a new instance of the Telnyx notification data source.
func NewNotificationTelnyxDataSource() datasource.DataSource {
return &NotificationTelnyxDataSource{}
}
// NotificationTelnyxDataSource manages Telnyx notification data source operations.
type NotificationTelnyxDataSource struct {
client *kuma.Client
}
// NotificationTelnyxDataSourceModel describes the data model for Telnyx notification data source.
type NotificationTelnyxDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
// Metadata returns the metadata for the data source.
func (*NotificationTelnyxDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_notification_telnyx"
}
// Schema returns the schema for the data source.
func (*NotificationTelnyxDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
MarkdownDescription: "Get Telnyx 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 *NotificationTelnyxDataSource) 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 *NotificationTelnyxDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var data NotificationTelnyxDataSourceModel
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 *NotificationTelnyxDataSource) readByID(
ctx context.Context,
data *NotificationTelnyxDataSourceModel,
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() != "telnyx" {
resp.Diagnostics.AddError(
"incorrect notification type",
fmt.Sprintf(
"notification with ID %d has type %q, expected \"telnyx\"",
data.ID.ValueInt64(),
notification.Type(),
),
)
return
}
data.Name = types.StringValue(notification.Name)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (d *NotificationTelnyxDataSource) readByName(
ctx context.Context,
data *NotificationTelnyxDataSourceModel,
resp *datasource.ReadResponse,
) {
id, ok := findNotificationByName(ctx, d.client, data.Name.ValueString(), "telnyx", &resp.Diagnostics)
if !ok {
return
}
data.ID = types.Int64Value(id)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}