-
Notifications
You must be signed in to change notification settings - Fork 950
Expand file tree
/
Copy pathresource_github_user_ssh_key.go
More file actions
135 lines (118 loc) · 3.2 KB
/
resource_github_user_ssh_key.go
File metadata and controls
135 lines (118 loc) · 3.2 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
package github
import (
"context"
"errors"
"log"
"net/http"
"strconv"
"strings"
"github.com/google/go-github/v82/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubUserSshKey() *schema.Resource {
return &schema.Resource{
Create: resourceGithubUserSshKeyCreate,
Read: resourceGithubUserSshKeyRead,
Delete: resourceGithubUserSshKeyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
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.",
DiffSuppressFunc: func(k, oldV, newV string, d *schema.ResourceData) bool {
newTrimmed := strings.TrimSpace(newV)
return oldV == newTrimmed
},
},
"url": {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the SSH key.",
},
"etag": {
Type: schema.TypeString,
Optional: true,
Computed: true,
DiffSuppressFunc: func(k, o, n string, d *schema.ResourceData) bool {
return true
},
DiffSuppressOnRefresh: true,
},
},
}
}
func resourceGithubUserSshKeyCreate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
title := d.Get("title").(string)
key := d.Get("key").(string)
ctx := context.Background()
userKey, _, err := client.Users.CreateKey(ctx, &github.Key{
Title: github.Ptr(title),
Key: github.Ptr(key),
})
if err != nil {
return err
}
d.SetId(strconv.FormatInt(*userKey.ID, 10))
return resourceGithubUserSshKeyRead(d, meta)
}
func resourceGithubUserSshKeyRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return unconvertibleIdErr(d.Id(), err)
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}
key, resp, err := client.Users.GetKey(ctx, id)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {
if ghErr.Response.StatusCode == http.StatusNotModified {
return nil
}
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[INFO] Removing user SSH key %s from state because it no longer exists in GitHub",
d.Id())
d.SetId("")
return nil
}
}
return err
}
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
return err
}
if err = d.Set("title", key.GetTitle()); err != nil {
return err
}
if err = d.Set("key", key.GetKey()); err != nil {
return err
}
if err = d.Set("url", key.GetURL()); err != nil {
return err
}
return nil
}
func resourceGithubUserSshKeyDelete(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return unconvertibleIdErr(d.Id(), err)
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
_, err = client.Users.DeleteKey(ctx, id)
return err
}