-
Notifications
You must be signed in to change notification settings - Fork 952
Expand file tree
/
Copy pathresource_github_user_ssh_key.go
More file actions
171 lines (147 loc) · 4.28 KB
/
resource_github_user_ssh_key.go
File metadata and controls
171 lines (147 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package github
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/google/go-github/v84/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubUserSshKey() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGithubUserSshKeyCreate,
ReadContext: resourceGithubUserSshKeyRead,
DeleteContext: resourceGithubUserSshKeyDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceGithubUserSshKeyImport,
},
Description: "Manages a SSH key for the authenticated user.",
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"title": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "A descriptive name for the new key.",
},
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The public SSH key to add to your GitHub account.",
},
"url": {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the SSH key.",
},
"key_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The unique identifier of the SSH key.",
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
},
StateUpgraders: []schema.StateUpgrader{
{
Version: 0,
Type: resourceGithubUserSshKeyV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceGithubUserSshKeyStateUpgradeV0,
},
},
}
}
func resourceGithubUserSshKeyCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
title := d.Get("title").(string)
key := d.Get("key").(string)
userKey, resp, err := client.Users.CreateKey(ctx, &github.Key{
Title: new(title),
Key: new(key),
})
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.FormatInt(userKey.GetID(), 10))
if err = d.Set("key_id", userKey.GetID()); err != nil {
return diag.FromErr(err)
}
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
return diag.FromErr(err)
}
if err = d.Set("url", userKey.GetURL()); err != nil {
return diag.FromErr(err)
}
if err = d.Set("title", userKey.GetTitle()); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceGithubUserSshKeyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
keyID := int64(d.Get("key_id").(int))
userKey, resp, err := client.Users.GetKey(ctx, keyID)
if err != nil {
return diag.FromErr(deleteResourceOn404AndSwallow304OtherwiseReturnError(err, d, "user SSH key (%d)", keyID))
}
// set computed fields
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
return diag.FromErr(err)
}
if err = d.Set("url", userKey.GetURL()); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceGithubUserSshKeyDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
keyID := int64(d.Get("key_id").(int))
// fallback to d.Id() for backward compatibility when key_id is not set
if keyID == 0 {
var err error
keyID, err = strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(fmt.Errorf("invalid SSH key ID format: %w", err))
}
}
resp, err := client.Users.DeleteKey(ctx, keyID)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
}
return diag.FromErr(err)
}
return nil
}
func resourceGithubUserSshKeyImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
client := meta.(*Owner).v3client
keyID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid SSH key ID format: %w", err)
}
key, _, err := client.Users.GetKey(ctx, keyID)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {
if ghErr.Response.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("SSH key with ID %d not found", keyID)
}
}
return nil, err
}
if err = d.Set("key_id", key.GetID()); err != nil {
return nil, err
}
if err = d.Set("title", key.GetTitle()); err != nil {
return nil, err
}
if err = d.Set("key", key.GetKey()); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}