github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  		Importer: &schema.ResourceImporter{
    24  			State: resourceKeyImporter,
    25  		},
    26  
    27  		Schema: map[string]*schema.Schema{
    28  			"name": &schema.Schema{
    29  				Description: "name of this key (will be generated from the key comment, if not set and comment present)",
    30  				Type:        schema.TypeString,
    31  				Optional:    true,
    32  				Computed:    true,
    33  				ForceNew:    true,
    34  			},
    35  			"key": &schema.Schema{
    36  				Description: "content of public key from disk",
    37  				Type:        schema.TypeString,
    38  				Required:    true,
    39  				ForceNew:    true,
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func resourceKeyCreate(d *schema.ResourceData, meta interface{}) error {
    46  	client := meta.(*cloudapi.Client)
    47  
    48  	if d.Get("name").(string) == "" {
    49  		parts := strings.SplitN(d.Get("key").(string), " ", 3)
    50  		if len(parts) == 3 {
    51  			d.Set("name", parts[2])
    52  		} else {
    53  			return ErrNoKeyComment
    54  		}
    55  	}
    56  
    57  	_, err := client.CreateKey(cloudapi.CreateKeyOpts{
    58  		Name: d.Get("name").(string),
    59  		Key:  d.Get("key").(string),
    60  	})
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	d.SetId(d.Get("name").(string))
    66  
    67  	err = resourceKeyRead(d, meta)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func resourceKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    76  	client := meta.(*cloudapi.Client)
    77  
    78  	keys, err := client.ListKeys()
    79  	if err != nil {
    80  		return false, err
    81  	}
    82  
    83  	for _, key := range keys {
    84  		if key.Name == d.Id() {
    85  			return true, nil
    86  		}
    87  	}
    88  
    89  	return false, nil
    90  }
    91  
    92  func resourceKeyRead(d *schema.ResourceData, meta interface{}) error {
    93  	client := meta.(*cloudapi.Client)
    94  
    95  	key, err := client.GetKey(d.Id())
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	d.Set("name", key.Name)
   101  	d.Set("key", key.Key)
   102  
   103  	return nil
   104  }
   105  
   106  func resourceKeyDelete(d *schema.ResourceData, meta interface{}) error {
   107  	client := meta.(*cloudapi.Client)
   108  
   109  	if err := client.DeleteKey(d.Get("name").(string)); err != nil {
   110  		return err
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  func resourceKeyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
   117  	return []*schema.ResourceData{d}, nil
   118  }