generated from hashicorp/terraform-provider-scaffolding-framework
/
tag_resource.go
188 lines (150 loc) · 5.77 KB
/
tag_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
package provider
import (
"context"
"fmt"
"regexp"
"strconv"
"github.com/devopsarr/radarr-go/radarr"
"github.com/devopsarr/terraform-provider-radarr/internal/helpers"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"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 tagResourceName = "tag"
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ resource.Resource = &TagResource{}
_ resource.ResourceWithImportState = &TagResource{}
)
func NewTagResource() resource.Resource {
return &TagResource{}
}
// TagResource defines the tag implementation.
type TagResource struct {
client *radarr.APIClient
}
// Tag describes the tag data model.
type Tag struct {
Label types.String `tfsdk:"label"`
ID types.Int64 `tfsdk:"id"`
}
func (r *TagResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + tagResourceName
}
func (r *TagResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "<!-- subcategory:Tags -->Tag resource.\nFor more information refer to [Tags](https://wiki.servarr.com/radarr/settings#tags) documentation.",
Attributes: map[string]schema.Attribute{
"label": schema.StringAttribute{
MarkdownDescription: "Tag label. It must be lowercase.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^.*[^A-Z]+.*$`),
"String cannot contains uppercase values",
),
},
},
"id": schema.Int64Attribute{
MarkdownDescription: "Tag ID.",
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
},
}
}
func (r *TagResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if client := helpers.ResourceConfigure(ctx, req, resp); client != nil {
r.client = client
}
}
func (r *TagResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var tag *Tag
resp.Diagnostics.Append(req.Plan.Get(ctx, &tag)...)
if resp.Diagnostics.HasError() {
return
}
// Create new Tag
request := *radarr.NewTagResource()
request.SetLabel(tag.Label.ValueString())
response, _, err := r.client.TagApi.CreateTag(ctx).TagResource(request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Create, tagResourceName, err))
return
}
tflog.Trace(ctx, "created tag: "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
tag.write(response)
resp.Diagnostics.Append(resp.State.Set(ctx, &tag)...)
}
func (r *TagResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var tag *Tag
resp.Diagnostics.Append(req.State.Get(ctx, &tag)...)
if resp.Diagnostics.HasError() {
return
}
// Get tag current value
response, _, err := r.client.TagApi.GetTagById(ctx, int32(tag.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, tagResourceName, err))
return
}
tflog.Trace(ctx, "read "+tagResourceName+": "+strconv.Itoa(int(response.GetId())))
// Map response body to resource schema attribute
tag.write(response)
resp.Diagnostics.Append(resp.State.Set(ctx, &tag)...)
}
func (r *TagResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var tag *Tag
resp.Diagnostics.Append(req.Plan.Get(ctx, &tag)...)
if resp.Diagnostics.HasError() {
return
}
// Update Tag
tagResource := *radarr.NewTagResource()
tagResource.SetLabel(tag.Label.ValueString())
tagResource.SetId(int32(tag.ID.ValueInt64()))
response, _, err := r.client.TagApi.UpdateTag(ctx, fmt.Sprint(tagResource.GetId())).TagResource(tagResource).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Update, tagResourceName, err))
return
}
tflog.Trace(ctx, "updated "+tagResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
tag.write(response)
resp.Diagnostics.Append(resp.State.Set(ctx, &tag)...)
}
func (r *TagResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var tag *Tag
resp.Diagnostics.Append(req.State.Get(ctx, &tag)...)
if resp.Diagnostics.HasError() {
return
}
// Delete tag current value
_, err := r.client.TagApi.DeleteTag(ctx, int32(tag.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, tagResourceName, err))
return
}
tflog.Trace(ctx, "deleted "+tagResourceName+": "+strconv.Itoa(int(tag.ID.ValueInt64())))
resp.State.RemoveResource(ctx)
}
func (r *TagResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
helpers.ImportStatePassthroughIntID(ctx, path.Root("id"), req, resp)
tflog.Trace(ctx, "imported "+tagResourceName+": "+req.ID)
}
func (t *Tag) write(tag *radarr.TagResource) {
t.ID = types.Int64Value(int64(tag.GetId()))
t.Label = types.StringValue(tag.GetLabel())
}