Skip to content

Commit 78de3d9

Browse files
fix: use tflog, context, test-structure
1 parent bbfe53f commit 78de3d9

File tree

2 files changed

+56
-84
lines changed

2 files changed

+56
-84
lines changed

github/resource_github_user_ssh_signing_key.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ package github
22

33
import (
44
"context"
5-
"log"
5+
"fmt"
66
"net/http"
77
"strconv"
88
"strings"
99

10-
"github.com/google/go-github/v63/github"
10+
"github.com/google/go-github/v81/github"
11+
"github.com/hashicorp/terraform-plugin-log/tflog"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1214
)
1315

1416
func resourceGithubUserSshSigningKey() *schema.Resource {
1517
return &schema.Resource{
16-
Create: resourceGithubUserSshSigningKeyCreate,
17-
Read: resourceGithubUserSshSigningKeyRead,
18-
Delete: resourceGithubUserSshSigningKeyDelete,
18+
CreateContext: resourceGithubUserSshSigningKeyCreate,
19+
ReadContext: resourceGithubUserSshSigningKeyRead,
20+
DeleteContext: resourceGithubUserSshSigningKeyDelete,
1921
Importer: &schema.ResourceImporter{
2022
StateContext: schema.ImportStatePassthroughContext,
2123
},
@@ -45,34 +47,32 @@ func resourceGithubUserSshSigningKey() *schema.Resource {
4547
}
4648
}
4749

48-
func resourceGithubUserSshSigningKeyCreate(d *schema.ResourceData, meta interface{}) error {
50+
func resourceGithubUserSshSigningKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
4951
client := meta.(*Owner).v3client
5052

5153
title := d.Get("title").(string)
5254
key := d.Get("key").(string)
53-
ctx := context.Background()
5455

5556
userKey, _, err := client.Users.CreateSSHSigningKey(ctx, &github.Key{
56-
Title: github.String(title),
57-
Key: github.String(key),
57+
Title: github.Ptr(title),
58+
Key: github.Ptr(key),
5859
})
5960
if err != nil {
60-
return err
61+
return diag.FromErr(err)
6162
}
6263

6364
d.SetId(strconv.FormatInt(*userKey.ID, 10))
6465

65-
return resourceGithubUserSshSigningKeyRead(d, meta)
66+
return resourceGithubUserSshSigningKeyRead(ctx, d, meta)
6667
}
6768

68-
func resourceGithubUserSshSigningKeyRead(d *schema.ResourceData, meta interface{}) error {
69+
func resourceGithubUserSshSigningKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
6970
client := meta.(*Owner).v3client
7071

7172
id, err := strconv.ParseInt(d.Id(), 10, 64)
7273
if err != nil {
73-
return unconvertibleIdErr(d.Id(), err)
74+
return diag.Errorf("failed to convert ID %s: %v", d.Id(), err)
7475
}
75-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
7676
if !d.IsNewResource() {
7777
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
7878
}
@@ -84,36 +84,37 @@ func resourceGithubUserSshSigningKeyRead(d *schema.ResourceData, meta interface{
8484
return nil
8585
}
8686
if ghErr.Response.StatusCode == http.StatusNotFound {
87-
log.Printf("[INFO] Removing user SSH key %s from state because it no longer exists in GitHub",
88-
d.Id())
87+
tflog.Info(ctx, fmt.Sprintf("Removing user SSH key %s from state because it no longer exists in GitHub", d.Id()), map[string]any{
88+
"ssh_signing_key_id": d.Id(),
89+
})
8990
d.SetId("")
9091
return nil
9192
}
9293
}
9394
}
9495

9596
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
96-
return err
97+
return diag.FromErr(err)
9798
}
9899
if err = d.Set("title", key.GetTitle()); err != nil {
99-
return err
100+
return diag.FromErr(err)
100101
}
101102
if err = d.Set("key", key.GetKey()); err != nil {
102-
return err
103+
return diag.FromErr(err)
103104
}
104105

105106
return nil
106107
}
107108

108-
func resourceGithubUserSshSigningKeyDelete(d *schema.ResourceData, meta interface{}) error {
109+
func resourceGithubUserSshSigningKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
109110
client := meta.(*Owner).v3client
110111

111112
id, err := strconv.ParseInt(d.Id(), 10, 64)
112113
if err != nil {
113-
return unconvertibleIdErr(d.Id(), err)
114+
return diag.Errorf("failed to convert ID %s: %v", d.Id(), err)
114115
}
115-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
116+
ctx = context.WithValue(ctx, ctxId, d.Id())
116117

117118
_, err = client.Users.DeleteSSHSigningKey(ctx, id)
118-
return err
119+
return diag.FromErr(err)
119120
}

github/resource_github_user_ssh_signing_key_test.go

Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import (
1414
)
1515

1616
func TestAccGithubUserSshSigningKey(t *testing.T) {
17-
18-
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
19-
testKey := newTestSigningKey()
20-
21-
t.Run("creates and destroys a user SSH key without error", func(t *testing.T) {
17+
t.Run("creates and destroys a user SSH signing key without error", func(t *testing.T) {
18+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
19+
testKey := newTestSigningKey()
2220

2321
config := fmt.Sprintf(`
2422
resource "github_user_ssh_signing_key" "test" {
@@ -29,43 +27,32 @@ func TestAccGithubUserSshSigningKey(t *testing.T) {
2927

3028
check := resource.ComposeTestCheckFunc(
3129
resource.TestMatchResourceAttr(
32-
"github_user_ssh_signing_key.test", "title",
30+
"github_user_ssh_signing_key.test",
31+
"title",
3332
regexp.MustCompile(randomID),
3433
),
3534
resource.TestMatchResourceAttr(
36-
"github_user_ssh_signing_key.test", "key",
35+
"github_user_ssh_signing_key.test",
36+
"key",
3737
regexp.MustCompile("^ssh-rsa "),
3838
),
3939
)
4040

41-
testCase := func(t *testing.T, mode string) {
42-
resource.Test(t, resource.TestCase{
43-
PreCheck: func() { skipUnlessMode(t, mode) },
44-
Providers: testAccProviders,
45-
Steps: []resource.TestStep{
46-
{
47-
Config: config,
48-
Check: check,
49-
},
41+
resource.Test(t, resource.TestCase{
42+
PreCheck: func() { skipUnauthenticated(t) },
43+
ProviderFactories: providerFactories,
44+
Steps: []resource.TestStep{
45+
{
46+
Config: config,
47+
Check: check,
5048
},
51-
})
52-
}
53-
54-
t.Run("with an anonymous account", func(t *testing.T) {
55-
t.Skip("anonymous account not supported for this operation")
56-
})
57-
58-
t.Run("with an individual account", func(t *testing.T) {
59-
testCase(t, individual)
60-
})
61-
62-
t.Run("with an organization account", func(t *testing.T) {
63-
testCase(t, organization)
49+
},
6450
})
65-
6651
})
6752

68-
t.Run("imports an individual account SSH key without error", func(t *testing.T) {
53+
t.Run("imports an individual account SSH signing key without error", func(t *testing.T) {
54+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
55+
testKey := newTestSigningKey()
6956

7057
config := fmt.Sprintf(`
7158
resource "github_user_ssh_signing_key" "test" {
@@ -79,42 +66,26 @@ func TestAccGithubUserSshSigningKey(t *testing.T) {
7966
resource.TestCheckResourceAttrSet("github_user_ssh_signing_key.test", "key"),
8067
)
8168

82-
testCase := func(t *testing.T, mode string) {
83-
resource.Test(t, resource.TestCase{
84-
PreCheck: func() { skipUnlessMode(t, mode) },
85-
Providers: testAccProviders,
86-
Steps: []resource.TestStep{
87-
{
88-
Config: config,
89-
Check: check,
90-
},
91-
{
92-
ResourceName: "github_user_ssh_signing_key.test",
93-
ImportState: true,
94-
ImportStateVerify: true,
95-
},
69+
resource.Test(t, resource.TestCase{
70+
PreCheck: func() { skipUnauthenticated(t) },
71+
ProviderFactories: providerFactories,
72+
Steps: []resource.TestStep{
73+
{
74+
Config: config,
75+
Check: check,
9676
},
97-
})
98-
}
99-
100-
t.Run("with an anonymous account", func(t *testing.T) {
101-
t.Skip("anonymous account not supported for this operation")
102-
})
103-
104-
t.Run("with an individual account", func(t *testing.T) {
105-
testCase(t, individual)
106-
})
107-
108-
t.Run("with an organization account", func(t *testing.T) {
109-
testCase(t, organization)
77+
{
78+
ResourceName: "github_user_ssh_signing_key.test",
79+
ImportState: true,
80+
ImportStateVerify: true,
81+
},
82+
},
11083
})
111-
11284
})
11385
}
11486

11587
func newTestSigningKey() string {
11688
privateKey, _ := rsa.GenerateKey(rand.Reader, 1024)
11789
publicKey, _ := ssh.NewPublicKey(&privateKey.PublicKey)
118-
testKey := strings.TrimRight(string(ssh.MarshalAuthorizedKey(publicKey)), "\n")
119-
return testKey
90+
return strings.TrimRight(string(ssh.MarshalAuthorizedKey(publicKey)), "\n")
12091
}

0 commit comments

Comments
 (0)