github.com/sathiyas/terraform@v0.6.9-0.20151210233947-3330da00b997/builtin/providers/digitalocean/resource_digitalocean_ssh_key.go (about)

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