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