add/get/delete keypairs

This commit is contained in:
Jon Perritt 2015-01-07 16:45:57 -07:00
parent 47955b1d44
commit 01e41646d3
2 changed files with 77 additions and 0 deletions

View File

@ -49,6 +49,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"openstack_compute_instance": resourceComputeInstance(),
"openstack_compute_keypair": resourceComputeKeypair(),
},
ConfigureFunc: configureProvider,

View File

@ -0,0 +1,76 @@
package openstack
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
)
func resourceComputeKeypair() *schema.Resource {
return &schema.Resource{
Create: resourceComputeKeypairCreate,
Read: resourceComputeKeypairRead,
Delete: resourceComputeKeypairDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"public_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceComputeKeypairCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
osClient := config.computeV2Client
createOpts := keypairs.CreateOpts{
Name: d.Get("name").(string),
PublicKey: d.Get("public_key").(string),
}
kp, err := keypairs.Create(osClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenStack keypair: %s", err)
}
d.SetId(kp.Name)
return resourceComputeKeypairRead(d, meta)
}
func resourceComputeKeypairRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
osClient := config.computeV2Client
kp, err := keypairs.Get(osClient, d.Id()).Extract()
if err != nil {
return fmt.Errorf("Error retrieving OpenStack keypair: %s", err)
}
d.Set("name", kp.Name)
d.Set("public_key", kp.PublicKey)
return nil
}
func resourceComputeKeypairDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
osClient := config.computeV2Client
err := keypairs.Delete(osClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting OpenStack keypair: %s", err)
}
d.SetId("")
return nil
}