This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
forked from hetznercloud/terraform-provider-hcloud
/
resource_hcloud_sshkey.go
181 lines (160 loc) · 4.39 KB
/
resource_hcloud_sshkey.go
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
172
173
174
175
176
177
178
179
180
181
package hcloud
import (
"context"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hetznercloud/hcloud-go/hcloud"
"golang.org/x/crypto/ssh"
)
func resourceSSHKey() *schema.Resource {
return &schema.Resource{
Create: resourceSSHKeyCreate,
Read: resourceSSHKeyRead,
Update: resourceSSHKeyUpdate,
Delete: resourceSSHKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"public_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: resourceSSHKeyPublicKeyDiffSuppress,
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func resourceSSHKeyPublicKeyDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
fingerprint := d.Get("fingerprint").(string)
if new != "" && fingerprint != "" {
publicKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(new))
if err != nil {
return false
}
return ssh.FingerprintLegacyMD5(publicKey) == fingerprint
}
return strings.TrimSpace(old) == strings.TrimSpace(new)
}
func resourceSSHKeyCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*hcloud.Client)
ctx := context.Background()
opts := hcloud.SSHKeyCreateOpts{
Name: d.Get("name").(string),
PublicKey: d.Get("public_key").(string),
}
if labels, ok := d.GetOk("labels"); ok {
tmpLabels := make(map[string]string)
for k, v := range labels.(map[string]interface{}) {
tmpLabels[k] = v.(string)
}
opts.Labels = tmpLabels
}
sshKey, _, err := client.SSHKey.Create(ctx, opts)
if err != nil {
return err
}
d.SetId(strconv.Itoa(sshKey.ID))
return resourceSSHKeyRead(d, m)
}
func resourceSSHKeyRead(d *schema.ResourceData, m interface{}) error {
client := m.(*hcloud.Client)
ctx := context.Background()
sshKeyID, err := strconv.Atoi(d.Id())
if err != nil {
log.Printf("[WARN] invalid SSH key id (%s), removing from state: %v", d.Id(), err)
d.SetId("")
return nil
}
sshKey, _, err := client.SSHKey.GetByID(ctx, sshKeyID)
if err != nil {
return err
}
if sshKey == nil {
log.Printf("[WARN] SSH key (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
setSSHKeySchema(d, sshKey)
return nil
}
func resourceSSHKeyUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*hcloud.Client)
ctx := context.Background()
sshKeyID, err := strconv.Atoi(d.Id())
if err != nil {
log.Printf("[WARN] invalid SSH key id (%s), removing from state: %v", d.Id(), err)
d.SetId("")
return nil
}
if d.HasChange("name") {
name := d.Get("name").(string)
_, _, err := client.SSHKey.Update(ctx, &hcloud.SSHKey{ID: sshKeyID}, hcloud.SSHKeyUpdateOpts{
Name: name,
})
if err != nil {
if hcerr, ok := err.(hcloud.Error); ok && hcerr.Code == hcloud.ErrorCodeNotFound {
log.Printf("[WARN] SSH key (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
d.SetPartial("name")
}
if d.HasChange("labels") {
labels := d.Get("labels").(map[string]string)
_, _, err := client.SSHKey.Update(ctx, &hcloud.SSHKey{ID: sshKeyID}, hcloud.SSHKeyUpdateOpts{
Labels: labels,
})
if err != nil {
if hcerr, ok := err.(hcloud.Error); ok && hcerr.Code == hcloud.ErrorCodeNotFound {
log.Printf("[WARN] SSH key (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
d.SetPartial("labels")
}
return resourceSSHKeyRead(d, m)
}
func resourceSSHKeyDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*hcloud.Client)
ctx := context.Background()
sshKeyID, err := strconv.Atoi(d.Id())
if err != nil {
log.Printf("[WARN] invalid SSH key id (%s), removing from state: %v", d.Id(), err)
d.SetId("")
return nil
}
if _, err := client.SSHKey.Delete(ctx, &hcloud.SSHKey{ID: sshKeyID}); err != nil {
if hcerr, ok := err.(hcloud.Error); ok && hcerr.Code == hcloud.ErrorCodeNotFound {
// SSH key has already been deleted
return nil
}
return err
}
return nil
}
func setSSHKeySchema(d *schema.ResourceData, s *hcloud.SSHKey) {
d.SetId(strconv.Itoa(s.ID))
d.Set("name", s.Name)
d.Set("fingerprint", s.Fingerprint)
d.Set("public_key", s.PublicKey)
d.Set("labels", s.Labels)
}