github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/builtin/providers/google/resource_compute_instance.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"code.google.com/p/google-api-go-client/compute/v1"
     9  	"code.google.com/p/google-api-go-client/googleapi"
    10  	"github.com/hashicorp/terraform/helper/hashcode"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceComputeInstance() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceComputeInstanceCreate,
    17  		Read:   resourceComputeInstanceRead,
    18  		Update: resourceComputeInstanceUpdate,
    19  		Delete: resourceComputeInstanceDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"description": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"machine_type": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  				ForceNew: true,
    38  			},
    39  
    40  			"zone": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  				ForceNew: true,
    44  			},
    45  
    46  			"disk": &schema.Schema{
    47  				Type:     schema.TypeList,
    48  				Required: true,
    49  				ForceNew: true,
    50  				Elem: &schema.Resource{
    51  					Schema: map[string]*schema.Schema{
    52  						// TODO(mitchellh): one of image or disk is required
    53  
    54  						"disk": &schema.Schema{
    55  							Type:     schema.TypeString,
    56  							Optional: true,
    57  							ForceNew: true,
    58  						},
    59  
    60  						"image": &schema.Schema{
    61  							Type:     schema.TypeString,
    62  							Optional: true,
    63  							ForceNew: true,
    64  						},
    65  
    66  						"type": &schema.Schema{
    67  							Type:     schema.TypeString,
    68  							Optional: true,
    69  							ForceNew: true,
    70  						},
    71  
    72  						"auto_delete": &schema.Schema{
    73  							Type:     schema.TypeBool,
    74  							Optional: true,
    75  							ForceNew: true,
    76  						},
    77  					},
    78  				},
    79  			},
    80  
    81  			"network_interface": &schema.Schema{
    82  				Type:     schema.TypeList,
    83  				Optional: true,
    84  				ForceNew: true,
    85  				Elem: &schema.Resource{
    86  					Schema: map[string]*schema.Schema{
    87  						"network": &schema.Schema{
    88  							Type:     schema.TypeString,
    89  							Required: true,
    90  							ForceNew: true,
    91  						},
    92  
    93  						"name": &schema.Schema{
    94  							Type:     schema.TypeString,
    95  							Computed: true,
    96  						},
    97  
    98  						"address": &schema.Schema{
    99  							Type:     schema.TypeString,
   100  							Computed: true,
   101  						},
   102  
   103  						"access_config": &schema.Schema{
   104  							Type:     schema.TypeList,
   105  							Optional: true,
   106  							Elem: &schema.Resource{
   107  								Schema: map[string]*schema.Schema{
   108  									"nat_ip": &schema.Schema{
   109  										Type:     schema.TypeString,
   110  										Computed: true,
   111  										Optional: true,
   112  									},
   113  								},
   114  							},
   115  						},
   116  					},
   117  				},
   118  			},
   119  
   120  			"network": &schema.Schema{
   121  				Type:     schema.TypeList,
   122  				Optional: true,
   123  				ForceNew: true,
   124  				Elem: &schema.Resource{
   125  					Schema: map[string]*schema.Schema{
   126  						"source": &schema.Schema{
   127  							Type:     schema.TypeString,
   128  							Required: true,
   129  							ForceNew: true,
   130  						},
   131  
   132  						"address": &schema.Schema{
   133  							Type:     schema.TypeString,
   134  							Optional: true,
   135  							ForceNew: true,
   136  						},
   137  
   138  						"name": &schema.Schema{
   139  							Type:     schema.TypeString,
   140  							Computed: true,
   141  						},
   142  
   143  						"internal_address": &schema.Schema{
   144  							Type:     schema.TypeString,
   145  							Computed: true,
   146  						},
   147  
   148  						"external_address": &schema.Schema{
   149  							Type:     schema.TypeString,
   150  							Computed: true,
   151  						},
   152  					},
   153  				},
   154  			},
   155  
   156  			"can_ip_forward": &schema.Schema{
   157  				Type:     schema.TypeBool,
   158  				Optional: true,
   159  				Default:  false,
   160  				ForceNew: true,
   161  			},
   162  
   163  			"metadata": &schema.Schema{
   164  				Type:     schema.TypeList,
   165  				Optional: true,
   166  				Elem: &schema.Schema{
   167  					Type: schema.TypeMap,
   168  				},
   169  			},
   170  
   171  			"service_account": &schema.Schema{
   172  				Type:     schema.TypeList,
   173  				Optional: true,
   174  				ForceNew: true,
   175  				Elem: &schema.Resource{
   176  					Schema: map[string]*schema.Schema{
   177  						"email": &schema.Schema{
   178  							Type:     schema.TypeString,
   179  							Computed: true,
   180  							ForceNew: true,
   181  						},
   182  
   183  						"scopes": &schema.Schema{
   184  							Type:     schema.TypeList,
   185  							Required: true,
   186  							ForceNew: true,
   187  							Elem: &schema.Schema{
   188  								Type: schema.TypeString,
   189  								StateFunc: func(v interface{}) string {
   190  									return canonicalizeServiceScope(v.(string))
   191  								},
   192  							},
   193  						},
   194  					},
   195  				},
   196  			},
   197  
   198  			"tags": &schema.Schema{
   199  				Type:     schema.TypeSet,
   200  				Optional: true,
   201  				Elem:     &schema.Schema{Type: schema.TypeString},
   202  				Set: func(v interface{}) int {
   203  					return hashcode.String(v.(string))
   204  				},
   205  			},
   206  
   207  			"metadata_fingerprint": &schema.Schema{
   208  				Type:     schema.TypeString,
   209  				Computed: true,
   210  			},
   211  
   212  			"tags_fingerprint": &schema.Schema{
   213  				Type:     schema.TypeString,
   214  				Computed: true,
   215  			},
   216  
   217  			"self_link": &schema.Schema{
   218  				Type:     schema.TypeString,
   219  				Computed: true,
   220  			},
   221  		},
   222  	}
   223  }
   224  
   225  func resourceOperationWaitZone(
   226  	config *Config, op *compute.Operation, zone string, activity string) error {
   227  
   228  	w := &OperationWaiter{
   229  		Service: config.clientCompute,
   230  		Op:      op,
   231  		Project: config.Project,
   232  		Zone:    zone,
   233  		Type:    OperationWaitZone,
   234  	}
   235  	state := w.Conf()
   236  	state.Delay = 10 * time.Second
   237  	state.Timeout = 10 * time.Minute
   238  	state.MinTimeout = 2 * time.Second
   239  	opRaw, err := state.WaitForState()
   240  	if err != nil {
   241  		return fmt.Errorf("Error waiting for %s: %s", activity, err)
   242  	}
   243  	op = opRaw.(*compute.Operation)
   244  	if op.Error != nil {
   245  		// Return the error
   246  		return OperationError(*op.Error)
   247  	}
   248  	return nil
   249  }
   250  
   251  func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) error {
   252  	config := meta.(*Config)
   253  
   254  	// Get the zone
   255  	log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string))
   256  	zone, err := config.clientCompute.Zones.Get(
   257  		config.Project, d.Get("zone").(string)).Do()
   258  	if err != nil {
   259  		return fmt.Errorf(
   260  			"Error loading zone '%s': %s", d.Get("zone").(string), err)
   261  	}
   262  
   263  	// Get the machine type
   264  	log.Printf("[DEBUG] Loading machine type: %s", d.Get("machine_type").(string))
   265  	machineType, err := config.clientCompute.MachineTypes.Get(
   266  		config.Project, zone.Name, d.Get("machine_type").(string)).Do()
   267  	if err != nil {
   268  		return fmt.Errorf(
   269  			"Error loading machine type: %s",
   270  			err)
   271  	}
   272  
   273  	// Build up the list of disks
   274  	disksCount := d.Get("disk.#").(int)
   275  	disks := make([]*compute.AttachedDisk, 0, disksCount)
   276  	for i := 0; i < disksCount; i++ {
   277  		prefix := fmt.Sprintf("disk.%d", i)
   278  
   279  		// var sourceLink string
   280  
   281  		// Build the disk
   282  		var disk compute.AttachedDisk
   283  		disk.Type = "PERSISTENT"
   284  		disk.Mode = "READ_WRITE"
   285  		disk.Boot = i == 0
   286  		disk.AutoDelete = true
   287  
   288  		if v, ok := d.GetOk(prefix + ".auto_delete"); ok {
   289  			disk.AutoDelete = v.(bool)
   290  		}
   291  
   292  		// Load up the disk for this disk if specified
   293  		if v, ok := d.GetOk(prefix + ".disk"); ok {
   294  			diskName := v.(string)
   295  			diskData, err := config.clientCompute.Disks.Get(
   296  				config.Project, zone.Name, diskName).Do()
   297  			if err != nil {
   298  				return fmt.Errorf(
   299  					"Error loading disk '%s': %s",
   300  					diskName, err)
   301  			}
   302  
   303  			disk.Source = diskData.SelfLink
   304  		}
   305  
   306  		// Load up the image for this disk if specified
   307  		if v, ok := d.GetOk(prefix + ".image"); ok {
   308  			imageName := v.(string)
   309  
   310  			imageUrl, err := resolveImage(config, imageName)
   311  			if err != nil {
   312  				return fmt.Errorf(
   313  					"Error resolving image name '%s': %s",
   314  					imageName, err)
   315  			}
   316  
   317  			disk.InitializeParams = &compute.AttachedDiskInitializeParams{
   318  				SourceImage: imageUrl,
   319  			}
   320  		}
   321  
   322  		if v, ok := d.GetOk(prefix + ".type"); ok {
   323  			diskTypeName := v.(string)
   324  			diskType, err := readDiskType(config, zone, diskTypeName)
   325  			if err != nil {
   326  				return fmt.Errorf(
   327  					"Error loading disk type '%s': %s",
   328  					diskTypeName, err)
   329  			}
   330  
   331  			disk.InitializeParams.DiskType = diskType.SelfLink
   332  		}
   333  
   334  		disks = append(disks, &disk)
   335  	}
   336  
   337  	networksCount := d.Get("network.#").(int)
   338  	networkInterfacesCount := d.Get("network_interface.#").(int)
   339  
   340  	if networksCount > 0 && networkInterfacesCount > 0 {
   341  		return fmt.Errorf("Error: cannot define both networks and network_interfaces.")
   342  	}
   343  	if networksCount == 0 && networkInterfacesCount == 0 {
   344  		return fmt.Errorf("Error: Must define at least one network_interface.")
   345  	}
   346  
   347  	var networkInterfaces []*compute.NetworkInterface
   348  
   349  	if networksCount > 0 {
   350  		// TODO: Delete this block when removing network { }
   351  		// Build up the list of networkInterfaces
   352  		networkInterfaces = make([]*compute.NetworkInterface, 0, networksCount)
   353  		for i := 0; i < networksCount; i++ {
   354  			prefix := fmt.Sprintf("network.%d", i)
   355  			// Load up the name of this network
   356  			networkName := d.Get(prefix + ".source").(string)
   357  			network, err := config.clientCompute.Networks.Get(
   358  				config.Project, networkName).Do()
   359  			if err != nil {
   360  				return fmt.Errorf(
   361  					"Error loading network '%s': %s",
   362  					networkName, err)
   363  			}
   364  
   365  			// Build the networkInterface
   366  			var iface compute.NetworkInterface
   367  			iface.AccessConfigs = []*compute.AccessConfig{
   368  				&compute.AccessConfig{
   369  					Type:  "ONE_TO_ONE_NAT",
   370  					NatIP: d.Get(prefix + ".address").(string),
   371  				},
   372  			}
   373  			iface.Network = network.SelfLink
   374  
   375  			networkInterfaces = append(networkInterfaces, &iface)
   376  		}
   377  	}
   378  
   379  	if networkInterfacesCount > 0 {
   380  		// Build up the list of networkInterfaces
   381  		networkInterfaces = make([]*compute.NetworkInterface, 0, networkInterfacesCount)
   382  		for i := 0; i < networkInterfacesCount; i++ {
   383  			prefix := fmt.Sprintf("network_interface.%d", i)
   384  			// Load up the name of this network_interfac
   385  			networkName := d.Get(prefix + ".network").(string)
   386  			network, err := config.clientCompute.Networks.Get(
   387  				config.Project, networkName).Do()
   388  			if err != nil {
   389  				return fmt.Errorf(
   390  					"Error referencing network '%s': %s",
   391  					networkName, err)
   392  			}
   393  
   394  			// Build the networkInterface
   395  			var iface compute.NetworkInterface
   396  			iface.Network = network.SelfLink
   397  
   398  			// Handle access_config structs
   399  			accessConfigsCount := d.Get(prefix + ".access_config.#").(int)
   400  			iface.AccessConfigs = make([]*compute.AccessConfig, accessConfigsCount)
   401  			for j := 0; j < accessConfigsCount; j++ {
   402  				acPrefix := fmt.Sprintf("%s.access_config.%d", prefix, j)
   403  				iface.AccessConfigs[j] = &compute.AccessConfig{
   404  					Type:  "ONE_TO_ONE_NAT",
   405  					NatIP: d.Get(acPrefix + ".nat_ip").(string),
   406  				}
   407  			}
   408  
   409  			networkInterfaces = append(networkInterfaces, &iface)
   410  		}
   411  	}
   412  
   413  	serviceAccountsCount := d.Get("service_account.#").(int)
   414  	serviceAccounts := make([]*compute.ServiceAccount, 0, serviceAccountsCount)
   415  	for i := 0; i < serviceAccountsCount; i++ {
   416  		prefix := fmt.Sprintf("service_account.%d", i)
   417  
   418  		scopesCount := d.Get(prefix + ".scopes.#").(int)
   419  		scopes := make([]string, 0, scopesCount)
   420  		for j := 0; j < scopesCount; j++ {
   421  			scope := d.Get(fmt.Sprintf(prefix+".scopes.%d", j)).(string)
   422  			scopes = append(scopes, canonicalizeServiceScope(scope))
   423  		}
   424  
   425  		serviceAccount := &compute.ServiceAccount{
   426  			Email:  "default",
   427  			Scopes: scopes,
   428  		}
   429  
   430  		serviceAccounts = append(serviceAccounts, serviceAccount)
   431  	}
   432  
   433  	// Create the instance information
   434  	instance := compute.Instance{
   435  		CanIpForward:      d.Get("can_ip_forward").(bool),
   436  		Description:       d.Get("description").(string),
   437  		Disks:             disks,
   438  		MachineType:       machineType.SelfLink,
   439  		Metadata:          resourceInstanceMetadata(d),
   440  		Name:              d.Get("name").(string),
   441  		NetworkInterfaces: networkInterfaces,
   442  		Tags:              resourceInstanceTags(d),
   443  		ServiceAccounts:   serviceAccounts,
   444  	}
   445  
   446  	log.Printf("[INFO] Requesting instance creation")
   447  	op, err := config.clientCompute.Instances.Insert(
   448  		config.Project, zone.Name, &instance).Do()
   449  	if err != nil {
   450  		return fmt.Errorf("Error creating instance: %s", err)
   451  	}
   452  
   453  	// Store the ID now
   454  	d.SetId(instance.Name)
   455  
   456  	// Wait for the operation to complete
   457  	waitErr := resourceOperationWaitZone(config, op, zone.Name, "instance to create")
   458  	if waitErr != nil {
   459  		// The resource didn't actually create
   460  		d.SetId("")
   461  		return waitErr
   462  	}
   463  
   464  	return resourceComputeInstanceRead(d, meta)
   465  }
   466  
   467  func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error {
   468  	config := meta.(*Config)
   469  
   470  	instance, err := config.clientCompute.Instances.Get(
   471  		config.Project, d.Get("zone").(string), d.Id()).Do()
   472  	if err != nil {
   473  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   474  			// The resource doesn't exist anymore
   475  			d.SetId("")
   476  
   477  			return nil
   478  		}
   479  
   480  		return fmt.Errorf("Error reading instance: %s", err)
   481  	}
   482  
   483  	d.Set("can_ip_forward", instance.CanIpForward)
   484  
   485  	// Set the service accounts
   486  	for i, serviceAccount := range instance.ServiceAccounts {
   487  		prefix := fmt.Sprintf("service_account.%d", i)
   488  		d.Set(prefix+".email", serviceAccount.Email)
   489  		d.Set(prefix+".scopes.#", len(serviceAccount.Scopes))
   490  		for j, scope := range serviceAccount.Scopes {
   491  			d.Set(fmt.Sprintf("%s.scopes.%d", prefix, j), scope)
   492  		}
   493  	}
   494  
   495  	networksCount := d.Get("network.#").(int)
   496  	networkInterfacesCount := d.Get("network_interface.#").(int)
   497  
   498  	if networksCount > 0 && networkInterfacesCount > 0 {
   499  		return fmt.Errorf("Error: cannot define both networks and network_interfaces.")
   500  	}
   501  	if networksCount == 0 && networkInterfacesCount == 0 {
   502  		return fmt.Errorf("Error: Must define at least one network_interface.")
   503  	}
   504  
   505  	// Set the networks
   506  	// Use the first external IP found for the default connection info.
   507  	externalIP := ""
   508  	internalIP := ""
   509  	if networksCount > 0 {
   510  		// TODO: Remove this when realizing deprecation of .network
   511  		for i, iface := range instance.NetworkInterfaces {
   512  			prefix := fmt.Sprintf("network.%d", i)
   513  			d.Set(prefix+".name", iface.Name)
   514  			log.Printf(prefix+".name = %s", iface.Name)
   515  
   516  			var natIP string
   517  			for _, config := range iface.AccessConfigs {
   518  				if config.Type == "ONE_TO_ONE_NAT" {
   519  					natIP = config.NatIP
   520  					break
   521  				}
   522  			}
   523  
   524  			if externalIP == "" && natIP != "" {
   525  				externalIP = natIP
   526  			}
   527  			d.Set(prefix+".external_address", natIP)
   528  
   529  			d.Set(prefix+".internal_address", iface.NetworkIP)
   530  		}
   531  	}
   532  
   533  	if networkInterfacesCount > 0 {
   534  		for i, iface := range instance.NetworkInterfaces {
   535  
   536  			prefix := fmt.Sprintf("network_interface.%d", i)
   537  			d.Set(prefix+".name", iface.Name)
   538  
   539  			// The first non-empty ip is left in natIP
   540  			var natIP string
   541  			for j, config := range iface.AccessConfigs {
   542  				acPrefix := fmt.Sprintf("%s.access_config.%d", prefix, j)
   543  				d.Set(acPrefix+".nat_ip", config.NatIP)
   544  				if natIP == "" {
   545  					natIP = config.NatIP
   546  				}
   547  			}
   548  
   549  			if externalIP == "" {
   550  				externalIP = natIP
   551  			}
   552  
   553  			d.Set(prefix+".address", iface.NetworkIP)
   554  			if internalIP == "" {
   555  				internalIP = iface.NetworkIP
   556  			}
   557  
   558  		}
   559  	}
   560  
   561  	// Fall back on internal ip if there is no external ip.  This makes sense in the situation where
   562  	// terraform is being used on a cloud instance and can therefore access the instances it creates
   563  	// via their internal ips.
   564  	sshIP := externalIP
   565  	if sshIP == "" {
   566  		sshIP = internalIP
   567  	}
   568  
   569  	// Initialize the connection info
   570  	d.SetConnInfo(map[string]string{
   571  		"type": "ssh",
   572  		"host": sshIP,
   573  	})
   574  
   575  	// Set the metadata fingerprint if there is one.
   576  	if instance.Metadata != nil {
   577  		d.Set("metadata_fingerprint", instance.Metadata.Fingerprint)
   578  	}
   579  
   580  	// Set the tags fingerprint if there is one.
   581  	if instance.Tags != nil {
   582  		d.Set("tags_fingerprint", instance.Tags.Fingerprint)
   583  	}
   584  
   585  	d.Set("self_link", instance.SelfLink)
   586  
   587  	return nil
   588  }
   589  
   590  func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
   591  	config := meta.(*Config)
   592  
   593  	zone := d.Get("zone").(string)
   594  
   595  	instance, err := config.clientCompute.Instances.Get(
   596  		config.Project, zone, d.Id()).Do()
   597  	if err != nil {
   598  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   599  			// The resource doesn't exist anymore
   600  			d.SetId("")
   601  
   602  			return nil
   603  		}
   604  
   605  		return fmt.Errorf("Error reading instance: %s", err)
   606  	}
   607  
   608  	// Enable partial mode for the resource since it is possible
   609  	d.Partial(true)
   610  
   611  	// If the Metadata has changed, then update that.
   612  	if d.HasChange("metadata") {
   613  		metadata := resourceInstanceMetadata(d)
   614  		op, err := config.clientCompute.Instances.SetMetadata(
   615  			config.Project, zone, d.Id(), metadata).Do()
   616  		if err != nil {
   617  			return fmt.Errorf("Error updating metadata: %s", err)
   618  		}
   619  
   620  		// 1 5 2
   621  		opErr := resourceOperationWaitZone(config, op, zone, "metadata to update")
   622  		if opErr != nil {
   623  			return opErr
   624  		}
   625  
   626  		d.SetPartial("metadata")
   627  	}
   628  
   629  	if d.HasChange("tags") {
   630  		tags := resourceInstanceTags(d)
   631  		op, err := config.clientCompute.Instances.SetTags(
   632  			config.Project, zone, d.Id(), tags).Do()
   633  		if err != nil {
   634  			return fmt.Errorf("Error updating tags: %s", err)
   635  		}
   636  
   637  		opErr := resourceOperationWaitZone(config, op, zone, "tags to update")
   638  		if opErr != nil {
   639  			return opErr
   640  		}
   641  
   642  		d.SetPartial("tags")
   643  	}
   644  
   645  	networkInterfacesCount := d.Get("network_interface.#").(int)
   646  	if networkInterfacesCount > 0 {
   647  		// Sanity check
   648  		if networkInterfacesCount != len(instance.NetworkInterfaces) {
   649  			return fmt.Errorf("Instance had unexpected number of network interfaces: %d", len(instance.NetworkInterfaces))
   650  		}
   651  		for i := 0; i < networkInterfacesCount; i++ {
   652  			prefix := fmt.Sprintf("network_interface.%d", i)
   653  			instNetworkInterface := instance.NetworkInterfaces[i]
   654  			networkName := d.Get(prefix + ".name").(string)
   655  
   656  			// TODO: This sanity check is broken by #929, disabled for now (by forcing the equality)
   657  			networkName = instNetworkInterface.Name
   658  			// Sanity check
   659  			if networkName != instNetworkInterface.Name {
   660  				return fmt.Errorf("Instance networkInterface had unexpected name: %s", instNetworkInterface.Name)
   661  			}
   662  
   663  			if d.HasChange(prefix + ".access_config") {
   664  
   665  				// TODO: This code deletes then recreates accessConfigs.  This is bad because it may
   666  				// leave the machine inaccessible from either ip if the creation part fails (network
   667  				// timeout etc).  However right now there is a GCE limit of 1 accessConfig so it is
   668  				// the only way to do it.  In future this should be revised to only change what is
   669  				// necessary, and also add before removing.
   670  
   671  				// Delete any accessConfig that currently exists in instNetworkInterface
   672  				for _, ac := range instNetworkInterface.AccessConfigs {
   673  					op, err := config.clientCompute.Instances.DeleteAccessConfig(
   674  						config.Project, zone, d.Id(), ac.Name, networkName).Do()
   675  					if err != nil {
   676  						return fmt.Errorf("Error deleting old access_config: %s", err)
   677  					}
   678  					opErr := resourceOperationWaitZone(config, op, zone, "old access_config to delete")
   679  					if opErr != nil {
   680  						return opErr
   681  					}
   682  				}
   683  
   684  				// Create new ones
   685  				accessConfigsCount := d.Get(prefix + ".access_config.#").(int)
   686  				for j := 0; j < accessConfigsCount; j++ {
   687  					acPrefix := fmt.Sprintf("%s.access_config.%d", prefix, j)
   688  					ac := &compute.AccessConfig{
   689  						Type:  "ONE_TO_ONE_NAT",
   690  						NatIP: d.Get(acPrefix + ".nat_ip").(string),
   691  					}
   692  					op, err := config.clientCompute.Instances.AddAccessConfig(
   693  						config.Project, zone, d.Id(), networkName, ac).Do()
   694  					if err != nil {
   695  						return fmt.Errorf("Error adding new access_config: %s", err)
   696  					}
   697  					opErr := resourceOperationWaitZone(config, op, zone, "new access_config to add")
   698  					if opErr != nil {
   699  						return opErr
   700  					}
   701  				}
   702  			}
   703  		}
   704  	}
   705  
   706  	// We made it, disable partial mode
   707  	d.Partial(false)
   708  
   709  	return resourceComputeInstanceRead(d, meta)
   710  }
   711  
   712  func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) error {
   713  	config := meta.(*Config)
   714  
   715  	zone := d.Get("zone").(string)
   716  	op, err := config.clientCompute.Instances.Delete(config.Project, zone, d.Id()).Do()
   717  	if err != nil {
   718  		return fmt.Errorf("Error deleting instance: %s", err)
   719  	}
   720  
   721  	// Wait for the operation to complete
   722  	opErr := resourceOperationWaitZone(config, op, zone, "instance to delete")
   723  	if opErr != nil {
   724  		return opErr
   725  	}
   726  
   727  	d.SetId("")
   728  	return nil
   729  }
   730  
   731  func resourceInstanceMetadata(d *schema.ResourceData) *compute.Metadata {
   732  	var metadata *compute.Metadata
   733  	if metadataList := d.Get("metadata").([]interface{}); len(metadataList) > 0 {
   734  		m := new(compute.Metadata)
   735  		m.Items = make([]*compute.MetadataItems, 0, len(metadataList))
   736  		for _, metadataMap := range metadataList {
   737  			for key, val := range metadataMap.(map[string]interface{}) {
   738  				// TODO: fix https://github.com/hashicorp/terraform/issues/883
   739  				//       and remove this workaround <3 phinze
   740  				if key == "#" {
   741  					continue
   742  				}
   743  				m.Items = append(m.Items, &compute.MetadataItems{
   744  					Key:   key,
   745  					Value: val.(string),
   746  				})
   747  			}
   748  		}
   749  
   750  		// Set the fingerprint. If the metadata has never been set before
   751  		// then this will just be blank.
   752  		m.Fingerprint = d.Get("metadata_fingerprint").(string)
   753  
   754  		metadata = m
   755  	}
   756  
   757  	return metadata
   758  }
   759  
   760  func resourceInstanceTags(d *schema.ResourceData) *compute.Tags {
   761  	// Calculate the tags
   762  	var tags *compute.Tags
   763  	if v := d.Get("tags"); v != nil {
   764  		vs := v.(*schema.Set)
   765  		tags = new(compute.Tags)
   766  		tags.Items = make([]string, vs.Len())
   767  		for i, v := range vs.List() {
   768  			tags.Items[i] = v.(string)
   769  		}
   770  
   771  		tags.Fingerprint = d.Get("tags_fingerprint").(string)
   772  	}
   773  
   774  	return tags
   775  }