github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/digitalocean/resource_digitalocean_ssh_key.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/digitalocean/godo"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceDigitalOceanSSHKey() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceDigitalOceanSSHKeyCreate,
    16  		Read:   resourceDigitalOceanSSHKeyRead,
    17  		Update: resourceDigitalOceanSSHKeyUpdate,
    18  		Delete: resourceDigitalOceanSSHKeyDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"id": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Computed: true,
    24  			},
    25  
    26  			"name": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  
    31  			"public_key": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"fingerprint": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Computed: true,
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func resourceDigitalOceanSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
    46  	client := meta.(*godo.Client)
    47  
    48  	// Build up our creation options
    49  	opts := &godo.KeyCreateRequest{
    50  		Name:      d.Get("name").(string),
    51  		PublicKey: d.Get("public_key").(string),
    52  	}
    53  
    54  	log.Printf("[DEBUG] SSH Key create configuration: %#v", opts)
    55  	key, _, err := client.Keys.Create(opts)
    56  	if err != nil {
    57  		return fmt.Errorf("Error creating SSH Key: %s", err)
    58  	}
    59  
    60  	d.SetId(strconv.Itoa(key.ID))
    61  	log.Printf("[INFO] SSH Key: %d", key.ID)
    62  
    63  	return resourceDigitalOceanSSHKeyRead(d, meta)
    64  }
    65  
    66  func resourceDigitalOceanSSHKeyRead(d *schema.ResourceData, meta interface{}) error {
    67  	client := meta.(*godo.Client)
    68  
    69  	id, err := strconv.Atoi(d.Id())
    70  	if err != nil {
    71  		return fmt.Errorf("invalid SSH key id: %v", err)
    72  	}
    73  
    74  	key, _, err := client.Keys.GetByID(id)
    75  	if err != nil {
    76  		// If the key is somehow already destroyed, mark as
    77  		// successfully gone
    78  		if strings.Contains(err.Error(), "404 Not Found") {
    79  			d.SetId("")
    80  			return nil
    81  		}
    82  
    83  		return fmt.Errorf("Error retrieving SSH key: %s", err)
    84  	}
    85  
    86  	d.Set("name", key.Name)
    87  	d.Set("fingerprint", key.Fingerprint)
    88  
    89  	return nil
    90  }
    91  
    92  func resourceDigitalOceanSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error {
    93  	client := meta.(*godo.Client)
    94  
    95  	id, err := strconv.Atoi(d.Id())
    96  	if err != nil {
    97  		return fmt.Errorf("invalid SSH key id: %v", err)
    98  	}
    99  
   100  	var newName string
   101  	if v, ok := d.GetOk("name"); ok {
   102  		newName = v.(string)
   103  	}
   104  
   105  	log.Printf("[DEBUG] SSH key update name: %#v", newName)
   106  	opts := &godo.KeyUpdateRequest{
   107  		Name: newName,
   108  	}
   109  	_, _, err = client.Keys.UpdateByID(id, opts)
   110  	if err != nil {
   111  		return fmt.Errorf("Failed to update SSH key: %s", err)
   112  	}
   113  
   114  	return resourceDigitalOceanSSHKeyRead(d, meta)
   115  }
   116  
   117  func resourceDigitalOceanSSHKeyDelete(d *schema.ResourceData, meta interface{}) error {
   118  	client := meta.(*godo.Client)
   119  
   120  	id, err := strconv.Atoi(d.Id())
   121  	if err != nil {
   122  		return fmt.Errorf("invalid SSH key id: %v", err)
   123  	}
   124  
   125  	log.Printf("[INFO] Deleting SSH key: %d", id)
   126  	_, err = client.Keys.DeleteByID(id)
   127  	if err != nil {
   128  		return fmt.Errorf("Error deleting SSH key: %s", err)
   129  	}
   130  
   131  	d.SetId("")
   132  	return nil
   133  }