forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_softlayer_ssh_key.go
159 lines (125 loc) · 3.5 KB
/
resource_softlayer_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
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
package softlayer
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform/helper/schema"
datatypes "github.com/maximilien/softlayer-go/data_types"
)
func resourceSoftLayerSSHKey() *schema.Resource {
return &schema.Resource{
Create: resourceSoftLayerSSHKeyCreate,
Read: resourceSoftLayerSSHKeyRead,
Update: resourceSoftLayerSSHKeyUpdate,
Delete: resourceSoftLayerSSHKeyDelete,
Exists: resourceSoftLayerSSHKeyExists,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"public_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"notes": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: nil,
},
},
}
}
func resourceSoftLayerSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).sshKeyService
// Build up our creation options
opts := datatypes.SoftLayer_Security_Ssh_Key{
Label: d.Get("name").(string),
Key: d.Get("public_key").(string),
}
if notes, ok := d.GetOk("notes"); ok {
opts.Notes = notes.(string)
}
res, err := client.CreateObject(opts)
if err != nil {
return fmt.Errorf("Error creating SSH Key: %s", err)
}
d.SetId(strconv.Itoa(res.Id))
log.Printf("[INFO] SSH Key: %d", res.Id)
return resourceSoftLayerSSHKeyRead(d, meta)
}
func resourceSoftLayerSSHKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).sshKeyService
keyId, _ := strconv.Atoi(d.Id())
key, err := client.GetObject(keyId)
if err != nil {
// If the key is somehow already destroyed, mark as
// succesfully gone
if strings.Contains(err.Error(), "404 Not Found") {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving SSH key: %s", err)
}
d.Set("id", key.Id)
d.Set("name", key.Label)
d.Set("public_key", strings.TrimSpace(key.Key))
d.Set("fingerprint", key.Fingerprint)
d.Set("notes", key.Notes)
return nil
}
func resourceSoftLayerSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).sshKeyService
keyId, _ := strconv.Atoi(d.Id())
key, err := client.GetObject(keyId)
if err != nil {
return fmt.Errorf("Error retrieving SSH key: %s", err)
}
if d.HasChange("name") {
key.Label = d.Get("name").(string)
}
if d.HasChange("notes") {
key.Notes = d.Get("notes").(string)
}
_, err = client.EditObject(keyId, key)
if err != nil {
return fmt.Errorf("Error editing SSH key: %s", err)
}
return nil
}
func resourceSoftLayerSSHKeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).sshKeyService
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Error deleting SSH Key: %s", err)
}
log.Printf("[INFO] Deleting SSH key: %d", id)
_, err = client.DeleteObject(id)
if err != nil {
return fmt.Errorf("Error deleting SSH key: %s", err)
}
d.SetId("")
return nil
}
func resourceSoftLayerSSHKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*Client).sshKeyService
if client == nil {
return false, fmt.Errorf("The client was nil.")
}
keyId, err := strconv.Atoi(d.Id())
if err != nil {
return false, fmt.Errorf("Not a valid ID, must be an integer: %s", err)
}
result, err := client.GetObject(keyId)
return result.Id == keyId && err == nil, nil
}