Skip to content

Commit

Permalink
Allow rate-limit to be set for the client key (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
fa93hws authored Nov 26, 2019
1 parent 59d2ffd commit 5ba3dbb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
21 changes: 17 additions & 4 deletions sentry/resource_sentry_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ func resourceSentryKey() *schema.Resource {
Computed: true,
},
"rate_limit_window": {
Type: schema.TypeInt,
Computed: true,
Type: schema.TypeInt,
Computed: true,
Description: "time window in second for rate_limit_count",
Optional: true,
},
"rate_limit_count": {
Type: schema.TypeInt,
Computed: true,
Type: schema.TypeInt,
Computed: true,
Description: "rate limit for the key in rate_limit_window time",
Optional: true,
},
"dsn_secret": {
Type: schema.TypeString,
Expand Down Expand Up @@ -149,6 +153,15 @@ func resourceSentryKeyUpdate(d *schema.ResourceData, meta interface{}) error {
Name: d.Get("name").(string),
}

rlc := d.Get("rate_limit_count").(int)
rlw := d.Get("rate_limit_window").(int)
if rlc+rlw > 0 {
params.RateLimit = &sentryclient.ProjectKeyRateLimit{
Window: rlw,
Count: rlc,
}
}

key, _, err := client.ProjectKeys.Update(org, project, id, params)
if err != nil {
return err
Expand Down
70 changes: 70 additions & 0 deletions sentry/resource_sentry_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ func TestAccSentryKey_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("sentry_key.test_key", "dsn_csp"),
),
},
{
Config: testAccSentryRateLimitUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckSentryKeyExists("sentry_key.test_key", &key),
// This only work if the account has enterprise plan enabled
testAccCheckSentryKeyAttributes(&key, &testAccSentryKeyExpectedAttributes{
RateLimit: &sentryclient.ProjectKeyRateLimit{
Count: 2000,
Window: 300,
},
}),
resource.TestCheckResourceAttr("sentry_key.test_key", "name", "Test key changed"),
resource.TestCheckResourceAttrSet("sentry_key.test_key", "public"),
resource.TestCheckResourceAttrSet("sentry_key.test_key", "secret"),
resource.TestCheckResourceAttrSet("sentry_key.test_key", "dsn_secret"),
resource.TestCheckResourceAttrSet("sentry_key.test_key", "dsn_public"),
resource.TestCheckResourceAttrSet("sentry_key.test_key", "dsn_csp"),
),
},
},
})
}
Expand Down Expand Up @@ -103,6 +122,35 @@ func testAccCheckSentryKeyExists(n string, projectKey *sentryclient.ProjectKey)
}
}

type testAccSentryKeyExpectedAttributes struct {
RateLimit *sentryclient.ProjectKeyRateLimit
}

func checkRateLimit(get *sentryclient.ProjectKeyRateLimit, want *sentryclient.ProjectKeyRateLimit) error {
if get == nil && want == nil {
return nil
}
if get == nil && want != nil {
return errors.New("got nil rate limit but want non-nil")
}
if get != nil && want == nil {
return errors.New("got non-nil rate limit but want nil")
}
if get.Window != want.Window {
return fmt.Errorf("got RateLimit.window %v; want %v", get.Window, want.Window)
}
if get.Count != want.Count {
return fmt.Errorf("got RateLimit.window %v; want %v", get.Count, want.Count)
}
return nil
}

func testAccCheckSentryKeyAttributes(key *sentryclient.ProjectKey, want *testAccSentryKeyExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {
return checkRateLimit(key.RateLimit, want.RateLimit)
}
}

var testAccSentryKeyConfig = fmt.Sprintf(`
resource "sentry_team" "test_team" {
organization = "%s"
Expand Down Expand Up @@ -140,3 +188,25 @@ var testAccSentryKeyUpdateConfig = fmt.Sprintf(`
name = "Test key changed"
}
`, testOrganization, testOrganization, testOrganization)

var testAccSentryRateLimitUpdateConfig = fmt.Sprintf(`
resource "sentry_team" "test_team" {
organization = "%s"
name = "Test team"
}
resource "sentry_project" "test_project" {
organization = "%s"
team = "${sentry_team.test_team.id}"
name = "Test project"
}
resource "sentry_key" "test_key" {
organization = "%s"
project = "${sentry_project.test_project.id}"
name = "Test key changed"
rate_limit_window = 300
rate_limit_count = 2000
}
`, testOrganization, testOrganization, testOrganization)
3 changes: 2 additions & 1 deletion sentryclient/project_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func (s *ProjectKeyService) Create(organizationSlug string, projectSlug string,

// UpdateProjectKeyParams are the parameters for ProjectKeyService.Update.
type UpdateProjectKeyParams struct {
Name string `json:"name,omitempty"`
Name string `json:"name,omitempty"`
RateLimit *ProjectKeyRateLimit `json:"rateLimit,omitempty"`
}

// Update a client key.
Expand Down
17 changes: 16 additions & 1 deletion sentryclient/project_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func TestProjectKeyService_Update(t *testing.T) {
assertMethod(t, "PUT", r)
assertPostJSON(t, map[string]interface{}{
"name": "Fabulous Key",
"rateLimit": map[string]interface{}{
"window": float64(300),
"count": float64(2000),
},
}, r)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{
Expand Down Expand Up @@ -186,14 +190,21 @@ func TestProjectKeyService_Update(t *testing.T) {
"name": "Fabulous Key",
"projectId": 2,
"public": "cfc7b0341c6e4f6ea1a9d256a30dba00",
"rateLimit": null,
"rateLimit": {
"window": 300,
"count": 2000
},
"secret": "a07dcd97aa56481f82aeabaed43ca448"
}`)
})

client := NewClient(httpClient, nil, "")
params := &UpdateProjectKeyParams{
Name: "Fabulous Key",
RateLimit: &ProjectKeyRateLimit{
Window: 300,
Count: 2000,
},
}
projectKey, _, err := client.ProjectKeys.Update("the-interstellar-jurisdiction", "pump-station", "befdbf32724c4ae0a3d286717b1f8127", params)
assert.NoError(t, err)
Expand All @@ -214,6 +225,10 @@ func TestProjectKeyService_Update(t *testing.T) {
CDN: "https://sentry.io/js-sdk-loader/cfc7b0341c6e4f6ea1a9d256a30dba00.min.js",
},
DateCreated: mustParseTime("2018-09-20T15:48:07.397Z"),
RateLimit: &ProjectKeyRateLimit{
Window: 300,
Count: 2000,
},
}
assert.Equal(t, expected, projectKey)
}
Expand Down

0 comments on commit 5ba3dbb

Please sign in to comment.