generated from hashicorp/terraform-provider-scaffolding-framework
/
notification_pushbullet_resource.go
319 lines (278 loc) · 12 KB
/
notification_pushbullet_resource.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package provider
import (
"context"
"strconv"
"github.com/devopsarr/readarr-go/readarr"
"github.com/devopsarr/terraform-provider-readarr/internal/helpers"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
const (
notificationPushbulletResourceName = "notification_pushbullet"
notificationPushbulletImplementation = "PushBullet"
notificationPushbulletConfigContract = "PushBulletSettings"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ resource.Resource = &NotificationPushbulletResource{}
_ resource.ResourceWithImportState = &NotificationPushbulletResource{}
)
func NewNotificationPushbulletResource() resource.Resource {
return &NotificationPushbulletResource{}
}
// NotificationPushbulletResource defines the notification implementation.
type NotificationPushbulletResource struct {
client *readarr.APIClient
}
// NotificationPushbullet describes the notification data model.
type NotificationPushbullet struct {
Tags types.Set `tfsdk:"tags"`
DeviceIds types.Set `tfsdk:"device_ids"`
ChannelTags types.Set `tfsdk:"channel_tags"`
SenderID types.String `tfsdk:"sender_id"`
Name types.String `tfsdk:"name"`
APIKey types.String `tfsdk:"api_key"`
ID types.Int64 `tfsdk:"id"`
OnGrab types.Bool `tfsdk:"on_grab"`
IncludeHealthWarnings types.Bool `tfsdk:"include_health_warnings"`
OnHealthIssue types.Bool `tfsdk:"on_health_issue"`
OnUpgrade types.Bool `tfsdk:"on_upgrade"`
OnReleaseImport types.Bool `tfsdk:"on_release_import"`
OnAuthorDelete types.Bool `tfsdk:"on_author_delete"`
OnBookDelete types.Bool `tfsdk:"on_book_delete"`
OnBookFileDelete types.Bool `tfsdk:"on_book_file_delete"`
OnBookFileDeleteForUpgrade types.Bool `tfsdk:"on_book_file_delete_for_upgrade"`
OnDownloadFailure types.Bool `tfsdk:"on_download_failure"`
OnImportFailure types.Bool `tfsdk:"on_import_failure"`
}
func (n NotificationPushbullet) toNotification() *Notification {
return &Notification{
Tags: n.Tags,
DeviceIds: n.DeviceIds,
ChannelTags: n.ChannelTags,
SenderID: n.SenderID,
APIKey: n.APIKey,
Name: n.Name,
ID: n.ID,
OnGrab: n.OnGrab,
OnReleaseImport: n.OnReleaseImport,
OnAuthorDelete: n.OnAuthorDelete,
IncludeHealthWarnings: n.IncludeHealthWarnings,
OnHealthIssue: n.OnHealthIssue,
OnBookDelete: n.OnBookDelete,
OnBookFileDelete: n.OnBookFileDelete,
OnUpgrade: n.OnUpgrade,
OnBookFileDeleteForUpgrade: n.OnBookFileDeleteForUpgrade,
OnDownloadFailure: n.OnDownloadFailure,
OnImportFailure: n.OnImportFailure,
Implementation: types.StringValue(notificationPushbulletImplementation),
ConfigContract: types.StringValue(notificationPushbulletConfigContract),
}
}
func (n *NotificationPushbullet) fromNotification(notification *Notification) {
n.Tags = notification.Tags
n.DeviceIds = notification.DeviceIds
n.ChannelTags = notification.ChannelTags
n.SenderID = notification.SenderID
n.APIKey = notification.APIKey
n.Name = notification.Name
n.ID = notification.ID
n.OnGrab = notification.OnGrab
n.OnBookFileDeleteForUpgrade = notification.OnBookFileDeleteForUpgrade
n.OnBookFileDelete = notification.OnBookFileDelete
n.IncludeHealthWarnings = notification.IncludeHealthWarnings
n.OnHealthIssue = notification.OnHealthIssue
n.OnAuthorDelete = notification.OnAuthorDelete
n.OnBookDelete = notification.OnBookDelete
n.OnUpgrade = notification.OnUpgrade
n.OnDownloadFailure = notification.OnDownloadFailure
n.OnImportFailure = notification.OnImportFailure
n.OnReleaseImport = notification.OnReleaseImport
}
func (r *NotificationPushbulletResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + notificationPushbulletResourceName
}
func (r *NotificationPushbulletResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "<!-- subcategory:Notifications -->Notification Pushbullet resource.\nFor more information refer to [Notification](https://wiki.servarr.com/readarr/settings#connect) and [Pushbullet](https://wiki.servarr.com/readarr/supported#pushbullet).",
Attributes: map[string]schema.Attribute{
"on_grab": schema.BoolAttribute{
MarkdownDescription: "On grab flag.",
Required: true,
},
"on_download_failure": schema.BoolAttribute{
MarkdownDescription: "On download failure flag.",
Required: true,
},
"on_upgrade": schema.BoolAttribute{
MarkdownDescription: "On upgrade flag.",
Required: true,
},
"on_author_delete": schema.BoolAttribute{
MarkdownDescription: "On author deleted flag.",
Required: true,
},
"on_book_delete": schema.BoolAttribute{
MarkdownDescription: "On book delete flag.",
Required: true,
},
"on_book_file_delete": schema.BoolAttribute{
MarkdownDescription: "On book file delete flag.",
Required: true,
},
"on_book_file_delete_for_upgrade": schema.BoolAttribute{
MarkdownDescription: "On book file delete for upgrade flag.",
Required: true,
},
"on_health_issue": schema.BoolAttribute{
MarkdownDescription: "On health issue flag.",
Required: true,
},
"on_import_failure": schema.BoolAttribute{
MarkdownDescription: "On import failure flag.",
Required: true,
},
"on_release_import": schema.BoolAttribute{
MarkdownDescription: "On release import flag.",
Required: true,
},
"include_health_warnings": schema.BoolAttribute{
MarkdownDescription: "Include health warnings.",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Notification name.",
Required: true,
},
"tags": schema.SetAttribute{
MarkdownDescription: "List of associated tags.",
Optional: true,
Computed: true,
ElementType: types.Int64Type,
},
"id": schema.Int64Attribute{
MarkdownDescription: "Notification ID.",
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
// Field values
"sender_id": schema.StringAttribute{
MarkdownDescription: "Sender ID.",
Optional: true,
Computed: true,
},
"api_key": schema.StringAttribute{
MarkdownDescription: "API key.",
Required: true,
Sensitive: true,
},
"device_ids": schema.SetAttribute{
MarkdownDescription: "List of devices IDs.",
Optional: true,
Computed: true,
ElementType: types.StringType,
},
"channel_tags": schema.SetAttribute{
MarkdownDescription: "List of channel tags.",
Optional: true,
Computed: true,
ElementType: types.StringType,
},
},
}
}
func (r *NotificationPushbulletResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if client := helpers.ResourceConfigure(ctx, req, resp); client != nil {
r.client = client
}
}
func (r *NotificationPushbulletResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var notification *NotificationPushbullet
resp.Diagnostics.Append(req.Plan.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Create new NotificationPushbullet
request := notification.read(ctx)
response, _, err := r.client.NotificationApi.CreateNotification(ctx).NotificationResource(*request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Create, notificationPushbulletResourceName, err))
return
}
tflog.Trace(ctx, "created "+notificationPushbulletResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
notification.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, ¬ification)...)
}
func (r *NotificationPushbulletResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var notification *NotificationPushbullet
resp.Diagnostics.Append(req.State.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Get NotificationPushbullet current value
response, _, err := r.client.NotificationApi.GetNotificationById(ctx, int32(int(notification.ID.ValueInt64()))).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, notificationPushbulletResourceName, err))
return
}
tflog.Trace(ctx, "read "+notificationPushbulletResourceName+": "+strconv.Itoa(int(response.GetId())))
// Map response body to resource schema attribute
notification.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, ¬ification)...)
}
func (r *NotificationPushbulletResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var notification *NotificationPushbullet
resp.Diagnostics.Append(req.Plan.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Update NotificationPushbullet
request := notification.read(ctx)
response, _, err := r.client.NotificationApi.UpdateNotification(ctx, strconv.Itoa(int(request.GetId()))).NotificationResource(*request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Update, notificationPushbulletResourceName, err))
return
}
tflog.Trace(ctx, "updated "+notificationPushbulletResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
notification.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, ¬ification)...)
}
func (r *NotificationPushbulletResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var notification *NotificationPushbullet
resp.Diagnostics.Append(req.State.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Delete NotificationPushbullet current value
_, err := r.client.NotificationApi.DeleteNotification(ctx, int32(notification.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, notificationPushbulletResourceName, err))
return
}
tflog.Trace(ctx, "deleted "+notificationPushbulletResourceName+": "+strconv.Itoa(int(notification.ID.ValueInt64())))
resp.State.RemoveResource(ctx)
}
func (r *NotificationPushbulletResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
helpers.ImportStatePassthroughIntID(ctx, path.Root("id"), req, resp)
tflog.Trace(ctx, "imported "+notificationPushbulletResourceName+": "+req.ID)
}
func (n *NotificationPushbullet) write(ctx context.Context, notification *readarr.NotificationResource) {
genericNotification := n.toNotification()
genericNotification.write(ctx, notification)
n.fromNotification(genericNotification)
}
func (n *NotificationPushbullet) read(ctx context.Context) *readarr.NotificationResource {
return n.toNotification().read(ctx)
}