Skip to content

Commit 61bc3c9

Browse files
Introduce checkClientGet for 404 handling (#70)
This new helper properly and uniformly handles Sentry API 404 errors with respect to the Terraform SDK. Prior to this commit, in most places an API error was always interpretted as a missing resource. Now, a 404 is considered a missing resource and other API errors are surfaced as real errors. This will ensure that, for example, a user with a malformed API token (bad permissions, etc) doesn't wipe out Terraform state. Fixes the following Github issues: - #40 - #57
1 parent b9d9128 commit 61bc3c9

7 files changed

+40
-26
lines changed

sentry/helpers.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package sentry
22

3+
import (
4+
"net/http"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7+
)
8+
39
// Bool returns a pointer to the bool value.
410
func Bool(v bool) *bool {
511
return &v
@@ -9,3 +15,21 @@ func Bool(v bool) *bool {
915
func Int(v int) *int {
1016
return &v
1117
}
18+
19+
// checkClientGet returns a `found` bool and an `error` to indicate if a Get request was successful.
20+
// The following return values are meaningful:
21+
// `true`, `nil` => a resource was successfully found
22+
// `false`, `nil` => a resource was successfully not found
23+
// `false`, `err` => encountered an unexpected error
24+
func checkClientGet(resp *http.Response, err error, d *schema.ResourceData) (bool, error) {
25+
if err != nil {
26+
if resp.StatusCode == http.StatusNotFound {
27+
d.SetId("")
28+
return false, nil
29+
}
30+
31+
return false, err
32+
}
33+
34+
return true, nil
35+
}

sentry/resource_sentry_key.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package sentry
22

33
import (
4-
"net/http"
5-
64
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
75
"github.com/jianyuan/go-sentry/sentry"
86
)
@@ -106,10 +104,7 @@ func resourceSentryKeyRead(d *schema.ResourceData, meta interface{}) error {
106104
project := d.Get("project").(string)
107105

108106
keys, resp, err := client.ProjectKeys.List(org, project)
109-
if err != nil && resp.StatusCode == http.StatusNotFound {
110-
d.SetId("")
111-
return nil
112-
} else if err != nil {
107+
if found, err := checkClientGet(resp, err, d); !found {
113108
return err
114109
}
115110

sentry/resource_sentry_organization.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ func resourceSentryOrganizationRead(d *schema.ResourceData, meta interface{}) er
6363
slug := d.Id()
6464
log.Printf("[DEBUG] Reading Sentry organization %s", slug)
6565

66-
org, _, err := client.Organizations.Get(slug)
67-
if err != nil {
68-
d.SetId("")
69-
return nil
66+
org, resp, err := client.Organizations.Get(slug)
67+
if found, err := checkClientGet(resp, err, d); !found {
68+
return err
7069
}
7170

7271
d.SetId(org.Slug)

sentry/resource_sentry_project.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ func resourceSentryProjectRead(d *schema.ResourceData, meta interface{}) error {
114114
slug := d.Id()
115115
org := d.Get("organization").(string)
116116

117-
proj, _, err := client.Projects.Get(org, slug)
118-
if err != nil {
119-
d.SetId("")
120-
return nil
117+
proj, resp, err := client.Projects.Get(org, slug)
118+
if found, err := checkClientGet(resp, err, d); !found {
119+
return err
121120
}
122121

123122
d.SetId(proj.Slug)

sentry/resource_sentry_project_plugin.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ func resourceSentryPluginRead(d *schema.ResourceData, meta interface{}) error {
7272
org := d.Get("organization").(string)
7373
project := d.Get("project").(string)
7474

75-
plugin, _, err := client.ProjectPlugins.Get(org, project, id)
76-
if err != nil {
77-
d.SetId("")
78-
return nil
75+
plugin, resp, err := client.ProjectPlugins.Get(org, project, id)
76+
if found, err := checkClientGet(resp, err, d); !found {
77+
return err
7978
}
8079

8180
d.SetId(plugin.ID)

sentry/resource_sentry_project_rule.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ func resourceSentryRuleRead(d *schema.ResourceData, meta interface{}) error {
135135
project := d.Get("project").(string)
136136
id := d.Id()
137137

138-
rules, _, err := client.Rules.List(org, project)
139-
if err != nil {
140-
d.SetId("")
141-
return nil
138+
rules, resp, err := client.Rules.List(org, project)
139+
if found, err := checkClientGet(resp, err, d); !found {
140+
return err
142141
}
143142

144143
var rule *sentry.Rule

sentry/resource_sentry_team.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ func resourceSentryTeamRead(d *schema.ResourceData, meta interface{}) error {
8080
org := d.Get("organization").(string)
8181
log.Printf("[DEBUG] Reading Sentry team %s (Organization: %s)", slug, org)
8282

83-
team, _, err := client.Teams.Get(org, slug)
84-
if err != nil {
85-
d.SetId("")
86-
return nil
83+
team, resp, err := client.Teams.Get(org, slug)
84+
if found, err := checkClientGet(resp, err, d); !found {
85+
return err
8786
}
8887

8988
d.SetId(team.Slug)

0 commit comments

Comments
 (0)