github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/google/resource_compute_instance_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"google.golang.org/api/compute/v1"
    12  )
    13  
    14  func TestAccComputeInstance_basic_deprecated_network(t *testing.T) {
    15  	var instance compute.Instance
    16  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccComputeInstance_basic_deprecated_network(instanceName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckComputeInstanceExists(
    27  						"google_compute_instance.foobar", &instance),
    28  					testAccCheckComputeInstanceTag(&instance, "foo"),
    29  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    30  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccComputeInstance_basic1(t *testing.T) {
    38  	var instance compute.Instance
    39  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testAccComputeInstance_basic(instanceName),
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckComputeInstanceExists(
    50  						"google_compute_instance.foobar", &instance),
    51  					testAccCheckComputeInstanceTag(&instance, "foo"),
    52  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    53  					testAccCheckComputeInstanceMetadata(&instance, "baz", "qux"),
    54  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccComputeInstance_basic2(t *testing.T) {
    62  	var instance compute.Instance
    63  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
    64  
    65  	resource.Test(t, resource.TestCase{
    66  		PreCheck:     func() { testAccPreCheck(t) },
    67  		Providers:    testAccProviders,
    68  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    69  		Steps: []resource.TestStep{
    70  			resource.TestStep{
    71  				Config: testAccComputeInstance_basic2(instanceName),
    72  				Check: resource.ComposeTestCheckFunc(
    73  					testAccCheckComputeInstanceExists(
    74  						"google_compute_instance.foobar", &instance),
    75  					testAccCheckComputeInstanceTag(&instance, "foo"),
    76  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    77  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccComputeInstance_basic3(t *testing.T) {
    85  	var instance compute.Instance
    86  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
    87  
    88  	resource.Test(t, resource.TestCase{
    89  		PreCheck:     func() { testAccPreCheck(t) },
    90  		Providers:    testAccProviders,
    91  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    92  		Steps: []resource.TestStep{
    93  			resource.TestStep{
    94  				Config: testAccComputeInstance_basic3(instanceName),
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testAccCheckComputeInstanceExists(
    97  						"google_compute_instance.foobar", &instance),
    98  					testAccCheckComputeInstanceTag(&instance, "foo"),
    99  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
   100  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   101  				),
   102  			},
   103  		},
   104  	})
   105  }
   106  
   107  func TestAccComputeInstance_basic4(t *testing.T) {
   108  	var instance compute.Instance
   109  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   110  
   111  	resource.Test(t, resource.TestCase{
   112  		PreCheck:     func() { testAccPreCheck(t) },
   113  		Providers:    testAccProviders,
   114  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   115  		Steps: []resource.TestStep{
   116  			resource.TestStep{
   117  				Config: testAccComputeInstance_basic4(instanceName),
   118  				Check: resource.ComposeTestCheckFunc(
   119  					testAccCheckComputeInstanceExists(
   120  						"google_compute_instance.foobar", &instance),
   121  					testAccCheckComputeInstanceTag(&instance, "foo"),
   122  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
   123  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   124  				),
   125  			},
   126  		},
   127  	})
   128  }
   129  
   130  func TestAccComputeInstance_basic5(t *testing.T) {
   131  	var instance compute.Instance
   132  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   133  
   134  	resource.Test(t, resource.TestCase{
   135  		PreCheck:     func() { testAccPreCheck(t) },
   136  		Providers:    testAccProviders,
   137  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   138  		Steps: []resource.TestStep{
   139  			resource.TestStep{
   140  				Config: testAccComputeInstance_basic5(instanceName),
   141  				Check: resource.ComposeTestCheckFunc(
   142  					testAccCheckComputeInstanceExists(
   143  						"google_compute_instance.foobar", &instance),
   144  					testAccCheckComputeInstanceTag(&instance, "foo"),
   145  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
   146  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   147  				),
   148  			},
   149  		},
   150  	})
   151  }
   152  
   153  func TestAccComputeInstance_IP(t *testing.T) {
   154  	var instance compute.Instance
   155  	var ipName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   156  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   157  
   158  	resource.Test(t, resource.TestCase{
   159  		PreCheck:     func() { testAccPreCheck(t) },
   160  		Providers:    testAccProviders,
   161  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   162  		Steps: []resource.TestStep{
   163  			resource.TestStep{
   164  				Config: testAccComputeInstance_ip(ipName, instanceName),
   165  				Check: resource.ComposeTestCheckFunc(
   166  					testAccCheckComputeInstanceExists(
   167  						"google_compute_instance.foobar", &instance),
   168  					testAccCheckComputeInstanceAccessConfigHasIP(&instance),
   169  				),
   170  			},
   171  		},
   172  	})
   173  }
   174  
   175  func TestAccComputeInstance_disksWithoutAutodelete(t *testing.T) {
   176  	var instance compute.Instance
   177  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   178  	var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
   179  
   180  	resource.Test(t, resource.TestCase{
   181  		PreCheck:     func() { testAccPreCheck(t) },
   182  		Providers:    testAccProviders,
   183  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   184  		Steps: []resource.TestStep{
   185  			resource.TestStep{
   186  				Config: testAccComputeInstance_disks(diskName, instanceName, false),
   187  				Check: resource.ComposeTestCheckFunc(
   188  					testAccCheckComputeInstanceExists(
   189  						"google_compute_instance.foobar", &instance),
   190  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   191  					testAccCheckComputeInstanceDisk(&instance, diskName, false, false),
   192  				),
   193  			},
   194  		},
   195  	})
   196  }
   197  
   198  func TestAccComputeInstance_disksWithAutodelete(t *testing.T) {
   199  	var instance compute.Instance
   200  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   201  	var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
   202  
   203  	resource.Test(t, resource.TestCase{
   204  		PreCheck:     func() { testAccPreCheck(t) },
   205  		Providers:    testAccProviders,
   206  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   207  		Steps: []resource.TestStep{
   208  			resource.TestStep{
   209  				Config: testAccComputeInstance_disks(diskName, instanceName, true),
   210  				Check: resource.ComposeTestCheckFunc(
   211  					testAccCheckComputeInstanceExists(
   212  						"google_compute_instance.foobar", &instance),
   213  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   214  					testAccCheckComputeInstanceDisk(&instance, diskName, true, false),
   215  				),
   216  			},
   217  		},
   218  	})
   219  }
   220  
   221  func TestAccComputeInstance_local_ssd(t *testing.T) {
   222  	var instance compute.Instance
   223  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   224  
   225  	resource.Test(t, resource.TestCase{
   226  		PreCheck:     func() { testAccPreCheck(t) },
   227  		Providers:    testAccProviders,
   228  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   229  		Steps: []resource.TestStep{
   230  			resource.TestStep{
   231  				Config: testAccComputeInstance_local_ssd(instanceName),
   232  				Check: resource.ComposeTestCheckFunc(
   233  					testAccCheckComputeInstanceExists(
   234  						"google_compute_instance.local-ssd", &instance),
   235  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   236  				),
   237  			},
   238  		},
   239  	})
   240  }
   241  
   242  func TestAccComputeInstance_update_deprecated_network(t *testing.T) {
   243  	var instance compute.Instance
   244  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   245  
   246  	resource.Test(t, resource.TestCase{
   247  		PreCheck:     func() { testAccPreCheck(t) },
   248  		Providers:    testAccProviders,
   249  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   250  		Steps: []resource.TestStep{
   251  			resource.TestStep{
   252  				Config: testAccComputeInstance_basic_deprecated_network(instanceName),
   253  				Check: resource.ComposeTestCheckFunc(
   254  					testAccCheckComputeInstanceExists(
   255  						"google_compute_instance.foobar", &instance),
   256  				),
   257  			},
   258  			resource.TestStep{
   259  				Config: testAccComputeInstance_update_deprecated_network(instanceName),
   260  				Check: resource.ComposeTestCheckFunc(
   261  					testAccCheckComputeInstanceExists(
   262  						"google_compute_instance.foobar", &instance),
   263  					testAccCheckComputeInstanceMetadata(
   264  						&instance, "bar", "baz"),
   265  					testAccCheckComputeInstanceTag(&instance, "baz"),
   266  				),
   267  			},
   268  		},
   269  	})
   270  }
   271  
   272  func TestAccComputeInstance_forceNewAndChangeMetadata(t *testing.T) {
   273  	var instance compute.Instance
   274  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   275  
   276  	resource.Test(t, resource.TestCase{
   277  		PreCheck:     func() { testAccPreCheck(t) },
   278  		Providers:    testAccProviders,
   279  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   280  		Steps: []resource.TestStep{
   281  			resource.TestStep{
   282  				Config: testAccComputeInstance_basic(instanceName),
   283  				Check: resource.ComposeTestCheckFunc(
   284  					testAccCheckComputeInstanceExists(
   285  						"google_compute_instance.foobar", &instance),
   286  				),
   287  			},
   288  			resource.TestStep{
   289  				Config: testAccComputeInstance_forceNewAndChangeMetadata(instanceName),
   290  				Check: resource.ComposeTestCheckFunc(
   291  					testAccCheckComputeInstanceExists(
   292  						"google_compute_instance.foobar", &instance),
   293  					testAccCheckComputeInstanceMetadata(
   294  						&instance, "qux", "true"),
   295  				),
   296  			},
   297  		},
   298  	})
   299  }
   300  
   301  func TestAccComputeInstance_update(t *testing.T) {
   302  	var instance compute.Instance
   303  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   304  
   305  	resource.Test(t, resource.TestCase{
   306  		PreCheck:     func() { testAccPreCheck(t) },
   307  		Providers:    testAccProviders,
   308  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   309  		Steps: []resource.TestStep{
   310  			resource.TestStep{
   311  				Config: testAccComputeInstance_basic(instanceName),
   312  				Check: resource.ComposeTestCheckFunc(
   313  					testAccCheckComputeInstanceExists(
   314  						"google_compute_instance.foobar", &instance),
   315  				),
   316  			},
   317  			resource.TestStep{
   318  				Config: testAccComputeInstance_update(instanceName),
   319  				Check: resource.ComposeTestCheckFunc(
   320  					testAccCheckComputeInstanceExists(
   321  						"google_compute_instance.foobar", &instance),
   322  					testAccCheckComputeInstanceMetadata(
   323  						&instance, "bar", "baz"),
   324  					testAccCheckComputeInstanceTag(&instance, "baz"),
   325  					testAccCheckComputeInstanceAccessConfig(&instance),
   326  				),
   327  			},
   328  		},
   329  	})
   330  }
   331  
   332  func TestAccComputeInstance_service_account(t *testing.T) {
   333  	var instance compute.Instance
   334  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   335  
   336  	resource.Test(t, resource.TestCase{
   337  		PreCheck:     func() { testAccPreCheck(t) },
   338  		Providers:    testAccProviders,
   339  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   340  		Steps: []resource.TestStep{
   341  			resource.TestStep{
   342  				Config: testAccComputeInstance_service_account(instanceName),
   343  				Check: resource.ComposeTestCheckFunc(
   344  					testAccCheckComputeInstanceExists(
   345  						"google_compute_instance.foobar", &instance),
   346  					testAccCheckComputeInstanceServiceAccount(&instance,
   347  						"https://www.googleapis.com/auth/compute.readonly"),
   348  					testAccCheckComputeInstanceServiceAccount(&instance,
   349  						"https://www.googleapis.com/auth/devstorage.read_only"),
   350  					testAccCheckComputeInstanceServiceAccount(&instance,
   351  						"https://www.googleapis.com/auth/userinfo.email"),
   352  				),
   353  			},
   354  		},
   355  	})
   356  }
   357  
   358  func TestAccComputeInstance_scheduling(t *testing.T) {
   359  	var instance compute.Instance
   360  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   361  
   362  	resource.Test(t, resource.TestCase{
   363  		PreCheck:     func() { testAccPreCheck(t) },
   364  		Providers:    testAccProviders,
   365  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   366  		Steps: []resource.TestStep{
   367  			resource.TestStep{
   368  				Config: testAccComputeInstance_scheduling(instanceName),
   369  				Check: resource.ComposeTestCheckFunc(
   370  					testAccCheckComputeInstanceExists(
   371  						"google_compute_instance.foobar", &instance),
   372  				),
   373  			},
   374  		},
   375  	})
   376  }
   377  
   378  func TestAccComputeInstance_subnet_auto(t *testing.T) {
   379  	var instance compute.Instance
   380  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   381  
   382  	resource.Test(t, resource.TestCase{
   383  		PreCheck:     func() { testAccPreCheck(t) },
   384  		Providers:    testAccProviders,
   385  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   386  		Steps: []resource.TestStep{
   387  			resource.TestStep{
   388  				Config: testAccComputeInstance_subnet_auto(instanceName),
   389  				Check: resource.ComposeTestCheckFunc(
   390  					testAccCheckComputeInstanceExists(
   391  						"google_compute_instance.foobar", &instance),
   392  					testAccCheckComputeInstanceHasSubnet(&instance),
   393  				),
   394  			},
   395  		},
   396  	})
   397  }
   398  
   399  func TestAccComputeInstance_subnet_custom(t *testing.T) {
   400  	var instance compute.Instance
   401  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   402  
   403  	resource.Test(t, resource.TestCase{
   404  		PreCheck:     func() { testAccPreCheck(t) },
   405  		Providers:    testAccProviders,
   406  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   407  		Steps: []resource.TestStep{
   408  			resource.TestStep{
   409  				Config: testAccComputeInstance_subnet_custom(instanceName),
   410  				Check: resource.ComposeTestCheckFunc(
   411  					testAccCheckComputeInstanceExists(
   412  						"google_compute_instance.foobar", &instance),
   413  					testAccCheckComputeInstanceHasSubnet(&instance),
   414  				),
   415  			},
   416  		},
   417  	})
   418  }
   419  
   420  func TestAccComputeInstance_address_auto(t *testing.T) {
   421  	var instance compute.Instance
   422  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   423  
   424  	resource.Test(t, resource.TestCase{
   425  		PreCheck:     func() { testAccPreCheck(t) },
   426  		Providers:    testAccProviders,
   427  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   428  		Steps: []resource.TestStep{
   429  			resource.TestStep{
   430  				Config: testAccComputeInstance_address_auto(instanceName),
   431  				Check: resource.ComposeTestCheckFunc(
   432  					testAccCheckComputeInstanceExists(
   433  						"google_compute_instance.foobar", &instance),
   434  					testAccCheckComputeInstanceHasAnyAddress(&instance),
   435  				),
   436  			},
   437  		},
   438  	})
   439  }
   440  
   441  func TestAccComputeInstance_address_custom(t *testing.T) {
   442  	var instance compute.Instance
   443  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   444  	var address = "10.0.200.200"
   445  	resource.Test(t, resource.TestCase{
   446  		PreCheck:     func() { testAccPreCheck(t) },
   447  		Providers:    testAccProviders,
   448  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   449  		Steps: []resource.TestStep{
   450  			resource.TestStep{
   451  				Config: testAccComputeInstance_address_custom(instanceName, address),
   452  				Check: resource.ComposeTestCheckFunc(
   453  					testAccCheckComputeInstanceExists(
   454  						"google_compute_instance.foobar", &instance),
   455  					testAccCheckComputeInstanceHasAddress(&instance, address),
   456  				),
   457  			},
   458  		},
   459  	})
   460  }
   461  func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
   462  	config := testAccProvider.Meta().(*Config)
   463  
   464  	for _, rs := range s.RootModule().Resources {
   465  		if rs.Type != "google_compute_instance" {
   466  			continue
   467  		}
   468  
   469  		_, err := config.clientCompute.Instances.Get(
   470  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   471  		if err == nil {
   472  			return fmt.Errorf("Instance still exists")
   473  		}
   474  	}
   475  
   476  	return nil
   477  }
   478  
   479  func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc {
   480  	return func(s *terraform.State) error {
   481  		rs, ok := s.RootModule().Resources[n]
   482  		if !ok {
   483  			return fmt.Errorf("Not found: %s", n)
   484  		}
   485  
   486  		if rs.Primary.ID == "" {
   487  			return fmt.Errorf("No ID is set")
   488  		}
   489  
   490  		config := testAccProvider.Meta().(*Config)
   491  
   492  		found, err := config.clientCompute.Instances.Get(
   493  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   494  		if err != nil {
   495  			return err
   496  		}
   497  
   498  		if found.Name != rs.Primary.ID {
   499  			return fmt.Errorf("Instance not found")
   500  		}
   501  
   502  		*instance = *found
   503  
   504  		return nil
   505  	}
   506  }
   507  
   508  func testAccCheckComputeInstanceMetadata(
   509  	instance *compute.Instance,
   510  	k string, v string) resource.TestCheckFunc {
   511  	return func(s *terraform.State) error {
   512  		if instance.Metadata == nil {
   513  			return fmt.Errorf("no metadata")
   514  		}
   515  
   516  		for _, item := range instance.Metadata.Items {
   517  			if k != item.Key {
   518  				continue
   519  			}
   520  
   521  			if item.Value != nil && v == *item.Value {
   522  				return nil
   523  			}
   524  
   525  			return fmt.Errorf("bad value for %s: %s", k, *item.Value)
   526  		}
   527  
   528  		return fmt.Errorf("metadata not found: %s", k)
   529  	}
   530  }
   531  
   532  func testAccCheckComputeInstanceAccessConfig(instance *compute.Instance) resource.TestCheckFunc {
   533  	return func(s *terraform.State) error {
   534  		for _, i := range instance.NetworkInterfaces {
   535  			if len(i.AccessConfigs) == 0 {
   536  				return fmt.Errorf("no access_config")
   537  			}
   538  		}
   539  
   540  		return nil
   541  	}
   542  }
   543  
   544  func testAccCheckComputeInstanceAccessConfigHasIP(instance *compute.Instance) resource.TestCheckFunc {
   545  	return func(s *terraform.State) error {
   546  		for _, i := range instance.NetworkInterfaces {
   547  			for _, c := range i.AccessConfigs {
   548  				if c.NatIP == "" {
   549  					return fmt.Errorf("no NAT IP")
   550  				}
   551  			}
   552  		}
   553  
   554  		return nil
   555  	}
   556  }
   557  
   558  func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
   559  	return func(s *terraform.State) error {
   560  		if instance.Disks == nil {
   561  			return fmt.Errorf("no disks")
   562  		}
   563  
   564  		for _, disk := range instance.Disks {
   565  			if strings.LastIndex(disk.Source, "/"+source) == len(disk.Source)-len(source)-1 && disk.AutoDelete == delete && disk.Boot == boot {
   566  				return nil
   567  			}
   568  		}
   569  
   570  		return fmt.Errorf("Disk not found: %s", source)
   571  	}
   572  }
   573  
   574  func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
   575  	return func(s *terraform.State) error {
   576  		if instance.Tags == nil {
   577  			return fmt.Errorf("no tags")
   578  		}
   579  
   580  		for _, k := range instance.Tags.Items {
   581  			if k == n {
   582  				return nil
   583  			}
   584  		}
   585  
   586  		return fmt.Errorf("tag not found: %s", n)
   587  	}
   588  }
   589  
   590  func testAccCheckComputeInstanceServiceAccount(instance *compute.Instance, scope string) resource.TestCheckFunc {
   591  	return func(s *terraform.State) error {
   592  		if count := len(instance.ServiceAccounts); count != 1 {
   593  			return fmt.Errorf("Wrong number of ServiceAccounts: expected 1, got %d", count)
   594  		}
   595  
   596  		for _, val := range instance.ServiceAccounts[0].Scopes {
   597  			if val == scope {
   598  				return nil
   599  			}
   600  		}
   601  
   602  		return fmt.Errorf("Scope not found: %s", scope)
   603  	}
   604  }
   605  
   606  func testAccCheckComputeInstanceHasSubnet(instance *compute.Instance) resource.TestCheckFunc {
   607  	return func(s *terraform.State) error {
   608  		for _, i := range instance.NetworkInterfaces {
   609  			if i.Subnetwork == "" {
   610  				return fmt.Errorf("no subnet")
   611  			}
   612  		}
   613  
   614  		return nil
   615  	}
   616  }
   617  
   618  func testAccCheckComputeInstanceHasAnyAddress(instance *compute.Instance) resource.TestCheckFunc {
   619  	return func(s *terraform.State) error {
   620  		for _, i := range instance.NetworkInterfaces {
   621  			if i.NetworkIP == "" {
   622  				return fmt.Errorf("no address")
   623  			}
   624  		}
   625  
   626  		return nil
   627  	}
   628  }
   629  
   630  func testAccCheckComputeInstanceHasAddress(instance *compute.Instance, address string) resource.TestCheckFunc {
   631  	return func(s *terraform.State) error {
   632  		for _, i := range instance.NetworkInterfaces {
   633  			if i.NetworkIP != address {
   634  				return fmt.Errorf("Wrong address found: expected %v, got %v", address, i.NetworkIP)
   635  			}
   636  		}
   637  
   638  		return nil
   639  	}
   640  }
   641  
   642  func testAccComputeInstance_basic_deprecated_network(instance string) string {
   643  	return fmt.Sprintf(`
   644  	resource "google_compute_instance" "foobar" {
   645  		name = "%s"
   646  		machine_type = "n1-standard-1"
   647  		zone = "us-central1-a"
   648  		can_ip_forward = false
   649  		tags = ["foo", "bar"]
   650  
   651  		disk {
   652  			image = "debian-8-jessie-v20160803"
   653  		}
   654  
   655  		network {
   656  			source = "default"
   657  		}
   658  
   659  		metadata {
   660  			foo = "bar"
   661  		}
   662  	}`, instance)
   663  }
   664  
   665  func testAccComputeInstance_update_deprecated_network(instance string) string {
   666  	return fmt.Sprintf(`
   667  	resource "google_compute_instance" "foobar" {
   668  		name = "%s"
   669  		machine_type = "n1-standard-1"
   670  		zone = "us-central1-a"
   671  		tags = ["baz"]
   672  
   673  		disk {
   674  			image = "debian-8-jessie-v20160803"
   675  		}
   676  
   677  		network {
   678  			source = "default"
   679  		}
   680  
   681  		metadata {
   682  			bar = "baz"
   683  		}
   684  	}`, instance)
   685  }
   686  
   687  func testAccComputeInstance_basic(instance string) string {
   688  	return fmt.Sprintf(`
   689  	resource "google_compute_instance" "foobar" {
   690  		name = "%s"
   691  		machine_type = "n1-standard-1"
   692  		zone = "us-central1-a"
   693  		can_ip_forward = false
   694  		tags = ["foo", "bar"]
   695  
   696  		disk {
   697  			image = "debian-8-jessie-v20160803"
   698  		}
   699  
   700  		network_interface {
   701  			network = "default"
   702  		}
   703  
   704  		metadata {
   705  			foo = "bar"
   706  			baz = "qux"
   707  		}
   708  
   709  		metadata_startup_script = "echo Hello"
   710  	}`, instance)
   711  }
   712  
   713  func testAccComputeInstance_basic2(instance string) string {
   714  	return fmt.Sprintf(`
   715  	resource "google_compute_instance" "foobar" {
   716  		name = "%s"
   717  		machine_type = "n1-standard-1"
   718  		zone = "us-central1-a"
   719  		can_ip_forward = false
   720  		tags = ["foo", "bar"]
   721  
   722  		disk {
   723  			image = "debian-8"
   724  		}
   725  
   726  		network_interface {
   727  			network = "default"
   728  		}
   729  
   730  		metadata {
   731  			foo = "bar"
   732  		}
   733  	}`, instance)
   734  }
   735  
   736  func testAccComputeInstance_basic3(instance string) string {
   737  	return fmt.Sprintf(`
   738  	resource "google_compute_instance" "foobar" {
   739  		name = "%s"
   740  		machine_type = "n1-standard-1"
   741  		zone = "us-central1-a"
   742  		can_ip_forward = false
   743  		tags = ["foo", "bar"]
   744  
   745  		disk {
   746  			image = "debian-cloud/debian-8-jessie-v20160803"
   747  		}
   748  
   749  		network_interface {
   750  			network = "default"
   751  		}
   752  
   753  
   754  		metadata {
   755  			foo = "bar"
   756  		}
   757  	}`, instance)
   758  }
   759  
   760  func testAccComputeInstance_basic4(instance string) string {
   761  	return fmt.Sprintf(`
   762  	resource "google_compute_instance" "foobar" {
   763  		name = "%s"
   764  		machine_type = "n1-standard-1"
   765  		zone = "us-central1-a"
   766  		can_ip_forward = false
   767  		tags = ["foo", "bar"]
   768  
   769  		disk {
   770  			image = "debian-cloud/debian-8"
   771  		}
   772  
   773  		network_interface {
   774  			network = "default"
   775  		}
   776  
   777  
   778  		metadata {
   779  			foo = "bar"
   780  		}
   781  	}`, instance)
   782  }
   783  
   784  func testAccComputeInstance_basic5(instance string) string {
   785  	return fmt.Sprintf(`
   786  	resource "google_compute_instance" "foobar" {
   787  		name = "%s"
   788  		machine_type = "n1-standard-1"
   789  		zone = "us-central1-a"
   790  		can_ip_forward = false
   791  		tags = ["foo", "bar"]
   792  
   793  		disk {
   794  			image = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160803"
   795  		}
   796  
   797  		network_interface {
   798  			network = "default"
   799  		}
   800  
   801  		metadata {
   802  			foo = "bar"
   803  		}
   804  	}`, instance)
   805  }
   806  
   807  // Update zone to ForceNew, and change metadata k/v entirely
   808  // Generates diff mismatch
   809  func testAccComputeInstance_forceNewAndChangeMetadata(instance string) string {
   810  	return fmt.Sprintf(`
   811  	resource "google_compute_instance" "foobar" {
   812  		name = "%s"
   813  		machine_type = "n1-standard-1"
   814  		zone = "us-central1-a"
   815  		zone = "us-central1-b"
   816  		tags = ["baz"]
   817  
   818  		disk {
   819  			image = "debian-8-jessie-v20160803"
   820  		}
   821  
   822  		network_interface {
   823  			network = "default"
   824  			access_config { }
   825  		}
   826  
   827  		metadata {
   828  			qux = "true"
   829  		}
   830  	}`, instance)
   831  }
   832  
   833  // Update metadata, tags, and network_interface
   834  func testAccComputeInstance_update(instance string) string {
   835  	return fmt.Sprintf(`
   836  	resource "google_compute_instance" "foobar" {
   837  		name = "%s"
   838  		machine_type = "n1-standard-1"
   839  		zone = "us-central1-a"
   840  		tags = ["baz"]
   841  
   842  		disk {
   843  			image = "debian-8-jessie-v20160803"
   844  		}
   845  
   846  		network_interface {
   847  			network = "default"
   848  			access_config { }
   849  		}
   850  
   851  		metadata {
   852  			bar = "baz"
   853  		}
   854  	}`, instance)
   855  }
   856  
   857  func testAccComputeInstance_ip(ip, instance string) string {
   858  	return fmt.Sprintf(`
   859  	resource "google_compute_address" "foo" {
   860  		name = "%s"
   861  	}
   862  
   863  	resource "google_compute_instance" "foobar" {
   864  		name = "%s"
   865  		machine_type = "n1-standard-1"
   866  		zone = "us-central1-a"
   867  		tags = ["foo", "bar"]
   868  
   869  		disk {
   870  			image = "debian-8-jessie-v20160803"
   871  		}
   872  
   873  		network_interface {
   874  			network = "default"
   875  			access_config {
   876  				nat_ip = "${google_compute_address.foo.address}"
   877  			}
   878  		}
   879  
   880  		metadata {
   881  			foo = "bar"
   882  		}
   883  	}`, ip, instance)
   884  }
   885  
   886  func testAccComputeInstance_disks(disk, instance string, autodelete bool) string {
   887  	return fmt.Sprintf(`
   888  	resource "google_compute_disk" "foobar" {
   889  		name = "%s"
   890  		size = 10
   891  		type = "pd-ssd"
   892  		zone = "us-central1-a"
   893  	}
   894  
   895  	resource "google_compute_instance" "foobar" {
   896  		name = "%s"
   897  		machine_type = "n1-standard-1"
   898  		zone = "us-central1-a"
   899  
   900  		disk {
   901  			image = "debian-8-jessie-v20160803"
   902  		}
   903  
   904  		disk {
   905  			disk = "${google_compute_disk.foobar.name}"
   906  			auto_delete = %v
   907  		}
   908  
   909  		network_interface {
   910  			network = "default"
   911  		}
   912  
   913  		metadata {
   914  			foo = "bar"
   915  		}
   916  	}`, disk, instance, autodelete)
   917  }
   918  
   919  func testAccComputeInstance_local_ssd(instance string) string {
   920  	return fmt.Sprintf(`
   921  	resource "google_compute_instance" "local-ssd" {
   922  		name = "%s"
   923  		machine_type = "n1-standard-1"
   924  		zone = "us-central1-a"
   925  
   926  		disk {
   927  			image = "debian-8-jessie-v20160803"
   928  		}
   929  
   930  		disk {
   931  			type = "local-ssd"
   932  			scratch = true
   933  		}
   934  
   935  		network_interface {
   936  			network = "default"
   937  		}
   938  
   939  	}`, instance)
   940  }
   941  
   942  func testAccComputeInstance_service_account(instance string) string {
   943  	return fmt.Sprintf(`
   944  	resource "google_compute_instance" "foobar" {
   945  		name = "%s"
   946  		machine_type = "n1-standard-1"
   947  		zone = "us-central1-a"
   948  
   949  		disk {
   950  			image = "debian-8-jessie-v20160803"
   951  		}
   952  
   953  		network_interface {
   954  			network = "default"
   955  		}
   956  
   957  		service_account {
   958  			scopes = [
   959  				"userinfo-email",
   960  				"compute-ro",
   961  				"storage-ro",
   962  			]
   963  		}
   964  	}`, instance)
   965  }
   966  
   967  func testAccComputeInstance_scheduling(instance string) string {
   968  	return fmt.Sprintf(`
   969  	resource "google_compute_instance" "foobar" {
   970  		name = "%s"
   971  		machine_type = "n1-standard-1"
   972  		zone = "us-central1-a"
   973  
   974  		disk {
   975  			image = "debian-8-jessie-v20160803"
   976  		}
   977  
   978  		network_interface {
   979  			network = "default"
   980  		}
   981  
   982  		scheduling {
   983  		}
   984  	}`, instance)
   985  }
   986  
   987  func testAccComputeInstance_subnet_auto(instance string) string {
   988  	return fmt.Sprintf(`
   989  	resource "google_compute_network" "inst-test-network" {
   990  		name = "inst-test-network-%s"
   991  		auto_create_subnetworks = true
   992  	}
   993  
   994  	resource "google_compute_instance" "foobar" {
   995  		name = "%s"
   996  		machine_type = "n1-standard-1"
   997  		zone = "us-central1-a"
   998  
   999  		disk {
  1000  			image = "debian-8-jessie-v20160803"
  1001  		}
  1002  
  1003  		network_interface {
  1004  			network = "${google_compute_network.inst-test-network.name}"
  1005  			access_config {	}
  1006  		}
  1007  
  1008  	}`, acctest.RandString(10), instance)
  1009  }
  1010  
  1011  func testAccComputeInstance_subnet_custom(instance string) string {
  1012  	return fmt.Sprintf(`
  1013  	resource "google_compute_network" "inst-test-network" {
  1014  		name = "inst-test-network-%s"
  1015  		auto_create_subnetworks = false
  1016  	}
  1017  
  1018  	resource "google_compute_subnetwork" "inst-test-subnetwork" {
  1019  		name = "inst-test-subnetwork-%s"
  1020  		ip_cidr_range = "10.0.0.0/16"
  1021  		region = "us-central1"
  1022  		network = "${google_compute_network.inst-test-network.self_link}"
  1023  	}
  1024  
  1025  	resource "google_compute_instance" "foobar" {
  1026  		name = "%s"
  1027  		machine_type = "n1-standard-1"
  1028  		zone = "us-central1-a"
  1029  
  1030  		disk {
  1031  			image = "debian-8-jessie-v20160803"
  1032  		}
  1033  
  1034  		network_interface {
  1035  			subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
  1036  			access_config {	}
  1037  		}
  1038  
  1039  	}`, acctest.RandString(10), acctest.RandString(10), instance)
  1040  }
  1041  
  1042  func testAccComputeInstance_address_auto(instance string) string {
  1043  	return fmt.Sprintf(`
  1044  	resource "google_compute_network" "inst-test-network" {
  1045  		name = "inst-test-network-%s"
  1046  	}
  1047  	resource "google_compute_subnetwork" "inst-test-subnetwork" {
  1048  		name = "inst-test-subnetwork-%s"
  1049  		ip_cidr_range = "10.0.0.0/16"
  1050  		region = "us-central1"
  1051  		network = "${google_compute_network.inst-test-network.self_link}"
  1052  	}
  1053  	resource "google_compute_instance" "foobar" {
  1054  		name = "%s"
  1055  		machine_type = "n1-standard-1"
  1056  		zone = "us-central1-a"
  1057  
  1058  		disk {
  1059  			image = "debian-8-jessie-v20160803"
  1060  		}
  1061  
  1062  		network_interface {
  1063  			subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
  1064  			access_config {	}
  1065  		}
  1066  
  1067  	}`, acctest.RandString(10), acctest.RandString(10), instance)
  1068  }
  1069  
  1070  func testAccComputeInstance_address_custom(instance, address string) string {
  1071  	return fmt.Sprintf(`
  1072  	resource "google_compute_network" "inst-test-network" {
  1073  		name = "inst-test-network-%s"
  1074  	}
  1075  	resource "google_compute_subnetwork" "inst-test-subnetwork" {
  1076  		name = "inst-test-subnetwork-%s"
  1077  		ip_cidr_range = "10.0.0.0/16"
  1078  		region = "us-central1"
  1079  		network = "${google_compute_network.inst-test-network.self_link}"
  1080  	}
  1081  	resource "google_compute_instance" "foobar" {
  1082  		name = "%s"
  1083  		machine_type = "n1-standard-1"
  1084  		zone = "us-central1-a"
  1085  
  1086  		disk {
  1087  			image = "debian-8-jessie-v20160803"
  1088  		}
  1089  
  1090  		network_interface {
  1091  			subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
  1092  		    address = "%s"
  1093  			access_config {	}
  1094  		}
  1095  
  1096  	}`, acctest.RandString(10), acctest.RandString(10), instance, address)
  1097  }