@@ -2,23 +2,23 @@ package github
22
33import (
44 "context"
5- "errors"
6- "log"
5+ "fmt"
76 "net/http"
87 "strconv"
9- "strings"
108
119 "github.com/google/go-github/v83/github"
10+ "github.com/hashicorp/terraform-plugin-log/tflog"
11+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1212 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313)
1414
1515func resourceGithubUserSshKey () * schema.Resource {
1616 return & schema.Resource {
17- Create : resourceGithubUserSshKeyCreate ,
18- Read : resourceGithubUserSshKeyRead ,
19- Delete : resourceGithubUserSshKeyDelete ,
17+ CreateContext : resourceGithubUserSshKeyCreate ,
18+ ReadContext : resourceGithubUserSshKeyRead ,
19+ DeleteContext : resourceGithubUserSshKeyDelete ,
2020 Importer : & schema.ResourceImporter {
21- StateContext : schema . ImportStatePassthroughContext ,
21+ StateContext : resourceGithubUserSshKeyImport ,
2222 },
2323
2424 Schema : map [string ]* schema.Schema {
@@ -33,15 +33,11 @@ func resourceGithubUserSshKey() *schema.Resource {
3333 Required : true ,
3434 ForceNew : true ,
3535 Description : "The public SSH key to add to your GitHub account." ,
36- DiffSuppressFunc : func (k , oldV , newV string , d * schema.ResourceData ) bool {
37- newTrimmed := strings .TrimSpace (newV )
38- return oldV == newTrimmed
39- },
4036 },
41- "url " : {
42- Type : schema .TypeString ,
37+ "key_id " : {
38+ Type : schema .TypeInt ,
4339 Computed : true ,
44- Description : "The URL of the SSH key." ,
40+ Description : "The unique identifier of the SSH key." ,
4541 },
4642 "etag" : {
4743 Type : schema .TypeString ,
@@ -51,80 +47,100 @@ func resourceGithubUserSshKey() *schema.Resource {
5147 }
5248}
5349
54- func resourceGithubUserSshKeyCreate (d * schema.ResourceData , meta any ) error {
50+ func resourceGithubUserSshKeyCreate (ctx context. Context , d * schema.ResourceData , meta any ) diag. Diagnostics {
5551 client := meta .(* Owner ).v3client
5652
5753 title := d .Get ("title" ).(string )
5854 key := d .Get ("key" ).(string )
59- ctx := context .Background ()
6055
61- userKey , _ , err := client .Users .CreateKey (ctx , & github.Key {
56+ userKey , resp , err := client .Users .CreateKey (ctx , & github.Key {
6257 Title : github .Ptr (title ),
6358 Key : github .Ptr (key ),
6459 })
6560 if err != nil {
66- return err
61+ return diag . FromErr ( err )
6762 }
6863
69- d .SetId (strconv .FormatInt (* userKey .ID , 10 ))
64+ d .SetId (strconv .FormatInt (userKey .GetID (), 10 ))
65+
66+ if err = d .Set ("key_id" , userKey .GetID ()); err != nil {
67+ return diag .FromErr (err )
68+ }
69+ if err = d .Set ("etag" , resp .Header .Get ("ETag" )); err != nil {
70+ return diag .FromErr (err )
71+ }
72+ if err = d .Set ("title" , userKey .GetTitle ()); err != nil {
73+ return diag .FromErr (err )
74+ }
7075
71- return resourceGithubUserSshKeyRead ( d , meta )
76+ return nil
7277}
7378
74- func resourceGithubUserSshKeyRead (d * schema.ResourceData , meta any ) error {
79+ func resourceGithubUserSshKeyRead (ctx context. Context , d * schema.ResourceData , meta any ) diag. Diagnostics {
7580 client := meta .(* Owner ).v3client
7681
77- id , err := strconv .ParseInt (d .Id (), 10 , 64 )
82+ keyID := d .Get ("key_id" ).(int64 )
83+ _ , _ , err := client .Users .GetKey (ctx , keyID )
7884 if err != nil {
79- return unconvertibleIdErr (d .Id (), err )
80- }
81- ctx := context .WithValue (context .Background (), ctxId , d .Id ())
82- if ! d .IsNewResource () {
83- ctx = context .WithValue (ctx , ctxEtag , d .Get ("etag" ).(string ))
84- }
85-
86- key , resp , err := client .Users .GetKey (ctx , id )
87- if err != nil {
88- var ghErr * github.ErrorResponse
89- if errors .As (err , & ghErr ) {
85+ if ghErr , ok := err .(* github.ErrorResponse ); ok {
9086 if ghErr .Response .StatusCode == http .StatusNotModified {
9187 return nil
9288 }
9389 if ghErr .Response .StatusCode == http .StatusNotFound {
94- log .Printf ("[INFO] Removing user SSH key %s from state because it no longer exists in GitHub" ,
95- d .Id ())
90+ tflog .Info (ctx , fmt .Sprintf ("Removing user SSH key %s from state because it no longer exists in GitHub" , d .Id ()), map [string ]any {
91+ "ssh_key_id" : d .Id (),
92+ })
9693 d .SetId ("" )
9794 return nil
9895 }
9996 }
100- return err
10197 }
98+ return nil
99+ }
102100
103- if err = d .Set ("etag" , resp .Header .Get ("ETag" )); err != nil {
104- return err
105- }
106- if err = d .Set ("title" , key .GetTitle ()); err != nil {
107- return err
108- }
109- if err = d .Set ("key" , key .GetKey ()); err != nil {
110- return err
111- }
112- if err = d .Set ("url" , key .GetURL ()); err != nil {
113- return err
114- }
101+ func resourceGithubUserSshKeyDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
102+ client := meta .(* Owner ).v3client
115103
116- return nil
104+ keyID := d .Get ("key_id" ).(int64 )
105+ resp , err := client .Users .DeleteKey (ctx , keyID )
106+ if resp .StatusCode == http .StatusNotFound {
107+ return nil
108+ }
109+ return diag .FromErr (err )
117110}
118111
119- func resourceGithubUserSshKeyDelete ( d * schema.ResourceData , meta any ) error {
112+ func resourceGithubUserSshKeyImport ( ctx context. Context , d * schema.ResourceData , meta any ) ([] * schema. ResourceData , error ) {
120113 client := meta .(* Owner ).v3client
121114
122- id , err := strconv .ParseInt (d .Id (), 10 , 64 )
115+ keyID , err := strconv .ParseInt (d .Id (), 10 , 64 )
116+ if err != nil {
117+ return nil , fmt .Errorf ("invalid SSH key ID format: %v" , err )
118+ }
119+
120+ key , resp , err := client .Users .GetKey (ctx , keyID )
123121 if err != nil {
124- return unconvertibleIdErr (d .Id (), err )
122+ if ghErr , ok := err .(* github.ErrorResponse ); ok {
123+ if ghErr .Response .StatusCode == http .StatusNotFound {
124+ return nil , fmt .Errorf ("SSH key with ID %d not found" , keyID )
125+ }
126+ }
127+ return nil , err
128+ }
129+
130+ d .SetId (strconv .FormatInt (key .GetID (), 10 ))
131+
132+ if err = d .Set ("key_id" , key .GetID ()); err != nil {
133+ return nil , err
134+ }
135+ if err = d .Set ("etag" , resp .Header .Get ("ETag" )); err != nil {
136+ return nil , err
137+ }
138+ if err = d .Set ("title" , key .GetTitle ()); err != nil {
139+ return nil , err
140+ }
141+ if err = d .Set ("key" , key .GetKey ()); err != nil {
142+ return nil , err
125143 }
126- ctx := context .WithValue (context .Background (), ctxId , d .Id ())
127144
128- _ , err = client .Users .DeleteKey (ctx , id )
129- return err
145+ return []* schema.ResourceData {d }, nil
130146}
0 commit comments