/
template_resource.go
236 lines (203 loc) · 7.23 KB
/
template_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"fmt"
"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/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/kenzo0107/sendgrid"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &templateResource{}
var _ resource.ResourceWithImportState = &templateResource{}
func newTemplateResource() resource.Resource {
return &templateResource{}
}
type templateResource struct {
client *sendgrid.Client
}
type templateResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Generation types.String `tfsdk:"generation"`
}
func (r *templateResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_template"
}
func (r *templateResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `
Provides template resource.
An HTML template that can establish a consistent design for [transactional emails](https://sendgrid.com/use-cases/transactional-email/).
Each parent account, as well as each Subuser, can create up to 300 different transactional templates. Templates are specific to the parent account or Subuser, meaning templates created on a parent account will not be accessible from the parent's Subuser accounts.
Transactional templates are templates created specifically for transactional email and are not to be confused with [Marketing Campaigns designs](https://sendgrid.com/docs/ui/sending-email/working-with-marketing-campaigns-email-designs/). For more information about transactional templates, please see our [Dynamic Transactional Templates documentation](https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/).
`,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the transactional template.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name for the transactional template. maxLength: 100",
Required: true,
},
"generation": schema.StringAttribute{
MarkdownDescription: "Defines the generation of the template. Allowed Values: `legacy`, `dynamic`",
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringOneOf("legacy", "dynamic"),
},
},
},
}
}
func (r *templateResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*sendgrid.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *sendgrid.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *templateResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan templateResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
res, err := retryOnRateLimit(ctx, func() (interface{}, error) {
return r.client.CreateTemplate(ctx, &sendgrid.InputCreateTemplate{
Name: plan.Name.ValueString(),
Generation: plan.Generation.ValueString(),
})
})
if err != nil {
resp.Diagnostics.AddError(
"Creating template",
fmt.Sprintf("Unable to create template, got error: %s", err),
)
return
}
o, ok := res.(*sendgrid.OutputCreateTemplate)
if !ok {
resp.Diagnostics.AddError(
"Creating template",
"Failed to assert type *sendgrid.OutputCreateTemplate",
)
return
}
plan = templateResourceModel{
ID: types.StringValue(o.ID),
Name: types.StringValue(o.Name),
Generation: types.StringValue(o.Generation),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *templateResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state templateResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
id := state.ID.ValueString()
o, err := r.client.GetTemplate(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Reading template",
fmt.Sprintf("Unable to read template (id: %v), got error: %s", id, err),
)
return
}
state = templateResourceModel{
ID: types.StringValue(o.ID),
Name: types.StringValue(o.Name),
Generation: types.StringValue(o.Generation),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *templateResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data, state templateResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
id := state.ID.ValueString()
o, err := r.client.UpdateTemplate(ctx, id, &sendgrid.InputUpdateTemplate{
Name: data.Name.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError(
"Updating template",
fmt.Sprintf("Unable to update template (id: %v), got error: %s", id, err),
)
return
}
data = templateResourceModel{
ID: state.ID,
Name: types.StringValue(o.Name),
Generation: types.StringValue(o.Generation),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *templateResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state templateResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
id := state.ID.ValueString()
if err := r.client.DeleteTemplate(ctx, id); err != nil {
resp.Diagnostics.AddError(
"Deleting template",
fmt.Sprintf("Unable to delete template (id: %v), got error: %s", id, err),
)
return
}
}
func (r *templateResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
var data templateResourceModel
id := req.ID
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
o, err := r.client.GetTemplate(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Importing template",
fmt.Sprintf("Unable to read template, got error: %s", err),
)
return
}
data = templateResourceModel{
ID: types.StringValue(o.ID),
Name: types.StringValue(o.Name),
Generation: types.StringValue(o.Generation),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
}