2014-08-25 20:48:20 +02:00
|
|
|
package google
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-12-17 00:33:00 +01:00
|
|
|
"strings"
|
2014-08-25 23:57:17 +02:00
|
|
|
|
2014-08-25 20:48:20 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
2014-08-25 20:48:20 +02:00
|
|
|
)
|
|
|
|
|
2015-05-01 03:21:21 +02:00
|
|
|
func stringScopeHashcode(v interface{}) int {
|
|
|
|
v = canonicalizeServiceScope(v.(string))
|
2016-02-08 00:51:26 +01:00
|
|
|
return schema.HashString(v)
|
2015-05-01 03:21:21 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 20:48:20 +02:00
|
|
|
func resourceComputeInstance() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeInstanceCreate,
|
2014-08-25 23:57:17 +02:00
|
|
|
Read: resourceComputeInstanceRead,
|
2014-08-26 22:48:49 +02:00
|
|
|
Update: resourceComputeInstanceUpdate,
|
2014-08-25 23:57:17 +02:00
|
|
|
Delete: resourceComputeInstanceDelete,
|
|
|
|
|
2015-05-01 03:21:21 +02:00
|
|
|
SchemaVersion: 2,
|
2015-04-14 02:04:10 +02:00
|
|
|
MigrateState: resourceComputeInstanceMigrateState,
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"machine_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"disk": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2014-08-26 06:35:23 +02:00
|
|
|
// TODO(mitchellh): one of image or disk is required
|
|
|
|
|
|
|
|
"disk": &schema.Schema{
|
2014-08-25 23:57:17 +02:00
|
|
|
Type: schema.TypeString,
|
2014-08-26 06:35:23 +02:00
|
|
|
Optional: true,
|
2015-02-13 18:55:16 +01:00
|
|
|
ForceNew: true,
|
2014-08-26 06:35:23 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"image": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-02-13 18:55:16 +01:00
|
|
|
ForceNew: true,
|
2014-08-25 23:57:17 +02:00
|
|
|
},
|
2014-10-07 06:59:09 +02:00
|
|
|
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-05-12 03:40:37 +02:00
|
|
|
"scratch": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-09-02 15:52:49 +02:00
|
|
|
"auto_delete": &schema.Schema{
|
2014-09-09 22:43:03 +02:00
|
|
|
Type: schema.TypeBool,
|
2014-09-02 15:52:49 +02:00
|
|
|
Optional: true,
|
2015-03-18 18:50:03 +01:00
|
|
|
Default: true,
|
2015-02-13 18:55:16 +01:00
|
|
|
ForceNew: true,
|
2014-09-02 15:52:49 +02:00
|
|
|
},
|
2015-03-24 17:45:20 +01:00
|
|
|
|
|
|
|
"size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2014-10-16 15:43:52 +02:00
|
|
|
|
|
|
|
"device_name": &schema.Schema{
|
2015-06-24 07:31:24 +02:00
|
|
|
Type: schema.TypeString,
|
2014-10-16 15:43:52 +02:00
|
|
|
Optional: true,
|
|
|
|
},
|
2014-08-25 23:57:17 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-08-25 20:48:20 +02:00
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
"network_interface": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"network": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-02-15 04:17:55 +01:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"subnetwork": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-02-06 09:21:22 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"access_config": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"nat_ip": &schema.Schema{
|
2015-02-20 19:22:26 +01:00
|
|
|
Type: schema.TypeString,
|
2015-02-06 09:21:22 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
2015-12-11 17:41:02 +01:00
|
|
|
|
|
|
|
"assigned_nat_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-02-06 09:21:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
"network": &schema.Schema{
|
2015-06-24 07:31:24 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-04-10 23:03:51 +02:00
|
|
|
Deprecated: "Please use network_interface",
|
2014-08-25 23:57:17 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"source": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-02-06 09:21:22 +01:00
|
|
|
ForceNew: true,
|
2014-08-25 23:57:17 +02:00
|
|
|
},
|
2014-08-26 00:47:21 +02:00
|
|
|
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-02-06 09:21:22 +01:00
|
|
|
ForceNew: true,
|
2014-08-26 00:47:21 +02:00
|
|
|
},
|
2014-08-26 01:23:28 +02:00
|
|
|
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-10-19 08:17:14 +02:00
|
|
|
|
2014-08-26 01:23:28 +02:00
|
|
|
"internal_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-10-19 08:17:14 +02:00
|
|
|
|
2014-10-19 08:03:37 +02:00
|
|
|
"external_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-08-25 23:57:17 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-08-26 00:10:30 +02:00
|
|
|
|
2014-10-07 18:24:13 +02:00
|
|
|
"can_ip_forward": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-07-02 03:24:34 +02:00
|
|
|
"metadata_startup_script": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-08-26 00:25:45 +02:00
|
|
|
"metadata": &schema.Schema{
|
2015-10-14 20:17:44 +02:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
Elem: schema.TypeString,
|
|
|
|
ValidateFunc: validateInstanceMetadata,
|
2014-08-26 00:25:45 +02:00
|
|
|
},
|
|
|
|
|
2014-10-07 10:16:50 +02:00
|
|
|
"service_account": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"email": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"scopes": &schema.Schema{
|
2015-05-01 03:21:21 +02:00
|
|
|
Type: schema.TypeSet,
|
2015-01-28 22:38:02 +01:00
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2014-10-07 10:16:50 +02:00
|
|
|
StateFunc: func(v interface{}) string {
|
|
|
|
return canonicalizeServiceScope(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2015-05-01 03:21:21 +02:00
|
|
|
Set: stringScopeHashcode,
|
2014-10-07 10:16:50 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-10-26 21:16:06 +01:00
|
|
|
"scheduling": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"on_host_maintenance": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"automatic_restart": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"preemptible": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-26 00:10:30 +02:00
|
|
|
"tags": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2014-08-26 00:10:30 +02:00
|
|
|
},
|
2014-08-26 22:48:49 +02:00
|
|
|
|
|
|
|
"metadata_fingerprint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-08-26 22:52:18 +02:00
|
|
|
|
|
|
|
"tags_fingerprint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-02-02 10:46:35 +01:00
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-08-25 23:57:17 +02:00
|
|
|
},
|
2014-08-25 20:48:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 23:33:02 +02:00
|
|
|
func getInstance(config *Config, d *schema.ResourceData) (*compute.Instance, error) {
|
|
|
|
instance, err := config.clientCompute.Instances.Get(
|
|
|
|
config.Project, d.Get("zone").(string), d.Id()).Do()
|
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
2015-11-13 21:36:03 +01:00
|
|
|
log.Printf("[WARN] Removing Instance %q because it's gone", d.Get("name").(string))
|
2015-08-31 23:33:02 +02:00
|
|
|
// The resource doesn't exist anymore
|
2015-11-05 00:38:17 +01:00
|
|
|
id := d.Id()
|
2015-08-31 23:33:02 +02:00
|
|
|
d.SetId("")
|
|
|
|
|
2015-11-05 00:38:17 +01:00
|
|
|
return nil, fmt.Errorf("Resource %s no longer exists", id)
|
2015-08-31 23:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Error reading instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance, nil
|
|
|
|
}
|
|
|
|
|
2014-08-25 20:48:20 +02:00
|
|
|
func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-08-25 23:57:17 +02:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
// Get the zone
|
|
|
|
log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string))
|
|
|
|
zone, err := config.clientCompute.Zones.Get(
|
|
|
|
config.Project, d.Get("zone").(string)).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error loading zone '%s': %s", d.Get("zone").(string), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the machine type
|
|
|
|
log.Printf("[DEBUG] Loading machine type: %s", d.Get("machine_type").(string))
|
|
|
|
machineType, err := config.clientCompute.MachineTypes.Get(
|
|
|
|
config.Project, zone.Name, d.Get("machine_type").(string)).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error loading machine type: %s",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the list of disks
|
|
|
|
disksCount := d.Get("disk.#").(int)
|
|
|
|
disks := make([]*compute.AttachedDisk, 0, disksCount)
|
|
|
|
for i := 0; i < disksCount; i++ {
|
2014-08-26 06:35:23 +02:00
|
|
|
prefix := fmt.Sprintf("disk.%d", i)
|
|
|
|
|
2014-09-02 15:52:21 +02:00
|
|
|
// var sourceLink string
|
|
|
|
|
|
|
|
// Build the disk
|
|
|
|
var disk compute.AttachedDisk
|
|
|
|
disk.Type = "PERSISTENT"
|
|
|
|
disk.Mode = "READ_WRITE"
|
|
|
|
disk.Boot = i == 0
|
2015-03-18 18:50:03 +01:00
|
|
|
disk.AutoDelete = d.Get(prefix + ".auto_delete").(bool)
|
2014-09-02 15:52:49 +02:00
|
|
|
|
2014-08-26 06:35:23 +02:00
|
|
|
// Load up the disk for this disk if specified
|
|
|
|
if v, ok := d.GetOk(prefix + ".disk"); ok {
|
|
|
|
diskName := v.(string)
|
2014-09-02 15:52:21 +02:00
|
|
|
diskData, err := config.clientCompute.Disks.Get(
|
2014-08-26 06:35:23 +02:00
|
|
|
config.Project, zone.Name, diskName).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error loading disk '%s': %s",
|
|
|
|
diskName, err)
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:52:21 +02:00
|
|
|
disk.Source = diskData.SelfLink
|
2015-05-12 03:40:37 +02:00
|
|
|
} else {
|
|
|
|
// Create a new disk
|
2015-06-24 07:31:24 +02:00
|
|
|
disk.InitializeParams = &compute.AttachedDiskInitializeParams{}
|
2015-05-12 03:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk(prefix + ".scratch"); ok {
|
|
|
|
if v.(bool) {
|
|
|
|
disk.Type = "SCRATCH"
|
|
|
|
}
|
2014-08-26 06:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load up the image for this disk if specified
|
|
|
|
if v, ok := d.GetOk(prefix + ".image"); ok {
|
|
|
|
imageName := v.(string)
|
2015-01-30 02:00:02 +01:00
|
|
|
|
|
|
|
imageUrl, err := resolveImage(config, imageName)
|
2014-08-26 06:35:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
2015-01-30 02:00:02 +01:00
|
|
|
"Error resolving image name '%s': %s",
|
2014-08-26 06:35:23 +02:00
|
|
|
imageName, err)
|
|
|
|
}
|
|
|
|
|
2015-05-12 03:40:37 +02:00
|
|
|
disk.InitializeParams.SourceImage = imageUrl
|
2014-08-25 23:57:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:35 +02:00
|
|
|
if v, ok := d.GetOk(prefix + ".type"); ok {
|
2014-10-07 06:59:09 +02:00
|
|
|
diskTypeName := v.(string)
|
|
|
|
diskType, err := readDiskType(config, zone, diskTypeName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error loading disk type '%s': %s",
|
|
|
|
diskTypeName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
disk.InitializeParams.DiskType = diskType.SelfLink
|
|
|
|
}
|
|
|
|
|
2015-03-24 17:45:20 +01:00
|
|
|
if v, ok := d.GetOk(prefix + ".size"); ok {
|
|
|
|
diskSizeGb := v.(int)
|
|
|
|
disk.InitializeParams.DiskSizeGb = int64(diskSizeGb)
|
|
|
|
}
|
|
|
|
|
2015-06-24 07:31:24 +02:00
|
|
|
if v, ok := d.GetOk(prefix + ".device_name"); ok {
|
2014-10-16 15:43:52 +02:00
|
|
|
disk.DeviceName = v.(string)
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
disks = append(disks, &disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
networksCount := d.Get("network.#").(int)
|
2015-02-06 09:21:22 +01:00
|
|
|
networkInterfacesCount := d.Get("network_interface.#").(int)
|
2014-08-25 23:57:17 +02:00
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
if networksCount > 0 && networkInterfacesCount > 0 {
|
|
|
|
return fmt.Errorf("Error: cannot define both networks and network_interfaces.")
|
|
|
|
}
|
|
|
|
if networksCount == 0 && networkInterfacesCount == 0 {
|
|
|
|
return fmt.Errorf("Error: Must define at least one network_interface.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var networkInterfaces []*compute.NetworkInterface
|
|
|
|
|
|
|
|
if networksCount > 0 {
|
|
|
|
// TODO: Delete this block when removing network { }
|
|
|
|
// Build up the list of networkInterfaces
|
|
|
|
networkInterfaces = make([]*compute.NetworkInterface, 0, networksCount)
|
|
|
|
for i := 0; i < networksCount; i++ {
|
|
|
|
prefix := fmt.Sprintf("network.%d", i)
|
|
|
|
// Load up the name of this network
|
|
|
|
networkName := d.Get(prefix + ".source").(string)
|
|
|
|
network, err := config.clientCompute.Networks.Get(
|
|
|
|
config.Project, networkName).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error loading network '%s': %s",
|
|
|
|
networkName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the networkInterface
|
|
|
|
var iface compute.NetworkInterface
|
|
|
|
iface.AccessConfigs = []*compute.AccessConfig{
|
|
|
|
&compute.AccessConfig{
|
|
|
|
Type: "ONE_TO_ONE_NAT",
|
|
|
|
NatIP: d.Get(prefix + ".address").(string),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
iface.Network = network.SelfLink
|
|
|
|
|
|
|
|
networkInterfaces = append(networkInterfaces, &iface)
|
2014-08-25 23:57:17 +02:00
|
|
|
}
|
2015-02-06 09:21:22 +01:00
|
|
|
}
|
2014-08-25 23:57:17 +02:00
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
if networkInterfacesCount > 0 {
|
|
|
|
// Build up the list of networkInterfaces
|
|
|
|
networkInterfaces = make([]*compute.NetworkInterface, 0, networkInterfacesCount)
|
|
|
|
for i := 0; i < networkInterfacesCount; i++ {
|
|
|
|
prefix := fmt.Sprintf("network_interface.%d", i)
|
|
|
|
// Load up the name of this network_interfac
|
|
|
|
networkName := d.Get(prefix + ".network").(string)
|
2016-02-15 04:17:55 +01:00
|
|
|
subnetworkName := d.Get(prefix + ".subnetwork").(string)
|
|
|
|
var networkLink, subnetworkLink string
|
|
|
|
|
|
|
|
if networkName != "" && subnetworkName != "" {
|
|
|
|
return fmt.Errorf("Cannot specify both network and subnetwork values.")
|
|
|
|
} else if networkName != "" {
|
|
|
|
network, err := config.clientCompute.Networks.Get(
|
|
|
|
config.Project, networkName).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error referencing network '%s': %s",
|
|
|
|
networkName, err)
|
|
|
|
}
|
|
|
|
networkLink = network.SelfLink
|
|
|
|
} else {
|
|
|
|
region := getRegionFromZone(d.Get("zone").(string))
|
|
|
|
subnetwork, err := config.clientCompute.Subnetworks.Get(
|
|
|
|
config.Project, region, subnetworkName).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error referencing subnetwork '%s' in region '%s': %s",
|
|
|
|
subnetworkName, region, err)
|
|
|
|
}
|
|
|
|
subnetworkLink = subnetwork.SelfLink
|
2015-02-06 09:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the networkInterface
|
|
|
|
var iface compute.NetworkInterface
|
2016-02-15 04:17:55 +01:00
|
|
|
iface.Network = networkLink
|
|
|
|
iface.Subnetwork = subnetworkLink
|
2015-02-06 09:21:22 +01:00
|
|
|
|
|
|
|
// Handle access_config structs
|
|
|
|
accessConfigsCount := d.Get(prefix + ".access_config.#").(int)
|
|
|
|
iface.AccessConfigs = make([]*compute.AccessConfig, accessConfigsCount)
|
|
|
|
for j := 0; j < accessConfigsCount; j++ {
|
|
|
|
acPrefix := fmt.Sprintf("%s.access_config.%d", prefix, j)
|
|
|
|
iface.AccessConfigs[j] = &compute.AccessConfig{
|
|
|
|
Type: "ONE_TO_ONE_NAT",
|
|
|
|
NatIP: d.Get(acPrefix + ".nat_ip").(string),
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 23:57:17 +02:00
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
networkInterfaces = append(networkInterfaces, &iface)
|
|
|
|
}
|
2014-08-25 23:57:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 10:16:50 +02:00
|
|
|
serviceAccountsCount := d.Get("service_account.#").(int)
|
|
|
|
serviceAccounts := make([]*compute.ServiceAccount, 0, serviceAccountsCount)
|
|
|
|
for i := 0; i < serviceAccountsCount; i++ {
|
|
|
|
prefix := fmt.Sprintf("service_account.%d", i)
|
|
|
|
|
2015-05-01 03:21:21 +02:00
|
|
|
scopesSet := d.Get(prefix + ".scopes").(*schema.Set)
|
|
|
|
scopes := make([]string, scopesSet.Len())
|
|
|
|
for i, v := range scopesSet.List() {
|
|
|
|
scopes[i] = canonicalizeServiceScope(v.(string))
|
2014-10-07 10:16:50 +02:00
|
|
|
}
|
|
|
|
|
2015-01-28 22:38:02 +01:00
|
|
|
serviceAccount := &compute.ServiceAccount{
|
2014-10-07 10:16:50 +02:00
|
|
|
Email: "default",
|
|
|
|
Scopes: scopes,
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceAccounts = append(serviceAccounts, serviceAccount)
|
|
|
|
}
|
|
|
|
|
2015-10-26 21:16:06 +01:00
|
|
|
prefix := "scheduling.0"
|
|
|
|
scheduling := &compute.Scheduling{}
|
|
|
|
|
|
|
|
if val, ok := d.GetOk(prefix + ".automatic_restart"); ok {
|
|
|
|
scheduling.AutomaticRestart = val.(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := d.GetOk(prefix + ".preemptible"); ok {
|
|
|
|
scheduling.Preemptible = val.(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := d.GetOk(prefix + ".on_host_maintenance"); ok {
|
|
|
|
scheduling.OnHostMaintenance = val.(string)
|
|
|
|
}
|
|
|
|
|
2015-07-02 03:24:34 +02:00
|
|
|
metadata, err := resourceInstanceMetadata(d)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating metadata: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
// Create the instance information
|
|
|
|
instance := compute.Instance{
|
2014-10-07 18:24:13 +02:00
|
|
|
CanIpForward: d.Get("can_ip_forward").(bool),
|
2014-08-26 00:25:45 +02:00
|
|
|
Description: d.Get("description").(string),
|
|
|
|
Disks: disks,
|
|
|
|
MachineType: machineType.SelfLink,
|
2015-07-02 03:24:34 +02:00
|
|
|
Metadata: metadata,
|
2014-08-25 23:57:17 +02:00
|
|
|
Name: d.Get("name").(string),
|
2015-02-06 09:21:22 +01:00
|
|
|
NetworkInterfaces: networkInterfaces,
|
2014-08-26 22:52:18 +02:00
|
|
|
Tags: resourceInstanceTags(d),
|
2014-10-07 10:16:50 +02:00
|
|
|
ServiceAccounts: serviceAccounts,
|
2015-10-26 21:16:06 +01:00
|
|
|
Scheduling: scheduling,
|
2014-08-25 23:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Requesting instance creation")
|
|
|
|
op, err := config.clientCompute.Instances.Insert(
|
|
|
|
config.Project, zone.Name, &instance).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the ID now
|
|
|
|
d.SetId(instance.Name)
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2015-09-24 22:30:12 +02:00
|
|
|
waitErr := computeOperationWaitZone(config, op, zone.Name, "instance to create")
|
2015-02-06 09:21:22 +01:00
|
|
|
if waitErr != nil {
|
2014-08-26 07:44:27 +02:00
|
|
|
// The resource didn't actually create
|
|
|
|
d.SetId("")
|
2015-02-06 09:21:22 +01:00
|
|
|
return waitErr
|
2014-08-26 07:44:27 +02:00
|
|
|
}
|
2014-08-25 23:57:17 +02:00
|
|
|
|
|
|
|
return resourceComputeInstanceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2015-11-05 22:15:35 +01:00
|
|
|
id := d.Id()
|
2015-10-07 22:35:06 +02:00
|
|
|
instance, err := getInstance(config, d)
|
2014-08-25 23:57:17 +02:00
|
|
|
if err != nil {
|
2015-11-05 00:38:17 +01:00
|
|
|
if strings.Contains(err.Error(), "no longer exists") {
|
2015-11-05 22:15:35 +01:00
|
|
|
log.Printf("[WARN] Google Compute Instance (%s) not found", id)
|
2015-11-05 00:38:17 +01:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-31 23:33:02 +02:00
|
|
|
return err
|
|
|
|
}
|
2014-08-26 07:49:14 +02:00
|
|
|
|
2015-10-07 22:35:06 +02:00
|
|
|
// Synch metadata
|
2015-08-31 23:33:02 +02:00
|
|
|
md := instance.Metadata
|
2014-08-26 07:49:14 +02:00
|
|
|
|
2016-01-05 17:37:52 +01:00
|
|
|
_md := MetadataFormatSchema(d.Get("metadata").(map[string]interface{}), md)
|
2015-10-14 20:17:44 +02:00
|
|
|
delete(_md, "startup-script")
|
|
|
|
|
2015-10-14 19:17:08 +02:00
|
|
|
if script, scriptExists := d.GetOk("metadata_startup_script"); scriptExists {
|
|
|
|
d.Set("metadata_startup_script", script)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.Set("metadata", _md); err != nil {
|
2015-08-31 23:33:02 +02:00
|
|
|
return fmt.Errorf("Error setting metadata: %s", err)
|
2014-08-25 23:57:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 18:24:13 +02:00
|
|
|
d.Set("can_ip_forward", instance.CanIpForward)
|
|
|
|
|
2014-10-07 10:16:50 +02:00
|
|
|
// Set the service accounts
|
2015-03-02 19:00:24 +01:00
|
|
|
serviceAccounts := make([]map[string]interface{}, 0, 1)
|
|
|
|
for _, serviceAccount := range instance.ServiceAccounts {
|
2015-05-01 03:21:21 +02:00
|
|
|
scopes := make([]interface{}, len(serviceAccount.Scopes))
|
2015-03-02 19:00:24 +01:00
|
|
|
for i, scope := range serviceAccount.Scopes {
|
|
|
|
scopes[i] = scope
|
2014-10-07 10:16:50 +02:00
|
|
|
}
|
2015-03-02 19:00:24 +01:00
|
|
|
serviceAccounts = append(serviceAccounts, map[string]interface{}{
|
|
|
|
"email": serviceAccount.Email,
|
2015-05-01 03:21:21 +02:00
|
|
|
"scopes": schema.NewSet(stringScopeHashcode, scopes),
|
2015-03-02 19:00:24 +01:00
|
|
|
})
|
2014-10-07 10:16:50 +02:00
|
|
|
}
|
2015-03-02 19:00:24 +01:00
|
|
|
d.Set("service_account", serviceAccounts)
|
2014-10-07 10:16:50 +02:00
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
networksCount := d.Get("network.#").(int)
|
|
|
|
networkInterfacesCount := d.Get("network_interface.#").(int)
|
|
|
|
|
|
|
|
if networksCount > 0 && networkInterfacesCount > 0 {
|
|
|
|
return fmt.Errorf("Error: cannot define both networks and network_interfaces.")
|
|
|
|
}
|
|
|
|
if networksCount == 0 && networkInterfacesCount == 0 {
|
|
|
|
return fmt.Errorf("Error: Must define at least one network_interface.")
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:23:28 +02:00
|
|
|
// Set the networks
|
2015-02-06 09:21:22 +01:00
|
|
|
// Use the first external IP found for the default connection info.
|
2014-10-19 09:04:17 +02:00
|
|
|
externalIP := ""
|
2015-02-06 09:21:22 +01:00
|
|
|
internalIP := ""
|
2015-03-02 19:00:24 +01:00
|
|
|
networks := make([]map[string]interface{}, 0, 1)
|
2015-02-06 09:21:22 +01:00
|
|
|
if networksCount > 0 {
|
|
|
|
// TODO: Remove this when realizing deprecation of .network
|
2015-04-09 19:29:59 +02:00
|
|
|
for i, iface := range instance.NetworkInterfaces {
|
2015-02-06 09:21:22 +01:00
|
|
|
var natIP string
|
|
|
|
for _, config := range iface.AccessConfigs {
|
|
|
|
if config.Type == "ONE_TO_ONE_NAT" {
|
|
|
|
natIP = config.NatIP
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if externalIP == "" && natIP != "" {
|
|
|
|
externalIP = natIP
|
|
|
|
}
|
|
|
|
|
2015-03-02 19:00:24 +01:00
|
|
|
network := make(map[string]interface{})
|
|
|
|
network["name"] = iface.Name
|
|
|
|
network["external_address"] = natIP
|
|
|
|
network["internal_address"] = iface.NetworkIP
|
2015-04-09 19:29:59 +02:00
|
|
|
network["source"] = d.Get(fmt.Sprintf("network.%d.source", i))
|
2015-03-02 19:00:24 +01:00
|
|
|
networks = append(networks, network)
|
2014-10-19 08:03:37 +02:00
|
|
|
}
|
2015-02-06 09:21:22 +01:00
|
|
|
}
|
2015-03-02 19:00:24 +01:00
|
|
|
d.Set("network", networks)
|
2014-10-19 09:04:17 +02:00
|
|
|
|
2015-03-02 19:00:24 +01:00
|
|
|
networkInterfaces := make([]map[string]interface{}, 0, 1)
|
2015-02-06 09:21:22 +01:00
|
|
|
if networkInterfacesCount > 0 {
|
2015-04-08 14:38:07 +02:00
|
|
|
for i, iface := range instance.NetworkInterfaces {
|
2015-02-06 09:21:22 +01:00
|
|
|
// The first non-empty ip is left in natIP
|
|
|
|
var natIP string
|
2015-03-02 19:00:24 +01:00
|
|
|
accessConfigs := make(
|
|
|
|
[]map[string]interface{}, 0, len(iface.AccessConfigs))
|
2015-12-11 17:41:02 +01:00
|
|
|
for j, config := range iface.AccessConfigs {
|
2015-03-02 19:00:24 +01:00
|
|
|
accessConfigs = append(accessConfigs, map[string]interface{}{
|
2015-12-11 17:41:02 +01:00
|
|
|
"nat_ip": d.Get(fmt.Sprintf("network_interface.%d.access_config.%d.nat_ip", i, j)),
|
|
|
|
"assigned_nat_ip": config.NatIP,
|
2015-03-02 19:00:24 +01:00
|
|
|
})
|
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
if natIP == "" {
|
|
|
|
natIP = config.NatIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if externalIP == "" {
|
|
|
|
externalIP = natIP
|
|
|
|
}
|
|
|
|
|
|
|
|
if internalIP == "" {
|
|
|
|
internalIP = iface.NetworkIP
|
|
|
|
}
|
|
|
|
|
2015-03-02 19:00:24 +01:00
|
|
|
networkInterfaces = append(networkInterfaces, map[string]interface{}{
|
|
|
|
"name": iface.Name,
|
|
|
|
"address": iface.NetworkIP,
|
2015-04-08 14:38:07 +02:00
|
|
|
"network": d.Get(fmt.Sprintf("network_interface.%d.network", i)),
|
2015-03-02 19:00:24 +01:00
|
|
|
"access_config": accessConfigs,
|
|
|
|
})
|
2014-10-19 08:03:37 +02:00
|
|
|
}
|
2015-02-06 09:21:22 +01:00
|
|
|
}
|
2015-03-02 19:00:24 +01:00
|
|
|
d.Set("network_interface", networkInterfaces)
|
2014-10-19 09:04:17 +02:00
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
// Fall back on internal ip if there is no external ip. This makes sense in the situation where
|
|
|
|
// terraform is being used on a cloud instance and can therefore access the instances it creates
|
|
|
|
// via their internal ips.
|
|
|
|
sshIP := externalIP
|
|
|
|
if sshIP == "" {
|
|
|
|
sshIP = internalIP
|
2014-08-26 01:23:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-19 09:04:17 +02:00
|
|
|
// Initialize the connection info
|
|
|
|
d.SetConnInfo(map[string]string{
|
|
|
|
"type": "ssh",
|
2015-02-06 09:21:22 +01:00
|
|
|
"host": sshIP,
|
2014-10-19 09:04:17 +02:00
|
|
|
})
|
|
|
|
|
2014-08-26 22:48:49 +02:00
|
|
|
// Set the metadata fingerprint if there is one.
|
|
|
|
if instance.Metadata != nil {
|
|
|
|
d.Set("metadata_fingerprint", instance.Metadata.Fingerprint)
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:52:18 +02:00
|
|
|
// Set the tags fingerprint if there is one.
|
|
|
|
if instance.Tags != nil {
|
|
|
|
d.Set("tags_fingerprint", instance.Tags.Fingerprint)
|
|
|
|
}
|
|
|
|
|
2015-02-02 10:46:35 +01:00
|
|
|
d.Set("self_link", instance.SelfLink)
|
2015-10-14 19:17:08 +02:00
|
|
|
d.SetId(instance.Name)
|
2015-02-02 10:46:35 +01:00
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:48:49 +02:00
|
|
|
func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
zone := d.Get("zone").(string)
|
|
|
|
|
2015-10-07 22:35:06 +02:00
|
|
|
instance, err := getInstance(config, d)
|
2015-02-06 09:21:22 +01:00
|
|
|
if err != nil {
|
2015-08-31 23:33:02 +02:00
|
|
|
return err
|
2015-02-06 09:21:22 +01:00
|
|
|
}
|
|
|
|
|
2014-08-27 05:31:35 +02:00
|
|
|
// Enable partial mode for the resource since it is possible
|
|
|
|
d.Partial(true)
|
|
|
|
|
2014-08-26 22:48:49 +02:00
|
|
|
// If the Metadata has changed, then update that.
|
|
|
|
if d.HasChange("metadata") {
|
2015-08-31 23:33:02 +02:00
|
|
|
o, n := d.GetChange("metadata")
|
2015-10-14 19:17:08 +02:00
|
|
|
if script, scriptExists := d.GetOk("metadata_startup_script"); scriptExists {
|
|
|
|
if _, ok := n.(map[string]interface{})["startup-script"]; ok {
|
|
|
|
return fmt.Errorf("Only one of metadata.startup-script and metadata_startup_script may be defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
n.(map[string]interface{})["startup-script"] = script
|
|
|
|
}
|
|
|
|
|
2015-08-31 23:33:02 +02:00
|
|
|
updateMD := func() error {
|
|
|
|
// Reload the instance in the case of a fingerprint mismatch
|
2015-10-07 22:35:06 +02:00
|
|
|
instance, err = getInstance(config, d)
|
2015-08-31 23:33:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
md := instance.Metadata
|
|
|
|
|
|
|
|
MetadataUpdate(o.(map[string]interface{}), n.(map[string]interface{}), md)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating metadata: %s", err)
|
|
|
|
}
|
|
|
|
op, err := config.clientCompute.Instances.SetMetadata(
|
|
|
|
config.Project, zone, d.Id(), md).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating metadata: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
opErr := computeOperationWaitZone(config, op, zone, "metadata to update")
|
2015-08-31 23:33:02 +02:00
|
|
|
if opErr != nil {
|
|
|
|
return opErr
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("metadata")
|
|
|
|
return nil
|
2014-08-26 22:48:49 +02:00
|
|
|
}
|
2014-08-27 05:31:35 +02:00
|
|
|
|
2015-08-31 23:33:02 +02:00
|
|
|
MetadataRetryWrapper(updateMD)
|
2014-08-26 22:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("tags") {
|
|
|
|
tags := resourceInstanceTags(d)
|
|
|
|
op, err := config.clientCompute.Instances.SetTags(
|
2015-02-06 09:21:22 +01:00
|
|
|
config.Project, zone, d.Id(), tags).Do()
|
2014-08-26 22:52:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating tags: %s", err)
|
|
|
|
}
|
2014-08-26 22:48:49 +02:00
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
opErr := computeOperationWaitZone(config, op, zone, "tags to update")
|
2015-02-06 09:21:22 +01:00
|
|
|
if opErr != nil {
|
|
|
|
return opErr
|
2014-08-26 22:52:18 +02:00
|
|
|
}
|
2014-08-27 05:31:35 +02:00
|
|
|
|
|
|
|
d.SetPartial("tags")
|
2014-08-26 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-10-26 21:16:06 +01:00
|
|
|
if d.HasChange("scheduling") {
|
|
|
|
prefix := "scheduling.0"
|
|
|
|
scheduling := &compute.Scheduling{}
|
|
|
|
|
|
|
|
if val, ok := d.GetOk(prefix + ".automatic_restart"); ok {
|
|
|
|
scheduling.AutomaticRestart = val.(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := d.GetOk(prefix + ".preemptible"); ok {
|
|
|
|
scheduling.Preemptible = val.(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := d.GetOk(prefix + ".on_host_maintenance"); ok {
|
|
|
|
scheduling.OnHostMaintenance = val.(string)
|
|
|
|
}
|
|
|
|
|
2015-10-29 23:10:44 +01:00
|
|
|
op, err := config.clientCompute.Instances.SetScheduling(config.Project,
|
2015-10-26 21:16:06 +01:00
|
|
|
zone, d.Id(), scheduling).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating scheduling policy: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opErr := computeOperationWaitZone(config, op, zone,
|
|
|
|
"scheduling policy update")
|
|
|
|
if opErr != nil {
|
|
|
|
return opErr
|
|
|
|
}
|
|
|
|
|
2015-10-29 23:10:44 +01:00
|
|
|
d.SetPartial("scheduling")
|
2015-10-26 21:16:06 +01:00
|
|
|
}
|
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
networkInterfacesCount := d.Get("network_interface.#").(int)
|
|
|
|
if networkInterfacesCount > 0 {
|
|
|
|
// Sanity check
|
|
|
|
if networkInterfacesCount != len(instance.NetworkInterfaces) {
|
|
|
|
return fmt.Errorf("Instance had unexpected number of network interfaces: %d", len(instance.NetworkInterfaces))
|
|
|
|
}
|
|
|
|
for i := 0; i < networkInterfacesCount; i++ {
|
|
|
|
prefix := fmt.Sprintf("network_interface.%d", i)
|
|
|
|
instNetworkInterface := instance.NetworkInterfaces[i]
|
2015-02-20 19:22:26 +01:00
|
|
|
networkName := d.Get(prefix + ".name").(string)
|
2015-02-06 09:21:22 +01:00
|
|
|
|
|
|
|
// TODO: This sanity check is broken by #929, disabled for now (by forcing the equality)
|
|
|
|
networkName = instNetworkInterface.Name
|
|
|
|
// Sanity check
|
|
|
|
if networkName != instNetworkInterface.Name {
|
|
|
|
return fmt.Errorf("Instance networkInterface had unexpected name: %s", instNetworkInterface.Name)
|
|
|
|
}
|
|
|
|
|
2015-02-20 19:22:26 +01:00
|
|
|
if d.HasChange(prefix + ".access_config") {
|
2015-02-06 09:21:22 +01:00
|
|
|
|
|
|
|
// TODO: This code deletes then recreates accessConfigs. This is bad because it may
|
|
|
|
// leave the machine inaccessible from either ip if the creation part fails (network
|
|
|
|
// timeout etc). However right now there is a GCE limit of 1 accessConfig so it is
|
|
|
|
// the only way to do it. In future this should be revised to only change what is
|
|
|
|
// necessary, and also add before removing.
|
|
|
|
|
|
|
|
// Delete any accessConfig that currently exists in instNetworkInterface
|
2015-02-20 19:22:26 +01:00
|
|
|
for _, ac := range instNetworkInterface.AccessConfigs {
|
2015-02-06 09:21:22 +01:00
|
|
|
op, err := config.clientCompute.Instances.DeleteAccessConfig(
|
2015-02-23 19:35:07 +01:00
|
|
|
config.Project, zone, d.Id(), ac.Name, networkName).Do()
|
2015-02-06 09:21:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting old access_config: %s", err)
|
|
|
|
}
|
2015-09-24 22:30:12 +02:00
|
|
|
opErr := computeOperationWaitZone(config, op, zone, "old access_config to delete")
|
2015-02-06 09:21:22 +01:00
|
|
|
if opErr != nil {
|
|
|
|
return opErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new ones
|
|
|
|
accessConfigsCount := d.Get(prefix + ".access_config.#").(int)
|
|
|
|
for j := 0; j < accessConfigsCount; j++ {
|
|
|
|
acPrefix := fmt.Sprintf("%s.access_config.%d", prefix, j)
|
|
|
|
ac := &compute.AccessConfig{
|
|
|
|
Type: "ONE_TO_ONE_NAT",
|
|
|
|
NatIP: d.Get(acPrefix + ".nat_ip").(string),
|
|
|
|
}
|
|
|
|
op, err := config.clientCompute.Instances.AddAccessConfig(
|
2015-02-20 19:22:26 +01:00
|
|
|
config.Project, zone, d.Id(), networkName, ac).Do()
|
2015-02-06 09:21:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error adding new access_config: %s", err)
|
|
|
|
}
|
2015-09-24 22:30:12 +02:00
|
|
|
opErr := computeOperationWaitZone(config, op, zone, "new access_config to add")
|
2015-02-06 09:21:22 +01:00
|
|
|
if opErr != nil {
|
|
|
|
return opErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 05:31:35 +02:00
|
|
|
// We made it, disable partial mode
|
|
|
|
d.Partial(false)
|
|
|
|
|
2014-08-26 22:48:49 +02:00
|
|
|
return resourceComputeInstanceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2015-02-06 09:21:22 +01:00
|
|
|
zone := d.Get("zone").(string)
|
2015-04-14 02:04:10 +02:00
|
|
|
log.Printf("[INFO] Requesting instance deletion: %s", d.Id())
|
2015-02-06 09:21:22 +01:00
|
|
|
op, err := config.clientCompute.Instances.Delete(config.Project, zone, d.Id()).Do()
|
2014-08-25 23:57:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2015-09-24 22:30:12 +02:00
|
|
|
opErr := computeOperationWaitZone(config, op, zone, "instance to delete")
|
2015-02-06 09:21:22 +01:00
|
|
|
if opErr != nil {
|
|
|
|
return opErr
|
2014-08-25 23:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
2014-08-25 20:48:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-08-26 22:48:49 +02:00
|
|
|
|
2015-07-02 03:24:34 +02:00
|
|
|
func resourceInstanceMetadata(d *schema.ResourceData) (*compute.Metadata, error) {
|
2015-04-14 02:04:10 +02:00
|
|
|
m := &compute.Metadata{}
|
2015-07-02 03:24:34 +02:00
|
|
|
mdMap := d.Get("metadata").(map[string]interface{})
|
2015-10-14 20:17:44 +02:00
|
|
|
if v, ok := d.GetOk("metadata_startup_script"); ok && v.(string) != "" {
|
|
|
|
mdMap["startup-script"] = v
|
2015-07-02 03:24:34 +02:00
|
|
|
}
|
|
|
|
if len(mdMap) > 0 {
|
2015-04-14 02:04:10 +02:00
|
|
|
m.Items = make([]*compute.MetadataItems, 0, len(mdMap))
|
|
|
|
for key, val := range mdMap {
|
2015-08-31 16:06:25 +02:00
|
|
|
v := val.(string)
|
2015-04-14 02:04:10 +02:00
|
|
|
m.Items = append(m.Items, &compute.MetadataItems{
|
|
|
|
Key: key,
|
2015-08-31 16:06:25 +02:00
|
|
|
Value: &v,
|
2015-04-14 02:04:10 +02:00
|
|
|
})
|
2014-08-26 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the fingerprint. If the metadata has never been set before
|
|
|
|
// then this will just be blank.
|
|
|
|
m.Fingerprint = d.Get("metadata_fingerprint").(string)
|
|
|
|
}
|
|
|
|
|
2015-07-02 03:24:34 +02:00
|
|
|
return m, nil
|
2014-08-26 22:48:49 +02:00
|
|
|
}
|
2014-08-26 22:52:18 +02:00
|
|
|
|
|
|
|
func resourceInstanceTags(d *schema.ResourceData) *compute.Tags {
|
|
|
|
// Calculate the tags
|
|
|
|
var tags *compute.Tags
|
|
|
|
if v := d.Get("tags"); v != nil {
|
2014-11-20 11:32:15 +01:00
|
|
|
vs := v.(*schema.Set)
|
2014-08-26 22:52:18 +02:00
|
|
|
tags = new(compute.Tags)
|
2014-11-20 11:32:15 +01:00
|
|
|
tags.Items = make([]string, vs.Len())
|
|
|
|
for i, v := range vs.List() {
|
2014-08-26 22:52:18 +02:00
|
|
|
tags.Items[i] = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
tags.Fingerprint = d.Get("tags_fingerprint").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags
|
|
|
|
}
|
2015-10-14 20:17:44 +02:00
|
|
|
|
|
|
|
func validateInstanceMetadata(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
mdMap := v.(map[string]interface{})
|
|
|
|
if _, ok := mdMap["startup-script"]; ok {
|
|
|
|
es = append(es, fmt.Errorf(
|
|
|
|
"Use metadata_startup_script instead of a startup-script key in %q.", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|