github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/triton/resource_key.go (about)

     1  package triton
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/joyent/gosdc/cloudapi"
     9  )
    10  
    11  var (
    12  	// ErrNoKeyComment will be returned when the key name cannot be generated from
    13  	// the key comment and is not otherwise specified.
    14  	ErrNoKeyComment = errors.New("no key comment found to use as a name (and none specified)")
    15  )
    16  
    17  func resourceKey() *schema.Resource {
    18  	return &schema.Resource{
    19  		Create: resourceKeyCreate,
    20  		Exists: resourceKeyExists,
    21  		Read:   resourceKeyRead,
    22  		Delete: resourceKeyDelete,
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"name": &schema.Schema{
    26  				Description: "name of this key (will be generated from the key comment, if not set and comment present)",
    27  				Type:        schema.TypeString,
    28  				Optional:    true,
    29  				Computed:    true,
    30  				ForceNew:    true,
    31  			},
    32  			"key": &schema.Schema{
    33  				Description: "content of public key from disk",
    34  				Type:        schema.TypeString,
    35  				Required:    true,
    36  				ForceNew:    true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceKeyCreate(d *schema.ResourceData, meta interface{}) error {
    43  	client := meta.(*cloudapi.Client)
    44  
    45  	if d.Get("name").(string) == "" {
    46  		parts := strings.SplitN(d.Get("key").(string), " ", 3)
    47  		if len(parts) == 3 {
    48  			d.Set("name", parts[2])
    49  		} else {
    50  			return ErrNoKeyComment
    51  		}
    52  	}
    53  
    54  	_, err := client.CreateKey(cloudapi.CreateKeyOpts{
    55  		Name: d.Get("name").(string),
    56  		Key:  d.Get("key").(string),
    57  	})
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	err = resourceKeyRead(d, meta)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func resourceKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    71  	client := meta.(*cloudapi.Client)
    72  
    73  	keys, err := client.ListKeys()
    74  	if err != nil {
    75  		return false, err
    76  	}
    77  
    78  	for _, key := range keys {
    79  		if key.Name == d.Id() {
    80  			return true, nil
    81  		}
    82  	}
    83  
    84  	return false, nil
    85  }
    86  
    87  func resourceKeyRead(d *schema.ResourceData, meta interface{}) error {
    88  	client := meta.(*cloudapi.Client)
    89  
    90  	key, err := client.GetKey(d.Get("name").(string))
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	d.SetId(key.Name)
    96  	d.Set("name", key.Name)
    97  	d.Set("key", key.Key)
    98  
    99  	return nil
   100  }
   101  
   102  func resourceKeyDelete(d *schema.ResourceData, meta interface{}) error {
   103  	client := meta.(*cloudapi.Client)
   104  
   105  	if err := client.DeleteKey(d.Get("name").(string)); err != nil {
   106  		return err
   107  	}
   108  
   109  	return nil
   110  }