generated from hashicorp/terraform-provider-scaffolding-framework
/
notification_kavita_resource.go
287 lines (246 loc) · 10.3 KB
/
notification_kavita_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
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 (
notificationKavitaResourceName = "notification_kavita"
notificationKavitaImplementation = "Kavita"
notificationKavitaConfigContract = "KavitaSettings"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ resource.Resource = &NotificationKavitaResource{}
_ resource.ResourceWithImportState = &NotificationKavitaResource{}
)
func NewNotificationKavitaResource() resource.Resource {
return &NotificationKavitaResource{}
}
// NotificationKavitaResource defines the notification implementation.
type NotificationKavitaResource struct {
client *readarr.APIClient
}
// NotificationKavita describes the notification data model.
type NotificationKavita struct {
Tags types.Set `tfsdk:"tags"`
APIKey types.String `tfsdk:"api_key"`
Host types.String `tfsdk:"host"`
Name types.String `tfsdk:"name"`
Port types.Int64 `tfsdk:"port"`
ID types.Int64 `tfsdk:"id"`
UseSSL types.Bool `tfsdk:"use_ssl"`
Notify types.Bool `tfsdk:"notify"`
OnUpgrade types.Bool `tfsdk:"on_upgrade"`
OnReleaseImport types.Bool `tfsdk:"on_release_import"`
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"`
OnBookRetag types.Bool `tfsdk:"on_book_retag"`
}
func (n NotificationKavita) toNotification() *Notification {
return &Notification{
Tags: n.Tags,
APIKey: n.APIKey,
Host: n.Host,
Name: n.Name,
Port: n.Port,
ID: n.ID,
UseSSL: n.UseSSL,
OnReleaseImport: n.OnReleaseImport,
Notify: n.Notify,
OnBookDelete: n.OnBookDelete,
OnBookFileDelete: n.OnBookFileDelete,
OnUpgrade: n.OnUpgrade,
OnBookFileDeleteForUpgrade: n.OnBookFileDeleteForUpgrade,
OnBookRetag: n.OnBookRetag,
Implementation: types.StringValue(notificationKavitaImplementation),
ConfigContract: types.StringValue(notificationKavitaConfigContract),
}
}
func (n *NotificationKavita) fromNotification(notification *Notification) {
n.Tags = notification.Tags
n.APIKey = notification.APIKey
n.Host = notification.Host
n.Name = notification.Name
n.Port = notification.Port
n.ID = notification.ID
n.UseSSL = notification.UseSSL
n.OnBookFileDeleteForUpgrade = notification.OnBookFileDeleteForUpgrade
n.OnBookFileDelete = notification.OnBookFileDelete
n.Notify = notification.Notify
n.OnBookDelete = notification.OnBookDelete
n.OnUpgrade = notification.OnUpgrade
n.OnReleaseImport = notification.OnReleaseImport
n.OnBookRetag = notification.OnBookRetag
}
func (r *NotificationKavitaResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + notificationKavitaResourceName
}
func (r *NotificationKavitaResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "<!-- subcategory:Notifications -->Notification Kavita resource.\nFor more information refer to [Notification](https://wiki.servarr.com/readarr/settings#connect) and [Kavita](https://wiki.servarr.com/readarr/supported#kavita).",
Attributes: map[string]schema.Attribute{
"on_upgrade": schema.BoolAttribute{
MarkdownDescription: "On upgrade 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_book_retag": schema.BoolAttribute{
MarkdownDescription: "On book retag flag.",
Required: true,
},
"on_release_import": schema.BoolAttribute{
MarkdownDescription: "On release import flag.",
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
"use_ssl": schema.BoolAttribute{
MarkdownDescription: "Use SSL flag.",
Optional: true,
Computed: true,
},
"notify": schema.BoolAttribute{
MarkdownDescription: "Update library.",
Required: true,
},
"port": schema.Int64Attribute{
MarkdownDescription: "Port.",
Required: true,
},
"api_key": schema.StringAttribute{
MarkdownDescription: "API key.",
Required: true,
Sensitive: true,
},
"host": schema.StringAttribute{
MarkdownDescription: "Host.",
Required: true,
},
},
}
}
func (r *NotificationKavitaResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if client := helpers.ResourceConfigure(ctx, req, resp); client != nil {
r.client = client
}
}
func (r *NotificationKavitaResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var notification *NotificationKavita
resp.Diagnostics.Append(req.Plan.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Create new NotificationKavita
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, notificationKavitaResourceName, err))
return
}
tflog.Trace(ctx, "created "+notificationKavitaResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
notification.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, ¬ification)...)
}
func (r *NotificationKavitaResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var notification *NotificationKavita
resp.Diagnostics.Append(req.State.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Get NotificationKavita 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, notificationKavitaResourceName, err))
return
}
tflog.Trace(ctx, "read "+notificationKavitaResourceName+": "+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 *NotificationKavitaResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var notification *NotificationKavita
resp.Diagnostics.Append(req.Plan.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Update NotificationKavita
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, notificationKavitaResourceName, err))
return
}
tflog.Trace(ctx, "updated "+notificationKavitaResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
notification.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, ¬ification)...)
}
func (r *NotificationKavitaResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var notification *NotificationKavita
resp.Diagnostics.Append(req.State.Get(ctx, ¬ification)...)
if resp.Diagnostics.HasError() {
return
}
// Delete NotificationKavita 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, notificationKavitaResourceName, err))
return
}
tflog.Trace(ctx, "deleted "+notificationKavitaResourceName+": "+strconv.Itoa(int(notification.ID.ValueInt64())))
resp.State.RemoveResource(ctx)
}
func (r *NotificationKavitaResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
helpers.ImportStatePassthroughIntID(ctx, path.Root("id"), req, resp)
tflog.Trace(ctx, "imported "+notificationKavitaResourceName+": "+req.ID)
}
func (n *NotificationKavita) write(ctx context.Context, notification *readarr.NotificationResource) {
genericNotification := n.toNotification()
genericNotification.write(ctx, notification)
n.fromNotification(genericNotification)
}
func (n *NotificationKavita) read(ctx context.Context) *readarr.NotificationResource {
return n.toNotification().read(ctx)
}