88 "strconv"
99
1010 "github.com/google/go-github/v84/github"
11- "github.com/hashicorp/go-cty/cty"
1211 "github.com/hashicorp/terraform-plugin-log/tflog"
1312 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1413 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -41,7 +40,6 @@ func resourceGithubUserSshKey() *schema.Resource {
4140 Type : schema .TypeString ,
4241 Computed : true ,
4342 Description : "The URL of the SSH key." ,
44- Deprecated : "Use key_id instead." ,
4543 },
4644 "key_id" : {
4745 Type : schema .TypeInt ,
@@ -57,31 +55,8 @@ func resourceGithubUserSshKey() *schema.Resource {
5755 StateUpgraders : []schema.StateUpgrader {
5856 {
5957 Version : 0 ,
60- Type : cty .Object (map [string ]cty.Type {
61- "id" : cty .String ,
62- "title" : cty .String ,
63- "key" : cty .String ,
64- "url" : cty .String ,
65- "etag" : cty .String ,
66- }),
67- Upgrade : func (ctx context.Context , rawState map [string ]any , meta any ) (map [string ]any , error ) {
68- if rawState == nil {
69- return nil , fmt .Errorf ("resource state upgrade failed, state is nil" )
70- }
71-
72- // copy d.Id() into key_id
73- if id , ok := rawState ["id" ].(string ); ok {
74- keyID , err := strconv .ParseInt (id , 10 , 64 )
75- if err != nil {
76- return nil , fmt .Errorf ("resource state upgrade failed, invalid SSH key ID format: %w" , err )
77- }
78- rawState ["key_id" ] = keyID
79- } else {
80- return nil , fmt .Errorf ("resource state upgrade failed, missing or invalid 'id' field in state" )
81- }
82-
83- return rawState , nil
84- },
58+ Type : resourceGithubUserSshKeyV0 ().CoreConfigSchema ().ImpliedType (),
59+ Upgrade : resourceGithubUserSshKeyStateUpgradeV0 ,
8560 },
8661 },
8762 }
@@ -94,8 +69,8 @@ func resourceGithubUserSshKeyCreate(ctx context.Context, d *schema.ResourceData,
9469 key := d .Get ("key" ).(string )
9570
9671 userKey , resp , err := client .Users .CreateKey (ctx , & github.Key {
97- Title : github . Ptr (title ),
98- Key : github . Ptr (key ),
72+ Title : new (title ),
73+ Key : new (key ),
9974 })
10075 if err != nil {
10176 return diag .FromErr (err )
@@ -109,6 +84,9 @@ func resourceGithubUserSshKeyCreate(ctx context.Context, d *schema.ResourceData,
10984 if err = d .Set ("etag" , resp .Header .Get ("ETag" )); err != nil {
11085 return diag .FromErr (err )
11186 }
87+ if err = d .Set ("url" , userKey .GetURL ()); err != nil {
88+ return diag .FromErr (err )
89+ }
11290 if err = d .Set ("title" , userKey .GetTitle ()); err != nil {
11391 return diag .FromErr (err )
11492 }
@@ -120,16 +98,7 @@ func resourceGithubUserSshKeyRead(ctx context.Context, d *schema.ResourceData, m
12098 client := meta .(* Owner ).v3client
12199
122100 keyID := int64 (d .Get ("key_id" ).(int ))
123- // fallback to d.Id() for backward compatibility when key_id is not set
124- if keyID == 0 {
125- var err error
126- keyID , err = strconv .ParseInt (d .Id (), 10 , 64 )
127- if err != nil {
128- return diag .FromErr (fmt .Errorf ("invalid SSH key ID format: %w" , err ))
129- }
130- }
131-
132- _ , _ , err := client .Users .GetKey (ctx , keyID )
101+ userKey , resp , err := client .Users .GetKey (ctx , keyID )
133102 if err != nil {
134103 var ghErr * github.ErrorResponse
135104 if errors .As (err , & ghErr ) {
@@ -145,6 +114,18 @@ func resourceGithubUserSshKeyRead(ctx context.Context, d *schema.ResourceData, m
145114 }
146115 }
147116 }
117+
118+ // set computed fields
119+ if err = d .Set ("key_id" , userKey .GetID ()); err != nil {
120+ return diag .FromErr (err )
121+ }
122+ if err = d .Set ("etag" , resp .Header .Get ("ETag" )); err != nil {
123+ return diag .FromErr (err )
124+ }
125+ if err = d .Set ("url" , userKey .GetURL ()); err != nil {
126+ return diag .FromErr (err )
127+ }
128+
148129 return nil
149130}
150131
@@ -179,7 +160,7 @@ func resourceGithubUserSshKeyImport(ctx context.Context, d *schema.ResourceData,
179160 return nil , fmt .Errorf ("invalid SSH key ID format: %w" , err )
180161 }
181162
182- key , resp , err := client .Users .GetKey (ctx , keyID )
163+ key , _ , err := client .Users .GetKey (ctx , keyID )
183164 if err != nil {
184165 var ghErr * github.ErrorResponse
185166 if errors .As (err , & ghErr ) {
@@ -192,12 +173,6 @@ func resourceGithubUserSshKeyImport(ctx context.Context, d *schema.ResourceData,
192173
193174 d .SetId (strconv .FormatInt (key .GetID (), 10 ))
194175
195- if err = d .Set ("key_id" , key .GetID ()); err != nil {
196- return nil , err
197- }
198- if err = d .Set ("etag" , resp .Header .Get ("ETag" )); err != nil {
199- return nil , err
200- }
201176 if err = d .Set ("title" , key .GetTitle ()); err != nil {
202177 return nil , err
203178 }
0 commit comments