2014-10-29 22:52:36 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2015-02-10 17:01:08 +01:00
|
|
|
"bytes"
|
2015-03-20 23:55:42 +01:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2014-10-29 22:52:36 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-09-12 22:21:55 +02:00
|
|
|
"os"
|
2014-10-29 22:52:36 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/rackspace/gophercloud"
|
2015-02-01 10:10:42 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
|
2015-04-01 17:31:21 +02:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip"
|
2015-01-04 17:52:49 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
|
2015-05-05 22:52:46 +02:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/schedulerhints"
|
2015-01-05 20:16:33 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
|
2016-06-15 21:26:18 +02:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/startstop"
|
2015-03-21 03:35:31 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/tenantnetworks"
|
2015-02-10 17:01:08 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/volumeattach"
|
2015-02-11 06:24:38 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
|
2015-02-10 06:20:23 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/images"
|
2014-10-29 22:52:36 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
|
|
|
|
)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeInstanceV2() *schema.Resource {
|
2014-10-29 22:52:36 +01:00
|
|
|
return &schema.Resource{
|
2015-01-26 19:09:27 +01:00
|
|
|
Create: resourceComputeInstanceV2Create,
|
|
|
|
Read: resourceComputeInstanceV2Read,
|
|
|
|
Update: resourceComputeInstanceV2Update,
|
|
|
|
Delete: resourceComputeInstanceV2Delete,
|
2014-10-29 22:52:36 +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
|
|
|
},
|
2014-10-29 22:52:36 +01:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2015-02-10 06:20:23 +01:00
|
|
|
"image_id": &schema.Schema{
|
2015-09-12 22:21:55 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Computed: true,
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
2015-02-10 06:20:23 +01:00
|
|
|
"image_name": &schema.Schema{
|
2015-09-12 22:21:55 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Computed: true,
|
2015-02-10 06:20:23 +01:00
|
|
|
},
|
2015-02-11 06:24:38 +01:00
|
|
|
"flavor_id": &schema.Schema{
|
2015-01-27 18:19:11 +01:00
|
|
|
Type: schema.TypeString,
|
2015-02-01 10:10:42 +01:00
|
|
|
Optional: true,
|
2015-01-27 18:19:11 +01:00
|
|
|
ForceNew: false,
|
2015-02-11 06:24:38 +01:00
|
|
|
Computed: true,
|
2016-04-08 23:28:54 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_FLAVOR_ID", nil),
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
2015-02-11 06:24:38 +01:00
|
|
|
"flavor_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
Computed: true,
|
2016-04-08 23:28:54 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_FLAVOR_NAME", nil),
|
2015-02-11 06:24:38 +01:00
|
|
|
},
|
2015-02-06 14:34:11 +01:00
|
|
|
"floating_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2015-04-01 17:31:21 +02:00
|
|
|
"user_data": &schema.Schema{
|
2015-03-20 23:55:42 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
// just stash the hash for state & diff comparisons
|
|
|
|
StateFunc: func(v interface{}) string {
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
hash := sha1.Sum([]byte(v.(string)))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2014-10-29 22:52:36 +01:00
|
|
|
"security_groups": &schema.Schema{
|
2015-10-27 07:50:39 +01:00
|
|
|
Type: schema.TypeSet,
|
2014-10-29 22:52:36 +01:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
2015-11-07 05:01:50 +01:00
|
|
|
Computed: true,
|
2014-10-29 22:52:36 +01:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2015-10-27 07:50:39 +01:00
|
|
|
Set: schema.HashString,
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
|
|
|
"availability_zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-02-01 01:21:57 +01:00
|
|
|
"network": &schema.Schema{
|
2014-10-29 22:52:36 +01:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-04-01 18:06:47 +02:00
|
|
|
Computed: true,
|
2014-10-29 22:52:36 +01:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"uuid": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-24 05:43:50 +02:00
|
|
|
ForceNew: true,
|
2015-03-24 14:24:04 +01:00
|
|
|
Computed: true,
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
2015-03-21 03:35:31 +01:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-24 05:43:50 +02:00
|
|
|
ForceNew: true,
|
2015-03-24 14:24:04 +01:00
|
|
|
Computed: true,
|
2015-03-21 03:35:31 +01:00
|
|
|
},
|
2014-10-29 22:52:36 +01:00
|
|
|
"port": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-24 05:43:50 +02:00
|
|
|
ForceNew: true,
|
2015-03-24 14:24:04 +01:00
|
|
|
Computed: true,
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
2015-03-21 03:16:46 +01:00
|
|
|
"fixed_ip_v4": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-24 05:43:50 +02:00
|
|
|
ForceNew: true,
|
2015-03-24 14:24:04 +01:00
|
|
|
Computed: true,
|
2015-03-21 03:16:46 +01:00
|
|
|
},
|
|
|
|
"fixed_ip_v6": &schema.Schema{
|
2014-10-29 22:52:36 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-24 05:43:50 +02:00
|
|
|
ForceNew: true,
|
2015-03-24 14:24:04 +01:00
|
|
|
Computed: true,
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
2016-01-24 21:39:35 +01:00
|
|
|
"floating_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-03-21 04:12:22 +01:00
|
|
|
"mac": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-03-24 14:24:04 +01:00
|
|
|
Computed: true,
|
2015-03-21 04:12:22 +01:00
|
|
|
},
|
2016-01-24 21:39:35 +01:00
|
|
|
"access_network": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"metadata": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
2015-01-04 17:52:49 +01:00
|
|
|
ForceNew: false,
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
|
|
|
"config_drive": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-01-07 19:38:23 +01:00
|
|
|
"admin_pass": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2014-10-29 22:52:36 +01:00
|
|
|
"access_ip_v4": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"access_ip_v6": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2015-01-04 17:52:49 +01:00
|
|
|
"key_pair": &schema.Schema{
|
2015-01-07 19:38:23 +01:00
|
|
|
Type: schema.TypeString,
|
2015-01-04 17:52:49 +01:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-02-01 10:10:42 +01:00
|
|
|
"block_device": &schema.Schema{
|
2015-12-12 19:51:21 +01:00
|
|
|
Type: schema.TypeList,
|
2015-02-01 10:10:42 +01:00
|
|
|
Optional: true,
|
2015-02-01 10:36:58 +01:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-12-12 21:32:10 +01:00
|
|
|
"source_type": &schema.Schema{
|
2015-02-01 10:36:58 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2016-05-29 00:40:36 +02:00
|
|
|
ForceNew: true,
|
2015-02-01 10:36:58 +01:00
|
|
|
},
|
2015-12-12 21:32:10 +01:00
|
|
|
"uuid": &schema.Schema{
|
2015-02-01 10:36:58 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-12 21:32:10 +01:00
|
|
|
Optional: true,
|
2016-05-29 00:40:36 +02:00
|
|
|
ForceNew: true,
|
2015-02-01 10:36:58 +01:00
|
|
|
},
|
|
|
|
"volume_size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
2016-02-27 06:56:12 +01:00
|
|
|
Optional: true,
|
2016-05-29 00:40:36 +02:00
|
|
|
ForceNew: true,
|
2015-02-01 10:36:58 +01:00
|
|
|
},
|
|
|
|
"destination_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-29 00:40:36 +02:00
|
|
|
ForceNew: true,
|
2015-02-01 10:36:58 +01:00
|
|
|
},
|
|
|
|
"boot_index": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2016-05-29 00:40:36 +02:00
|
|
|
ForceNew: true,
|
2015-02-01 10:36:58 +01:00
|
|
|
},
|
2015-09-12 06:15:21 +02:00
|
|
|
"delete_on_termination": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
2016-05-29 00:40:36 +02:00
|
|
|
ForceNew: true,
|
2015-09-12 06:15:21 +02:00
|
|
|
},
|
2015-12-12 21:32:10 +01:00
|
|
|
"guest_format": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-29 00:40:36 +02:00
|
|
|
ForceNew: true,
|
2015-12-12 21:32:10 +01:00
|
|
|
},
|
2015-02-01 10:36:58 +01:00
|
|
|
},
|
|
|
|
},
|
2015-02-01 10:10:42 +01:00
|
|
|
},
|
2015-02-10 17:01:08 +01:00
|
|
|
"volume": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-08-13 21:59:25 +02:00
|
|
|
Optional: true,
|
2015-02-10 17:01:08 +01:00
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"volume_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-08-13 21:59:25 +02:00
|
|
|
Required: true,
|
2015-02-10 17:01:08 +01:00
|
|
|
},
|
|
|
|
"device": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceComputeVolumeAttachmentHash,
|
|
|
|
},
|
2015-05-05 22:52:46 +02:00
|
|
|
"scheduler_hints": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"group": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"different_host": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
"same_host": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
"query": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
"target_cell": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"build_near_host_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceComputeSchedulerHintsHash,
|
|
|
|
},
|
2016-01-11 04:53:30 +01:00
|
|
|
"personality": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"file": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"content": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceComputeInstancePersonalityHash,
|
|
|
|
},
|
2016-06-15 21:26:18 +02:00
|
|
|
"stop_before_destroy": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
2014-10-29 22:52:36 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) error {
|
2014-10-29 22:52:36 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
computeClient, err := config.computeV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-01-04 17:52:49 +01:00
|
|
|
var createOpts servers.CreateOptsBuilder
|
|
|
|
|
2015-09-12 22:21:55 +02:00
|
|
|
// Determines the Image ID using the following rules:
|
|
|
|
// If a bootable block_device was specified, ignore the image altogether.
|
|
|
|
// If an image_id was specified, use it.
|
|
|
|
// If an image_name was specified, look up the image ID, report if error.
|
|
|
|
imageId, err := getImageIDFromConfig(computeClient, d)
|
2015-02-10 06:20:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-11 06:24:38 +01:00
|
|
|
flavorId, err := getFlavorID(computeClient, d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-27 06:56:12 +01:00
|
|
|
// determine if block_device configuration is correct
|
|
|
|
// this includes valid combinations and required attributes
|
|
|
|
if err := checkBlockDeviceConfig(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
// check if floating IP configuration is correct
|
|
|
|
if err := checkInstanceFloatingIPs(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a list of networks with the information given upon creation.
|
|
|
|
// Error out if an invalid network configuration was used.
|
|
|
|
networkDetails, err := getInstanceNetworks(computeClient, d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-21 03:35:31 +01:00
|
|
|
networks := make([]servers.Network, len(networkDetails))
|
|
|
|
for i, net := range networkDetails {
|
|
|
|
networks[i] = servers.Network{
|
|
|
|
UUID: net["uuid"].(string),
|
|
|
|
Port: net["port"].(string),
|
|
|
|
FixedIP: net["fixed_ip_v4"].(string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 06:35:18 +01:00
|
|
|
createOpts = &servers.CreateOpts{
|
2015-01-05 20:16:33 +01:00
|
|
|
Name: d.Get("name").(string),
|
2015-02-10 06:20:23 +01:00
|
|
|
ImageRef: imageId,
|
2015-02-11 06:24:38 +01:00
|
|
|
FlavorRef: flavorId,
|
2015-01-26 19:09:27 +01:00
|
|
|
SecurityGroups: resourceInstanceSecGroupsV2(d),
|
2014-10-29 22:52:36 +01:00
|
|
|
AvailabilityZone: d.Get("availability_zone").(string),
|
2015-03-21 03:35:31 +01:00
|
|
|
Networks: networks,
|
2015-01-26 19:09:27 +01:00
|
|
|
Metadata: resourceInstanceMetadataV2(d),
|
2014-10-29 22:52:36 +01:00
|
|
|
ConfigDrive: d.Get("config_drive").(bool),
|
2015-01-07 19:38:23 +01:00
|
|
|
AdminPass: d.Get("admin_pass").(string),
|
2015-03-20 23:55:42 +01:00
|
|
|
UserData: []byte(d.Get("user_data").(string)),
|
2016-01-11 04:53:30 +01:00
|
|
|
Personality: resourceInstancePersonalityV2(d),
|
2014-10-29 22:52:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 19:38:23 +01:00
|
|
|
if keyName, ok := d.Get("key_pair").(string); ok && keyName != "" {
|
|
|
|
createOpts = &keypairs.CreateOptsExt{
|
2016-02-12 22:57:39 +01:00
|
|
|
CreateOptsBuilder: createOpts,
|
|
|
|
KeyName: keyName,
|
2015-01-04 17:52:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 19:51:21 +01:00
|
|
|
if vL, ok := d.GetOk("block_device"); ok {
|
2015-12-12 21:32:10 +01:00
|
|
|
blockDevices := resourceInstanceBlockDevicesV2(d, vL.([]interface{}))
|
|
|
|
createOpts = &bootfromvolume.CreateOptsExt{
|
2016-05-17 18:23:53 +02:00
|
|
|
CreateOptsBuilder: createOpts,
|
|
|
|
BlockDevice: blockDevices,
|
2015-02-01 10:10:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 22:52:46 +02:00
|
|
|
schedulerHintsRaw := d.Get("scheduler_hints").(*schema.Set).List()
|
|
|
|
if len(schedulerHintsRaw) > 0 {
|
|
|
|
log.Printf("[DEBUG] schedulerhints: %+v", schedulerHintsRaw)
|
|
|
|
schedulerHints := resourceInstanceSchedulerHintsV2(d, schedulerHintsRaw[0].(map[string]interface{}))
|
|
|
|
createOpts = &schedulerhints.CreateOptsExt{
|
2016-02-12 22:57:39 +01:00
|
|
|
CreateOptsBuilder: createOpts,
|
|
|
|
SchedulerHints: schedulerHints,
|
2015-05-05 22:52:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
2015-09-12 22:21:55 +02:00
|
|
|
|
|
|
|
// If a block_device is used, use the bootfromvolume.Create function as it allows an empty ImageRef.
|
|
|
|
// Otherwise, use the normal servers.Create function.
|
|
|
|
var server *servers.Server
|
|
|
|
if _, ok := d.GetOk("block_device"); ok {
|
|
|
|
server, err = bootfromvolume.Create(computeClient, createOpts).Extract()
|
|
|
|
} else {
|
|
|
|
server, err = servers.Create(computeClient, createOpts).Extract()
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:52:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack server: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] Instance ID: %s", server.ID)
|
|
|
|
|
|
|
|
// Store the ID now
|
|
|
|
d.SetId(server.ID)
|
|
|
|
|
|
|
|
// Wait for the instance to become running so we can get some attributes
|
|
|
|
// that aren't available until later.
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for instance (%s) to become running",
|
|
|
|
server.ID)
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"BUILD"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"ACTIVE"},
|
2015-01-26 19:09:27 +01:00
|
|
|
Refresh: ServerV2StateRefreshFunc(computeClient, server.ID),
|
2015-12-08 23:03:33 +01:00
|
|
|
Timeout: 30 * time.Minute,
|
2014-10-29 22:52:36 +01:00
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:45:06 +01:00
|
|
|
_, err = stateConf.WaitForState()
|
2014-10-29 22:52:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for instance (%s) to become ready: %s",
|
|
|
|
server.ID, err)
|
|
|
|
}
|
2016-01-24 21:39:35 +01:00
|
|
|
|
|
|
|
// Now that the instance has been created, we need to do an early read on the
|
|
|
|
// networks in order to associate floating IPs
|
|
|
|
_, err = getInstanceNetworksAndAddresses(computeClient, d)
|
|
|
|
|
|
|
|
// If floating IPs were specified, associate them after the instance has launched.
|
|
|
|
err = associateFloatingIPsToInstance(computeClient, d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-02-06 14:34:11 +01:00
|
|
|
}
|
2014-10-29 22:52:36 +01:00
|
|
|
|
2015-09-12 19:56:38 +02:00
|
|
|
// if volumes were specified, attach them after the instance has launched.
|
|
|
|
if v, ok := d.GetOk("volume"); ok {
|
2015-02-10 17:01:08 +01:00
|
|
|
vols := v.(*schema.Set).List()
|
2015-09-12 19:56:38 +02:00
|
|
|
if blockClient, err := config.blockStorageV1Client(d.Get("region").(string)); err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
|
|
|
|
} else {
|
|
|
|
if err := attachVolumesToInstance(computeClient, blockClient, d.Id(), vols); err != nil {
|
|
|
|
return err
|
2015-02-10 17:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceComputeInstanceV2Read(d, meta)
|
2014-10-29 22:52:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) error {
|
2014-10-29 22:52:36 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
computeClient, err := config.computeV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2014-10-29 22:52:36 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
server, err := servers.Get(computeClient, d.Id()).Extract()
|
2014-10-29 22:52:36 +01:00
|
|
|
if err != nil {
|
2015-02-10 04:27:39 +01:00
|
|
|
return CheckDeleted(d, err, "server")
|
2014-10-29 22:52:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Retreived Server %s: %+v", d.Id(), server)
|
|
|
|
|
|
|
|
d.Set("name", server.Name)
|
2015-03-21 04:12:22 +01:00
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
// Get the instance network and address information
|
|
|
|
networks, err := getInstanceNetworksAndAddresses(computeClient, d)
|
2015-03-21 04:12:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
// Determine the best IPv4 and IPv6 addresses to access the instance with
|
|
|
|
hostv4, hostv6 := getInstanceAccessAddresses(d, networks)
|
2015-03-24 14:24:04 +01:00
|
|
|
|
2016-02-28 22:39:45 +01:00
|
|
|
if server.AccessIPv4 != "" && hostv4 == "" {
|
|
|
|
hostv4 = server.AccessIPv4
|
|
|
|
}
|
|
|
|
|
|
|
|
if server.AccessIPv6 != "" && hostv6 == "" {
|
|
|
|
hostv6 = server.AccessIPv6
|
|
|
|
}
|
|
|
|
|
2015-03-21 04:12:22 +01:00
|
|
|
d.Set("network", networks)
|
|
|
|
d.Set("access_ip_v4", hostv4)
|
2015-02-10 06:47:35 +01:00
|
|
|
d.Set("access_ip_v6", hostv6)
|
2014-10-29 22:52:36 +01:00
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
// Determine the best IP address to use for SSH connectivity.
|
|
|
|
// Prefer IPv4 over IPv6.
|
|
|
|
preferredSSHAddress := ""
|
2015-02-10 06:47:35 +01:00
|
|
|
if hostv4 != "" {
|
2016-01-24 21:39:35 +01:00
|
|
|
preferredSSHAddress = hostv4
|
2015-02-10 06:47:35 +01:00
|
|
|
} else if hostv6 != "" {
|
2016-01-24 21:39:35 +01:00
|
|
|
preferredSSHAddress = hostv6
|
2015-02-10 06:47:35 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
if preferredSSHAddress != "" {
|
2015-02-10 06:47:35 +01:00
|
|
|
// Initialize the connection info
|
|
|
|
d.SetConnInfo(map[string]string{
|
|
|
|
"type": "ssh",
|
2016-01-24 21:39:35 +01:00
|
|
|
"host": preferredSSHAddress,
|
2015-02-10 06:47:35 +01:00
|
|
|
})
|
|
|
|
}
|
2014-10-29 22:52:36 +01:00
|
|
|
|
|
|
|
d.Set("metadata", server.Metadata)
|
2015-01-05 20:16:33 +01:00
|
|
|
|
2015-02-12 05:56:26 +01:00
|
|
|
secGrpNames := []string{}
|
2015-02-12 21:58:12 +01:00
|
|
|
for _, sg := range server.SecurityGroups {
|
|
|
|
secGrpNames = append(secGrpNames, sg["name"].(string))
|
|
|
|
}
|
2015-02-12 05:56:26 +01:00
|
|
|
d.Set("security_groups", secGrpNames)
|
2015-01-05 20:16:33 +01:00
|
|
|
|
2015-02-11 06:24:38 +01:00
|
|
|
flavorId, ok := server.Flavor["id"].(string)
|
2015-01-05 18:05:25 +01:00
|
|
|
if !ok {
|
2015-01-10 22:59:59 +01:00
|
|
|
return fmt.Errorf("Error setting OpenStack server's flavor: %v", server.Flavor)
|
2015-01-05 18:05:25 +01:00
|
|
|
}
|
2015-02-11 06:24:38 +01:00
|
|
|
d.Set("flavor_id", flavorId)
|
|
|
|
|
|
|
|
flavor, err := flavors.Get(computeClient, flavorId).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Set("flavor_name", flavor.Name)
|
2014-10-29 22:52:36 +01:00
|
|
|
|
2015-09-12 22:21:55 +02:00
|
|
|
// Set the instance's image information appropriately
|
|
|
|
if err := setImageInformation(computeClient, server, d); err != nil {
|
2015-02-11 06:33:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:01:08 +01:00
|
|
|
// volume attachments
|
2015-09-12 19:56:38 +02:00
|
|
|
if err := getVolumeAttachments(computeClient, d); err != nil {
|
2015-02-10 17:01:08 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:52:36 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeInstanceV2Update(d *schema.ResourceData, meta interface{}) error {
|
2014-10-29 22:52:36 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
computeClient, err := config.computeV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2014-10-29 22:52:36 +01:00
|
|
|
|
|
|
|
var updateOpts servers.UpdateOpts
|
|
|
|
if d.HasChange("name") {
|
|
|
|
updateOpts.Name = d.Get("name").(string)
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:45:05 +01:00
|
|
|
if updateOpts != (servers.UpdateOpts{}) {
|
|
|
|
_, err := servers.Update(computeClient, d.Id(), updateOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack server: %s", err)
|
|
|
|
}
|
2015-01-02 20:40:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("metadata") {
|
|
|
|
var metadataOpts servers.MetadataOpts
|
|
|
|
metadataOpts = make(servers.MetadataOpts)
|
|
|
|
newMetadata := d.Get("metadata").(map[string]interface{})
|
|
|
|
for k, v := range newMetadata {
|
|
|
|
metadataOpts[k] = v.(string)
|
|
|
|
}
|
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err := servers.UpdateMetadata(computeClient, d.Id(), metadataOpts).Extract()
|
2015-01-02 20:40:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack server (%s) metadata: %s", d.Id(), err)
|
2014-10-29 22:52:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 20:16:33 +01:00
|
|
|
if d.HasChange("security_groups") {
|
|
|
|
oldSGRaw, newSGRaw := d.GetChange("security_groups")
|
2015-10-28 02:11:32 +01:00
|
|
|
oldSGSet := oldSGRaw.(*schema.Set)
|
|
|
|
newSGSet := newSGRaw.(*schema.Set)
|
2015-01-05 20:16:33 +01:00
|
|
|
secgroupsToAdd := newSGSet.Difference(oldSGSet)
|
|
|
|
secgroupsToRemove := oldSGSet.Difference(newSGSet)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Security groups to add: %v", secgroupsToAdd)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Security groups to remove: %v", secgroupsToRemove)
|
|
|
|
|
|
|
|
for _, g := range secgroupsToRemove.List() {
|
2015-01-26 18:54:07 +01:00
|
|
|
err := secgroups.RemoveServerFromGroup(computeClient, d.Id(), g.(string)).ExtractErr()
|
2016-07-02 16:18:04 +02:00
|
|
|
if err != nil && err.Error() != "EOF" {
|
2015-02-17 18:48:23 +01:00
|
|
|
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
2015-02-13 01:25:45 +01:00
|
|
|
if !ok {
|
2016-07-02 16:18:04 +02:00
|
|
|
return fmt.Errorf("Error removing security group (%s) from OpenStack server (%s): %s", g, d.Id(), err)
|
2015-02-13 01:25:45 +01:00
|
|
|
}
|
|
|
|
if errCode.Actual == 404 {
|
|
|
|
continue
|
|
|
|
} else {
|
2016-07-02 16:18:04 +02:00
|
|
|
return fmt.Errorf("Error removing security group (%s) from OpenStack server (%s): %s", g, d.Id(), err)
|
2015-02-13 01:25:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-07-02 16:18:04 +02:00
|
|
|
log.Printf("[DEBUG] Removed security group (%s) from instance (%s)", g, d.Id())
|
2015-01-05 20:16:33 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-02 16:18:04 +02:00
|
|
|
|
2015-05-18 21:45:50 +02:00
|
|
|
for _, g := range secgroupsToAdd.List() {
|
|
|
|
err := secgroups.AddServerToGroup(computeClient, d.Id(), g.(string)).ExtractErr()
|
2016-07-02 16:18:04 +02:00
|
|
|
if err != nil && err.Error() != "EOF" {
|
|
|
|
return fmt.Errorf("Error adding security group (%s) to OpenStack server (%s): %s", g, d.Id(), err)
|
2015-05-18 21:45:50 +02:00
|
|
|
}
|
2016-07-02 16:18:04 +02:00
|
|
|
log.Printf("[DEBUG] Added security group (%s) to instance (%s)", g, d.Id())
|
2015-05-18 21:45:50 +02:00
|
|
|
}
|
2015-01-05 20:16:33 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 19:38:23 +01:00
|
|
|
if d.HasChange("admin_pass") {
|
|
|
|
if newPwd, ok := d.Get("admin_pass").(string); ok {
|
2015-01-26 18:54:07 +01:00
|
|
|
err := servers.ChangeAdminPassword(computeClient, d.Id(), newPwd).ExtractErr()
|
2015-01-07 19:38:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error changing admin password of OpenStack server (%s): %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 14:34:11 +01:00
|
|
|
if d.HasChange("floating_ip") {
|
2015-04-01 17:31:21 +02:00
|
|
|
oldFIP, newFIP := d.GetChange("floating_ip")
|
|
|
|
log.Printf("[DEBUG] Old Floating IP: %v", oldFIP)
|
|
|
|
log.Printf("[DEBUG] New Floating IP: %v", newFIP)
|
|
|
|
if oldFIP.(string) != "" {
|
2016-01-24 21:39:35 +01:00
|
|
|
log.Printf("[DEBUG] Attempting to disassociate %s from %s", oldFIP, d.Id())
|
|
|
|
if err := disassociateFloatingIPFromInstance(computeClient, oldFIP.(string), d.Id(), ""); err != nil {
|
2015-04-01 17:31:21 +02:00
|
|
|
return fmt.Errorf("Error disassociating Floating IP during update: %s", err)
|
2015-02-06 14:34:11 +01:00
|
|
|
}
|
2015-04-01 17:31:21 +02:00
|
|
|
}
|
2015-02-06 14:34:11 +01:00
|
|
|
|
2015-04-01 17:31:21 +02:00
|
|
|
if newFIP.(string) != "" {
|
2016-01-24 21:39:35 +01:00
|
|
|
log.Printf("[DEBUG] Attempting to associate %s to %s", newFIP, d.Id())
|
|
|
|
if err := associateFloatingIPToInstance(computeClient, newFIP.(string), d.Id(), ""); err != nil {
|
2015-04-01 17:31:21 +02:00
|
|
|
return fmt.Errorf("Error associating Floating IP during update: %s", err)
|
2015-02-06 14:34:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
if d.HasChange("network") {
|
|
|
|
oldNetworks, newNetworks := d.GetChange("network")
|
|
|
|
oldNetworkList := oldNetworks.([]interface{})
|
|
|
|
newNetworkList := newNetworks.([]interface{})
|
|
|
|
for i, oldNet := range oldNetworkList {
|
2016-02-28 20:02:53 +01:00
|
|
|
var oldFIP, newFIP string
|
|
|
|
var oldFixedIP, newFixedIP string
|
2016-01-24 21:39:35 +01:00
|
|
|
|
2016-02-28 20:02:53 +01:00
|
|
|
if oldNetRaw, ok := oldNet.(map[string]interface{}); ok {
|
|
|
|
oldFIP = oldNetRaw["floating_ip"].(string)
|
|
|
|
oldFixedIP = oldNetRaw["fixed_ip_v4"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newNetworkList) > i {
|
|
|
|
if newNetRaw, ok := newNetworkList[i].(map[string]interface{}); ok {
|
|
|
|
newFIP = newNetRaw["floating_ip"].(string)
|
|
|
|
newFixedIP = newNetRaw["fixed_ip_v4"].(string)
|
|
|
|
}
|
|
|
|
}
|
2016-01-24 21:39:35 +01:00
|
|
|
|
|
|
|
// Only changes to the floating IP are supported
|
2016-05-10 11:12:19 +02:00
|
|
|
if oldFIP != "" && oldFIP != newFIP {
|
2016-01-24 21:39:35 +01:00
|
|
|
log.Printf("[DEBUG] Attempting to disassociate %s from %s", oldFIP, d.Id())
|
|
|
|
if err := disassociateFloatingIPFromInstance(computeClient, oldFIP, d.Id(), oldFixedIP); err != nil {
|
|
|
|
return fmt.Errorf("Error disassociating Floating IP during update: %s", err)
|
|
|
|
}
|
2016-05-10 11:12:19 +02:00
|
|
|
}
|
2016-01-24 21:39:35 +01:00
|
|
|
|
2016-05-10 11:12:19 +02:00
|
|
|
if newFIP != "" && oldFIP != newFIP {
|
2016-01-24 21:39:35 +01:00
|
|
|
log.Printf("[DEBUG] Attempting to associate %s to %s", newFIP, d.Id())
|
|
|
|
if err := associateFloatingIPToInstance(computeClient, newFIP, d.Id(), newFixedIP); err != nil {
|
|
|
|
return fmt.Errorf("Error associating Floating IP during update: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:01:08 +01:00
|
|
|
if d.HasChange("volume") {
|
|
|
|
// old attachments and new attachments
|
|
|
|
oldAttachments, newAttachments := d.GetChange("volume")
|
|
|
|
// for each old attachment, detach the volume
|
|
|
|
oldAttachmentSet := oldAttachments.(*schema.Set).List()
|
2016-05-19 11:28:55 +02:00
|
|
|
|
2016-08-13 21:59:25 +02:00
|
|
|
log.Printf("[DEBUG] Attempting to detach the following volumes: %#v", oldAttachmentSet)
|
2015-09-12 19:56:38 +02:00
|
|
|
if blockClient, err := config.blockStorageV1Client(d.Get("region").(string)); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if err := detachVolumesFromInstance(computeClient, blockClient, d.Id(), oldAttachmentSet); err != nil {
|
2015-02-10 17:01:08 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for each new attachment, attach the volume
|
|
|
|
newAttachmentSet := newAttachments.(*schema.Set).List()
|
2015-09-12 19:56:38 +02:00
|
|
|
if blockClient, err := config.blockStorageV1Client(d.Get("region").(string)); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if err := attachVolumesToInstance(computeClient, blockClient, d.Id(), newAttachmentSet); err != nil {
|
2015-02-10 17:01:08 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("volume")
|
|
|
|
}
|
|
|
|
|
2015-02-12 06:05:38 +01:00
|
|
|
if d.HasChange("flavor_id") || d.HasChange("flavor_name") {
|
2016-04-05 17:09:41 +02:00
|
|
|
var newFlavorId string
|
|
|
|
var err error
|
|
|
|
if d.HasChange("flavor_id") {
|
|
|
|
newFlavorId = d.Get("flavor_id").(string)
|
|
|
|
} else {
|
|
|
|
newFlavorName := d.Get("flavor_name").(string)
|
|
|
|
newFlavorId, err = flavors.IDFromName(computeClient, newFlavorName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-12 06:05:38 +01:00
|
|
|
}
|
2016-04-05 17:09:41 +02:00
|
|
|
|
2015-01-05 18:05:25 +01:00
|
|
|
resizeOpts := &servers.ResizeOpts{
|
2016-04-05 17:09:41 +02:00
|
|
|
FlavorRef: newFlavorId,
|
2015-01-05 18:05:25 +01:00
|
|
|
}
|
2015-03-24 13:59:55 +01:00
|
|
|
log.Printf("[DEBUG] Resize configuration: %#v", resizeOpts)
|
2015-02-12 06:05:38 +01:00
|
|
|
err = servers.Resize(computeClient, d.Id(), resizeOpts).ExtractErr()
|
2015-01-05 18:05:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error resizing OpenStack server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the instance to finish resizing.
|
|
|
|
log.Printf("[DEBUG] Waiting for instance (%s) to finish resizing", d.Id())
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"RESIZE"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"VERIFY_RESIZE"},
|
2015-01-26 19:09:27 +01:00
|
|
|
Refresh: ServerV2StateRefreshFunc(computeClient, d.Id()),
|
2016-05-19 11:28:55 +02:00
|
|
|
Timeout: 30 * time.Minute,
|
2015-01-05 18:05:25 +01:00
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for instance (%s) to resize: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm resize.
|
|
|
|
log.Printf("[DEBUG] Confirming resize")
|
2015-01-26 18:54:07 +01:00
|
|
|
err = servers.ConfirmResize(computeClient, d.Id()).ExtractErr()
|
2015-01-05 18:05:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error confirming resize of OpenStack server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stateConf = &resource.StateChangeConf{
|
|
|
|
Pending: []string{"VERIFY_RESIZE"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"ACTIVE"},
|
2015-01-26 19:09:27 +01:00
|
|
|
Refresh: ServerV2StateRefreshFunc(computeClient, d.Id()),
|
2016-05-19 11:28:55 +02:00
|
|
|
Timeout: 30 * time.Minute,
|
2015-01-05 18:05:25 +01:00
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for instance (%s) to confirm resize: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceComputeInstanceV2Read(d, meta)
|
2014-10-29 22:52:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeInstanceV2Delete(d *schema.ResourceData, meta interface{}) error {
|
2014-10-29 22:52:36 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
computeClient, err := config.computeV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2014-10-29 22:52:36 +01:00
|
|
|
|
2016-08-13 21:59:25 +02:00
|
|
|
// Make sure all volumes are detached before deleting
|
|
|
|
volumes := d.Get("volume")
|
|
|
|
if volumeSet, ok := volumes.(*schema.Set); ok {
|
|
|
|
volumeList := volumeSet.List()
|
|
|
|
if len(volumeList) > 0 {
|
|
|
|
log.Printf("[DEBUG] Attempting to detach the following volumes: %#v", volumeList)
|
|
|
|
if blockClient, err := config.blockStorageV1Client(d.Get("region").(string)); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if err := detachVolumesFromInstance(computeClient, blockClient, d.Id(), volumeList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 21:26:18 +02:00
|
|
|
if d.Get("stop_before_destroy").(bool) {
|
|
|
|
err = startstop.Stop(computeClient, d.Id()).ExtractErr()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] Error stopping OpenStack instance: %s", err)
|
|
|
|
} else {
|
|
|
|
stopStateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"ACTIVE"},
|
|
|
|
Target: []string{"SHUTOFF"},
|
|
|
|
Refresh: ServerV2StateRefreshFunc(computeClient, d.Id()),
|
|
|
|
Timeout: 3 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Waiting for instance (%s) to stop", d.Id())
|
|
|
|
_, err = stopStateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] Error waiting for instance (%s) to stop: %s, proceeding to delete", d.Id(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
err = servers.Delete(computeClient, d.Id()).ExtractErr()
|
2014-10-29 22:52:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the instance to delete before moving on.
|
2015-01-05 18:05:25 +01:00
|
|
|
log.Printf("[DEBUG] Waiting for instance (%s) to delete", d.Id())
|
2014-10-29 22:52:36 +01:00
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
2016-06-15 21:26:18 +02:00
|
|
|
Pending: []string{"ACTIVE", "SHUTOFF"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"DELETED"},
|
2015-01-26 19:09:27 +01:00
|
|
|
Refresh: ServerV2StateRefreshFunc(computeClient, d.Id()),
|
2015-12-08 23:03:33 +01:00
|
|
|
Timeout: 30 * time.Minute,
|
2014-10-29 22:52:36 +01:00
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for instance (%s) to delete: %s",
|
|
|
|
d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-01 10:10:42 +01:00
|
|
|
// ServerV2StateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
2014-10-29 22:52:36 +01:00
|
|
|
// an OpenStack instance.
|
2015-01-26 19:09:27 +01:00
|
|
|
func ServerV2StateRefreshFunc(client *gophercloud.ServiceClient, instanceID string) resource.StateRefreshFunc {
|
2014-10-29 22:52:36 +01:00
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
s, err := servers.Get(client, instanceID).Extract()
|
|
|
|
if err != nil {
|
2015-02-17 18:48:23 +01:00
|
|
|
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
2015-02-04 04:30:10 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
if errCode.Actual == 404 {
|
|
|
|
return s, "DELETED", nil
|
|
|
|
}
|
2014-10-29 22:52:36 +01:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, s.Status, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceInstanceSecGroupsV2(d *schema.ResourceData) []string {
|
2015-10-27 07:50:39 +01:00
|
|
|
rawSecGroups := d.Get("security_groups").(*schema.Set).List()
|
2015-02-12 06:29:31 +01:00
|
|
|
secgroups := make([]string, len(rawSecGroups))
|
|
|
|
for i, raw := range rawSecGroups {
|
2015-01-05 20:16:33 +01:00
|
|
|
secgroups[i] = raw.(string)
|
|
|
|
}
|
|
|
|
return secgroups
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
// getInstanceNetworks collects instance network information from different sources
|
|
|
|
// and aggregates it all together.
|
|
|
|
func getInstanceNetworksAndAddresses(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) ([]map[string]interface{}, error) {
|
|
|
|
server, err := servers.Get(computeClient, d.Id()).Extract()
|
2016-05-10 11:12:19 +02:00
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, CheckDeleted(d, err, "server")
|
|
|
|
}
|
|
|
|
|
|
|
|
networkDetails, err := getInstanceNetworks(computeClient, d)
|
|
|
|
addresses := getInstanceAddresses(server.Addresses)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there are no networkDetails, make networks at least a length of 1
|
|
|
|
networkLength := 1
|
|
|
|
if len(networkDetails) > 0 {
|
|
|
|
networkLength = len(networkDetails)
|
|
|
|
}
|
|
|
|
networks := make([]map[string]interface{}, networkLength)
|
|
|
|
|
|
|
|
// Loop through all networks and addresses,
|
|
|
|
// merge relevant address details.
|
|
|
|
if len(networkDetails) == 0 {
|
|
|
|
for netName, n := range addresses {
|
|
|
|
networks[0] = map[string]interface{}{
|
|
|
|
"name": netName,
|
|
|
|
"fixed_ip_v4": n["fixed_ip_v4"],
|
|
|
|
"fixed_ip_v6": n["fixed_ip_v6"],
|
|
|
|
"floating_ip": n["floating_ip"],
|
|
|
|
"mac": n["mac"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i, net := range networkDetails {
|
|
|
|
n := addresses[net["name"].(string)]
|
|
|
|
|
|
|
|
networks[i] = map[string]interface{}{
|
|
|
|
"uuid": networkDetails[i]["uuid"],
|
|
|
|
"name": networkDetails[i]["name"],
|
|
|
|
"port": networkDetails[i]["port"],
|
|
|
|
"fixed_ip_v4": n["fixed_ip_v4"],
|
|
|
|
"fixed_ip_v6": n["fixed_ip_v6"],
|
|
|
|
"floating_ip": n["floating_ip"],
|
|
|
|
"mac": n["mac"],
|
|
|
|
"access_network": networkDetails[i]["access_network"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] networks: %+v", networks)
|
|
|
|
|
|
|
|
return networks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInstanceNetworks(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) ([]map[string]interface{}, error) {
|
2015-02-01 01:21:57 +01:00
|
|
|
rawNetworks := d.Get("network").([]interface{})
|
2015-06-24 01:19:42 +02:00
|
|
|
newNetworks := make([]map[string]interface{}, 0, len(rawNetworks))
|
2015-03-21 03:35:31 +01:00
|
|
|
var tenantnet tenantnetworks.Network
|
|
|
|
|
2015-04-01 23:31:55 +02:00
|
|
|
tenantNetworkExt := true
|
2015-06-24 01:19:42 +02:00
|
|
|
for _, raw := range rawNetworks {
|
|
|
|
// Not sure what causes this, but it is a possibility (see GH-2323).
|
|
|
|
// Since we call this function to reconcile what we'll save in the
|
|
|
|
// state anyways, we just ignore it.
|
|
|
|
if raw == nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-21 03:35:31 +01:00
|
|
|
|
2015-06-24 01:19:42 +02:00
|
|
|
rawMap := raw.(map[string]interface{})
|
2016-01-24 21:39:35 +01:00
|
|
|
|
2015-03-21 03:35:31 +01:00
|
|
|
allPages, err := tenantnetworks.List(computeClient).AllPages()
|
|
|
|
if err != nil {
|
2015-04-01 23:31:55 +02:00
|
|
|
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
|
|
|
if !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-21 03:35:31 +01:00
|
|
|
|
2016-03-02 22:17:59 +01:00
|
|
|
if errCode.Actual == 404 || errCode.Actual == 403 {
|
2015-04-01 23:31:55 +02:00
|
|
|
tenantNetworkExt = false
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-21 03:35:31 +01:00
|
|
|
}
|
|
|
|
|
2015-04-01 23:31:55 +02:00
|
|
|
networkID := ""
|
|
|
|
networkName := ""
|
|
|
|
if tenantNetworkExt {
|
|
|
|
networkList, err := tenantnetworks.ExtractNetworks(allPages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-03-21 03:35:31 +01:00
|
|
|
}
|
2015-04-01 23:31:55 +02:00
|
|
|
|
|
|
|
for _, network := range networkList {
|
|
|
|
if network.Name == rawMap["name"] {
|
|
|
|
tenantnet = network
|
|
|
|
}
|
|
|
|
if network.ID == rawMap["uuid"] {
|
|
|
|
tenantnet = network
|
|
|
|
}
|
2015-03-21 03:35:31 +01:00
|
|
|
}
|
2015-04-01 23:31:55 +02:00
|
|
|
|
|
|
|
networkID = tenantnet.ID
|
|
|
|
networkName = tenantnet.Name
|
|
|
|
} else {
|
|
|
|
networkID = rawMap["uuid"].(string)
|
|
|
|
networkName = rawMap["name"].(string)
|
2015-03-21 03:35:31 +01:00
|
|
|
}
|
|
|
|
|
2015-06-24 01:19:42 +02:00
|
|
|
newNetworks = append(newNetworks, map[string]interface{}{
|
2016-01-24 21:39:35 +01:00
|
|
|
"uuid": networkID,
|
|
|
|
"name": networkName,
|
|
|
|
"port": rawMap["port"].(string),
|
|
|
|
"fixed_ip_v4": rawMap["fixed_ip_v4"].(string),
|
|
|
|
"access_network": rawMap["access_network"].(bool),
|
2015-06-24 01:19:42 +02:00
|
|
|
})
|
2014-10-29 22:52:36 +01:00
|
|
|
}
|
2015-03-21 03:35:31 +01:00
|
|
|
|
2015-03-21 04:12:22 +01:00
|
|
|
log.Printf("[DEBUG] networks: %+v", newNetworks)
|
2015-03-21 03:35:31 +01:00
|
|
|
return newNetworks, nil
|
2014-10-29 22:52:36 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
func getInstanceAddresses(addresses map[string]interface{}) map[string]map[string]interface{} {
|
2015-03-21 04:12:22 +01:00
|
|
|
addrs := make(map[string]map[string]interface{})
|
|
|
|
for n, networkAddresses := range addresses {
|
|
|
|
addrs[n] = make(map[string]interface{})
|
|
|
|
for _, element := range networkAddresses.([]interface{}) {
|
|
|
|
address := element.(map[string]interface{})
|
|
|
|
if address["OS-EXT-IPS:type"] == "floating" {
|
|
|
|
addrs[n]["floating_ip"] = address["addr"]
|
|
|
|
} else {
|
|
|
|
if address["version"].(float64) == 4 {
|
|
|
|
addrs[n]["fixed_ip_v4"] = address["addr"].(string)
|
|
|
|
} else {
|
|
|
|
addrs[n]["fixed_ip_v6"] = fmt.Sprintf("[%s]", address["addr"].(string))
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 17:47:19 +01:00
|
|
|
if mac, ok := address["OS-EXT-IPS-MAC:mac_addr"]; ok {
|
|
|
|
addrs[n]["mac"] = mac.(string)
|
|
|
|
}
|
2015-03-21 04:12:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Addresses: %+v", addresses)
|
|
|
|
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:39:35 +01:00
|
|
|
func getInstanceAccessAddresses(d *schema.ResourceData, networks []map[string]interface{}) (string, string) {
|
|
|
|
var hostv4, hostv6 string
|
|
|
|
|
|
|
|
// Start with a global floating IP
|
|
|
|
floatingIP := d.Get("floating_ip").(string)
|
|
|
|
if floatingIP != "" {
|
|
|
|
hostv4 = floatingIP
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:17:51 +02:00
|
|
|
// Loop through all networks
|
|
|
|
// If the network has a valid floating, fixed v4, or fixed v6 address
|
|
|
|
// and hostv4 or hostv6 is not set, set hostv4/hostv6.
|
|
|
|
// If the network is an "access_network" overwrite hostv4/hostv6.
|
2016-01-24 21:39:35 +01:00
|
|
|
for _, n := range networks {
|
2016-04-06 21:17:51 +02:00
|
|
|
var accessNetwork bool
|
|
|
|
|
|
|
|
if an, ok := n["access_network"].(bool); ok && an {
|
|
|
|
accessNetwork = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if fixedIPv4, ok := n["fixed_ip_v4"].(string); ok && fixedIPv4 != "" {
|
|
|
|
if hostv4 == "" || accessNetwork {
|
|
|
|
hostv4 = fixedIPv4
|
2016-01-24 21:39:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:17:51 +02:00
|
|
|
if floatingIP, ok := n["floating_ip"].(string); ok && floatingIP != "" {
|
|
|
|
if hostv4 == "" || accessNetwork {
|
|
|
|
hostv4 = floatingIP
|
|
|
|
}
|
2016-01-24 21:39:35 +01:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:17:51 +02:00
|
|
|
if fixedIPv6, ok := n["fixed_ip_v6"].(string); ok && fixedIPv6 != "" {
|
|
|
|
if hostv6 == "" || accessNetwork {
|
|
|
|
hostv6 = fixedIPv6
|
|
|
|
}
|
2016-01-24 21:39:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack Instance Network Access Addresses: %s, %s", hostv4, hostv6)
|
|
|
|
|
|
|
|
return hostv4, hostv6
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkInstanceFloatingIPs(d *schema.ResourceData) error {
|
|
|
|
rawNetworks := d.Get("network").([]interface{})
|
|
|
|
floatingIP := d.Get("floating_ip").(string)
|
|
|
|
|
|
|
|
for _, raw := range rawNetworks {
|
|
|
|
if raw == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMap := raw.(map[string]interface{})
|
|
|
|
|
|
|
|
// Error if a floating IP was specified both globally and in the network block.
|
|
|
|
if floatingIP != "" && rawMap["floating_ip"] != "" {
|
|
|
|
return fmt.Errorf("Cannot specify a floating IP both globally and in a network block.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func associateFloatingIPsToInstance(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) error {
|
|
|
|
floatingIP := d.Get("floating_ip").(string)
|
|
|
|
rawNetworks := d.Get("network").([]interface{})
|
|
|
|
instanceID := d.Id()
|
|
|
|
|
|
|
|
if floatingIP != "" {
|
|
|
|
if err := associateFloatingIPToInstance(computeClient, floatingIP, instanceID, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, raw := range rawNetworks {
|
|
|
|
if raw == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMap := raw.(map[string]interface{})
|
|
|
|
if rawMap["floating_ip"].(string) != "" {
|
|
|
|
floatingIP := rawMap["floating_ip"].(string)
|
|
|
|
fixedIP := rawMap["fixed_ip_v4"].(string)
|
|
|
|
if err := associateFloatingIPToInstance(computeClient, floatingIP, instanceID, fixedIP); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func associateFloatingIPToInstance(computeClient *gophercloud.ServiceClient, floatingIP string, instanceID string, fixedIP string) error {
|
|
|
|
associateOpts := floatingip.AssociateOpts{
|
|
|
|
ServerID: instanceID,
|
|
|
|
FloatingIP: floatingIP,
|
|
|
|
FixedIP: fixedIP,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := floatingip.AssociateInstance(computeClient, associateOpts).ExtractErr(); err != nil {
|
|
|
|
return fmt.Errorf("Error associating floating IP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func disassociateFloatingIPFromInstance(computeClient *gophercloud.ServiceClient, floatingIP string, instanceID string, fixedIP string) error {
|
|
|
|
associateOpts := floatingip.AssociateOpts{
|
|
|
|
ServerID: instanceID,
|
|
|
|
FloatingIP: floatingIP,
|
|
|
|
FixedIP: fixedIP,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := floatingip.DisassociateInstance(computeClient, associateOpts).ExtractErr(); err != nil {
|
|
|
|
return fmt.Errorf("Error disassociating floating IP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceInstanceMetadataV2(d *schema.ResourceData) map[string]string {
|
2014-10-29 22:52:36 +01:00
|
|
|
m := make(map[string]string)
|
|
|
|
for key, val := range d.Get("metadata").(map[string]interface{}) {
|
|
|
|
m[key] = val.(string)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2015-02-01 10:10:42 +01:00
|
|
|
|
2015-12-12 21:32:10 +01:00
|
|
|
func resourceInstanceBlockDevicesV2(d *schema.ResourceData, bds []interface{}) []bootfromvolume.BlockDevice {
|
|
|
|
blockDeviceOpts := make([]bootfromvolume.BlockDevice, len(bds))
|
|
|
|
for i, bd := range bds {
|
|
|
|
bdM := bd.(map[string]interface{})
|
|
|
|
sourceType := bootfromvolume.SourceType(bdM["source_type"].(string))
|
|
|
|
blockDeviceOpts[i] = bootfromvolume.BlockDevice{
|
|
|
|
UUID: bdM["uuid"].(string),
|
2015-09-12 06:15:21 +02:00
|
|
|
SourceType: sourceType,
|
2015-12-12 21:32:10 +01:00
|
|
|
VolumeSize: bdM["volume_size"].(int),
|
|
|
|
DestinationType: bdM["destination_type"].(string),
|
|
|
|
BootIndex: bdM["boot_index"].(int),
|
|
|
|
DeleteOnTermination: bdM["delete_on_termination"].(bool),
|
|
|
|
GuestFormat: bdM["guest_format"].(string),
|
|
|
|
}
|
2015-02-01 10:10:42 +01:00
|
|
|
}
|
|
|
|
|
2015-12-12 21:32:10 +01:00
|
|
|
log.Printf("[DEBUG] Block Device Options: %+v", blockDeviceOpts)
|
|
|
|
return blockDeviceOpts
|
2015-02-01 10:10:42 +01:00
|
|
|
}
|
2015-02-06 14:34:11 +01:00
|
|
|
|
2015-05-05 22:52:46 +02:00
|
|
|
func resourceInstanceSchedulerHintsV2(d *schema.ResourceData, schedulerHintsRaw map[string]interface{}) schedulerhints.SchedulerHints {
|
|
|
|
differentHost := []string{}
|
|
|
|
if len(schedulerHintsRaw["different_host"].([]interface{})) > 0 {
|
|
|
|
for _, dh := range schedulerHintsRaw["different_host"].([]interface{}) {
|
|
|
|
differentHost = append(differentHost, dh.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sameHost := []string{}
|
|
|
|
if len(schedulerHintsRaw["same_host"].([]interface{})) > 0 {
|
|
|
|
for _, sh := range schedulerHintsRaw["same_host"].([]interface{}) {
|
|
|
|
sameHost = append(sameHost, sh.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query := make([]interface{}, len(schedulerHintsRaw["query"].([]interface{})))
|
|
|
|
if len(schedulerHintsRaw["query"].([]interface{})) > 0 {
|
|
|
|
for _, q := range schedulerHintsRaw["query"].([]interface{}) {
|
|
|
|
query = append(query, q.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
schedulerHints := schedulerhints.SchedulerHints{
|
|
|
|
Group: schedulerHintsRaw["group"].(string),
|
|
|
|
DifferentHost: differentHost,
|
|
|
|
SameHost: sameHost,
|
|
|
|
Query: query,
|
|
|
|
TargetCell: schedulerHintsRaw["target_cell"].(string),
|
|
|
|
BuildNearHostIP: schedulerHintsRaw["build_near_host_ip"].(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
return schedulerHints
|
|
|
|
}
|
|
|
|
|
2015-09-12 22:21:55 +02:00
|
|
|
func getImageIDFromConfig(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) {
|
2015-12-12 21:32:10 +01:00
|
|
|
// If block_device was used, an Image does not need to be specified, unless an image/local
|
|
|
|
// combination was used. This emulates normal boot behavior. Otherwise, ignore the image altogether.
|
|
|
|
if vL, ok := d.GetOk("block_device"); ok {
|
|
|
|
needImage := false
|
|
|
|
for _, v := range vL.([]interface{}) {
|
|
|
|
vM := v.(map[string]interface{})
|
|
|
|
if vM["source_type"] == "image" && vM["destination_type"] == "local" {
|
|
|
|
needImage = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !needImage {
|
|
|
|
return "", nil
|
|
|
|
}
|
2015-09-12 22:21:55 +02:00
|
|
|
}
|
2015-02-10 06:20:23 +01:00
|
|
|
|
2015-09-12 22:21:55 +02:00
|
|
|
if imageId := d.Get("image_id").(string); imageId != "" {
|
2015-02-11 05:11:04 +01:00
|
|
|
return imageId, nil
|
2015-09-12 22:21:55 +02:00
|
|
|
} else {
|
|
|
|
// try the OS_IMAGE_ID environment variable
|
|
|
|
if v := os.Getenv("OS_IMAGE_ID"); v != "" {
|
|
|
|
return v, nil
|
|
|
|
}
|
2015-02-11 05:11:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
imageName := d.Get("image_name").(string)
|
2015-09-12 22:21:55 +02:00
|
|
|
if imageName == "" {
|
|
|
|
// try the OS_IMAGE_NAME environment variable
|
|
|
|
if v := os.Getenv("OS_IMAGE_NAME"); v != "" {
|
|
|
|
imageName = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 05:11:04 +01:00
|
|
|
if imageName != "" {
|
2015-09-12 22:21:55 +02:00
|
|
|
imageId, err := images.IDFromName(computeClient, imageName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return imageId, nil
|
|
|
|
}
|
2015-02-10 06:20:23 +01:00
|
|
|
|
2015-09-12 22:21:55 +02:00
|
|
|
return "", fmt.Errorf("Neither a boot device, image ID, or image name were able to be determined.")
|
|
|
|
}
|
2015-02-10 06:20:23 +01:00
|
|
|
|
2015-09-12 22:21:55 +02:00
|
|
|
func setImageInformation(computeClient *gophercloud.ServiceClient, server *servers.Server, d *schema.ResourceData) error {
|
2015-12-12 21:32:10 +01:00
|
|
|
// If block_device was used, an Image does not need to be specified, unless an image/local
|
|
|
|
// combination was used. This emulates normal boot behavior. Otherwise, ignore the image altogether.
|
|
|
|
if vL, ok := d.GetOk("block_device"); ok {
|
|
|
|
needImage := false
|
|
|
|
for _, v := range vL.([]interface{}) {
|
|
|
|
vM := v.(map[string]interface{})
|
|
|
|
if vM["source_type"] == "image" && vM["destination_type"] == "local" {
|
|
|
|
needImage = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !needImage {
|
|
|
|
d.Set("image_id", "Attempt to boot from volume - no image supplied")
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-12 22:21:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
imageId := server.Image["id"].(string)
|
|
|
|
if imageId != "" {
|
|
|
|
d.Set("image_id", imageId)
|
|
|
|
if image, err := images.Get(computeClient, imageId).Extract(); err != nil {
|
|
|
|
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if errCode.Actual == 404 {
|
|
|
|
// If the image name can't be found, set the value to "Image not found".
|
|
|
|
// The most likely scenario is that the image no longer exists in the Image Service
|
|
|
|
// but the instance still has a record from when it existed.
|
|
|
|
d.Set("image_name", "Image not found")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.Set("image_name", image.Name)
|
2015-02-10 06:20:23 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-12 22:21:55 +02:00
|
|
|
|
|
|
|
return nil
|
2015-02-10 06:20:23 +01:00
|
|
|
}
|
2015-02-11 06:24:38 +01:00
|
|
|
|
|
|
|
func getFlavorID(client *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) {
|
|
|
|
flavorId := d.Get("flavor_id").(string)
|
|
|
|
|
|
|
|
if flavorId != "" {
|
|
|
|
return flavorId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
flavorName := d.Get("flavor_name").(string)
|
2016-04-05 17:09:41 +02:00
|
|
|
return flavors.IDFromName(client, flavorName)
|
2015-02-11 06:24:38 +01:00
|
|
|
}
|
2015-02-10 17:01:08 +01:00
|
|
|
|
|
|
|
func resourceComputeVolumeAttachmentHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["volume_id"].(string)))
|
2015-09-12 06:15:21 +02:00
|
|
|
|
2015-02-10 17:01:08 +01:00
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
2015-05-05 22:52:46 +02:00
|
|
|
func resourceComputeSchedulerHintsHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
if m["group"] != nil {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["group"].(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if m["target_cell"] != nil {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["target_cell"].(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if m["build_host_near_ip"] != nil {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["build_host_near_ip"].(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["different_host"].([]interface{})))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["same_host"].([]interface{})))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["query"].([]interface{})))
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:01:08 +01:00
|
|
|
func attachVolumesToInstance(computeClient *gophercloud.ServiceClient, blockClient *gophercloud.ServiceClient, serverId string, vols []interface{}) error {
|
2015-09-12 19:56:38 +02:00
|
|
|
for _, v := range vols {
|
|
|
|
va := v.(map[string]interface{})
|
|
|
|
volumeId := va["volume_id"].(string)
|
|
|
|
device := va["device"].(string)
|
|
|
|
|
|
|
|
s := ""
|
|
|
|
if serverId != "" {
|
|
|
|
s = serverId
|
|
|
|
} else if va["server_id"] != "" {
|
|
|
|
s = va["server_id"].(string)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Unable to determine server ID to attach volume.")
|
|
|
|
}
|
2015-02-10 17:01:08 +01:00
|
|
|
|
2015-09-12 19:56:38 +02:00
|
|
|
vaOpts := &volumeattach.CreateOpts{
|
|
|
|
Device: device,
|
|
|
|
VolumeID: volumeId,
|
|
|
|
}
|
2015-02-10 17:01:08 +01:00
|
|
|
|
2015-09-12 19:56:38 +02:00
|
|
|
if _, err := volumeattach.Create(computeClient, s, vaOpts).Extract(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-10 17:01:08 +01:00
|
|
|
|
2015-09-12 19:56:38 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"attaching", "available"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"in-use"},
|
2015-09-12 19:56:38 +02:00
|
|
|
Refresh: VolumeV1StateRefreshFunc(blockClient, va["volume_id"].(string)),
|
|
|
|
Timeout: 30 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 2 * time.Second,
|
|
|
|
}
|
2015-02-10 17:01:08 +01:00
|
|
|
|
2015-09-12 19:56:38 +02:00
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return err
|
2015-02-10 17:01:08 +01:00
|
|
|
}
|
2015-09-12 19:56:38 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] Attached volume %s to instance %s", volumeId, serverId)
|
2015-02-10 17:01:08 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func detachVolumesFromInstance(computeClient *gophercloud.ServiceClient, blockClient *gophercloud.ServiceClient, serverId string, vols []interface{}) error {
|
2015-09-12 19:56:38 +02:00
|
|
|
for _, v := range vols {
|
|
|
|
va := v.(map[string]interface{})
|
|
|
|
aId := va["id"].(string)
|
2015-02-10 17:01:08 +01:00
|
|
|
|
2016-08-13 21:59:25 +02:00
|
|
|
log.Printf("[INFO] Attempting to detach volume %s", va["volume_id"])
|
2015-09-12 19:56:38 +02:00
|
|
|
if err := volumeattach.Delete(computeClient, serverId, aId).ExtractErr(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-10 17:01:08 +01:00
|
|
|
|
2015-09-12 19:56:38 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"detaching", "in-use"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"available"},
|
2015-09-12 19:56:38 +02:00
|
|
|
Refresh: VolumeV1StateRefreshFunc(blockClient, va["volume_id"].(string)),
|
|
|
|
Timeout: 30 * time.Minute,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
MinTimeout: 2 * time.Second,
|
|
|
|
}
|
2015-02-10 17:01:08 +01:00
|
|
|
|
2015-09-12 19:56:38 +02:00
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return err
|
2015-02-10 17:01:08 +01:00
|
|
|
}
|
2015-09-12 19:56:38 +02:00
|
|
|
log.Printf("[INFO] Detached volume %s from instance %s", va["volume_id"], serverId)
|
2015-02-10 17:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 19:56:38 +02:00
|
|
|
func getVolumeAttachments(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) error {
|
2016-08-13 21:59:25 +02:00
|
|
|
var vols []map[string]interface{}
|
2015-02-10 17:01:08 +01:00
|
|
|
|
2016-08-13 21:59:25 +02:00
|
|
|
allPages, err := volumeattach.List(computeClient, d.Id()).AllPages()
|
2015-02-10 17:01:08 +01:00
|
|
|
if err != nil {
|
2015-09-12 19:56:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-13 21:59:25 +02:00
|
|
|
allVolumeAttachments, err := volumeattach.ExtractVolumeAttachments(allPages)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-02-10 17:01:08 +01:00
|
|
|
}
|
2015-09-12 19:56:38 +02:00
|
|
|
|
2016-08-13 21:59:25 +02:00
|
|
|
if v, ok := d.GetOk("volume"); ok {
|
|
|
|
volumes := v.(*schema.Set).List()
|
|
|
|
for _, volume := range volumes {
|
|
|
|
if volumeMap, ok := volume.(map[string]interface{}); ok {
|
|
|
|
if v, ok := volumeMap["volume_id"].(string); ok {
|
|
|
|
for _, volumeAttachment := range allVolumeAttachments {
|
|
|
|
if v == volumeAttachment.ID {
|
|
|
|
vol := make(map[string]interface{})
|
|
|
|
vol["id"] = volumeAttachment.ID
|
|
|
|
vol["volume_id"] = volumeAttachment.VolumeID
|
|
|
|
vol["device"] = volumeAttachment.Device
|
|
|
|
vols = append(vols, vol)
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 19:56:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 21:59:25 +02:00
|
|
|
log.Printf("[INFO] Volume attachments: %v", vols)
|
|
|
|
d.Set("volume", vols)
|
|
|
|
|
2016-02-27 06:56:12 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkBlockDeviceConfig(d *schema.ResourceData) error {
|
2015-12-12 19:51:21 +01:00
|
|
|
if vL, ok := d.GetOk("block_device"); ok {
|
2015-12-12 21:32:10 +01:00
|
|
|
for _, v := range vL.([]interface{}) {
|
|
|
|
vM := v.(map[string]interface{})
|
2016-02-27 06:56:12 +01:00
|
|
|
|
2015-12-12 21:32:10 +01:00
|
|
|
if vM["source_type"] != "blank" && vM["uuid"] == "" {
|
|
|
|
return fmt.Errorf("You must specify a uuid for %s block device types", vM["source_type"])
|
|
|
|
}
|
2016-02-27 06:56:12 +01:00
|
|
|
|
|
|
|
if vM["source_type"] == "image" && vM["destination_type"] == "volume" {
|
|
|
|
if vM["volume_size"] == 0 {
|
|
|
|
return fmt.Errorf("You must specify a volume_size when creating a volume from an image")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vM["source_type"] == "blank" && vM["destination_type"] == "local" {
|
|
|
|
if vM["volume_size"] == 0 {
|
|
|
|
return fmt.Errorf("You must specify a volume_size when creating a blank block device")
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 19:56:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-02-10 17:01:08 +01:00
|
|
|
}
|
2016-01-11 04:53:30 +01:00
|
|
|
|
|
|
|
func resourceComputeInstancePersonalityHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["file"].(string)))
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceInstancePersonalityV2(d *schema.ResourceData) servers.Personality {
|
|
|
|
var personalities servers.Personality
|
|
|
|
|
|
|
|
if v := d.Get("personality"); v != nil {
|
|
|
|
personalityList := v.(*schema.Set).List()
|
|
|
|
if len(personalityList) > 0 {
|
|
|
|
for _, p := range personalityList {
|
|
|
|
rawPersonality := p.(map[string]interface{})
|
|
|
|
file := servers.File{
|
|
|
|
Path: rawPersonality["file"].(string),
|
|
|
|
Contents: []byte(rawPersonality["content"].(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack Compute Instance Personality: %+v", file)
|
|
|
|
|
|
|
|
personalities = append(personalities, &file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return personalities
|
|
|
|
}
|