generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_profile_resource.go
212 lines (172 loc) · 7.34 KB
/
sync_profile_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
package provider
import (
"context"
"fmt"
"strconv"
"github.com/devopsarr/prowlarr-go/prowlarr"
"github.com/devopsarr/terraform-provider-prowlarr/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 syncProfileResourceName = "sync_profile"
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ resource.Resource = &SyncProfileResource{}
_ resource.ResourceWithImportState = &SyncProfileResource{}
)
func NewSyncProfileResource() resource.Resource {
return &SyncProfileResource{}
}
// SyncProfileResource defines the sync profile implementation.
type SyncProfileResource struct {
client *prowlarr.APIClient
}
// SyncProfile describes the sync profile data model.
type SyncProfile struct {
Name types.String `tfsdk:"name"`
ID types.Int64 `tfsdk:"id"`
MinimumSeeders types.Int64 `tfsdk:"minimum_seeders"`
EnableRss types.Bool `tfsdk:"enable_rss"`
EnableInteractiveSearch types.Bool `tfsdk:"enable_interactive_search"`
EnableAutomaticSearch types.Bool `tfsdk:"enable_automatic_search"`
}
func (r *SyncProfileResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + syncProfileResourceName
}
func (r *SyncProfileResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "<!-- subcategory:Applications -->Sync Profile resource.\nFor more information refer to [Sync Profiles](https://wiki.servarr.com/prowlarr/settings#sync-profiles) documentation.",
Attributes: map[string]schema.Attribute{
"enable_rss": schema.BoolAttribute{
MarkdownDescription: "Enable RSS flag.",
Required: true,
},
"enable_interactive_search": schema.BoolAttribute{
MarkdownDescription: "Enable interactive search flag.",
Required: true,
},
"enable_automatic_search": schema.BoolAttribute{
MarkdownDescription: "Enable automatic search flag.",
Required: true,
},
"id": schema.Int64Attribute{
MarkdownDescription: "Sync Profile ID.",
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
"minimum_seeders": schema.Int64Attribute{
MarkdownDescription: "Minimum seeders.",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Name.",
Required: true,
},
},
}
}
func (r *SyncProfileResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if client := helpers.ResourceConfigure(ctx, req, resp); client != nil {
r.client = client
}
}
func (r *SyncProfileResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var profile *SyncProfile
resp.Diagnostics.Append(req.Plan.Get(ctx, &profile)...)
if resp.Diagnostics.HasError() {
return
}
// Create new Sync Profile
request := profile.read()
response, _, err := r.client.AppProfileApi.CreateAppProfile(ctx).AppProfileResource(*request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Create, syncProfileResourceName, err))
return
}
tflog.Trace(ctx, "created sync profile: "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
profile.write(response)
resp.Diagnostics.Append(resp.State.Set(ctx, &profile)...)
}
func (r *SyncProfileResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var profile *SyncProfile
resp.Diagnostics.Append(req.State.Get(ctx, &profile)...)
if resp.Diagnostics.HasError() {
return
}
// Get sync profile current value
response, _, err := r.client.AppProfileApi.GetAppProfileById(ctx, int32(profile.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, syncProfileResourceName, err))
return
}
tflog.Trace(ctx, "read "+syncProfileResourceName+": "+strconv.Itoa(int(response.GetId())))
// Map response body to resource schema attribute
profile.write(response)
resp.Diagnostics.Append(resp.State.Set(ctx, &profile)...)
}
func (r *SyncProfileResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var profile *SyncProfile
resp.Diagnostics.Append(req.Plan.Get(ctx, &profile)...)
if resp.Diagnostics.HasError() {
return
}
// Update SyncProfile
request := profile.read()
response, _, err := r.client.AppProfileApi.UpdateAppProfile(ctx, fmt.Sprint(request.GetId())).AppProfileResource(*request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Update, syncProfileResourceName, err))
return
}
tflog.Trace(ctx, "updated "+syncProfileResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
profile.write(response)
resp.Diagnostics.Append(resp.State.Set(ctx, &profile)...)
}
func (r *SyncProfileResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var profile *SyncProfile
resp.Diagnostics.Append(req.State.Get(ctx, &profile)...)
if resp.Diagnostics.HasError() {
return
}
// Delete sync profile current value
_, err := r.client.AppProfileApi.DeleteAppProfile(ctx, int32(profile.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, syncProfileResourceName, err))
return
}
tflog.Trace(ctx, "deleted "+syncProfileResourceName+": "+strconv.Itoa(int(profile.ID.ValueInt64())))
resp.State.RemoveResource(ctx)
}
func (r *SyncProfileResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
helpers.ImportStatePassthroughIntID(ctx, path.Root("id"), req, resp)
tflog.Trace(ctx, "imported "+syncProfileResourceName+": "+req.ID)
}
func (s *SyncProfile) read() *prowlarr.AppProfileResource {
profile := *prowlarr.NewAppProfileResource()
profile.SetName(s.Name.ValueString())
profile.SetId(int32(s.ID.ValueInt64()))
profile.SetMinimumSeeders(int32(s.MinimumSeeders.ValueInt64()))
profile.SetEnableRss(s.EnableRss.ValueBool())
profile.SetEnableInteractiveSearch(s.EnableInteractiveSearch.ValueBool())
profile.SetEnableAutomaticSearch(s.EnableAutomaticSearch.ValueBool())
return &profile
}
func (s *SyncProfile) write(profile *prowlarr.AppProfileResource) {
s.ID = types.Int64Value(int64(profile.GetId()))
s.Name = types.StringValue(profile.GetName())
s.MinimumSeeders = types.Int64Value(int64(profile.GetMinimumSeeders()))
s.EnableRss = types.BoolValue(profile.GetEnableRss())
s.EnableInteractiveSearch = types.BoolValue(profile.GetEnableInteractiveSearch())
s.EnableAutomaticSearch = types.BoolValue(profile.GetEnableAutomaticSearch())
}