github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"id": {
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  
    29  			"name": {
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  			},
    33  
    34  			"public_key": {
    35  				Type:             schema.TypeString,
    36  				Required:         true,
    37  				ForceNew:         true,
    38  				DiffSuppressFunc: resourceDigitalOceanSSHKeyPublicKeyDiffSuppress,
    39  			},
    40  
    41  			"fingerprint": {
    42  				Type:     schema.TypeString,
    43  				Computed: true,
    44  			},
    45  		},
    46  	}
    47  }
    48  
    49  func resourceDigitalOceanSSHKeyPublicKeyDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
    50  	return strings.TrimSpace(old) == strings.TrimSpace(new)
    51  }
    52  
    53  func resourceDigitalOceanSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
    54  	client := meta.(*godo.Client)
    55  
    56  	// Build up our creation options
    57  	opts := &godo.KeyCreateRequest{
    58  		Name:      d.Get("name").(string),
    59  		PublicKey: d.Get("public_key").(string),
    60  	}
    61  
    62  	log.Printf("[DEBUG] SSH Key create configuration: %#v", opts)
    63  	key, _, err := client.Keys.Create(opts)
    64  	if err != nil {
    65  		return fmt.Errorf("Error creating SSH Key: %s", err)
    66  	}
    67  
    68  	d.SetId(strconv.Itoa(key.ID))
    69  	log.Printf("[INFO] SSH Key: %d", key.ID)
    70  
    71  	return resourceDigitalOceanSSHKeyRead(d, meta)
    72  }
    73  
    74  func resourceDigitalOceanSSHKeyRead(d *schema.ResourceData, meta interface{}) error {
    75  	client := meta.(*godo.Client)
    76  
    77  	id, err := strconv.Atoi(d.Id())
    78  	if err != nil {
    79  		return fmt.Errorf("invalid SSH key id: %v", err)
    80  	}
    81  
    82  	key, resp, err := client.Keys.GetByID(id)
    83  	if err != nil {
    84  		// If the key is somehow already destroyed, mark as
    85  		// successfully gone
    86  		if resp != nil && resp.StatusCode == 404 {
    87  			d.SetId("")
    88  			return nil
    89  		}
    90  
    91  		return fmt.Errorf("Error retrieving SSH key: %s", err)
    92  	}
    93  
    94  	d.Set("name", key.Name)
    95  	d.Set("fingerprint", key.Fingerprint)
    96  	d.Set("public_key", key.PublicKey)
    97  
    98  	return nil
    99  }
   100  
   101  func resourceDigitalOceanSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error {
   102  	client := meta.(*godo.Client)
   103  
   104  	id, err := strconv.Atoi(d.Id())
   105  	if err != nil {
   106  		return fmt.Errorf("invalid SSH key id: %v", err)
   107  	}
   108  
   109  	var newName string
   110  	if v, ok := d.GetOk("name"); ok {
   111  		newName = v.(string)
   112  	}
   113  
   114  	log.Printf("[DEBUG] SSH key update name: %#v", newName)
   115  	opts := &godo.KeyUpdateRequest{
   116  		Name: newName,
   117  	}
   118  	_, _, err = client.Keys.UpdateByID(id, opts)
   119  	if err != nil {
   120  		return fmt.Errorf("Failed to update SSH key: %s", err)
   121  	}
   122  
   123  	return resourceDigitalOceanSSHKeyRead(d, meta)
   124  }
   125  
   126  func resourceDigitalOceanSSHKeyDelete(d *schema.ResourceData, meta interface{}) error {
   127  	client := meta.(*godo.Client)
   128  
   129  	id, err := strconv.Atoi(d.Id())
   130  	if err != nil {
   131  		return fmt.Errorf("invalid SSH key id: %v", err)
   132  	}
   133  
   134  	log.Printf("[INFO] Deleting SSH key: %d", id)
   135  	_, err = client.Keys.DeleteByID(id)
   136  	if err != nil {
   137  		return fmt.Errorf("Error deleting SSH key: %s", err)
   138  	}
   139  
   140  	d.SetId("")
   141  	return nil
   142  }