diff --git a/builtin/providers/openstack/provider.go b/builtin/providers/openstack/provider.go index 356081d19..2a5aad71b 100644 --- a/builtin/providers/openstack/provider.go +++ b/builtin/providers/openstack/provider.go @@ -49,6 +49,7 @@ func Provider() terraform.ResourceProvider { ResourcesMap: map[string]*schema.Resource{ "openstack_compute_instance": resourceComputeInstance(), + "openstack_compute_keypair": resourceComputeKeypair(), }, ConfigureFunc: configureProvider, diff --git a/builtin/providers/openstack/resource_openstack_compute_keypair.go b/builtin/providers/openstack/resource_openstack_compute_keypair.go new file mode 100644 index 000000000..a8ca16e08 --- /dev/null +++ b/builtin/providers/openstack/resource_openstack_compute_keypair.go @@ -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 +}