88 "strconv"
99
1010 "github.com/google/go-github/v83/github"
11+ "github.com/hashicorp/go-cty/cty"
1112 "github.com/hashicorp/terraform-plugin-log/tflog"
1213 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -22,6 +23,7 @@ func resourceGithubUserSshKey() *schema.Resource {
2223 StateContext : resourceGithubUserSshKeyImport ,
2324 },
2425
26+ SchemaVersion : 1 ,
2527 Schema : map [string ]* schema.Schema {
2628 "title" : {
2729 Type : schema .TypeString ,
@@ -35,6 +37,12 @@ func resourceGithubUserSshKey() *schema.Resource {
3537 ForceNew : true ,
3638 Description : "The public SSH key to add to your GitHub account." ,
3739 },
40+ "url" : {
41+ Type : schema .TypeString ,
42+ Computed : true ,
43+ Description : "The URL of the SSH key." ,
44+ Deprecated : "Use key_id instead." ,
45+ },
3846 "key_id" : {
3947 Type : schema .TypeInt ,
4048 Computed : true ,
@@ -45,6 +53,37 @@ func resourceGithubUserSshKey() *schema.Resource {
4553 Computed : true ,
4654 },
4755 },
56+
57+ StateUpgraders : []schema.StateUpgrader {
58+ {
59+ 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+ },
85+ },
86+ },
4887 }
4988}
5089
@@ -80,7 +119,16 @@ func resourceGithubUserSshKeyCreate(ctx context.Context, d *schema.ResourceData,
80119func resourceGithubUserSshKeyRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
81120 client := meta .(* Owner ).v3client
82121
83- keyID := d .Get ("key_id" ).(int64 )
122+ 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+
84132 _ , _ , err := client .Users .GetKey (ctx , keyID )
85133 if err != nil {
86134 var ghErr * github.ErrorResponse
@@ -103,12 +151,24 @@ func resourceGithubUserSshKeyRead(ctx context.Context, d *schema.ResourceData, m
103151func resourceGithubUserSshKeyDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
104152 client := meta .(* Owner ).v3client
105153
106- keyID := d .Get ("key_id" ).(int64 )
154+ keyID := int64 (d .Get ("key_id" ).(int ))
155+ // fallback to d.Id() for backward compatibility when key_id is not set
156+ if keyID == 0 {
157+ var err error
158+ keyID , err = strconv .ParseInt (d .Id (), 10 , 64 )
159+ if err != nil {
160+ return diag .FromErr (fmt .Errorf ("invalid SSH key ID format: %w" , err ))
161+ }
162+ }
163+
107164 resp , err := client .Users .DeleteKey (ctx , keyID )
108- if resp .StatusCode == http .StatusNotFound {
109- return nil
165+ if err != nil {
166+ if resp != nil && resp .StatusCode == http .StatusNotFound {
167+ return nil
168+ }
169+ return diag .FromErr (err )
110170 }
111- return diag . FromErr ( err )
171+ return nil
112172}
113173
114174func resourceGithubUserSshKeyImport (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
0 commit comments