Skip to content

Commit 6df3529

Browse files
committed
feat: new sentry_all_projects_spike_protection resource (#429)
1 parent b187a07 commit 6df3529

File tree

11 files changed

+537
-89
lines changed

11 files changed

+537
-89
lines changed

docs/data-sources/all_projects.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ data "sentry_projects" "default" {
2828

2929
### Read-Only
3030

31+
- `project_slugs` (Set of String) The slugs of the projects.
3132
- `projects` (Attributes Set) The list of projects. (see [below for nested schema](#nestedatt--projects))
3233

3334
<a id="nestedatt--projects"></a>
@@ -40,16 +41,5 @@ Read-Only:
4041
- `features` (Set of String) The features of this project.
4142
- `id` (String) The ID of this project.
4243
- `name` (String) The name of this project.
43-
- `organization` (Attributes) The organization associated with this project. (see [below for nested schema](#nestedatt--projects--organization))
4444
- `platform` (String) The platform of this project.
4545
- `slug` (String) The slug of this project.
46-
- `status` (String) The status of this project.
47-
48-
<a id="nestedatt--projects--organization"></a>
49-
### Nested Schema for `projects.organization`
50-
51-
Read-Only:
52-
53-
- `id` (String) The ID of this organization.
54-
- `name` (String) The name of this organization.
55-
- `slug` (String) The slug of this organization.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "sentry_all_projects_spike_protection Resource - terraform-provider-sentry"
4+
subcategory: ""
5+
description: |-
6+
Enable spike protection for all projects in an organization.
7+
---
8+
9+
# sentry_all_projects_spike_protection (Resource)
10+
11+
Enable spike protection for all projects in an organization.
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Enable spike protection for several projects in a Sentry organization.
17+
resource "sentry_project" "web-app" {
18+
organization = "my-organization"
19+
20+
teams = ["my-first-team"]
21+
name = "web-app"
22+
slug = "web-app"
23+
24+
platform = "go"
25+
}
26+
27+
resource "sentry_project" "mobile-app" {
28+
organization = "my-organization"
29+
30+
teams = ["my-second-team"]
31+
name = "mobile-app"
32+
slug = "mobile-app"
33+
34+
platform = "android"
35+
}
36+
37+
resource "sentry_all_projects_spike_protection" "main" {
38+
organization = "my-organization"
39+
projects = [
40+
sentry_project.web-app.id,
41+
sentry_project.mobile-app.id,
42+
]
43+
enabled = true
44+
}
45+
46+
# Use the `sentry_all_projects` data source to get all projects in a Sentry organization and enable spike protection for all of them.
47+
data "sentry_all_projects" "all" {
48+
organization = "my-organization"
49+
}
50+
51+
resource "sentry_all_projects_spike_protection" "main" {
52+
organization = data.sentry_all_projects.all.organization
53+
projects = data.sentry_all_projects.all.project_slugs
54+
enabled = true
55+
}
56+
```
57+
58+
<!-- schema generated by tfplugindocs -->
59+
## Schema
60+
61+
### Required
62+
63+
- `enabled` (Boolean) Toggle the browser-extensions, localhost, filtered-transaction, or web-crawlers filter on or off for all projects.
64+
- `organization` (String) The slug of the organization the resource belongs to.
65+
- `projects` (Set of String) The slugs of the projects to enable or disable spike protection for.

docs/resources/project_spike_protection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ resource "sentry_project_spike_protection" "default" {
3737

3838
- `enabled` (Boolean) Toggle the browser-extensions, localhost, filtered-transaction, or web-crawlers filter on or off.
3939
- `organization` (String) The slug of the organization the project belongs to.
40-
- `project` (String) The slug of the project to create the filter for.
40+
- `project` (String) The slug of the project to enable or disable spike protection for.
4141

4242
### Read-Only
4343

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Enable spike protection for several projects in a Sentry organization.
2+
resource "sentry_project" "web-app" {
3+
organization = "my-organization"
4+
5+
teams = ["my-first-team"]
6+
name = "web-app"
7+
slug = "web-app"
8+
9+
platform = "go"
10+
}
11+
12+
resource "sentry_project" "mobile-app" {
13+
organization = "my-organization"
14+
15+
teams = ["my-second-team"]
16+
name = "mobile-app"
17+
slug = "mobile-app"
18+
19+
platform = "android"
20+
}
21+
22+
resource "sentry_all_projects_spike_protection" "main" {
23+
organization = "my-organization"
24+
projects = [
25+
sentry_project.web-app.id,
26+
sentry_project.mobile-app.id,
27+
]
28+
enabled = true
29+
}
30+
31+
# Use the `sentry_all_projects` data source to get all projects in a Sentry organization and enable spike protection for all of them.
32+
data "sentry_all_projects" "all" {
33+
organization = "my-organization"
34+
}
35+
36+
resource "sentry_all_projects_spike_protection" "main" {
37+
organization = data.sentry_all_projects.all.organization
38+
projects = data.sentry_all_projects.all.project_slugs
39+
enabled = true
40+
}

internal/provider/data_source_all_projects.go

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ type AllProjectsDataSource struct {
2323
}
2424

2525
type AllProjectsDataSourceProjectModel struct {
26-
Id types.String `tfsdk:"id"`
27-
Slug types.String `tfsdk:"slug"`
28-
Name types.String `tfsdk:"name"`
29-
Platform types.String `tfsdk:"platform"`
30-
DateCreated types.String `tfsdk:"date_created"`
31-
Features types.Set `tfsdk:"features"`
32-
Color types.String `tfsdk:"color"`
33-
Status types.String `tfsdk:"status"`
34-
Organization OrganizationModel `tfsdk:"organization"`
26+
Id types.String `tfsdk:"id"`
27+
Slug types.String `tfsdk:"slug"`
28+
Name types.String `tfsdk:"name"`
29+
Platform types.String `tfsdk:"platform"`
30+
DateCreated types.String `tfsdk:"date_created"`
31+
Features types.Set `tfsdk:"features"`
32+
Color types.String `tfsdk:"color"`
3533
}
3634

3735
func (m *AllProjectsDataSourceProjectModel) Fill(project sentry.Project) error {
@@ -48,23 +46,25 @@ func (m *AllProjectsDataSourceProjectModel) Fill(project sentry.Project) error {
4846
m.Features = types.SetValueMust(types.StringType, featureElements)
4947

5048
m.Color = types.StringValue(project.Color)
51-
m.Status = types.StringValue(project.Status)
52-
m.Organization = OrganizationModel{}
53-
if err := m.Organization.Fill(project.Organization); err != nil {
54-
return err
55-
}
5649

5750
return nil
5851
}
5952

6053
type AllProjectsDataSourceModel struct {
6154
Organization types.String `tfsdk:"organization"`
55+
ProjectSlugs types.Set `tfsdk:"project_slugs"`
6256
Projects []AllProjectsDataSourceProjectModel `tfsdk:"projects"`
6357
}
6458

6559
func (m *AllProjectsDataSourceModel) Fill(organization string, projects []sentry.Project) error {
6660
m.Organization = types.StringValue(organization)
6761

62+
projectSlugElements := []attr.Value{}
63+
for _, project := range projects {
64+
projectSlugElements = append(projectSlugElements, types.StringValue(project.Slug))
65+
}
66+
m.ProjectSlugs = types.SetValueMust(types.StringType, projectSlugElements)
67+
6868
for _, project := range projects {
6969
p := AllProjectsDataSourceProjectModel{}
7070
if err := p.Fill(project); err != nil {
@@ -89,8 +89,14 @@ func (d *AllProjectsDataSource) Schema(ctx context.Context, req datasource.Schem
8989
MarkdownDescription: "The slug of the organization the resource belongs to.",
9090
Required: true,
9191
},
92+
"project_slugs": schema.SetAttribute{
93+
MarkdownDescription: "The slugs of the projects.",
94+
Computed: true,
95+
ElementType: types.StringType,
96+
},
9297
"projects": schema.SetNestedAttribute{
9398
MarkdownDescription: "The list of projects.",
99+
Computed: true,
94100
NestedObject: schema.NestedAttributeObject{
95101
Attributes: map[string]schema.Attribute{
96102
"id": schema.StringAttribute{
@@ -122,31 +128,8 @@ func (d *AllProjectsDataSource) Schema(ctx context.Context, req datasource.Schem
122128
MarkdownDescription: "The color of this project.",
123129
Computed: true,
124130
},
125-
"status": schema.StringAttribute{
126-
MarkdownDescription: "The status of this project.",
127-
Computed: true,
128-
},
129-
"organization": schema.SingleNestedAttribute{
130-
MarkdownDescription: "The organization associated with this project.",
131-
Computed: true,
132-
Attributes: map[string]schema.Attribute{
133-
"id": schema.StringAttribute{
134-
MarkdownDescription: "The ID of this organization.",
135-
Computed: true,
136-
},
137-
"slug": schema.StringAttribute{
138-
MarkdownDescription: "The slug of this organization.",
139-
Computed: true,
140-
},
141-
"name": schema.StringAttribute{
142-
MarkdownDescription: "The name of this organization.",
143-
Computed: true,
144-
},
145-
},
146-
},
147131
},
148132
},
149-
Computed: true,
150133
},
151134
},
152135
}
@@ -161,10 +144,10 @@ func (d *AllProjectsDataSource) Read(ctx context.Context, req datasource.ReadReq
161144
}
162145

163146
var allProjects []sentry.Project
164-
params := &sentry.ListProjectsParams{}
147+
params := &sentry.ListOrganizationProjectsParams{}
165148

166149
for {
167-
projects, apiResp, err := d.client.Projects.List(ctx, params)
150+
projects, apiResp, err := d.client.OrganizationProjects.List(ctx, data.Organization.ValueString(), params)
168151
if err != nil {
169152
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Read error: %s", err))
170153
return

internal/provider/data_source_all_projects_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,30 @@ import (
1212
)
1313

1414
func TestAccAllProjectsDataSource(t *testing.T) {
15-
dn := "data.sentry_all_projects.test"
16-
team := acctest.RandomWithPrefix("tf-team")
17-
project := acctest.RandomWithPrefix("tf-project")
15+
teamName := acctest.RandomWithPrefix("tf-team")
16+
projectName := acctest.RandomWithPrefix("tf-project")
17+
rn := "data.sentry_all_projects.test"
1818

1919
resource.Test(t, resource.TestCase{
2020
PreCheck: func() { acctest.PreCheck(t) },
2121
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
2222
Steps: []resource.TestStep{
2323
{
24-
Config: testAccAllProjectsDataSourceConfig(team, project),
24+
Config: testAccAllProjectsDataSourceConfig(teamName, projectName),
2525
ConfigStateChecks: []statecheck.StateCheck{
26-
statecheck.ExpectKnownValue(dn, tfjsonpath.New("projects"), knownvalue.ListPartial(map[int]knownvalue.Check{
27-
0: knownvalue.ObjectExact(map[string]knownvalue.Check{
26+
statecheck.ExpectKnownValue(rn, tfjsonpath.New("organization"), knownvalue.StringExact(acctest.TestOrganization)),
27+
statecheck.ExpectKnownValue(rn, tfjsonpath.New("project_slugs"), knownvalue.SetPartial([]knownvalue.Check{
28+
knownvalue.StringExact(projectName),
29+
})),
30+
statecheck.ExpectKnownValue(rn, tfjsonpath.New("projects"), knownvalue.SetPartial([]knownvalue.Check{
31+
knownvalue.ObjectExact(map[string]knownvalue.Check{
2832
"id": knownvalue.NotNull(),
29-
"slug": knownvalue.NotNull(),
30-
"name": knownvalue.NotNull(),
31-
"platform": knownvalue.NotNull(),
33+
"slug": knownvalue.StringExact(projectName),
34+
"name": knownvalue.StringExact(projectName),
35+
"platform": knownvalue.StringExact("go"),
3236
"date_created": knownvalue.NotNull(),
3337
"features": knownvalue.NotNull(),
3438
"color": knownvalue.NotNull(),
35-
"status": knownvalue.NotNull(),
36-
"organization": knownvalue.ObjectExact(map[string]knownvalue.Check{
37-
"id": knownvalue.NotNull(),
38-
"slug": knownvalue.NotNull(),
39-
"name": knownvalue.NotNull(),
40-
}),
4139
}),
4240
})),
4341
},
@@ -63,6 +61,8 @@ resource "sentry_project" "test" {
6361
6462
data "sentry_all_projects" "test" {
6563
organization = data.sentry_organization.test.slug
64+
65+
depends_on = [sentry_project.test]
6666
}
6767
`, teamName, projectName)
6868
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (p *SentryProvider) Configure(ctx context.Context, req provider.ConfigureRe
9494

9595
func (p *SentryProvider) Resources(ctx context.Context) []func() resource.Resource {
9696
return []func() resource.Resource{
97+
NewAllProjectsSpikeProtectionResource,
9798
NewClientKeyResource,
9899
NewIssueAlertResource,
99100
NewNotificationActionResource,

0 commit comments

Comments
 (0)