forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_cloudstack_ssh_keypair.go
145 lines (115 loc) · 3.39 KB
/
resource_cloudstack_ssh_keypair.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
package cloudstack
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/xanzy/go-cloudstack/cloudstack"
)
func resourceCloudStackSSHKeyPair() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackSSHKeyPairCreate,
Read: resourceCloudStackSSHKeyPairRead,
Delete: resourceCloudStackSSHKeyPairDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"public_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"private_key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
name := d.Get("name").(string)
publicKey := d.Get("public_key").(string)
if publicKey != "" {
// Register supplied key
p := cs.SSH.NewRegisterSSHKeyPairParams(name, publicKey)
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
_, err := cs.SSH.RegisterSSHKeyPair(p)
if err != nil {
return err
}
} else {
// No key supplied, must create one and return the private key
p := cs.SSH.NewCreateSSHKeyPairParams(name)
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
r, err := cs.SSH.CreateSSHKeyPair(p)
if err != nil {
return err
}
d.Set("private_key", r.Privatekey)
}
log.Printf("[DEBUG] Key pair successfully generated at Cloudstack")
d.SetId(name)
return resourceCloudStackSSHKeyPairRead(d, meta)
}
func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
log.Printf("[DEBUG] looking for key pair with name %s", d.Id())
p := cs.SSH.NewListSSHKeyPairsParams()
p.SetName(d.Id())
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
r, err := cs.SSH.ListSSHKeyPairs(p)
if err != nil {
return err
}
if r.Count == 0 {
log.Printf("[DEBUG] Key pair %s does not exist", d.Id())
d.SetId("")
return nil
}
//SSHKeyPair name is unique in a cloudstack account so dont need to check for multiple
d.Set("name", r.SSHKeyPairs[0].Name)
d.Set("fingerprint", r.SSHKeyPairs[0].Fingerprint)
return nil
}
func resourceCloudStackSSHKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Create a new parameter struct
p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
// Remove the SSH Keypair
_, err := cs.SSH.DeleteSSHKeyPair(p)
if err != nil {
// This is a very poor way to be told the ID does no longer exist :(
if strings.Contains(err.Error(), fmt.Sprintf(
"A key pair with name '%s' does not exist for account", d.Id())) {
return nil
}
return fmt.Errorf("Error deleting key pair: %s", err)
}
return nil
}