2015-01-08 00:45:57 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-24 13:59:55 +01:00
|
|
|
"log"
|
2015-01-08 00:45:57 +01:00
|
|
|
|
2016-09-04 04:53:16 +02:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
|
2015-01-08 00:45:57 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeKeypairV2() *schema.Resource {
|
2015-01-08 00:45:57 +01:00
|
|
|
return &schema.Resource{
|
2015-01-26 19:09:27 +01:00
|
|
|
Create: resourceComputeKeypairV2Create,
|
|
|
|
Read: resourceComputeKeypairV2Read,
|
|
|
|
Delete: resourceComputeKeypairV2Delete,
|
2016-07-03 17:39:20 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-01-08 00:45:57 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-01-26 18:54:07 +01:00
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2016-04-08 23:28:54 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
|
2015-01-26 18:54:07 +01:00
|
|
|
},
|
2015-01-08 00:45:57 +01:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"public_key": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-10-10 12:25:06 +02:00
|
|
|
"value_specs": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-01-08 00:45:57 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeKeypairV2Create(d *schema.ResourceData, meta interface{}) error {
|
2015-01-08 00:45:57 +01:00
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
computeClient, err := config.computeV2Client(GetRegion(d))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2015-01-08 00:45:57 +01:00
|
|
|
|
2016-10-10 12:25:06 +02:00
|
|
|
createOpts := KeyPairCreateOpts{
|
|
|
|
keypairs.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
PublicKey: d.Get("public_key").(string),
|
|
|
|
},
|
|
|
|
MapValueSpecs(d),
|
2015-01-08 00:45:57 +01:00
|
|
|
}
|
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
2015-01-26 18:54:07 +01:00
|
|
|
kp, err := keypairs.Create(computeClient, createOpts).Extract()
|
2015-01-08 00:45:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack keypair: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(kp.Name)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceComputeKeypairV2Read(d, meta)
|
2015-01-08 00:45:57 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeKeypairV2Read(d *schema.ResourceData, meta interface{}) error {
|
2015-01-08 00:45:57 +01:00
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
computeClient, err := config.computeV2Client(GetRegion(d))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2015-01-08 00:45:57 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
kp, err := keypairs.Get(computeClient, d.Id()).Extract()
|
2015-01-08 00:45:57 +01:00
|
|
|
if err != nil {
|
2015-02-10 04:27:39 +01:00
|
|
|
return CheckDeleted(d, err, "keypair")
|
2015-01-08 00:45:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", kp.Name)
|
|
|
|
d.Set("public_key", kp.PublicKey)
|
2016-12-04 15:24:07 +01:00
|
|
|
d.Set("region", GetRegion(d))
|
2015-01-08 00:45:57 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeKeypairV2Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-01-08 00:45:57 +01:00
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
computeClient, err := config.computeV2Client(GetRegion(d))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2015-01-08 00:45:57 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
err = keypairs.Delete(computeClient, d.Id()).ExtractErr()
|
2015-01-08 00:45:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack keypair: %s", err)
|
|
|
|
}
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|