-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathresource_ssh_key.go
131 lines (105 loc) · 2.57 KB
/
resource_ssh_key.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
package main
import (
"fmt"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"
vscale "github.com/vscale/go-vscale"
)
func resourceSSHKey() *schema.Resource {
return &schema.Resource{
Create: resourceSSHKeyCreate,
Read: resourceSSHKeyRead,
Delete: resourceSSHKeyDelete,
Exists: resourceSSHKeyExists,
Schema: map[string]*schema.Schema{
// "id": &schema.Schema{
// Type: schema.TypeString,
// Computed: true,
// },
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceSSHKeyCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*vscale.WebClient)
name := d.Get("name").(string)
key := d.Get("key").(string)
sshKey, _, err := client.SSHKey.Create(key, name)
if err != nil {
return errors.Wrap(err, "creating ssh key failed")
}
d.SetId(strconv.FormatInt(sshKey.ID, 10))
return nil
}
func resourceSSHKeyRead(d *schema.ResourceData, m interface{}) error {
client := m.(*vscale.WebClient)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return errors.Wrap(err, "invalid SSH key id")
}
keys, _, err := client.SSHKey.List()
if err != nil {
return errors.Wrap(err, "listing ssh keys failed")
}
var sshKey vscale.SSHKey
if keys != nil {
for _, key := range *keys {
if key.ID == id {
sshKey = key
}
}
}
if sshKey.ID == 0 {
d.SetId("")
return nil
}
d.Set("key", sshKey.Key)
d.Set("name", sshKey.Name)
return nil
}
func resourceSSHKeyExists(d *schema.ResourceData, m interface{}) (bool, error) {
client := m.(*vscale.WebClient)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return false, errors.Wrap(err, "invalid SSH key id")
}
keys, _, err := client.SSHKey.List()
if err != nil {
return true, errors.Wrap(err, "listing ssh keys failed")
}
if keys != nil {
for _, key := range *keys {
if key.ID == id {
return true, nil
}
}
}
d.SetId("")
return false, nil
}
func resourceSSHKeyDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*vscale.WebClient)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return errors.Wrap(err, "invalid SSH key id")
}
ok, res, err := client.SSHKey.Remove(id)
if err != nil {
return errors.Wrap(err, "removing SSH key failed")
}
if !ok && res.StatusCode != 200 {
errText := fmt.Sprintf("removing SSH key failed, http code: %d", res.StatusCode)
return errors.New(errText)
}
return nil
}