github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/triton/resource_key.go (about)

     1  package triton
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/joyent/triton-go"
    10  )
    11  
    12  func resourceKey() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create:   resourceKeyCreate,
    15  		Exists:   resourceKeyExists,
    16  		Read:     resourceKeyRead,
    17  		Delete:   resourceKeyDelete,
    18  		Timeouts: fastResourceTimeout,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": {
    25  				Description: "Name of the key (generated from the key comment if not set)",
    26  				Type:        schema.TypeString,
    27  				Optional:    true,
    28  				Computed:    true,
    29  				ForceNew:    true,
    30  			},
    31  			"key": {
    32  				Description: "Content of public key from disk in OpenSSH format",
    33  				Type:        schema.TypeString,
    34  				Required:    true,
    35  				ForceNew:    true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func resourceKeyCreate(d *schema.ResourceData, meta interface{}) error {
    42  	client := meta.(*triton.Client)
    43  
    44  	if keyName := d.Get("name").(string); keyName == "" {
    45  		parts := strings.SplitN(d.Get("key").(string), " ", 3)
    46  		if len(parts) == 3 {
    47  			d.Set("name", parts[2])
    48  		} else {
    49  			return errors.New("No key name specified, and key material has no comment")
    50  		}
    51  	}
    52  
    53  	_, err := client.Keys().CreateKey(context.Background(), &triton.CreateKeyInput{
    54  		Name: d.Get("name").(string),
    55  		Key:  d.Get("key").(string),
    56  	})
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	d.SetId(d.Get("name").(string))
    62  
    63  	return resourceKeyRead(d, meta)
    64  }
    65  
    66  func resourceKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    67  	client := meta.(*triton.Client)
    68  
    69  	_, err := client.Keys().GetKey(context.Background(), &triton.GetKeyInput{
    70  		KeyName: d.Id(),
    71  	})
    72  	if err != nil {
    73  		return false, err
    74  	}
    75  
    76  	return true, nil
    77  }
    78  
    79  func resourceKeyRead(d *schema.ResourceData, meta interface{}) error {
    80  	client := meta.(*triton.Client)
    81  
    82  	key, err := client.Keys().GetKey(context.Background(), &triton.GetKeyInput{
    83  		KeyName: d.Id(),
    84  	})
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	d.Set("name", key.Name)
    90  	d.Set("key", key.Key)
    91  
    92  	return nil
    93  }
    94  
    95  func resourceKeyDelete(d *schema.ResourceData, meta interface{}) error {
    96  	client := meta.(*triton.Client)
    97  
    98  	return client.Keys().DeleteKey(context.Background(), &triton.DeleteKeyInput{
    99  		KeyName: d.Id(),
   100  	})
   101  }