generated from hashicorp/terraform-provider-scaffolding-framework
/
tag_resource.go
203 lines (164 loc) · 5.12 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package provider
import (
"context"
"fmt"
"strconv"
"github.com/devopsarr/terraform-provider-sonarr/internal/helpers"
"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"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ provider.ResourceType = resourceTagType{}
_ resource.Resource = resourceTag{}
_ resource.ResourceWithImportState = resourceTag{}
)
type resourceTagType struct{}
type resourceTag struct {
provider sonarrProvider
}
// Tag is the tag resource.
type Tag struct {
ID types.Int64 `tfsdk:"id"`
Label types.String `tfsdk:"label"`
}
func (t resourceTagType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "Tag resource",
Attributes: map[string]tfsdk.Attribute{
"label": {
MarkdownDescription: "Tag value",
Required: true,
Type: types.StringType,
Validators: []tfsdk.AttributeValidator{
helpers.StringLowercase(),
},
},
"id": {
MarkdownDescription: "Tag ID",
Computed: true,
Type: types.Int64Type,
PlanModifiers: tfsdk.AttributePlanModifiers{
resource.UseStateForUnknown(),
},
},
},
}, nil
}
func (t resourceTagType) NewResource(ctx context.Context, in provider.Provider) (resource.Resource, diag.Diagnostics) {
provider, diags := convertProviderType(in)
return resourceTag{
provider: provider,
}, diags
}
func (r resourceTag) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan Tag
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Create new Tag
request := starr.Tag{
Label: plan.Label.Value,
}
response, err := r.provider.client.AddTagContext(ctx, &request)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create tag, got error: %s", err))
return
}
tflog.Trace(ctx, "created tag: "+strconv.Itoa(response.ID))
// Generate resource state struct
result := writeTag(response)
diags = resp.State.Set(ctx, &result)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r resourceTag) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var state Tag
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get tag current value
response, err := r.provider.client.GetTagContext(ctx, int(state.ID.Value))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read tags, got error: %s", err))
return
}
// Map response body to resource schema attribute
result := writeTag(response)
diags = resp.State.Set(ctx, &result)
resp.Diagnostics.Append(diags...)
}
func (r resourceTag) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var plan Tag
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Update Tag
request := starr.Tag{
Label: plan.Label.Value,
ID: int(plan.ID.Value),
}
response, err := r.provider.client.UpdateTagContext(ctx, &request)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update tag, got error: %s", err))
return
}
tflog.Trace(ctx, "update tag: "+strconv.Itoa(response.ID))
// Generate resource state struct
result := writeTag(response)
diags = resp.State.Set(ctx, &result)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r resourceTag) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state Tag
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Delete tag current value
err := r.provider.client.DeleteTagContext(ctx, int(state.ID.Value))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read tags, got error: %s", err))
return
}
resp.State.RemoveResource(ctx)
}
func (r resourceTag) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
id, err := strconv.Atoi(req.ID)
if err != nil {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: ID. Got: %q", req.ID),
)
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), id)...)
}
func writeTag(tag *starr.Tag) *Tag {
return &Tag{
ID: types.Int64{Value: int64(tag.ID)},
Label: types.String{Value: tag.Label},
}
}