Skip to content

Commit

Permalink
feat: Enable/disable spike protection
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyuan committed Dec 7, 2023
1 parent 449195c commit 900aced
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
12 changes: 7 additions & 5 deletions sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,24 @@ type Client struct {
common service

// Services
DashboardWidgets *DashboardWidgetsService
Dashboards *DashboardsService
DashboardWidgets *DashboardWidgetsService
IssueAlerts *IssueAlertsService
MetricAlerts *MetricAlertsService
OrganizationCodeMappings *OrganizationCodeMappingsService
OrganizationIntegrations *OrganizationIntegrationsService
OrganizationMembers *OrganizationMembersService
OrganizationRepositories *OrganizationRepositoriesService
Organizations *OrganizationsService
ProjectFilters *ProjectFiltersService
ProjectKeys *ProjectKeysService
ProjectOwnerships *ProjectOwnershipsService
ProjectPlugins *ProjectPluginsService
Projects *ProjectsService
ProjectFilters *ProjectFiltersService
ReleaseDeployments *ReleaseDeploymentsService
Teams *TeamsService
SpikeProtections *SpikeProtectionsService
TeamMembers *TeamMembersService
Teams *TeamsService
}

type service struct {
Expand All @@ -84,8 +85,8 @@ func NewClient(httpClient *http.Client) *Client {
UserAgent: userAgent,
}
c.common.client = c
c.DashboardWidgets = (*DashboardWidgetsService)(&c.common)
c.Dashboards = (*DashboardsService)(&c.common)
c.DashboardWidgets = (*DashboardWidgetsService)(&c.common)
c.IssueAlerts = (*IssueAlertsService)(&c.common)
c.MetricAlerts = (*MetricAlertsService)(&c.common)
c.OrganizationCodeMappings = (*OrganizationCodeMappingsService)(&c.common)
Expand All @@ -99,8 +100,9 @@ func NewClient(httpClient *http.Client) *Client {
c.ProjectPlugins = (*ProjectPluginsService)(&c.common)
c.Projects = (*ProjectsService)(&c.common)
c.ReleaseDeployments = (*ReleaseDeploymentsService)(&c.common)
c.Teams = (*TeamsService)(&c.common)
c.SpikeProtections = (*SpikeProtectionsService)(&c.common)
c.TeamMembers = (*TeamMembersService)(&c.common)
c.Teams = (*TeamsService)(&c.common)
return c
}

Expand Down
33 changes: 33 additions & 0 deletions sentry/spike_protections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package sentry

import (
"context"
"fmt"
"net/http"
)

type SpikeProtectionsService service

type SpikeProtectionParams struct {
Projects []string `json:"projects"`
}

func (s *SpikeProtectionsService) Enable(ctx context.Context, organizationSlug string, params *SpikeProtectionParams) (*Response, error) {
u := fmt.Sprintf("0/organizations/%v/spike-protections/", organizationSlug)
req, err := s.client.NewRequest(http.MethodPost, u, params)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}

func (s *SpikeProtectionsService) Disable(ctx context.Context, organizationSlug string, params *SpikeProtectionParams) (*Response, error) {
u := fmt.Sprintf("0/organizations/%v/spike-protections/", organizationSlug)
req, err := s.client.NewRequest(http.MethodDelete, u, params)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}
47 changes: 47 additions & 0 deletions sentry/spike_protections_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sentry

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSpikeProtectionsService_Enable(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/api/0/organizations/organization_slug/spike-protections/", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, http.MethodPost, r)
assertPostJSON(t, map[string]interface{}{
"projects": []interface{}{"$all"},
}, r)
})

params := &SpikeProtectionParams{
Projects: []string{"$all"},
}
ctx := context.Background()
_, err := client.SpikeProtections.Enable(ctx, "organization_slug", params)
assert.NoError(t, err)
}

func TestSpikeProtectionsService_Disable(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/api/0/organizations/organization_slug/spike-protections/", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, http.MethodDelete, r)
assertPostJSON(t, map[string]interface{}{
"projects": []interface{}{"$all"},
}, r)
})

params := &SpikeProtectionParams{
Projects: []string{"$all"},
}
ctx := context.Background()
_, err := client.SpikeProtections.Disable(ctx, "organization_slug", params)
assert.NoError(t, err)
}

0 comments on commit 900aced

Please sign in to comment.