Skip to content

Commit 3fa41d6

Browse files
fix: use deleteResourceOn404AndSwallow304OtherwiseReturnError
1 parent e3d891e commit 3fa41d6

2 files changed

Lines changed: 2 additions & 28 deletions

File tree

github/resource_github_user_ssh_key.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strconv"
99

1010
"github.com/google/go-github/v84/github"
11-
"github.com/hashicorp/terraform-plugin-log/tflog"
1211
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1312
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1413
)
@@ -102,19 +101,7 @@ func resourceGithubUserSshKeyRead(ctx context.Context, d *schema.ResourceData, m
102101
keyID := int64(d.Get("key_id").(int))
103102
userKey, resp, err := client.Users.GetKey(ctx, keyID)
104103
if err != nil {
105-
var ghErr *github.ErrorResponse
106-
if errors.As(err, &ghErr) {
107-
if ghErr.Response.StatusCode == http.StatusNotModified {
108-
return nil
109-
}
110-
if ghErr.Response.StatusCode == http.StatusNotFound {
111-
tflog.Info(ctx, "Removing user SSH key from state because it no longer exists in GitHub", map[string]any{
112-
"ssh_key_id": d.Id(),
113-
})
114-
d.SetId("")
115-
return nil
116-
}
117-
}
104+
return diag.FromErr(deleteResourceOn404AndSwallow304OtherwiseReturnError(err, d, "user SSH key (%d)", keyID))
118105
}
119106

120107
// set computed fields

github/resource_github_user_ssh_signing_key.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strconv"
99

1010
"github.com/google/go-github/v84/github"
11-
"github.com/hashicorp/terraform-plugin-log/tflog"
1211
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1312
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1413
)
@@ -85,19 +84,7 @@ func resourceGithubUserSshSigningKeyRead(ctx context.Context, d *schema.Resource
8584
keyID := int64(d.Get("key_id").(int))
8685
userKey, resp, err := client.Users.GetSSHSigningKey(ctx, keyID)
8786
if err != nil {
88-
var ghErr *github.ErrorResponse
89-
if errors.As(err, &ghErr) {
90-
if ghErr.Response.StatusCode == http.StatusNotModified {
91-
return nil
92-
}
93-
if ghErr.Response.StatusCode == http.StatusNotFound {
94-
tflog.Info(ctx, "Removing user SSH signing key from state because it no longer exists in GitHub", map[string]any{
95-
"ssh_signing_key_id": d.Id(),
96-
})
97-
d.SetId("")
98-
return nil
99-
}
100-
}
87+
return diag.FromErr(deleteResourceOn404AndSwallow304OtherwiseReturnError(err, d, "user SSH signing key (%d)", keyID))
10188
}
10289

10390
// set computed fields

0 commit comments

Comments
 (0)