2015-02-25 12:38:23 +01:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-05-15 15:54:16 +02:00
|
|
|
"context"
|
2015-02-25 12:38:23 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-09-27 06:06:51 +02:00
|
|
|
"strconv"
|
2016-08-14 22:38:37 +02:00
|
|
|
"strings"
|
2015-02-25 12:38:23 +01:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
"github.com/digitalocean/godo"
|
2015-02-25 12:38:23 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceDigitalOceanSSHKey() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceDigitalOceanSSHKeyCreate,
|
|
|
|
Read: resourceDigitalOceanSSHKeyRead,
|
|
|
|
Update: resourceDigitalOceanSSHKeyUpdate,
|
|
|
|
Delete: resourceDigitalOceanSSHKeyDelete,
|
2016-06-29 17:09:34 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-08-31 22:26:57 +02:00
|
|
|
"id": {
|
2015-02-25 12:38:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-08-31 22:26:57 +02:00
|
|
|
"name": {
|
2015-02-25 12:38:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2016-08-31 22:26:57 +02:00
|
|
|
"public_key": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
DiffSuppressFunc: resourceDigitalOceanSSHKeyPublicKeyDiffSuppress,
|
2015-02-25 12:38:23 +01:00
|
|
|
},
|
|
|
|
|
2016-08-31 22:26:57 +02:00
|
|
|
"fingerprint": {
|
2015-02-25 12:38:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 22:26:57 +02:00
|
|
|
func resourceDigitalOceanSSHKeyPublicKeyDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
|
|
|
|
return strings.TrimSpace(old) == strings.TrimSpace(new)
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:38:23 +01:00
|
|
|
func resourceDigitalOceanSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
// Build up our creation options
|
2015-09-27 06:06:51 +02:00
|
|
|
opts := &godo.KeyCreateRequest{
|
2015-02-25 12:38:23 +01:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
PublicKey: d.Get("public_key").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] SSH Key create configuration: %#v", opts)
|
2017-05-15 15:54:16 +02:00
|
|
|
key, _, err := client.Keys.Create(context.Background(), opts)
|
2015-02-25 12:38:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating SSH Key: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
d.SetId(strconv.Itoa(key.ID))
|
|
|
|
log.Printf("[INFO] SSH Key: %d", key.ID)
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
return resourceDigitalOceanSSHKeyRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanSSHKeyRead(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
2015-02-25 12:38:23 +01:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
id, err := strconv.Atoi(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid SSH key id: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
key, resp, err := client.Keys.GetByID(context.Background(), id)
|
2015-02-25 12:38:23 +01:00
|
|
|
if err != nil {
|
|
|
|
// If the key is somehow already destroyed, mark as
|
2015-09-11 20:56:20 +02:00
|
|
|
// successfully gone
|
2016-03-21 15:25:11 +01:00
|
|
|
if resp != nil && resp.StatusCode == 404 {
|
2015-02-25 12:38:23 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error retrieving SSH key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", key.Name)
|
|
|
|
d.Set("fingerprint", key.Fingerprint)
|
2016-06-29 17:09:34 +02:00
|
|
|
d.Set("public_key", key.PublicKey)
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid SSH key id: %v", err)
|
|
|
|
}
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
var newName string
|
|
|
|
if v, ok := d.GetOk("name"); ok {
|
|
|
|
newName = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] SSH key update name: %#v", newName)
|
2015-09-27 06:06:51 +02:00
|
|
|
opts := &godo.KeyUpdateRequest{
|
|
|
|
Name: newName,
|
|
|
|
}
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err = client.Keys.UpdateByID(context.Background(), id, opts)
|
2015-02-25 12:38:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to update SSH key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceDigitalOceanSSHKeyRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDigitalOceanSSHKeyDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid SSH key id: %v", err)
|
|
|
|
}
|
2015-02-25 12:38:23 +01:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
log.Printf("[INFO] Deleting SSH key: %d", id)
|
2017-05-15 15:54:16 +02:00
|
|
|
_, err = client.Keys.DeleteByID(context.Background(), id)
|
2015-02-25 12:38:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting SSH key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|