2015-05-07 16:10:57 +02:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-09 12:38:05 +02:00
|
|
|
"io/ioutil"
|
2015-05-07 16:10:57 +02:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-06-09 12:38:05 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2015-05-07 16:10:57 +02:00
|
|
|
"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,
|
|
|
|
},
|
|
|
|
|
|
|
|
"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 != "" {
|
2015-06-09 12:38:05 +02:00
|
|
|
// Register supplied key
|
|
|
|
keyPath, err := homedir.Expand(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error expanding the public key path: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ioutil.ReadFile(keyPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading the public key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := cs.SSH.NewRegisterSSHKeyPairParams(name, string(key))
|
|
|
|
_, err = cs.SSH.RegisterSSHKeyPair(p)
|
2015-05-07 16:10:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2015-06-09 12:38:05 +02:00
|
|
|
// No key supplied, must create one and return the private key
|
2015-05-07 16:10:57 +02:00
|
|
|
p := cs.SSH.NewCreateSSHKeyPairParams(name)
|
|
|
|
r, err := cs.SSH.CreateSSHKeyPair(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Set("private_key", r.Privatekey)
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
log.Printf("[DEBUG] Key pair successfully generated at Cloudstack")
|
|
|
|
d.SetId(name)
|
|
|
|
|
2015-05-07 16:10:57 +02:00
|
|
|
return resourceCloudStackSSHKeyPairRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
log.Printf("[DEBUG] looking for key pair with name %s", d.Id())
|
2015-05-07 16:10:57 +02:00
|
|
|
|
|
|
|
p := cs.SSH.NewListSSHKeyPairsParams()
|
2015-06-09 12:38:05 +02:00
|
|
|
p.SetName(d.Id())
|
2015-05-07 16:10:57 +02:00
|
|
|
|
|
|
|
r, err := cs.SSH.ListSSHKeyPairs(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.Count == 0 {
|
2015-06-09 12:38:05 +02:00
|
|
|
log.Printf("[DEBUG] Key pair %s does not exist", d.Id())
|
|
|
|
d.SetId("")
|
2015-05-07 16:10:57 +02:00
|
|
|
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
|
2015-06-09 12:38:05 +02:00
|
|
|
p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
|
2015-05-07 16:10:57 +02:00
|
|
|
|
|
|
|
// Remove the SSH Keypair
|
|
|
|
_, err := cs.SSH.DeleteSSHKeyPair(p)
|
|
|
|
if err != nil {
|
2015-10-05 14:05:21 +02:00
|
|
|
// This is a very poor way to be told the ID does no longer exist :(
|
2015-05-07 16:10:57 +02:00
|
|
|
if strings.Contains(err.Error(), fmt.Sprintf(
|
2015-06-09 12:38:05 +02:00
|
|
|
"A key pair with name '%s' does not exist for account", d.Id())) {
|
2015-05-07 16:10:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
return fmt.Errorf("Error deleting key pair: %s", err)
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|