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

     1  package packet
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/packethost/packngo"
    10  )
    11  
    12  func resourcePacketSSHKey() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourcePacketSSHKeyCreate,
    15  		Read:   resourcePacketSSHKeyRead,
    16  		Update: resourcePacketSSHKeyUpdate,
    17  		Delete: resourcePacketSSHKeyDelete,
    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  			"created": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Computed: true,
    44  			},
    45  
    46  			"updated": &schema.Schema{
    47  				Type:     schema.TypeString,
    48  				Computed: true,
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  func resourcePacketSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
    55  	client := meta.(*packngo.Client)
    56  
    57  	createRequest := &packngo.SSHKeyCreateRequest{
    58  		Label: d.Get("name").(string),
    59  		Key:   d.Get("public_key").(string),
    60  	}
    61  
    62  	log.Printf("[DEBUG] SSH Key create configuration: %#v", createRequest)
    63  	key, _, err := client.SSHKeys.Create(createRequest)
    64  	if err != nil {
    65  		return fmt.Errorf("Error creating SSH Key: %s", err)
    66  	}
    67  
    68  	d.SetId(key.ID)
    69  	log.Printf("[INFO] SSH Key: %s", key.ID)
    70  
    71  	return resourcePacketSSHKeyRead(d, meta)
    72  }
    73  
    74  func resourcePacketSSHKeyRead(d *schema.ResourceData, meta interface{}) error {
    75  	client := meta.(*packngo.Client)
    76  
    77  	key, _, err := client.SSHKeys.Get(d.Id())
    78  	if err != nil {
    79  		// If the key is somehow already destroyed, mark as
    80  		// succesfully gone
    81  		if strings.Contains(err.Error(), "404") {
    82  			d.SetId("")
    83  			return nil
    84  		}
    85  
    86  		return fmt.Errorf("Error retrieving SSH key: %s", err)
    87  	}
    88  
    89  	d.Set("id", key.ID)
    90  	d.Set("name", key.Label)
    91  	d.Set("public_key", key.Key)
    92  	d.Set("fingerprint", key.FingerPrint)
    93  	d.Set("created", key.Created)
    94  	d.Set("updated", key.Updated)
    95  
    96  	return nil
    97  }
    98  
    99  func resourcePacketSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error {
   100  	client := meta.(*packngo.Client)
   101  
   102  	updateRequest := &packngo.SSHKeyUpdateRequest{
   103  		ID:    d.Get("id").(string),
   104  		Label: d.Get("name").(string),
   105  		Key:   d.Get("public_key").(string),
   106  	}
   107  
   108  	log.Printf("[DEBUG] SSH key update: %#v", d.Get("id"))
   109  	_, _, err := client.SSHKeys.Update(updateRequest)
   110  	if err != nil {
   111  		return fmt.Errorf("Failed to update SSH key: %s", err)
   112  	}
   113  
   114  	return resourcePacketSSHKeyRead(d, meta)
   115  }
   116  
   117  func resourcePacketSSHKeyDelete(d *schema.ResourceData, meta interface{}) error {
   118  	client := meta.(*packngo.Client)
   119  
   120  	log.Printf("[INFO] Deleting SSH key: %s", d.Id())
   121  	_, err := client.SSHKeys.Delete(d.Id())
   122  	if err != nil {
   123  		return fmt.Errorf("Error deleting SSH key: %s", err)
   124  	}
   125  
   126  	d.SetId("")
   127  	return nil
   128  }