github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/rundeck/resource_private_key.go (about)

     1  package rundeck
     2  
     3  import (
     4  	"crypto/sha1"
     5  	"encoding/hex"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  
     9  	"github.com/apparentlymart/go-rundeck-api/rundeck"
    10  )
    11  
    12  func resourceRundeckPrivateKey() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: CreateOrUpdatePrivateKey,
    15  		Update: CreateOrUpdatePrivateKey,
    16  		Delete: DeletePrivateKey,
    17  		Exists: PrivateKeyExists,
    18  		Read:   ReadPrivateKey,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"path": &schema.Schema{
    22  				Type:        schema.TypeString,
    23  				Required:    true,
    24  				Description: "Path to the key within the key store",
    25  				ForceNew:    true,
    26  			},
    27  
    28  			"key_material": &schema.Schema{
    29  				Type:        schema.TypeString,
    30  				Required:    true,
    31  				Description: "The private key material to store, in PEM format",
    32  				StateFunc: func(v interface{}) string {
    33  					switch v.(type) {
    34  					case string:
    35  						hash := sha1.Sum([]byte(v.(string)))
    36  						return hex.EncodeToString(hash[:])
    37  					default:
    38  						return ""
    39  					}
    40  				},
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func CreateOrUpdatePrivateKey(d *schema.ResourceData, meta interface{}) error {
    47  	client := meta.(*rundeck.Client)
    48  
    49  	path := d.Get("path").(string)
    50  	keyMaterial := d.Get("key_material").(string)
    51  
    52  	var err error
    53  
    54  	if d.Id() != "" {
    55  		err = client.ReplacePrivateKey(path, keyMaterial)
    56  	} else {
    57  		err = client.CreatePrivateKey(path, keyMaterial)
    58  	}
    59  
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	d.SetId(path)
    65  
    66  	return ReadPrivateKey(d, meta)
    67  }
    68  
    69  func DeletePrivateKey(d *schema.ResourceData, meta interface{}) error {
    70  	client := meta.(*rundeck.Client)
    71  
    72  	path := d.Id()
    73  
    74  	// The only "delete" call we have is oblivious to key type, but
    75  	// that's okay since our Exists implementation makes sure that we
    76  	// won't try to delete a key of the wrong type since we'll pretend
    77  	// that it's already been deleted.
    78  	err := client.DeleteKey(path)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	d.SetId("")
    84  	return nil
    85  }
    86  
    87  func ReadPrivateKey(d *schema.ResourceData, meta interface{}) error {
    88  	// Nothing to read for a private key: existence is all we need to
    89  	// worry about, and PrivateKeyExists took care of that.
    90  	return nil
    91  }
    92  
    93  func PrivateKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    94  	client := meta.(*rundeck.Client)
    95  
    96  	path := d.Id()
    97  
    98  	key, err := client.GetKeyMeta(path)
    99  	if err != nil {
   100  		if _, ok := err.(rundeck.NotFoundError); ok {
   101  			err = nil
   102  		}
   103  		return false, err
   104  	}
   105  
   106  	if key.KeyType != "private" {
   107  		// If the key type isn't public then as far as this resource is
   108  		// concerned it doesn't exist. (We'll fail properly when we try to
   109  		// create a key where one already exists.)
   110  		return false, nil
   111  	}
   112  
   113  	return true, nil
   114  }