2014-08-25 20:48:20 +02:00
|
|
|
package google
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.google.com/p/google-api-go-client/compute/v1"
|
2014-08-26 07:49:14 +02:00
|
|
|
"code.google.com/p/google-api-go-client/googleapi"
|
2014-08-26 00:10:30 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-08-25 20:48:20 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
|
|
|
"image": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: 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,
|
|
|
|
},
|
|
|
|
|
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,
|
|
|
|
},
|
2014-08-25 23:57:17 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-08-25 20:48:20 +02:00
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
"network": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"source": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2014-08-26 00:47:21 +02:00
|
|
|
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
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-11-20 18:40:17 +01:00
|
|
|
"service_accounts": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"email": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"scopes": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-10-07 18:24:13 +02:00
|
|
|
"can_ip_forward": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-08-26 00:25:45 +02:00
|
|
|
"metadata": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
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-01-28 22:38:02 +01:00
|
|
|
Type: schema.TypeList,
|
|
|
|
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))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-26 00:10:30 +02:00
|
|
|
"tags": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
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,
|
|
|
|
},
|
2014-08-25 23:57:17 +02:00
|
|
|
},
|
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
|
|
|
|
disk.AutoDelete = true
|
2014-08-26 06:35:23 +02:00
|
|
|
|
2014-09-02 15:52:49 +02:00
|
|
|
if v, ok := d.GetOk(prefix + ".auto_delete"); ok {
|
|
|
|
disk.AutoDelete = v.(bool)
|
|
|
|
}
|
|
|
|
|
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
|
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)
|
|
|
|
image, err := readImage(config, imageName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error loading image '%s': %s",
|
|
|
|
imageName, err)
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:52:21 +02:00
|
|
|
disk.InitializeParams = &compute.AttachedDiskInitializeParams{
|
|
|
|
SourceImage: image.SelfLink,
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
disks = append(disks, &disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the list of networks
|
|
|
|
networksCount := d.Get("network.#").(int)
|
|
|
|
networks := make([]*compute.NetworkInterface, 0, networksCount)
|
|
|
|
for i := 0; i < networksCount; i++ {
|
2014-08-26 00:47:21 +02:00
|
|
|
prefix := fmt.Sprintf("network.%d", i)
|
2014-08-25 23:57:17 +02:00
|
|
|
// Load up the name of this network
|
2014-08-26 00:47:21 +02:00
|
|
|
networkName := d.Get(prefix + ".source").(string)
|
2014-08-25 23:57:17 +02:00
|
|
|
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 disk
|
|
|
|
var iface compute.NetworkInterface
|
|
|
|
iface.AccessConfigs = []*compute.AccessConfig{
|
|
|
|
&compute.AccessConfig{
|
2014-08-26 00:47:21 +02:00
|
|
|
Type: "ONE_TO_ONE_NAT",
|
|
|
|
NatIP: d.Get(prefix + ".address").(string),
|
2014-08-25 23:57:17 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
iface.Network = network.SelfLink
|
|
|
|
|
|
|
|
networks = append(networks, &iface)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
scopesCount := d.Get(prefix + ".scopes.#").(int)
|
|
|
|
scopes := make([]string, 0, scopesCount)
|
|
|
|
for j := 0; j < scopesCount; j++ {
|
2015-01-28 22:38:02 +01:00
|
|
|
scope := d.Get(fmt.Sprintf(prefix+".scopes.%d", j)).(string)
|
2014-10-07 10:16:50 +02:00
|
|
|
scopes = append(scopes, canonicalizeServiceScope(scope))
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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,
|
2014-08-26 22:48:49 +02:00
|
|
|
Metadata: resourceInstanceMetadata(d),
|
2014-08-25 23:57:17 +02:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
NetworkInterfaces: networks,
|
2014-08-26 22:52:18 +02:00
|
|
|
Tags: resourceInstanceTags(d),
|
2014-10-07 10:16:50 +02:00
|
|
|
ServiceAccounts: serviceAccounts,
|
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
|
|
|
|
w := &OperationWaiter{
|
|
|
|
Service: config.clientCompute,
|
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
Zone: zone.Name,
|
|
|
|
Type: OperationWaitZone,
|
|
|
|
}
|
|
|
|
state := w.Conf()
|
|
|
|
state.Delay = 10 * time.Second
|
|
|
|
state.Timeout = 10 * time.Minute
|
|
|
|
state.MinTimeout = 2 * time.Second
|
2014-08-26 07:44:27 +02:00
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
2014-08-25 23:57:17 +02:00
|
|
|
return fmt.Errorf("Error waiting for instance to create: %s", err)
|
|
|
|
}
|
2014-08-26 07:44:27 +02:00
|
|
|
op = opRaw.(*compute.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
// The resource didn't actually create
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
// Return the error
|
|
|
|
return OperationError(*op.Error)
|
|
|
|
}
|
2014-08-25 23:57:17 +02:00
|
|
|
|
|
|
|
return resourceComputeInstanceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2014-08-26 01:23:28 +02:00
|
|
|
instance, err := config.clientCompute.Instances.Get(
|
2014-08-25 23:57:17 +02:00
|
|
|
config.Project, d.Get("zone").(string), d.Id()).Do()
|
|
|
|
if err != nil {
|
2014-08-26 07:49:14 +02:00
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
return fmt.Errorf("Error reading instance: %s", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
for i, serviceAccount := range instance.ServiceAccounts {
|
|
|
|
prefix := fmt.Sprintf("service_account.%d", i)
|
2015-01-28 22:38:02 +01:00
|
|
|
d.Set(prefix+".email", serviceAccount.Email)
|
|
|
|
d.Set(prefix+".scopes.#", len(serviceAccount.Scopes))
|
2014-10-07 10:16:50 +02:00
|
|
|
for j, scope := range serviceAccount.Scopes {
|
|
|
|
d.Set(fmt.Sprintf("%s.scopes.%d", prefix, j), scope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:23:28 +02:00
|
|
|
// Set the networks
|
2014-10-19 09:04:17 +02:00
|
|
|
externalIP := ""
|
2014-08-26 01:23:28 +02:00
|
|
|
for i, iface := range instance.NetworkInterfaces {
|
|
|
|
prefix := fmt.Sprintf("network.%d", i)
|
|
|
|
d.Set(prefix+".name", iface.Name)
|
2014-10-19 08:03:37 +02:00
|
|
|
|
2014-10-19 09:04:17 +02:00
|
|
|
// Use the first external IP found for the default connection info.
|
|
|
|
natIP := resourceInstanceNatIP(iface)
|
|
|
|
if externalIP == "" && natIP != "" {
|
|
|
|
externalIP = natIP
|
2014-10-19 08:03:37 +02:00
|
|
|
}
|
2014-10-19 09:04:17 +02:00
|
|
|
d.Set(prefix+".external_address", natIP)
|
|
|
|
|
2014-08-26 01:23:28 +02:00
|
|
|
d.Set(prefix+".internal_address", iface.NetworkIP)
|
|
|
|
}
|
|
|
|
|
2014-10-19 09:04:17 +02:00
|
|
|
// Initialize the connection info
|
|
|
|
d.SetConnInfo(map[string]string{
|
|
|
|
"type": "ssh",
|
|
|
|
"host": externalIP,
|
|
|
|
})
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
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") {
|
|
|
|
metadata := resourceInstanceMetadata(d)
|
|
|
|
op, err := config.clientCompute.Instances.SetMetadata(
|
|
|
|
config.Project, d.Get("zone").(string), d.Id(), metadata).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating metadata: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &OperationWaiter{
|
|
|
|
Service: config.clientCompute,
|
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
Zone: d.Get("zone").(string),
|
|
|
|
Type: OperationWaitZone,
|
|
|
|
}
|
|
|
|
state := w.Conf()
|
|
|
|
state.Delay = 1 * time.Second
|
|
|
|
state.Timeout = 5 * time.Minute
|
|
|
|
state.MinTimeout = 2 * time.Second
|
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for metadata to update: %s", err)
|
|
|
|
}
|
|
|
|
op = opRaw.(*compute.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
// Return the error
|
|
|
|
return OperationError(*op.Error)
|
|
|
|
}
|
2014-08-27 05:31:35 +02:00
|
|
|
|
|
|
|
d.SetPartial("metadata")
|
2014-08-26 22:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("tags") {
|
|
|
|
tags := resourceInstanceTags(d)
|
|
|
|
op, err := config.clientCompute.Instances.SetTags(
|
|
|
|
config.Project, d.Get("zone").(string), d.Id(), tags).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating tags: %s", err)
|
|
|
|
}
|
2014-08-26 22:48:49 +02:00
|
|
|
|
2014-08-26 22:52:18 +02:00
|
|
|
w := &OperationWaiter{
|
|
|
|
Service: config.clientCompute,
|
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
Zone: d.Get("zone").(string),
|
|
|
|
Type: OperationWaitZone,
|
|
|
|
}
|
|
|
|
state := w.Conf()
|
|
|
|
state.Delay = 1 * time.Second
|
|
|
|
state.Timeout = 5 * time.Minute
|
|
|
|
state.MinTimeout = 2 * time.Second
|
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for tags to update: %s", err)
|
|
|
|
}
|
|
|
|
op = opRaw.(*compute.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
// Return the error
|
|
|
|
return OperationError(*op.Error)
|
|
|
|
}
|
2014-08-27 05:31:35 +02:00
|
|
|
|
|
|
|
d.SetPartial("tags")
|
2014-08-26 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
op, err := config.clientCompute.Instances.Delete(
|
|
|
|
config.Project, d.Get("zone").(string), d.Id()).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
|
|
|
w := &OperationWaiter{
|
|
|
|
Service: config.clientCompute,
|
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
Zone: d.Get("zone").(string),
|
|
|
|
Type: OperationWaitZone,
|
|
|
|
}
|
|
|
|
state := w.Conf()
|
|
|
|
state.Delay = 5 * time.Second
|
|
|
|
state.Timeout = 5 * time.Minute
|
|
|
|
state.MinTimeout = 2 * time.Second
|
2014-08-26 07:44:27 +02:00
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for instance to delete: %s", err)
|
|
|
|
}
|
|
|
|
op = opRaw.(*compute.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
// Return the error
|
|
|
|
return OperationError(*op.Error)
|
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
|
|
|
|
|
|
|
func resourceInstanceMetadata(d *schema.ResourceData) *compute.Metadata {
|
|
|
|
var metadata *compute.Metadata
|
2015-01-28 22:38:02 +01:00
|
|
|
if metadataList := d.Get("metadata").([]interface{}); len(metadataList) > 0 {
|
2014-08-26 22:48:49 +02:00
|
|
|
m := new(compute.Metadata)
|
2015-01-28 22:38:02 +01:00
|
|
|
m.Items = make([]*compute.MetadataItems, 0, len(metadataList))
|
|
|
|
for _, metadataMap := range metadataList {
|
|
|
|
for key, val := range metadataMap.(map[string]interface{}) {
|
|
|
|
// TODO: fix https://github.com/hashicorp/terraform/issues/883
|
|
|
|
// and remove this workaround <3 phinze
|
|
|
|
if key == "#" {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-26 22:48:49 +02:00
|
|
|
m.Items = append(m.Items, &compute.MetadataItems{
|
2015-01-28 22:38:02 +01:00
|
|
|
Key: key,
|
|
|
|
Value: val.(string),
|
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)
|
|
|
|
|
|
|
|
metadata = m
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
}
|
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
|
|
|
|
}
|
2014-10-19 09:04:17 +02:00
|
|
|
|
|
|
|
// resourceInstanceNatIP acquires the first NatIP with a "ONE_TO_ONE_NAT" type
|
|
|
|
// in the compute.NetworkInterface's AccessConfigs.
|
|
|
|
func resourceInstanceNatIP(iface *compute.NetworkInterface) (natIP string) {
|
|
|
|
for _, config := range iface.AccessConfigs {
|
|
|
|
if config.Type == "ONE_TO_ONE_NAT" {
|
|
|
|
natIP = config.NatIP
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return natIP
|
|
|
|
}
|