forked from jianyuan/terraform-provider-sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
428 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package sentry | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/canva/terraform-provider-sentry/sentryclient" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceSentryFilter() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceSentryFilterUpdate, | ||
Read: resourceSentryFilterRead, | ||
Update: resourceSentryFilterUpdate, | ||
Delete: resourceSentryFilterDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceSentryFilterImporter, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"organization": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The slug of the organization the project belongs to", | ||
}, | ||
"project": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The slug of the project to create the plugin for", | ||
}, | ||
"browser_extension": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
Description: "Whether to filter out events from browser extension", | ||
}, | ||
"legacy_browsers": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Description: "Events from these legacy browsers will be ignored", | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceSentryFilterRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*sentryclient.Client) | ||
org := d.Get("organization").(string) | ||
project := d.Get("project").(string) | ||
filterConfig, _, err := client.ProjectFilter.Get(org, project) | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId(fmt.Sprintf("%s-%s_filter", org, project)) | ||
d.Set("organization", org) | ||
d.Set("project", project) | ||
d.Set("browser_extension", filterConfig.BrowserExtension) | ||
return nil | ||
} | ||
|
||
func resourceSentryFilterUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*sentryclient.Client) | ||
browserExtension := d.Get("browser_extension").(bool) | ||
inputLegacyBrowsers := d.Get("legacy_browsers").([]interface{}) | ||
legacyBrowsers := make([]string, len(inputLegacyBrowsers)) | ||
for idx, browser := range inputLegacyBrowsers { | ||
legacyBrowsers[idx] = browser.(string) | ||
} | ||
org := d.Get("organization").(string) | ||
project := d.Get("project").(string) | ||
_, err := client.ProjectFilter.UpdateBrowserExtensions(org, project, browserExtension) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = client.ProjectFilter.UpdateLegacyBrowser(org, project, legacyBrowsers) | ||
if err != nil { | ||
return err | ||
} | ||
return resourceSentryFilterRead(d, meta) | ||
} | ||
|
||
func resourceSentryFilterDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*sentryclient.Client) | ||
org := d.Get("organization").(string) | ||
project := d.Get("project").(string) | ||
_, err := client.ProjectFilter.UpdateBrowserExtensions(org, project, false) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = client.ProjectFilter.UpdateLegacyBrowser(org, project, []string{}) | ||
if err != nil { | ||
return err | ||
} | ||
return resourceSentryFilterRead(d, meta) | ||
} | ||
|
||
func resourceSentryFilterImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
addrID := d.Id() | ||
|
||
log.Printf("[DEBUG] Importing filter using ADDR ID %s", addrID) | ||
|
||
parts := strings.Split(addrID, "/") | ||
|
||
if len(parts) != 3 { | ||
return nil, errors.New("Project import requires an ADDR ID of the following schema org-slug/project-slug/rule-id") | ||
} | ||
|
||
d.Set("organization", parts[0]) | ||
d.Set("project", parts[1]) | ||
d.SetId(parts[2]) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package sentry | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/canva/terraform-provider-sentry/sentryclient" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccSentryFilterUpdate(t *testing.T) { | ||
var filterConfig sentryclient.FilterConfig | ||
testAccSentryFilterUpdateConfig := fmt.Sprintf(` | ||
resource "sentry_filter" "test_filter" { | ||
organization = "%s" | ||
project = "%s" | ||
browser_extension = true | ||
legacy_browsers = ["ie_pre_9","ie10"] | ||
} | ||
`, testOrganization, projectSlug) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckSentryFilterDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSentryFilterUpdateConfig, | ||
Check: testFilterConfig(&filterConfig, true, []string{"ie_pre_9", "ie10"}), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func contains(array []string, element string) bool { | ||
for _, a := range array { | ||
if a == element { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func testFilterConfig(filterConfig *sentryclient.FilterConfig, browserExtension bool, legacyBrowsers []string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*sentryclient.Client) | ||
filterConfig, _, err := client.ProjectFilter.Get(testOrganization, projectSlug) | ||
if err != nil { | ||
return err | ||
} | ||
if filterConfig.BrowserExtension != browserExtension { | ||
return fmt.Errorf("got browser_extension %t; want %t", filterConfig.BrowserExtension, browserExtension) | ||
} | ||
|
||
for _, browser := range legacyBrowsers { | ||
if !contains(filterConfig.LegacyBrowsers, browser) { | ||
return fmt.Errorf("got legacy_browser %v; want %v", filterConfig.LegacyBrowsers, legacyBrowsers) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckSentryFilterDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*sentryclient.Client) | ||
filterConfig, _, err := client.ProjectFilter.Get(testOrganization, projectSlug) | ||
if err != nil { | ||
return err | ||
} | ||
if filterConfig.BrowserExtension != false { | ||
return fmt.Errorf("got browser_extension %t; want false", filterConfig.BrowserExtension) | ||
} | ||
if len(filterConfig.LegacyBrowsers) != 0 { | ||
return fmt.Errorf("got legacy_browser %v; want []", filterConfig.LegacyBrowsers) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.