server keypair

This commit is contained in:
Jon Perritt 2015-01-04 09:52:49 -07:00
parent cc9ee787ac
commit 3112103acf
1 changed files with 20 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud" "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers" "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
) )
@ -81,6 +82,7 @@ func resourceComputeInstance() *schema.Resource {
"metadata": &schema.Schema{ "metadata": &schema.Schema{
Type: schema.TypeMap, Type: schema.TypeMap,
Optional: true, Optional: true,
ForceNew: false,
}, },
"config_drive": &schema.Schema{ "config_drive": &schema.Schema{
@ -102,6 +104,12 @@ func resourceComputeInstance() *schema.Resource {
Optional: true, Optional: true,
ForceNew: false, ForceNew: false,
}, },
"key_pair": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
}, },
} }
} }
@ -110,7 +118,9 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
config := meta.(*Config) config := meta.(*Config)
osClient := config.computeV2Client osClient := config.computeV2Client
createOpts := &servers.CreateOpts{ var createOpts servers.CreateOptsBuilder
serverCreateOpts := &servers.CreateOpts{
Name: d.Get("name").(string), Name: d.Get("name").(string),
ImageRef: d.Get("image_ref").(string), ImageRef: d.Get("image_ref").(string),
FlavorRef: d.Get("flavor_ref").(string), FlavorRef: d.Get("flavor_ref").(string),
@ -121,6 +131,15 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
ConfigDrive: d.Get("config_drive").(bool), ConfigDrive: d.Get("config_drive").(bool),
} }
if kp, ok := d.Get("key_pair").(map[string]interface{}); ok && kp != nil {
if keyName, ok := kp["name"].(string); ok && keyName != "" {
createOpts = &keypairs.CreateOptsExt{
serverCreateOpts,
keyName,
}
}
}
log.Printf("[INFO] Requesting instance creation") log.Printf("[INFO] Requesting instance creation")
server, err := servers.Create(osClient, createOpts).Extract() server, err := servers.Create(osClient, createOpts).Extract()
if err != nil { if err != nil {