generated from hashicorp/terraform-provider-scaffolding-framework
/
naming_resource.go
257 lines (220 loc) · 8.34 KB
/
naming_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
package provider
import (
"context"
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"golift.io/starr/sonarr"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ provider.ResourceType = resourceNamingType{}
_ resource.Resource = resourceNaming{}
_ resource.ResourceWithImportState = resourceNaming{}
)
type resourceNamingType struct{}
type resourceNaming struct {
provider sonarrProvider
}
// Naming is the Naming resource.
type Naming struct {
RenameEpisodes types.Bool `tfsdk:"rename_episodes"`
ReplaceIllegalCharacters types.Bool `tfsdk:"replace_illegal_characters"`
ID types.Int64 `tfsdk:"id"`
MultiEpisodeStyle types.Int64 `tfsdk:"multi_episode_style"`
DailyEpisodeFormat types.String `tfsdk:"daily_episode_format"`
AnimeEpisodeFormat types.String `tfsdk:"anime_episode_format"`
SeriesFolderFormat types.String `tfsdk:"series_folder_format"`
SeasonFolderFormat types.String `tfsdk:"season_folder_format"`
SpecialsFolderFormat types.String `tfsdk:"specials_folder_format"`
StandardEpisodeFormat types.String `tfsdk:"standard_episode_format"`
}
func (t resourceNamingType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "Naming resource",
Attributes: map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "ID of naming",
Computed: true,
Type: types.Int64Type,
PlanModifiers: tfsdk.AttributePlanModifiers{
resource.UseStateForUnknown(),
},
},
"rename_episodes": {
MarkdownDescription: "Sonarr will use the existing file name if false",
Required: true,
Type: types.BoolType,
},
"replace_illegal_characters": {
MarkdownDescription: "Replace illegal characters. They will be removed if false",
Required: true,
Type: types.BoolType,
},
"multi_episode_style": {
MarkdownDescription: "Multi episode style. 0 - 'Extend' 1 - 'Duplicate' 2 - 'Repeat' 3 - 'Scene' 4 - 'Range' 5 - 'Prefixed Range'",
Required: true,
Type: types.Int64Type,
},
"daily_episode_format": {
MarkdownDescription: "Daily episode format",
Required: true,
Type: types.StringType,
},
"anime_episode_format": {
MarkdownDescription: "Anime episode format",
Required: true,
Type: types.StringType,
},
"series_folder_format": {
MarkdownDescription: "Series folder format",
Required: true,
Type: types.StringType,
},
"season_folder_format": {
MarkdownDescription: "Season folder format",
Required: true,
Type: types.StringType,
},
"specials_folder_format": {
MarkdownDescription: "Special folder format",
Required: true,
Type: types.StringType,
},
"standard_episode_format": {
MarkdownDescription: "Standard episode formatss",
Required: true,
Type: types.StringType,
},
},
}, nil
}
func (t resourceNamingType) NewResource(ctx context.Context, in provider.Provider) (resource.Resource, diag.Diagnostics) {
provider, diags := convertProviderType(in)
return resourceNaming{
provider: provider,
}, diags
}
func (r resourceNaming) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan Naming
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Init call if we remove this it the very first update on a brand new instance will fail
init, err := r.provider.client.GetNamingContext(ctx)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to init naming, got error: %s", err))
return
}
_, err = r.provider.client.UpdateNamingContext(ctx, init)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to init naming, got error: %s", err))
return
}
// Build Create resource
data := readNaming(&plan)
data.ID = 1
// Create new Naming
response, err := r.provider.client.UpdateNamingContext(ctx, data)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create naming, got error: %s", err))
return
}
tflog.Trace(ctx, "created naming: "+strconv.Itoa(int(response.ID)))
// Generate resource state struct
result := writeNaming(response)
diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r resourceNaming) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var state Naming
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get naming current value
response, err := r.provider.client.GetNamingContext(ctx)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read namings, got error: %s", err))
return
}
// Map response body to resource schema attribute
result := writeNaming(response)
diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
}
func (r resourceNaming) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var plan Naming
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Build Update resource
data := readNaming(&plan)
// Update Naming
response, err := r.provider.client.UpdateNamingContext(ctx, data)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update naming, got error: %s", err))
return
}
tflog.Trace(ctx, "update naming: "+strconv.Itoa(int(response.ID)))
// Generate resource state struct
result := writeNaming(response)
diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r resourceNaming) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Naming cannot be really deleted just removing configuration
resp.State.RemoveResource(ctx)
}
func (r resourceNaming) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), 1)...)
}
func writeNaming(naming *sonarr.Naming) *Naming {
return &Naming{
RenameEpisodes: types.Bool{Value: naming.RenameEpisodes},
ReplaceIllegalCharacters: types.Bool{Value: naming.ReplaceIllegalCharacters},
ID: types.Int64{Value: naming.ID},
MultiEpisodeStyle: types.Int64{Value: naming.MultiEpisodeStyle},
DailyEpisodeFormat: types.String{Value: naming.DailyEpisodeFormat},
AnimeEpisodeFormat: types.String{Value: naming.AnimeEpisodeFormat},
SeriesFolderFormat: types.String{Value: naming.SeriesFolderFormat},
SeasonFolderFormat: types.String{Value: naming.SeasonFolderFormat},
SpecialsFolderFormat: types.String{Value: naming.SpecialsFolderFormat},
StandardEpisodeFormat: types.String{Value: naming.StandardEpisodeFormat},
}
}
func readNaming(naming *Naming) *sonarr.Naming {
return &sonarr.Naming{
RenameEpisodes: naming.RenameEpisodes.Value,
ReplaceIllegalCharacters: naming.ReplaceIllegalCharacters.Value,
ID: naming.ID.Value,
MultiEpisodeStyle: naming.MultiEpisodeStyle.Value,
DailyEpisodeFormat: naming.DailyEpisodeFormat.Value,
AnimeEpisodeFormat: naming.AnimeEpisodeFormat.Value,
SeriesFolderFormat: naming.SeriesFolderFormat.Value,
SeasonFolderFormat: naming.SeasonFolderFormat.Value,
SpecialsFolderFormat: naming.SpecialsFolderFormat.Value,
StandardEpisodeFormat: naming.StandardEpisodeFormat.Value,
}
}