/
resource_project_inbound_data_filter.go
289 lines (242 loc) · 9.72 KB
/
resource_project_inbound_data_filter.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package provider
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"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/jianyuan/go-sentry/v2/sentry"
)
var _ resource.Resource = &ProjectInboundDataFilterResource{}
var _ resource.ResourceWithImportState = &ProjectInboundDataFilterResource{}
func NewProjectInboundDataFilterResource() resource.Resource {
return &ProjectInboundDataFilterResource{}
}
type ProjectInboundDataFilterResource struct {
client *sentry.Client
}
type ProjectInboundDataFilterResourceModel struct {
Id types.String `tfsdk:"id"`
Organization types.String `tfsdk:"organization"`
Project types.String `tfsdk:"project"`
FilterId types.String `tfsdk:"filter_id"`
Active types.Bool `tfsdk:"active"`
Subfilters types.List `tfsdk:"subfilters"`
}
func (m *ProjectInboundDataFilterResourceModel) Fill(organization string, project string, filterId string, filter sentry.ProjectInboundDataFilter) error {
m.Id = types.StringValue(buildThreePartID(organization, project, filterId))
m.Organization = types.StringValue(organization)
m.Project = types.StringValue(project)
m.FilterId = types.StringValue(filterId)
if filter.Active.IsBool {
m.Active = types.BoolValue(filter.Active.BoolVal)
} else {
subfilterElements := []attr.Value{}
for _, subfilter := range filter.Active.StringSliceVal {
subfilterElements = append(subfilterElements, types.StringValue(subfilter))
}
m.Subfilters = types.ListValueMust(types.StringType, subfilterElements)
}
return nil
}
func (r *ProjectInboundDataFilterResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_project_inbound_data_filter"
}
func (r *ProjectInboundDataFilterResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Sentry Project Inbound Data Filter resource. This resource is used to create and manage inbound data filters for a project. For more information on what filters are available, see the [Sentry documentation](https://docs.sentry.io/api/projects/update-an-inbound-data-filter/).",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The ID of this resource.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"organization": schema.StringAttribute{
Description: "The slug of the organization the project belongs to.",
Required: true,
},
"project": schema.StringAttribute{
Description: "The slug of the project to create the filter for.",
Required: true,
},
"filter_id": schema.StringAttribute{
Description: "The type of filter toggle to update. See the [Sentry documentation](https://docs.sentry.io/api/projects/update-an-inbound-data-filter/) for a list of available filters.",
Required: true,
},
"active": schema.BoolAttribute{
Description: "Toggle the browser-extensions, localhost, filtered-transaction, or web-crawlers filter on or off.",
Optional: true,
Validators: []validator.Bool{
boolvalidator.ConflictsWith(
path.MatchRelative().AtParent().AtName("subfilters"),
),
},
},
"subfilters": schema.ListAttribute{
Description: "Specifies which legacy browser filters should be active. Anything excluded from the list will be disabled. See the [Sentry documentation](https://docs.sentry.io/api/projects/update-an-inbound-data-filter/) for a list of available subfilters.",
Optional: true,
ElementType: types.StringType,
Validators: []validator.List{
listvalidator.ConflictsWith(
path.MatchRelative().AtParent().AtName("active"),
),
},
},
},
}
}
func (r *ProjectInboundDataFilterResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*sentry.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *sentry.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *ProjectInboundDataFilterResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data ProjectInboundDataFilterResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
subfilters := []string{}
if !data.Subfilters.IsNull() {
resp.Diagnostics.Append(data.Subfilters.ElementsAs(ctx, &subfilters, false)...)
}
_, err := r.client.ProjectInboundDataFilters.Update(
ctx,
data.Organization.ValueString(),
data.Project.ValueString(),
data.FilterId.ValueString(),
&sentry.UpdateProjectInboundDataFilterParams{
Active: data.Active.ValueBoolPointer(),
Subfilters: subfilters,
},
)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error creating project inbound data filter: %s", err.Error()))
return
}
data.Id = types.StringValue(buildThreePartID(data.Organization.ValueString(), data.Project.ValueString(), data.FilterId.ValueString()))
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ProjectInboundDataFilterResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data ProjectInboundDataFilterResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
filters, _, err := r.client.ProjectInboundDataFilters.List(
ctx,
data.Organization.ValueString(),
data.Project.ValueString(),
)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading project inbound data filters: %s", err.Error()))
resp.State.RemoveResource(ctx)
return
}
var foundFilter *sentry.ProjectInboundDataFilter
for _, filter := range filters {
if filter.ID == data.FilterId.ValueString() {
foundFilter = filter
break
}
}
if foundFilter == nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading project inbound data filters: %s", "Filter not found"))
resp.State.RemoveResource(ctx)
return
}
if err := data.Fill(data.Organization.ValueString(), data.Project.ValueString(), data.FilterId.ValueString(), *foundFilter); err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error filling project inbound data filter: %s", err.Error()))
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ProjectInboundDataFilterResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan ProjectInboundDataFilterResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
subfilters := []string{}
if !plan.Subfilters.IsNull() {
resp.Diagnostics.Append(plan.Subfilters.ElementsAs(ctx, &subfilters, false)...)
}
_, err := r.client.ProjectInboundDataFilters.Update(
ctx,
plan.Organization.ValueString(),
plan.Project.ValueString(),
plan.FilterId.ValueString(),
&sentry.UpdateProjectInboundDataFilterParams{
Active: plan.Active.ValueBoolPointer(),
Subfilters: subfilters,
},
)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error updating project inbound data filter: %s", err.Error()))
return
}
plan.Id = types.StringValue(buildThreePartID(plan.Organization.ValueString(), plan.Project.ValueString(), plan.FilterId.ValueString()))
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *ProjectInboundDataFilterResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data ProjectInboundDataFilterResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
apiResp, err := r.client.ProjectInboundDataFilters.Update(
ctx,
data.Organization.ValueString(),
data.Project.ValueString(),
data.FilterId.ValueString(),
&sentry.UpdateProjectInboundDataFilterParams{
Active: sentry.Bool(false),
},
)
if apiResp.StatusCode == http.StatusNotFound {
return
}
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error deleting project inbound data filter: %s", err.Error()))
return
}
}
func (r *ProjectInboundDataFilterResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
organization, project, filterID, err := splitThreePartID(req.ID, "organization", "project-slug", "filter-id")
if err != nil {
resp.Diagnostics.AddError("Import Error", fmt.Sprintf("Unable to import team, got error: %s", err))
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(
ctx, path.Root("organization"), organization,
)...)
resp.Diagnostics.Append(resp.State.SetAttribute(
ctx, path.Root("project"), project,
)...)
resp.Diagnostics.Append(resp.State.SetAttribute(
ctx, path.Root("filter_id"), filterID,
)...)
resp.Diagnostics.Append(resp.State.SetAttribute(
ctx, path.Root("id"), req.ID,
)...)
}