github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/rundeck/resource_public_key.go (about)

     1  package rundeck
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/schema"
     5  
     6  	"github.com/apparentlymart/go-rundeck-api/rundeck"
     7  )
     8  
     9  func resourceRundeckPublicKey() *schema.Resource {
    10  	return &schema.Resource{
    11  		Create: CreatePublicKey,
    12  		Update: UpdatePublicKey,
    13  		Delete: DeletePublicKey,
    14  		Exists: PublicKeyExists,
    15  		Read:   ReadPublicKey,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"path": &schema.Schema{
    19  				Type:        schema.TypeString,
    20  				Required:    true,
    21  				Description: "Path to the key within the key store",
    22  				ForceNew:    true,
    23  			},
    24  
    25  			"key_material": &schema.Schema{
    26  				Type:        schema.TypeString,
    27  				Optional:    true,
    28  				Computed:    true,
    29  				Description: "The public key data to store, in the usual OpenSSH public key file format",
    30  			},
    31  
    32  			"url": &schema.Schema{
    33  				Type:        schema.TypeString,
    34  				Computed:    true,
    35  				Description: "URL at which the key content can be retrieved",
    36  			},
    37  
    38  			"delete": &schema.Schema{
    39  				Type:        schema.TypeBool,
    40  				Computed:    true,
    41  				Description: "True if the key should be deleted when the resource is deleted. Defaults to true if key_material is provided in the configuration.",
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func CreatePublicKey(d *schema.ResourceData, meta interface{}) error {
    48  	client := meta.(*rundeck.Client)
    49  
    50  	path := d.Get("path").(string)
    51  	keyMaterial := d.Get("key_material").(string)
    52  
    53  	if keyMaterial != "" {
    54  		err := client.CreatePublicKey(path, keyMaterial)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		d.Set("delete", true)
    59  	}
    60  
    61  	d.SetId(path)
    62  
    63  	return ReadPublicKey(d, meta)
    64  }
    65  
    66  func UpdatePublicKey(d *schema.ResourceData, meta interface{}) error {
    67  	client := meta.(*rundeck.Client)
    68  
    69  	if d.HasChange("key_material") {
    70  		path := d.Get("path").(string)
    71  		keyMaterial := d.Get("key_material").(string)
    72  
    73  		err := client.ReplacePublicKey(path, keyMaterial)
    74  		if err != nil {
    75  			return err
    76  		}
    77  	}
    78  
    79  	return ReadPublicKey(d, meta)
    80  }
    81  
    82  func DeletePublicKey(d *schema.ResourceData, meta interface{}) error {
    83  	client := meta.(*rundeck.Client)
    84  
    85  	path := d.Id()
    86  
    87  	// Since this resource can be used both to create and to read existing
    88  	// public keys, we'll only actually delete the key if we remember that
    89  	// we created the key in the first place, or if the user explicitly
    90  	// opted in to have an existing key deleted.
    91  	if d.Get("delete").(bool) {
    92  		// The only "delete" call we have is oblivious to key type, but
    93  		// that's okay since our Exists implementation makes sure that we
    94  		// won't try to delete a key of the wrong type since we'll pretend
    95  		// that it's already been deleted.
    96  		err := client.DeleteKey(path)
    97  		if err != nil {
    98  			return err
    99  		}
   100  	}
   101  
   102  	d.SetId("")
   103  	return nil
   104  }
   105  
   106  func ReadPublicKey(d *schema.ResourceData, meta interface{}) error {
   107  	client := meta.(*rundeck.Client)
   108  
   109  	path := d.Id()
   110  
   111  	key, err := client.GetKeyMeta(path)
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	keyMaterial, err := client.GetKeyContent(path)
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	d.Set("key_material", keyMaterial)
   122  	d.Set("url", key.URL)
   123  
   124  	return nil
   125  }
   126  
   127  func PublicKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   128  	client := meta.(*rundeck.Client)
   129  
   130  	path := d.Id()
   131  
   132  	key, err := client.GetKeyMeta(path)
   133  	if err != nil {
   134  		if _, ok := err.(rundeck.NotFoundError); ok {
   135  			err = nil
   136  		}
   137  		return false, err
   138  	}
   139  
   140  	if key.KeyType != "public" {
   141  		// If the key type isn't public then as far as this resource is
   142  		// concerned it doesn't exist. (We'll fail properly when we try to
   143  		// create a key where one already exists.)
   144  		return false, nil
   145  	}
   146  
   147  	return true, nil
   148  }