github.com/ggiamarchi/terraform@v0.3.7-0.20150607194748-ed2a66a46a71/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair.go (about)

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