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