forked from hashicorp/terraform
/
resource_ssh_key.go
118 lines (96 loc) · 2.48 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
package opc
import (
"fmt"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceOPCSSHKey() *schema.Resource {
return &schema.Resource{
Create: resourceOPCSSHKeyCreate,
Read: resourceOPCSSHKeyRead,
Update: resourceOPCSSHKeyUpdate,
Delete: resourceOPCSSHKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"key": {
Type: schema.TypeString,
Required: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
}
}
func resourceOPCSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).SSHKeys()
name := d.Get("name").(string)
key := d.Get("key").(string)
enabled := d.Get("enabled").(bool)
input := compute.CreateSSHKeyInput{
Name: name,
Key: key,
Enabled: enabled,
}
info, err := client.CreateSSHKey(&input)
if err != nil {
return fmt.Errorf("Error creating ssh key %s: %s", name, err)
}
d.SetId(info.Name)
return resourceOPCSSHKeyRead(d, meta)
}
func resourceOPCSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).SSHKeys()
name := d.Get("name").(string)
key := d.Get("key").(string)
enabled := d.Get("enabled").(bool)
input := compute.UpdateSSHKeyInput{
Name: name,
Key: key,
Enabled: enabled,
}
_, err := client.UpdateSSHKey(&input)
if err != nil {
return fmt.Errorf("Error updating ssh key %s: %s", name, err)
}
return resourceOPCSSHKeyRead(d, meta)
}
func resourceOPCSSHKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).SSHKeys()
name := d.Id()
input := compute.GetSSHKeyInput{
Name: name,
}
result, err := client.GetSSHKey(&input)
if err != nil {
if compute.WasNotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading ssh key %s: %s", name, err)
}
d.Set("name", result.Name)
d.Set("key", result.Key)
d.Set("enabled", result.Enabled)
return nil
}
func resourceOPCSSHKeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).SSHKeys()
name := d.Id()
input := compute.DeleteSSHKeyInput{
Name: name,
}
if err := client.DeleteSSHKey(&input); err != nil {
return fmt.Errorf("Error deleting ssh key %s: %s", name, err)
}
return nil
}