github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/mitchellh/go-homedir"
    11  	"github.com/xanzy/go-cloudstack/cloudstack"
    12  )
    13  
    14  func resourceCloudStackSSHKeyPair() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceCloudStackSSHKeyPairCreate,
    17  		Read:   resourceCloudStackSSHKeyPairRead,
    18  		Delete: resourceCloudStackSSHKeyPairDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"public_key": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				ForceNew: true,
    31  			},
    32  
    33  			"private_key": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Computed: true,
    36  			},
    37  
    38  			"fingerprint": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
    47  	cs := meta.(*cloudstack.CloudStackClient)
    48  
    49  	name := d.Get("name").(string)
    50  	publicKey := d.Get("public_key").(string)
    51  
    52  	if publicKey != "" {
    53  		// Register supplied key
    54  		keyPath, err := homedir.Expand(publicKey)
    55  		if err != nil {
    56  			return fmt.Errorf("Error expanding the public key path: %v", err)
    57  		}
    58  
    59  		key, err := ioutil.ReadFile(keyPath)
    60  		if err != nil {
    61  			return fmt.Errorf("Error reading the public key: %v", err)
    62  		}
    63  
    64  		p := cs.SSH.NewRegisterSSHKeyPairParams(name, string(key))
    65  		_, err = cs.SSH.RegisterSSHKeyPair(p)
    66  		if err != nil {
    67  			return err
    68  		}
    69  	} else {
    70  		// No key supplied, must create one and return the private key
    71  		p := cs.SSH.NewCreateSSHKeyPairParams(name)
    72  		r, err := cs.SSH.CreateSSHKeyPair(p)
    73  		if err != nil {
    74  			return err
    75  		}
    76  		d.Set("private_key", r.Privatekey)
    77  	}
    78  
    79  	log.Printf("[DEBUG] Key pair successfully generated at Cloudstack")
    80  	d.SetId(name)
    81  
    82  	return resourceCloudStackSSHKeyPairRead(d, meta)
    83  }
    84  
    85  func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{}) error {
    86  	cs := meta.(*cloudstack.CloudStackClient)
    87  
    88  	log.Printf("[DEBUG] looking for key pair with name %s", d.Id())
    89  
    90  	p := cs.SSH.NewListSSHKeyPairsParams()
    91  	p.SetName(d.Id())
    92  
    93  	r, err := cs.SSH.ListSSHKeyPairs(p)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	if r.Count == 0 {
    98  		log.Printf("[DEBUG] Key pair %s does not exist", d.Id())
    99  		d.SetId("")
   100  		return nil
   101  	}
   102  
   103  	//SSHKeyPair name is unique in a cloudstack account so dont need to check for multiple
   104  	d.Set("name", r.SSHKeyPairs[0].Name)
   105  	d.Set("fingerprint", r.SSHKeyPairs[0].Fingerprint)
   106  
   107  	return nil
   108  }
   109  
   110  func resourceCloudStackSSHKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
   111  	cs := meta.(*cloudstack.CloudStackClient)
   112  
   113  	// Create a new parameter struct
   114  	p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
   115  
   116  	// Remove the SSH Keypair
   117  	_, err := cs.SSH.DeleteSSHKeyPair(p)
   118  	if err != nil {
   119  		// This is a very poor way to be told the ID does no longer exist :(
   120  		if strings.Contains(err.Error(), fmt.Sprintf(
   121  			"A key pair with name '%s' does not exist for account", d.Id())) {
   122  			return nil
   123  		}
   124  
   125  		return fmt.Errorf("Error deleting key pair: %s", err)
   126  	}
   127  
   128  	return nil
   129  }