generated from hashicorp/terraform-provider-scaffolding-framework
/
indexer_nyaa_resource.go
308 lines (267 loc) · 10.6 KB
/
indexer_nyaa_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
package provider
import (
"context"
"fmt"
"strconv"
"github.com/devopsarr/terraform-provider-whisparr/internal/helpers"
"github.com/devopsarr/whisparr-go/whisparr"
"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 (
indexerNyaaResourceName = "indexer_nyaa"
indexerNyaaImplementation = "Nyaa"
indexerNyaaConfigContract = "NyaaSettings"
indexerNyaaProtocol = "torrent"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ resource.Resource = &IndexerNyaaResource{}
_ resource.ResourceWithImportState = &IndexerNyaaResource{}
)
func NewIndexerNyaaResource() resource.Resource {
return &IndexerNyaaResource{}
}
// IndexerNyaaResource defines the Nyaa indexer implementation.
type IndexerNyaaResource struct {
client *whisparr.APIClient
}
// IndexerNyaa describes the Nyaa indexer data model.
type IndexerNyaa struct {
Tags types.Set `tfsdk:"tags"`
MultiLanguages types.Set `tfsdk:"multi_languages"`
RequiredFlags types.Set `tfsdk:"required_flags"`
Name types.String `tfsdk:"name"`
BaseURL types.String `tfsdk:"base_url"`
AdditionalParameters types.String `tfsdk:"additional_parameters"`
Priority types.Int64 `tfsdk:"priority"`
ID types.Int64 `tfsdk:"id"`
DownloadClientID types.Int64 `tfsdk:"download_client_id"`
MinimumSeeders types.Int64 `tfsdk:"minimum_seeders"`
SeedTime types.Int64 `tfsdk:"seed_time"`
SeedRatio types.Float64 `tfsdk:"seed_ratio"`
EnableAutomaticSearch types.Bool `tfsdk:"enable_automatic_search"`
EnableRss types.Bool `tfsdk:"enable_rss"`
EnableInteractiveSearch types.Bool `tfsdk:"enable_interactive_search"`
}
func (i IndexerNyaa) toIndexer() *Indexer {
return &Indexer{
EnableAutomaticSearch: i.EnableAutomaticSearch,
EnableInteractiveSearch: i.EnableInteractiveSearch,
EnableRss: i.EnableRss,
Priority: i.Priority,
DownloadClientID: i.DownloadClientID,
ID: i.ID,
Name: i.Name,
AdditionalParameters: i.AdditionalParameters,
MinimumSeeders: i.MinimumSeeders,
SeedTime: i.SeedTime,
SeedRatio: i.SeedRatio,
BaseURL: i.BaseURL,
Tags: i.Tags,
MultiLanguages: i.MultiLanguages,
RequiredFlags: i.RequiredFlags,
Implementation: types.StringValue(indexerNyaaImplementation),
ConfigContract: types.StringValue(indexerNyaaConfigContract),
Protocol: types.StringValue(indexerNyaaProtocol),
}
}
func (i *IndexerNyaa) fromIndexer(indexer *Indexer) {
i.EnableAutomaticSearch = indexer.EnableAutomaticSearch
i.EnableInteractiveSearch = indexer.EnableInteractiveSearch
i.EnableRss = indexer.EnableRss
i.Priority = indexer.Priority
i.DownloadClientID = indexer.DownloadClientID
i.ID = indexer.ID
i.Name = indexer.Name
i.AdditionalParameters = indexer.AdditionalParameters
i.MinimumSeeders = indexer.MinimumSeeders
i.SeedTime = indexer.SeedTime
i.SeedRatio = indexer.SeedRatio
i.BaseURL = indexer.BaseURL
i.Tags = indexer.Tags
i.MultiLanguages = indexer.MultiLanguages
i.RequiredFlags = indexer.RequiredFlags
}
func (r *IndexerNyaaResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + indexerNyaaResourceName
}
func (r *IndexerNyaaResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "<!-- subcategory:Indexers -->Indexer Nyaa resource.\nFor more information refer to [Indexer](https://wiki.servarr.com/whisparr/settings#indexers) and [Nyaa](https://wiki.servarr.com/whisparr/supported#nyaa).",
Attributes: map[string]schema.Attribute{
"enable_automatic_search": schema.BoolAttribute{
MarkdownDescription: "Enable automatic search flag.",
Optional: true,
Computed: true,
},
"enable_interactive_search": schema.BoolAttribute{
MarkdownDescription: "Enable interactive search flag.",
Optional: true,
Computed: true,
},
"enable_rss": schema.BoolAttribute{
MarkdownDescription: "Enable RSS flag.",
Optional: true,
Computed: true,
},
"priority": schema.Int64Attribute{
MarkdownDescription: "Priority.",
Optional: true,
Computed: true,
},
"download_client_id": schema.Int64Attribute{
MarkdownDescription: "Download client ID.",
Optional: true,
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "IndexerNyaa name.",
Required: true,
},
"tags": schema.SetAttribute{
MarkdownDescription: "List of associated tags.",
Optional: true,
Computed: true,
ElementType: types.Int64Type,
},
"id": schema.Int64Attribute{
MarkdownDescription: "IndexerNyaa ID.",
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
// Field values
"minimum_seeders": schema.Int64Attribute{
MarkdownDescription: "Minimum seeders.",
Optional: true,
Computed: true,
},
"seed_time": schema.Int64Attribute{
MarkdownDescription: "Seed time.",
Optional: true,
Computed: true,
},
"seed_ratio": schema.Float64Attribute{
MarkdownDescription: "Seed ratio.",
Optional: true,
Computed: true,
},
"additional_parameters": schema.StringAttribute{
MarkdownDescription: "Additional parameters.",
Optional: true,
Computed: true,
},
"base_url": schema.StringAttribute{
MarkdownDescription: "Base URL.",
Required: true,
},
"multi_languages": schema.SetAttribute{
MarkdownDescription: "Languages list.",
Optional: true,
Computed: true,
ElementType: types.Int64Type,
},
"required_flags": schema.SetAttribute{
MarkdownDescription: "Flag list.",
Optional: true,
Computed: true,
ElementType: types.Int64Type,
},
},
}
}
func (r *IndexerNyaaResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if client := helpers.ResourceConfigure(ctx, req, resp); client != nil {
r.client = client
}
}
func (r *IndexerNyaaResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var indexer *IndexerNyaa
resp.Diagnostics.Append(req.Plan.Get(ctx, &indexer)...)
if resp.Diagnostics.HasError() {
return
}
// Create new IndexerNyaa
request := indexer.read(ctx)
response, _, err := r.client.IndexerApi.CreateIndexer(ctx).IndexerResource(*request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Create, indexerNyaaResourceName, err))
return
}
tflog.Trace(ctx, "created "+indexerNyaaResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
indexer.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, &indexer)...)
}
func (r *IndexerNyaaResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var indexer *IndexerNyaa
resp.Diagnostics.Append(req.State.Get(ctx, &indexer)...)
if resp.Diagnostics.HasError() {
return
}
// Get IndexerNyaa current value
response, _, err := r.client.IndexerApi.GetIndexerById(ctx, int32(indexer.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, indexerNyaaResourceName, err))
return
}
tflog.Trace(ctx, "read "+indexerNyaaResourceName+": "+strconv.Itoa(int(response.GetId())))
// Map response body to resource schema attribute
indexer.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, &indexer)...)
}
func (r *IndexerNyaaResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var indexer *IndexerNyaa
resp.Diagnostics.Append(req.Plan.Get(ctx, &indexer)...)
if resp.Diagnostics.HasError() {
return
}
// Update IndexerNyaa
request := indexer.read(ctx)
response, _, err := r.client.IndexerApi.UpdateIndexer(ctx, strconv.Itoa(int(request.GetId()))).IndexerResource(*request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, fmt.Sprintf("Unable to update "+indexerNyaaResourceName+", got error: %s", err))
return
}
tflog.Trace(ctx, "updated "+indexerNyaaResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
indexer.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, &indexer)...)
}
func (r *IndexerNyaaResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var indexer *IndexerNyaa
resp.Diagnostics.Append(req.State.Get(ctx, &indexer)...)
if resp.Diagnostics.HasError() {
return
}
// Delete IndexerNyaa current value
_, err := r.client.IndexerApi.DeleteIndexer(ctx, int32(indexer.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, indexerNyaaResourceName, err))
return
}
tflog.Trace(ctx, "deleted "+indexerNyaaResourceName+": "+strconv.Itoa(int(indexer.ID.ValueInt64())))
resp.State.RemoveResource(ctx)
}
func (r *IndexerNyaaResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
helpers.ImportStatePassthroughIntID(ctx, path.Root("id"), req, resp)
tflog.Trace(ctx, "imported "+indexerNyaaResourceName+": "+req.ID)
}
func (i *IndexerNyaa) write(ctx context.Context, indexer *whisparr.IndexerResource) {
genericIndexer := i.toIndexer()
genericIndexer.write(ctx, indexer)
i.fromIndexer(genericIndexer)
}
func (i *IndexerNyaa) read(ctx context.Context) *whisparr.IndexerResource {
return i.toIndexer().read(ctx)
}