github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  			"project": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Optional: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"private_key": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Computed: true,
    40  			},
    41  
    42  			"fingerprint": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Computed: true,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
    51  	cs := meta.(*cloudstack.CloudStackClient)
    52  
    53  	name := d.Get("name").(string)
    54  	publicKey := d.Get("public_key").(string)
    55  
    56  	if publicKey != "" {
    57  		// Register supplied key
    58  		p := cs.SSH.NewRegisterSSHKeyPairParams(name, publicKey)
    59  
    60  		// If there is a project supplied, we retrieve and set the project id
    61  		if err := setProjectid(p, cs, d); err != nil {
    62  			return err
    63  		}
    64  
    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  
    73  		// If there is a project supplied, we retrieve and set the project id
    74  		if err := setProjectid(p, cs, d); err != nil {
    75  			return err
    76  		}
    77  
    78  		r, err := cs.SSH.CreateSSHKeyPair(p)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		d.Set("private_key", r.Privatekey)
    83  	}
    84  
    85  	log.Printf("[DEBUG] Key pair successfully generated at Cloudstack")
    86  	d.SetId(name)
    87  
    88  	return resourceCloudStackSSHKeyPairRead(d, meta)
    89  }
    90  
    91  func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{}) error {
    92  	cs := meta.(*cloudstack.CloudStackClient)
    93  
    94  	log.Printf("[DEBUG] looking for key pair with name %s", d.Id())
    95  
    96  	p := cs.SSH.NewListSSHKeyPairsParams()
    97  	p.SetName(d.Id())
    98  
    99  	// If there is a project supplied, we retrieve and set the project id
   100  	if err := setProjectid(p, cs, d); err != nil {
   101  		return err
   102  	}
   103  
   104  	r, err := cs.SSH.ListSSHKeyPairs(p)
   105  	if err != nil {
   106  		return err
   107  	}
   108  	if r.Count == 0 {
   109  		log.Printf("[DEBUG] Key pair %s does not exist", d.Id())
   110  		d.SetId("")
   111  		return nil
   112  	}
   113  
   114  	//SSHKeyPair name is unique in a cloudstack account so dont need to check for multiple
   115  	d.Set("name", r.SSHKeyPairs[0].Name)
   116  	d.Set("fingerprint", r.SSHKeyPairs[0].Fingerprint)
   117  
   118  	return nil
   119  }
   120  
   121  func resourceCloudStackSSHKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
   122  	cs := meta.(*cloudstack.CloudStackClient)
   123  
   124  	// Create a new parameter struct
   125  	p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
   126  
   127  	// If there is a project supplied, we retrieve and set the project id
   128  	if err := setProjectid(p, cs, d); err != nil {
   129  		return err
   130  	}
   131  
   132  	// Remove the SSH Keypair
   133  	_, err := cs.SSH.DeleteSSHKeyPair(p)
   134  	if err != nil {
   135  		// This is a very poor way to be told the ID does no longer exist :(
   136  		if strings.Contains(err.Error(), fmt.Sprintf(
   137  			"A key pair with name '%s' does not exist for account", d.Id())) {
   138  			return nil
   139  		}
   140  
   141  		return fmt.Errorf("Error deleting key pair: %s", err)
   142  	}
   143  
   144  	return nil
   145  }