Skip to content

Commit

Permalink
Merge pull request #33 from Nessex/add-project-key-rate-limit
Browse files Browse the repository at this point in the history
Add project key rate limit
  • Loading branch information
jianyuan authored Dec 2, 2019
2 parents 3e700f6 + a50c0a0 commit 1176a44
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
11 changes: 11 additions & 0 deletions sentry/resource_sentry_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ func resourceSentryKey() *schema.Resource {
},
"rate_limit_window": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"rate_limit_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"dsn_secret": {
Expand All @@ -80,6 +82,10 @@ func resourceSentryKeyCreate(d *schema.ResourceData, meta interface{}) error {
project := d.Get("project").(string)
params := &sentry.CreateProjectKeyParams{
Name: d.Get("name").(string),
RateLimit: &sentry.ProjectKeyRateLimit{
Window: d.Get("rate_limit_window").(int),
Count: d.Get("rate_limit_count").(int),
},
}

key, _, err := client.ProjectKeys.Create(org, project, params)
Expand All @@ -88,6 +94,7 @@ func resourceSentryKeyCreate(d *schema.ResourceData, meta interface{}) error {
}

d.SetId(key.ID)

return resourceSentryKeyRead(d, meta)
}

Expand Down Expand Up @@ -147,6 +154,10 @@ func resourceSentryKeyUpdate(d *schema.ResourceData, meta interface{}) error {
project := d.Get("project").(string)
params := &sentry.UpdateProjectKeyParams{
Name: d.Get("name").(string),
RateLimit: &sentry.ProjectKeyRateLimit{
Window: d.Get("rate_limit_window").(int),
Count: d.Get("rate_limit_count").(int),
},
}

key, _, err := client.ProjectKeys.Update(org, project, id, params)
Expand Down
60 changes: 52 additions & 8 deletions sentry/resource_sentry_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ func TestAccSentryKey_basic(t *testing.T) {
})
}

func TestAccSentryKey_RateLimit(t *testing.T) {
var key sentry.ProjectKey

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSentryKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccSentryKeyConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckSentryKeyExists("sentry_key.test_key_rate_limit", &key),
resource.TestCheckResourceAttr("sentry_key.test_key_rate_limit", "rate_limit_window", "86400"),
resource.TestCheckResourceAttr("sentry_key.test_key_rate_limit", "rate_limit_count", "1000"),
),
},
{
Config: testAccSentryKeyUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckSentryKeyExists("sentry_key.test_key_rate_limit", &key),
resource.TestCheckResourceAttr("sentry_key.test_key_rate_limit", "rate_limit_window", "100"),
resource.TestCheckResourceAttr("sentry_key.test_key_rate_limit", "rate_limit_count", "100"),
),
},
},
})
}

func testAccCheckSentryKeyDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*sentry.Client)

Expand Down Expand Up @@ -105,38 +133,54 @@ func testAccCheckSentryKeyExists(n string, projectKey *sentry.ProjectKey) resour

var testAccSentryKeyConfig = fmt.Sprintf(`
resource "sentry_team" "test_team" {
organization = "%s"
organization = "%[1]s"
name = "Test team"
}
resource "sentry_project" "test_project" {
organization = "%s"
organization = "%[1]s"
team = "${sentry_team.test_team.id}"
name = "Test project"
}
resource "sentry_key" "test_key" {
organization = "%s"
organization = "%[1]s"
project = "${sentry_project.test_project.id}"
name = "Test key"
}
resource "sentry_key" "test_key_rate_limit" {
organization = "%[1]s"
project = "${sentry_project.test_project.id}"
name = "Test key"
rate_limit_window = 86400
rate_limit_count = 1000
}
`, testOrganization, testOrganization, testOrganization)
`, testOrganization)

var testAccSentryKeyUpdateConfig = fmt.Sprintf(`
resource "sentry_team" "test_team" {
organization = "%s"
organization = "%[1]s"
name = "Test team"
}
resource "sentry_project" "test_project" {
organization = "%s"
organization = "%[1]s"
team = "${sentry_team.test_team.id}"
name = "Test project"
}
resource "sentry_key" "test_key" {
organization = "%s"
organization = "%[1]s"
project = "${sentry_project.test_project.id}"
name = "Test key changed"
}
`, testOrganization, testOrganization, testOrganization)
resource "sentry_key" "test_key_rate_limit" {
organization = "%[1]s"
project = "${sentry_project.test_project.id}"
name = "Test key"
rate_limit_window = 100
rate_limit_count = 100
}
`, testOrganization)

0 comments on commit 1176a44

Please sign in to comment.