generated from hashicorp/terraform-provider-scaffolding-framework
/
notification_prowl_resource.go
300 lines (259 loc) · 11.1 KB
/
notification_prowl_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
package provider
import (
"context"
"strconv"
"github.com/devopsarr/terraform-provider-whisparr/internal/helpers"
"github.com/devopsarr/whisparr-go/whisparr"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
const (
notificationProwlResourceName = "notification_prowl"
notificationProwlImplementation = "Prowl"
notificationProwlConfigContract = "ProwlSettings"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ resource.Resource = &NotificationProwlResource{}
_ resource.ResourceWithImportState = &NotificationProwlResource{}
)
func NewNotificationProwlResource() resource.Resource {
return &NotificationProwlResource{}
}
// NotificationProwlResource defines the notification implementation.
type NotificationProwlResource struct {
client *whisparr.APIClient
}
// NotificationProwl describes the notification data model.
type NotificationProwl struct {
Tags types.Set `tfsdk:"tags"`
Name types.String `tfsdk:"name"`
APIKey types.String `tfsdk:"api_key"`
Priority types.Int64 `tfsdk:"priority"`
ID types.Int64 `tfsdk:"id"`
OnGrab types.Bool `tfsdk:"on_grab"`
OnMovieFileDeleteForUpgrade types.Bool `tfsdk:"on_movie_file_delete_for_upgrade"`
OnMovieFileDelete types.Bool `tfsdk:"on_movie_file_delete"`
IncludeHealthWarnings types.Bool `tfsdk:"include_health_warnings"`
OnApplicationUpdate types.Bool `tfsdk:"on_application_update"`
OnHealthIssue types.Bool `tfsdk:"on_health_issue"`
OnMovieDelete types.Bool `tfsdk:"on_movie_delete"`
OnUpgrade types.Bool `tfsdk:"on_upgrade"`
OnDownload types.Bool `tfsdk:"on_download"`
}
func (n NotificationProwl) toNotification() *Notification {
return &Notification{
Tags: n.Tags,
APIKey: n.APIKey,
Priority: n.Priority,
Name: n.Name,
ID: n.ID,
OnGrab: n.OnGrab,
OnMovieFileDeleteForUpgrade: n.OnMovieFileDeleteForUpgrade,
OnMovieFileDelete: n.OnMovieFileDelete,
IncludeHealthWarnings: n.IncludeHealthWarnings,
OnApplicationUpdate: n.OnApplicationUpdate,
OnHealthIssue: n.OnHealthIssue,
OnMovieDelete: n.OnMovieDelete,
OnUpgrade: n.OnUpgrade,
OnDownload: n.OnDownload,
ConfigContract: types.StringValue(notificationProwlConfigContract),
Implementation: types.StringValue(notificationProwlImplementation),
}
}
func (n *NotificationProwl) fromNotification(notification *Notification) {
n.Tags = notification.Tags
n.APIKey = notification.APIKey
n.Priority = notification.Priority
n.Name = notification.Name
n.ID = notification.ID
n.OnGrab = notification.OnGrab
n.OnMovieFileDeleteForUpgrade = notification.OnMovieFileDeleteForUpgrade
n.OnMovieFileDelete = notification.OnMovieFileDelete
n.IncludeHealthWarnings = notification.IncludeHealthWarnings
n.OnApplicationUpdate = notification.OnApplicationUpdate
n.OnHealthIssue = notification.OnHealthIssue
n.OnMovieDelete = notification.OnMovieDelete
n.OnUpgrade = notification.OnUpgrade
n.OnDownload = notification.OnDownload
}
func (r *NotificationProwlResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + notificationProwlResourceName
}
func (r *NotificationProwlResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "<!-- subcategory:Notifications -->Notification Prowl resource.\nFor more information refer to [Notification](https://wiki.servarr.com/whisparr/settings#connect) and [Prowl](https://wiki.servarr.com/whisparr/supported#prowl).",
Attributes: map[string]schema.Attribute{
"on_grab": schema.BoolAttribute{
MarkdownDescription: "On grab flag.",
Optional: true,
Computed: true,
},
"on_download": schema.BoolAttribute{
MarkdownDescription: "On download flag.",
Optional: true,
Computed: true,
},
"on_upgrade": schema.BoolAttribute{
MarkdownDescription: "On upgrade flag.",
Optional: true,
Computed: true,
},
"on_movie_delete": schema.BoolAttribute{
MarkdownDescription: "On movie delete flag.",
Required: true,
},
"on_movie_file_delete": schema.BoolAttribute{
MarkdownDescription: "On movie file delete flag.",
Optional: true,
Computed: true,
},
"on_movie_file_delete_for_upgrade": schema.BoolAttribute{
MarkdownDescription: "On movie file delete for upgrade flag.",
Optional: true,
Computed: true,
},
"on_health_issue": schema.BoolAttribute{
MarkdownDescription: "On health issue flag.",
Optional: true,
Computed: true,
},
"on_application_update": schema.BoolAttribute{
MarkdownDescription: "On application update flag.",
Optional: true,
Computed: true,
},
"include_health_warnings": schema.BoolAttribute{
MarkdownDescription: "Include health warnings.",
Optional: true,
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "NotificationProwl 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
"priority": schema.Int64Attribute{
MarkdownDescription: "Priority.`-2` Very Low, `-1` Low, `0` Normal, `1` High, `2` Emergency.",
Optional: true,
Computed: true,
Validators: []validator.Int64{
int64validator.OneOf(-2, -1, 0, 1, 2),
},
},
"api_key": schema.StringAttribute{
MarkdownDescription: "API key.",
Required: true,
Sensitive: true,
},
},
}
}
func (r *NotificationProwlResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if client := helpers.ResourceConfigure(ctx, req, resp); client != nil {
r.client = client
}
}
func (r *NotificationProwlResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var notification *NotificationProwl
resp.Diagnostics.Append(req.Plan.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Create new NotificationProwl
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, notificationProwlResourceName, err))
return
}
tflog.Trace(ctx, "created "+notificationProwlResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
notification.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, ¬ification)...)
}
func (r *NotificationProwlResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var notification *NotificationProwl
resp.Diagnostics.Append(req.State.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Get NotificationProwl current value
response, _, err := r.client.NotificationApi.GetNotificationById(ctx, int32(notification.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, notificationProwlResourceName, err))
return
}
tflog.Trace(ctx, "read "+notificationProwlResourceName+": "+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 *NotificationProwlResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var notification *NotificationProwl
resp.Diagnostics.Append(req.Plan.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Update NotificationProwl
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, notificationProwlResourceName, err))
return
}
tflog.Trace(ctx, "updated "+notificationProwlResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
notification.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, ¬ification)...)
}
func (r *NotificationProwlResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var notification *NotificationProwl
resp.Diagnostics.Append(req.State.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Delete NotificationProwl 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, notificationProwlResourceName, err))
return
}
tflog.Trace(ctx, "deleted "+notificationProwlResourceName+": "+strconv.Itoa(int(notification.ID.ValueInt64())))
resp.State.RemoveResource(ctx)
}
func (r *NotificationProwlResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
helpers.ImportStatePassthroughIntID(ctx, path.Root("id"), req, resp)
tflog.Trace(ctx, "imported "+notificationProwlResourceName+": "+req.ID)
}
func (n *NotificationProwl) write(ctx context.Context, notification *whisparr.NotificationResource) {
genericNotification := n.toNotification()
genericNotification.write(ctx, notification)
n.fromNotification(genericNotification)
}
func (n *NotificationProwl) read(ctx context.Context) *whisparr.NotificationResource {
return n.toNotification().read(ctx)
}