Skip to content

Commit b05e59e

Browse files
feat: import
1 parent c917dee commit b05e59e

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

github/resource_github_user_ssh_signing_key.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func resourceGithubUserSshSigningKey() *schema.Resource {
1919
ReadContext: resourceGithubUserSshSigningKeyRead,
2020
DeleteContext: resourceGithubUserSshSigningKeyDelete,
2121
Importer: &schema.ResourceImporter{
22-
StateContext: schema.ImportStatePassthroughContext,
22+
StateContext: resourceGithubUserSshSigningKeyImport,
2323
},
2424

2525
Schema: map[string]*schema.Schema{
@@ -80,7 +80,7 @@ func resourceGithubUserSshSigningKeyCreate(ctx context.Context, d *schema.Resour
8080
return nil
8181
}
8282

83-
func resourceGithubUserSshSigningKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
83+
func resourceGithubUserSshSigningKeyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
8484
client := meta.(*Owner).v3client
8585

8686
keyID := d.Get("key_id").(int64)
@@ -102,7 +102,7 @@ func resourceGithubUserSshSigningKeyRead(ctx context.Context, d *schema.Resource
102102
return nil
103103
}
104104

105-
func resourceGithubUserSshSigningKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
105+
func resourceGithubUserSshSigningKeyDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
106106
client := meta.(*Owner).v3client
107107

108108
keyID := d.Get("key_id").(int64)
@@ -112,3 +112,39 @@ func resourceGithubUserSshSigningKeyDelete(ctx context.Context, d *schema.Resour
112112
}
113113
return diag.FromErr(err)
114114
}
115+
116+
func resourceGithubUserSshSigningKeyImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
117+
client := meta.(*Owner).v3client
118+
119+
keyID, err := strconv.ParseInt(d.Id(), 10, 64)
120+
if err != nil {
121+
return nil, fmt.Errorf("invalid SSH signing key ID format: %v", err)
122+
}
123+
124+
key, resp, err := client.Users.GetSSHSigningKey(ctx, keyID)
125+
if err != nil {
126+
if ghErr, ok := err.(*github.ErrorResponse); ok {
127+
if ghErr.Response.StatusCode == http.StatusNotFound {
128+
return nil, fmt.Errorf("SSH signing key with ID %d not found", keyID)
129+
}
130+
}
131+
return nil, err
132+
}
133+
134+
d.SetId(strconv.FormatInt(key.GetID(), 10))
135+
136+
if err = d.Set("key_id", key.GetID()); err != nil {
137+
return nil, err
138+
}
139+
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
140+
return nil, err
141+
}
142+
if err = d.Set("title", key.GetTitle()); err != nil {
143+
return nil, err
144+
}
145+
if err = d.Set("key", key.GetKey()); err != nil {
146+
return nil, err
147+
}
148+
149+
return []*schema.ResourceData{d}, nil
150+
}

0 commit comments

Comments
 (0)