github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_instance_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"regexp"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  	"google.golang.org/api/compute/v1"
    14  )
    15  
    16  func TestAccComputeInstance_basic_deprecated_network(t *testing.T) {
    17  	var instance compute.Instance
    18  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccComputeInstance_basic_deprecated_network(instanceName),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckComputeInstanceExists(
    29  						"google_compute_instance.foobar", &instance),
    30  					testAccCheckComputeInstanceTag(&instance, "foo"),
    31  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    32  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccComputeInstance_basic1(t *testing.T) {
    40  	var instance compute.Instance
    41  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: testAccComputeInstance_basic(instanceName),
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckComputeInstanceExists(
    52  						"google_compute_instance.foobar", &instance),
    53  					testAccCheckComputeInstanceTag(&instance, "foo"),
    54  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    55  					testAccCheckComputeInstanceMetadata(&instance, "baz", "qux"),
    56  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccComputeInstance_basic2(t *testing.T) {
    64  	var instance compute.Instance
    65  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccComputeInstance_basic2(instanceName),
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckComputeInstanceExists(
    76  						"google_compute_instance.foobar", &instance),
    77  					testAccCheckComputeInstanceTag(&instance, "foo"),
    78  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    79  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
    80  				),
    81  			},
    82  		},
    83  	})
    84  }
    85  
    86  func TestAccComputeInstance_basic3(t *testing.T) {
    87  	var instance compute.Instance
    88  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
    89  
    90  	resource.Test(t, resource.TestCase{
    91  		PreCheck:     func() { testAccPreCheck(t) },
    92  		Providers:    testAccProviders,
    93  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    94  		Steps: []resource.TestStep{
    95  			resource.TestStep{
    96  				Config: testAccComputeInstance_basic3(instanceName),
    97  				Check: resource.ComposeTestCheckFunc(
    98  					testAccCheckComputeInstanceExists(
    99  						"google_compute_instance.foobar", &instance),
   100  					testAccCheckComputeInstanceTag(&instance, "foo"),
   101  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
   102  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   103  				),
   104  			},
   105  		},
   106  	})
   107  }
   108  
   109  func TestAccComputeInstance_basic4(t *testing.T) {
   110  	var instance compute.Instance
   111  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   112  
   113  	resource.Test(t, resource.TestCase{
   114  		PreCheck:     func() { testAccPreCheck(t) },
   115  		Providers:    testAccProviders,
   116  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   117  		Steps: []resource.TestStep{
   118  			resource.TestStep{
   119  				Config: testAccComputeInstance_basic4(instanceName),
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testAccCheckComputeInstanceExists(
   122  						"google_compute_instance.foobar", &instance),
   123  					testAccCheckComputeInstanceTag(&instance, "foo"),
   124  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
   125  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func TestAccComputeInstance_basic5(t *testing.T) {
   133  	var instance compute.Instance
   134  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   135  
   136  	resource.Test(t, resource.TestCase{
   137  		PreCheck:     func() { testAccPreCheck(t) },
   138  		Providers:    testAccProviders,
   139  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   140  		Steps: []resource.TestStep{
   141  			resource.TestStep{
   142  				Config: testAccComputeInstance_basic5(instanceName),
   143  				Check: resource.ComposeTestCheckFunc(
   144  					testAccCheckComputeInstanceExists(
   145  						"google_compute_instance.foobar", &instance),
   146  					testAccCheckComputeInstanceTag(&instance, "foo"),
   147  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
   148  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   149  				),
   150  			},
   151  		},
   152  	})
   153  }
   154  
   155  func TestAccComputeInstance_IP(t *testing.T) {
   156  	var instance compute.Instance
   157  	var ipName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   158  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   159  
   160  	resource.Test(t, resource.TestCase{
   161  		PreCheck:     func() { testAccPreCheck(t) },
   162  		Providers:    testAccProviders,
   163  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   164  		Steps: []resource.TestStep{
   165  			resource.TestStep{
   166  				Config: testAccComputeInstance_ip(ipName, instanceName),
   167  				Check: resource.ComposeTestCheckFunc(
   168  					testAccCheckComputeInstanceExists(
   169  						"google_compute_instance.foobar", &instance),
   170  					testAccCheckComputeInstanceAccessConfigHasIP(&instance),
   171  				),
   172  			},
   173  		},
   174  	})
   175  }
   176  
   177  func TestAccComputeInstance_disksWithoutAutodelete(t *testing.T) {
   178  	var instance compute.Instance
   179  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   180  	var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
   181  
   182  	resource.Test(t, resource.TestCase{
   183  		PreCheck:     func() { testAccPreCheck(t) },
   184  		Providers:    testAccProviders,
   185  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   186  		Steps: []resource.TestStep{
   187  			resource.TestStep{
   188  				Config: testAccComputeInstance_disks(diskName, instanceName, false),
   189  				Check: resource.ComposeTestCheckFunc(
   190  					testAccCheckComputeInstanceExists(
   191  						"google_compute_instance.foobar", &instance),
   192  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   193  					testAccCheckComputeInstanceDisk(&instance, diskName, false, false),
   194  				),
   195  			},
   196  		},
   197  	})
   198  }
   199  
   200  func TestAccComputeInstance_disksWithAutodelete(t *testing.T) {
   201  	var instance compute.Instance
   202  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   203  	var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
   204  
   205  	resource.Test(t, resource.TestCase{
   206  		PreCheck:     func() { testAccPreCheck(t) },
   207  		Providers:    testAccProviders,
   208  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   209  		Steps: []resource.TestStep{
   210  			resource.TestStep{
   211  				Config: testAccComputeInstance_disks(diskName, instanceName, true),
   212  				Check: resource.ComposeTestCheckFunc(
   213  					testAccCheckComputeInstanceExists(
   214  						"google_compute_instance.foobar", &instance),
   215  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   216  					testAccCheckComputeInstanceDisk(&instance, diskName, true, false),
   217  				),
   218  			},
   219  		},
   220  	})
   221  }
   222  
   223  func TestAccComputeInstance_diskEncryption(t *testing.T) {
   224  	var instance compute.Instance
   225  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   226  	var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
   227  
   228  	resource.Test(t, resource.TestCase{
   229  		PreCheck:     func() { testAccPreCheck(t) },
   230  		Providers:    testAccProviders,
   231  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   232  		Steps: []resource.TestStep{
   233  			resource.TestStep{
   234  				Config: testAccComputeInstance_disks_encryption(diskName, instanceName),
   235  				Check: resource.ComposeTestCheckFunc(
   236  					testAccCheckComputeInstanceExists(
   237  						"google_compute_instance.foobar", &instance),
   238  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   239  					testAccCheckComputeInstanceDisk(&instance, diskName, true, false),
   240  					testAccCheckComputeInstanceDiskEncryptionKey("google_compute_instance.foobar", &instance),
   241  				),
   242  			},
   243  		},
   244  	})
   245  }
   246  
   247  func TestAccComputeInstance_attachedDisk(t *testing.T) {
   248  	var instance compute.Instance
   249  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   250  	var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
   251  
   252  	resource.Test(t, resource.TestCase{
   253  		PreCheck:     func() { testAccPreCheck(t) },
   254  		Providers:    testAccProviders,
   255  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   256  		Steps: []resource.TestStep{
   257  			resource.TestStep{
   258  				Config: testAccComputeInstance_attachedDisk(diskName, instanceName),
   259  				Check: resource.ComposeTestCheckFunc(
   260  					testAccCheckComputeInstanceExists(
   261  						"google_compute_instance.foobar", &instance),
   262  					testAccCheckComputeInstanceDisk(&instance, diskName, false, true),
   263  				),
   264  			},
   265  		},
   266  	})
   267  }
   268  
   269  func TestAccComputeInstance_noDisk(t *testing.T) {
   270  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   271  
   272  	resource.Test(t, resource.TestCase{
   273  		PreCheck:     func() { testAccPreCheck(t) },
   274  		Providers:    testAccProviders,
   275  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   276  		Steps: []resource.TestStep{
   277  			resource.TestStep{
   278  				Config:      testAccComputeInstance_noDisk(instanceName),
   279  				ExpectError: regexp.MustCompile("At least one disk or attached_disk must be set"),
   280  			},
   281  		},
   282  	})
   283  }
   284  
   285  func TestAccComputeInstance_local_ssd(t *testing.T) {
   286  	var instance compute.Instance
   287  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   288  
   289  	resource.Test(t, resource.TestCase{
   290  		PreCheck:     func() { testAccPreCheck(t) },
   291  		Providers:    testAccProviders,
   292  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   293  		Steps: []resource.TestStep{
   294  			resource.TestStep{
   295  				Config: testAccComputeInstance_local_ssd(instanceName),
   296  				Check: resource.ComposeTestCheckFunc(
   297  					testAccCheckComputeInstanceExists(
   298  						"google_compute_instance.local-ssd", &instance),
   299  					testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
   300  				),
   301  			},
   302  		},
   303  	})
   304  }
   305  
   306  func TestAccComputeInstance_update_deprecated_network(t *testing.T) {
   307  	var instance compute.Instance
   308  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   309  
   310  	resource.Test(t, resource.TestCase{
   311  		PreCheck:     func() { testAccPreCheck(t) },
   312  		Providers:    testAccProviders,
   313  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   314  		Steps: []resource.TestStep{
   315  			resource.TestStep{
   316  				Config: testAccComputeInstance_basic_deprecated_network(instanceName),
   317  				Check: resource.ComposeTestCheckFunc(
   318  					testAccCheckComputeInstanceExists(
   319  						"google_compute_instance.foobar", &instance),
   320  				),
   321  			},
   322  			resource.TestStep{
   323  				Config: testAccComputeInstance_update_deprecated_network(instanceName),
   324  				Check: resource.ComposeTestCheckFunc(
   325  					testAccCheckComputeInstanceExists(
   326  						"google_compute_instance.foobar", &instance),
   327  					testAccCheckComputeInstanceMetadata(
   328  						&instance, "bar", "baz"),
   329  					testAccCheckComputeInstanceTag(&instance, "baz"),
   330  				),
   331  			},
   332  		},
   333  	})
   334  }
   335  
   336  func TestAccComputeInstance_forceNewAndChangeMetadata(t *testing.T) {
   337  	var instance compute.Instance
   338  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   339  
   340  	resource.Test(t, resource.TestCase{
   341  		PreCheck:     func() { testAccPreCheck(t) },
   342  		Providers:    testAccProviders,
   343  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   344  		Steps: []resource.TestStep{
   345  			resource.TestStep{
   346  				Config: testAccComputeInstance_basic(instanceName),
   347  				Check: resource.ComposeTestCheckFunc(
   348  					testAccCheckComputeInstanceExists(
   349  						"google_compute_instance.foobar", &instance),
   350  				),
   351  			},
   352  			resource.TestStep{
   353  				Config: testAccComputeInstance_forceNewAndChangeMetadata(instanceName),
   354  				Check: resource.ComposeTestCheckFunc(
   355  					testAccCheckComputeInstanceExists(
   356  						"google_compute_instance.foobar", &instance),
   357  					testAccCheckComputeInstanceMetadata(
   358  						&instance, "qux", "true"),
   359  				),
   360  			},
   361  		},
   362  	})
   363  }
   364  
   365  func TestAccComputeInstance_update(t *testing.T) {
   366  	var instance compute.Instance
   367  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   368  
   369  	resource.Test(t, resource.TestCase{
   370  		PreCheck:     func() { testAccPreCheck(t) },
   371  		Providers:    testAccProviders,
   372  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   373  		Steps: []resource.TestStep{
   374  			resource.TestStep{
   375  				Config: testAccComputeInstance_basic(instanceName),
   376  				Check: resource.ComposeTestCheckFunc(
   377  					testAccCheckComputeInstanceExists(
   378  						"google_compute_instance.foobar", &instance),
   379  				),
   380  			},
   381  			resource.TestStep{
   382  				Config: testAccComputeInstance_update(instanceName),
   383  				Check: resource.ComposeTestCheckFunc(
   384  					testAccCheckComputeInstanceExists(
   385  						"google_compute_instance.foobar", &instance),
   386  					testAccCheckComputeInstanceMetadata(
   387  						&instance, "bar", "baz"),
   388  					testAccCheckComputeInstanceTag(&instance, "baz"),
   389  					testAccCheckComputeInstanceAccessConfig(&instance),
   390  				),
   391  			},
   392  		},
   393  	})
   394  }
   395  
   396  func TestAccComputeInstance_service_account(t *testing.T) {
   397  	var instance compute.Instance
   398  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   399  
   400  	resource.Test(t, resource.TestCase{
   401  		PreCheck:     func() { testAccPreCheck(t) },
   402  		Providers:    testAccProviders,
   403  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   404  		Steps: []resource.TestStep{
   405  			resource.TestStep{
   406  				Config: testAccComputeInstance_service_account(instanceName),
   407  				Check: resource.ComposeTestCheckFunc(
   408  					testAccCheckComputeInstanceExists(
   409  						"google_compute_instance.foobar", &instance),
   410  					testAccCheckComputeInstanceServiceAccount(&instance,
   411  						"https://www.googleapis.com/auth/compute.readonly"),
   412  					testAccCheckComputeInstanceServiceAccount(&instance,
   413  						"https://www.googleapis.com/auth/devstorage.read_only"),
   414  					testAccCheckComputeInstanceServiceAccount(&instance,
   415  						"https://www.googleapis.com/auth/userinfo.email"),
   416  				),
   417  			},
   418  		},
   419  	})
   420  }
   421  
   422  func TestAccComputeInstance_scheduling(t *testing.T) {
   423  	var instance compute.Instance
   424  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   425  
   426  	resource.Test(t, resource.TestCase{
   427  		PreCheck:     func() { testAccPreCheck(t) },
   428  		Providers:    testAccProviders,
   429  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   430  		Steps: []resource.TestStep{
   431  			resource.TestStep{
   432  				Config: testAccComputeInstance_scheduling(instanceName),
   433  				Check: resource.ComposeTestCheckFunc(
   434  					testAccCheckComputeInstanceExists(
   435  						"google_compute_instance.foobar", &instance),
   436  				),
   437  			},
   438  		},
   439  	})
   440  }
   441  
   442  func TestAccComputeInstance_subnet_auto(t *testing.T) {
   443  	var instance compute.Instance
   444  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   445  
   446  	resource.Test(t, resource.TestCase{
   447  		PreCheck:     func() { testAccPreCheck(t) },
   448  		Providers:    testAccProviders,
   449  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   450  		Steps: []resource.TestStep{
   451  			resource.TestStep{
   452  				Config: testAccComputeInstance_subnet_auto(instanceName),
   453  				Check: resource.ComposeTestCheckFunc(
   454  					testAccCheckComputeInstanceExists(
   455  						"google_compute_instance.foobar", &instance),
   456  					testAccCheckComputeInstanceHasSubnet(&instance),
   457  				),
   458  			},
   459  		},
   460  	})
   461  }
   462  
   463  func TestAccComputeInstance_subnet_custom(t *testing.T) {
   464  	var instance compute.Instance
   465  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   466  
   467  	resource.Test(t, resource.TestCase{
   468  		PreCheck:     func() { testAccPreCheck(t) },
   469  		Providers:    testAccProviders,
   470  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   471  		Steps: []resource.TestStep{
   472  			resource.TestStep{
   473  				Config: testAccComputeInstance_subnet_custom(instanceName),
   474  				Check: resource.ComposeTestCheckFunc(
   475  					testAccCheckComputeInstanceExists(
   476  						"google_compute_instance.foobar", &instance),
   477  					testAccCheckComputeInstanceHasSubnet(&instance),
   478  				),
   479  			},
   480  		},
   481  	})
   482  }
   483  
   484  func TestAccComputeInstance_subnet_xpn(t *testing.T) {
   485  	var instance compute.Instance
   486  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   487  	var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
   488  
   489  	resource.Test(t, resource.TestCase{
   490  		PreCheck:     func() { testAccPreCheck(t) },
   491  		Providers:    testAccProviders,
   492  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   493  		Steps: []resource.TestStep{
   494  			resource.TestStep{
   495  				Config: testAccComputeInstance_subnet_xpn(instanceName, xpn_host),
   496  				Check: resource.ComposeTestCheckFunc(
   497  					testAccCheckComputeInstanceExists(
   498  						"google_compute_instance.foobar", &instance),
   499  					testAccCheckComputeInstanceHasSubnet(&instance),
   500  				),
   501  			},
   502  		},
   503  	})
   504  }
   505  
   506  func TestAccComputeInstance_address_auto(t *testing.T) {
   507  	var instance compute.Instance
   508  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   509  
   510  	resource.Test(t, resource.TestCase{
   511  		PreCheck:     func() { testAccPreCheck(t) },
   512  		Providers:    testAccProviders,
   513  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   514  		Steps: []resource.TestStep{
   515  			resource.TestStep{
   516  				Config: testAccComputeInstance_address_auto(instanceName),
   517  				Check: resource.ComposeTestCheckFunc(
   518  					testAccCheckComputeInstanceExists(
   519  						"google_compute_instance.foobar", &instance),
   520  					testAccCheckComputeInstanceHasAnyAddress(&instance),
   521  				),
   522  			},
   523  		},
   524  	})
   525  }
   526  
   527  func TestAccComputeInstance_address_custom(t *testing.T) {
   528  	var instance compute.Instance
   529  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   530  	var address = "10.0.200.200"
   531  	resource.Test(t, resource.TestCase{
   532  		PreCheck:     func() { testAccPreCheck(t) },
   533  		Providers:    testAccProviders,
   534  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   535  		Steps: []resource.TestStep{
   536  			resource.TestStep{
   537  				Config: testAccComputeInstance_address_custom(instanceName, address),
   538  				Check: resource.ComposeTestCheckFunc(
   539  					testAccCheckComputeInstanceExists(
   540  						"google_compute_instance.foobar", &instance),
   541  					testAccCheckComputeInstanceHasAddress(&instance, address),
   542  				),
   543  			},
   544  		},
   545  	})
   546  }
   547  
   548  func TestAccComputeInstance_private_image_family(t *testing.T) {
   549  	var instance compute.Instance
   550  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   551  	var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
   552  	var imageName = fmt.Sprintf("instance-testi-%s", acctest.RandString(10))
   553  	var familyName = fmt.Sprintf("instance-testf-%s", acctest.RandString(10))
   554  
   555  	resource.Test(t, resource.TestCase{
   556  		PreCheck:     func() { testAccPreCheck(t) },
   557  		Providers:    testAccProviders,
   558  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   559  		Steps: []resource.TestStep{
   560  			resource.TestStep{
   561  				Config: testAccComputeInstance_private_image_family(diskName, imageName, familyName, instanceName),
   562  				Check: resource.ComposeTestCheckFunc(
   563  					testAccCheckComputeInstanceExists(
   564  						"google_compute_instance.foobar", &instance),
   565  				),
   566  			},
   567  		},
   568  	})
   569  }
   570  
   571  func TestAccComputeInstance_invalid_disk(t *testing.T) {
   572  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   573  	var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10))
   574  
   575  	resource.Test(t, resource.TestCase{
   576  		PreCheck:     func() { testAccPreCheck(t) },
   577  		Providers:    testAccProviders,
   578  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   579  		Steps: []resource.TestStep{
   580  			resource.TestStep{
   581  				Config:      testAccComputeInstance_invalid_disk(diskName, instanceName),
   582  				ExpectError: regexp.MustCompile("Error: cannot define both disk and type."),
   583  			},
   584  		},
   585  	})
   586  }
   587  
   588  func TestAccComputeInstance_forceChangeMachineTypeManually(t *testing.T) {
   589  	var instance compute.Instance
   590  	var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
   591  
   592  	resource.Test(t, resource.TestCase{
   593  		PreCheck:     func() { testAccPreCheck(t) },
   594  		Providers:    testAccProviders,
   595  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   596  		Steps: []resource.TestStep{
   597  			resource.TestStep{
   598  				Config: testAccComputeInstance_basic(instanceName),
   599  				Check: resource.ComposeTestCheckFunc(
   600  					testAccCheckComputeInstanceExists("google_compute_instance.foobar", &instance),
   601  					testAccCheckComputeInstanceUpdateMachineType("google_compute_instance.foobar"),
   602  				),
   603  				ExpectNonEmptyPlan: true,
   604  			},
   605  		},
   606  	})
   607  }
   608  
   609  func testAccCheckComputeInstanceUpdateMachineType(n string) resource.TestCheckFunc {
   610  	return func(s *terraform.State) error {
   611  		rs, ok := s.RootModule().Resources[n]
   612  		if !ok {
   613  			return fmt.Errorf("Not found: %s", n)
   614  		}
   615  
   616  		if rs.Primary.ID == "" {
   617  			return fmt.Errorf("No ID is set")
   618  		}
   619  
   620  		config := testAccProvider.Meta().(*Config)
   621  
   622  		op, err := config.clientCompute.Instances.Stop(config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   623  		if err != nil {
   624  			return fmt.Errorf("Could not stop instance: %s", err)
   625  		}
   626  		err = computeOperationWaitZone(config, op, config.Project, rs.Primary.Attributes["zone"], "Waiting on stop")
   627  		if err != nil {
   628  			return fmt.Errorf("Could not stop instance: %s", err)
   629  		}
   630  
   631  		machineType := compute.InstancesSetMachineTypeRequest{
   632  			MachineType: "zones/us-central1-a/machineTypes/f1-micro",
   633  		}
   634  
   635  		op, err = config.clientCompute.Instances.SetMachineType(
   636  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID, &machineType).Do()
   637  		if err != nil {
   638  			return fmt.Errorf("Could not change machine type: %s", err)
   639  		}
   640  		err = computeOperationWaitZone(config, op, config.Project, rs.Primary.Attributes["zone"], "Waiting machine type change")
   641  		if err != nil {
   642  			return fmt.Errorf("Could not change machine type: %s", err)
   643  		}
   644  		return nil
   645  	}
   646  }
   647  
   648  func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
   649  	config := testAccProvider.Meta().(*Config)
   650  
   651  	for _, rs := range s.RootModule().Resources {
   652  		if rs.Type != "google_compute_instance" {
   653  			continue
   654  		}
   655  
   656  		_, err := config.clientCompute.Instances.Get(
   657  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   658  		if err == nil {
   659  			return fmt.Errorf("Instance still exists")
   660  		}
   661  	}
   662  
   663  	return nil
   664  }
   665  
   666  func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc {
   667  	return func(s *terraform.State) error {
   668  		rs, ok := s.RootModule().Resources[n]
   669  		if !ok {
   670  			return fmt.Errorf("Not found: %s", n)
   671  		}
   672  
   673  		if rs.Primary.ID == "" {
   674  			return fmt.Errorf("No ID is set")
   675  		}
   676  
   677  		config := testAccProvider.Meta().(*Config)
   678  
   679  		found, err := config.clientCompute.Instances.Get(
   680  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   681  		if err != nil {
   682  			return err
   683  		}
   684  
   685  		if found.Name != rs.Primary.ID {
   686  			return fmt.Errorf("Instance not found")
   687  		}
   688  
   689  		*instance = *found
   690  
   691  		return nil
   692  	}
   693  }
   694  
   695  func testAccCheckComputeInstanceMetadata(
   696  	instance *compute.Instance,
   697  	k string, v string) resource.TestCheckFunc {
   698  	return func(s *terraform.State) error {
   699  		if instance.Metadata == nil {
   700  			return fmt.Errorf("no metadata")
   701  		}
   702  
   703  		for _, item := range instance.Metadata.Items {
   704  			if k != item.Key {
   705  				continue
   706  			}
   707  
   708  			if item.Value != nil && v == *item.Value {
   709  				return nil
   710  			}
   711  
   712  			return fmt.Errorf("bad value for %s: %s", k, *item.Value)
   713  		}
   714  
   715  		return fmt.Errorf("metadata not found: %s", k)
   716  	}
   717  }
   718  
   719  func testAccCheckComputeInstanceAccessConfig(instance *compute.Instance) resource.TestCheckFunc {
   720  	return func(s *terraform.State) error {
   721  		for _, i := range instance.NetworkInterfaces {
   722  			if len(i.AccessConfigs) == 0 {
   723  				return fmt.Errorf("no access_config")
   724  			}
   725  		}
   726  
   727  		return nil
   728  	}
   729  }
   730  
   731  func testAccCheckComputeInstanceAccessConfigHasIP(instance *compute.Instance) resource.TestCheckFunc {
   732  	return func(s *terraform.State) error {
   733  		for _, i := range instance.NetworkInterfaces {
   734  			for _, c := range i.AccessConfigs {
   735  				if c.NatIP == "" {
   736  					return fmt.Errorf("no NAT IP")
   737  				}
   738  			}
   739  		}
   740  
   741  		return nil
   742  	}
   743  }
   744  
   745  func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
   746  	return func(s *terraform.State) error {
   747  		if instance.Disks == nil {
   748  			return fmt.Errorf("no disks")
   749  		}
   750  
   751  		for _, disk := range instance.Disks {
   752  			if strings.LastIndex(disk.Source, "/"+source) == len(disk.Source)-len(source)-1 && disk.AutoDelete == delete && disk.Boot == boot {
   753  				return nil
   754  			}
   755  		}
   756  
   757  		return fmt.Errorf("Disk not found: %s", source)
   758  	}
   759  }
   760  
   761  func testAccCheckComputeInstanceDiskEncryptionKey(n string, instance *compute.Instance) resource.TestCheckFunc {
   762  	return func(s *terraform.State) error {
   763  		rs, ok := s.RootModule().Resources[n]
   764  		if !ok {
   765  			return fmt.Errorf("Not found: %s", n)
   766  		}
   767  
   768  		for i, disk := range instance.Disks {
   769  			attr := rs.Primary.Attributes[fmt.Sprintf("disk.%d.disk_encryption_key_sha256", i)]
   770  			if disk.DiskEncryptionKey == nil && attr != "" {
   771  				return fmt.Errorf("Disk %d has mismatched encryption key.\nTF State: %+v\nGCP State: <empty>", i, attr)
   772  			}
   773  			if disk.DiskEncryptionKey != nil && attr != disk.DiskEncryptionKey.Sha256 {
   774  				return fmt.Errorf("Disk %d has mismatched encryption key.\nTF State: %+v\nGCP State: %+v",
   775  					i, attr, disk.DiskEncryptionKey.Sha256)
   776  			}
   777  		}
   778  		return nil
   779  	}
   780  }
   781  
   782  func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
   783  	return func(s *terraform.State) error {
   784  		if instance.Tags == nil {
   785  			return fmt.Errorf("no tags")
   786  		}
   787  
   788  		for _, k := range instance.Tags.Items {
   789  			if k == n {
   790  				return nil
   791  			}
   792  		}
   793  
   794  		return fmt.Errorf("tag not found: %s", n)
   795  	}
   796  }
   797  
   798  func testAccCheckComputeInstanceServiceAccount(instance *compute.Instance, scope string) resource.TestCheckFunc {
   799  	return func(s *terraform.State) error {
   800  		if count := len(instance.ServiceAccounts); count != 1 {
   801  			return fmt.Errorf("Wrong number of ServiceAccounts: expected 1, got %d", count)
   802  		}
   803  
   804  		for _, val := range instance.ServiceAccounts[0].Scopes {
   805  			if val == scope {
   806  				return nil
   807  			}
   808  		}
   809  
   810  		return fmt.Errorf("Scope not found: %s", scope)
   811  	}
   812  }
   813  
   814  func testAccCheckComputeInstanceHasSubnet(instance *compute.Instance) resource.TestCheckFunc {
   815  	return func(s *terraform.State) error {
   816  		for _, i := range instance.NetworkInterfaces {
   817  			if i.Subnetwork == "" {
   818  				return fmt.Errorf("no subnet")
   819  			}
   820  		}
   821  
   822  		return nil
   823  	}
   824  }
   825  
   826  func testAccCheckComputeInstanceHasAnyAddress(instance *compute.Instance) resource.TestCheckFunc {
   827  	return func(s *terraform.State) error {
   828  		for _, i := range instance.NetworkInterfaces {
   829  			if i.NetworkIP == "" {
   830  				return fmt.Errorf("no address")
   831  			}
   832  		}
   833  
   834  		return nil
   835  	}
   836  }
   837  
   838  func testAccCheckComputeInstanceHasAddress(instance *compute.Instance, address string) resource.TestCheckFunc {
   839  	return func(s *terraform.State) error {
   840  		for _, i := range instance.NetworkInterfaces {
   841  			if i.NetworkIP != address {
   842  				return fmt.Errorf("Wrong address found: expected %v, got %v", address, i.NetworkIP)
   843  			}
   844  		}
   845  
   846  		return nil
   847  	}
   848  }
   849  
   850  func testAccComputeInstance_basic_deprecated_network(instance string) string {
   851  	return fmt.Sprintf(`
   852  	resource "google_compute_instance" "foobar" {
   853  		name = "%s"
   854  		machine_type = "n1-standard-1"
   855  		zone = "us-central1-a"
   856  		can_ip_forward = false
   857  		tags = ["foo", "bar"]
   858  
   859  		disk {
   860  			image = "debian-8-jessie-v20160803"
   861  		}
   862  
   863  		network {
   864  			source = "default"
   865  		}
   866  
   867  		metadata {
   868  			foo = "bar"
   869  		}
   870  	}`, instance)
   871  }
   872  
   873  func testAccComputeInstance_update_deprecated_network(instance string) string {
   874  	return fmt.Sprintf(`
   875  	resource "google_compute_instance" "foobar" {
   876  		name = "%s"
   877  		machine_type = "n1-standard-1"
   878  		zone = "us-central1-a"
   879  		tags = ["baz"]
   880  
   881  		disk {
   882  			image = "debian-8-jessie-v20160803"
   883  		}
   884  
   885  		network {
   886  			source = "default"
   887  		}
   888  
   889  		metadata {
   890  			bar = "baz"
   891  		}
   892  	}`, instance)
   893  }
   894  
   895  func testAccComputeInstance_basic(instance string) string {
   896  	return fmt.Sprintf(`
   897  	resource "google_compute_instance" "foobar" {
   898  		name = "%s"
   899  		machine_type = "n1-standard-1"
   900  		zone = "us-central1-a"
   901  		can_ip_forward = false
   902  		tags = ["foo", "bar"]
   903  
   904  		disk {
   905  			image = "debian-8-jessie-v20160803"
   906  		}
   907  
   908  		network_interface {
   909  			network = "default"
   910  		}
   911  
   912  		metadata {
   913  			foo = "bar"
   914  			baz = "qux"
   915  		}
   916  
   917  		create_timeout = 5
   918  
   919  		metadata_startup_script = "echo Hello"
   920  	}`, instance)
   921  }
   922  
   923  func testAccComputeInstance_basic2(instance string) string {
   924  	return fmt.Sprintf(`
   925  	resource "google_compute_instance" "foobar" {
   926  		name = "%s"
   927  		machine_type = "n1-standard-1"
   928  		zone = "us-central1-a"
   929  		can_ip_forward = false
   930  		tags = ["foo", "bar"]
   931  
   932  		disk {
   933  			image = "debian-8"
   934  		}
   935  
   936  		network_interface {
   937  			network = "default"
   938  		}
   939  
   940  		metadata {
   941  			foo = "bar"
   942  		}
   943  	}`, instance)
   944  }
   945  
   946  func testAccComputeInstance_basic3(instance string) string {
   947  	return fmt.Sprintf(`
   948  	resource "google_compute_instance" "foobar" {
   949  		name = "%s"
   950  		machine_type = "n1-standard-1"
   951  		zone = "us-central1-a"
   952  		can_ip_forward = false
   953  		tags = ["foo", "bar"]
   954  
   955  		disk {
   956  			image = "debian-cloud/debian-8-jessie-v20160803"
   957  		}
   958  
   959  		network_interface {
   960  			network = "default"
   961  		}
   962  
   963  
   964  		metadata {
   965  			foo = "bar"
   966  		}
   967  	}`, instance)
   968  }
   969  
   970  func testAccComputeInstance_basic4(instance string) string {
   971  	return fmt.Sprintf(`
   972  	resource "google_compute_instance" "foobar" {
   973  		name = "%s"
   974  		machine_type = "n1-standard-1"
   975  		zone = "us-central1-a"
   976  		can_ip_forward = false
   977  		tags = ["foo", "bar"]
   978  
   979  		disk {
   980  			image = "debian-cloud/debian-8"
   981  		}
   982  
   983  		network_interface {
   984  			network = "default"
   985  		}
   986  
   987  
   988  		metadata {
   989  			foo = "bar"
   990  		}
   991  	}`, instance)
   992  }
   993  
   994  func testAccComputeInstance_basic5(instance string) string {
   995  	return fmt.Sprintf(`
   996  	resource "google_compute_instance" "foobar" {
   997  		name = "%s"
   998  		machine_type = "n1-standard-1"
   999  		zone = "us-central1-a"
  1000  		can_ip_forward = false
  1001  		tags = ["foo", "bar"]
  1002  
  1003  		disk {
  1004  			image = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160803"
  1005  		}
  1006  
  1007  		network_interface {
  1008  			network = "default"
  1009  		}
  1010  
  1011  		metadata {
  1012  			foo = "bar"
  1013  		}
  1014  	}`, instance)
  1015  }
  1016  
  1017  // Update zone to ForceNew, and change metadata k/v entirely
  1018  // Generates diff mismatch
  1019  func testAccComputeInstance_forceNewAndChangeMetadata(instance string) string {
  1020  	return fmt.Sprintf(`
  1021  	resource "google_compute_instance" "foobar" {
  1022  		name = "%s"
  1023  		machine_type = "n1-standard-1"
  1024  		zone = "us-central1-a"
  1025  		zone = "us-central1-b"
  1026  		tags = ["baz"]
  1027  
  1028  		disk {
  1029  			image = "debian-8-jessie-v20160803"
  1030  		}
  1031  
  1032  		network_interface {
  1033  			network = "default"
  1034  			access_config { }
  1035  		}
  1036  
  1037  		metadata {
  1038  			qux = "true"
  1039  		}
  1040  	}`, instance)
  1041  }
  1042  
  1043  // Update metadata, tags, and network_interface
  1044  func testAccComputeInstance_update(instance string) string {
  1045  	return fmt.Sprintf(`
  1046  	resource "google_compute_instance" "foobar" {
  1047  		name = "%s"
  1048  		machine_type = "n1-standard-1"
  1049  		zone = "us-central1-a"
  1050  		tags = ["baz"]
  1051  
  1052  		disk {
  1053  			image = "debian-8-jessie-v20160803"
  1054  		}
  1055  
  1056  		network_interface {
  1057  			network = "default"
  1058  			access_config { }
  1059  		}
  1060  
  1061  		metadata {
  1062  			bar = "baz"
  1063  		}
  1064  	}`, instance)
  1065  }
  1066  
  1067  func testAccComputeInstance_ip(ip, instance string) string {
  1068  	return fmt.Sprintf(`
  1069  	resource "google_compute_address" "foo" {
  1070  		name = "%s"
  1071  	}
  1072  
  1073  	resource "google_compute_instance" "foobar" {
  1074  		name = "%s"
  1075  		machine_type = "n1-standard-1"
  1076  		zone = "us-central1-a"
  1077  		tags = ["foo", "bar"]
  1078  
  1079  		disk {
  1080  			image = "debian-8-jessie-v20160803"
  1081  		}
  1082  
  1083  		network_interface {
  1084  			network = "default"
  1085  			access_config {
  1086  				nat_ip = "${google_compute_address.foo.address}"
  1087  			}
  1088  		}
  1089  
  1090  		metadata {
  1091  			foo = "bar"
  1092  		}
  1093  	}`, ip, instance)
  1094  }
  1095  
  1096  func testAccComputeInstance_disks(disk, instance string, autodelete bool) string {
  1097  	return fmt.Sprintf(`
  1098  	resource "google_compute_disk" "foobar" {
  1099  		name = "%s"
  1100  		size = 10
  1101  		type = "pd-ssd"
  1102  		zone = "us-central1-a"
  1103  	}
  1104  
  1105  	resource "google_compute_instance" "foobar" {
  1106  		name = "%s"
  1107  		machine_type = "n1-standard-1"
  1108  		zone = "us-central1-a"
  1109  
  1110  		disk {
  1111  			image = "debian-8-jessie-v20160803"
  1112  		}
  1113  
  1114  		disk {
  1115  			disk = "${google_compute_disk.foobar.name}"
  1116  			auto_delete = %v
  1117  		}
  1118  
  1119  		network_interface {
  1120  			network = "default"
  1121  		}
  1122  
  1123  		metadata {
  1124  			foo = "bar"
  1125  		}
  1126  	}`, disk, instance, autodelete)
  1127  }
  1128  
  1129  func testAccComputeInstance_disks_encryption(disk, instance string) string {
  1130  	return fmt.Sprintf(`
  1131  	resource "google_compute_disk" "foobar" {
  1132  		name = "%s"
  1133  		size = 10
  1134  		type = "pd-ssd"
  1135  		zone = "us-central1-a"
  1136  	}
  1137  
  1138  	resource "google_compute_instance" "foobar" {
  1139  		name = "%s"
  1140  		machine_type = "n1-standard-1"
  1141  		zone = "us-central1-a"
  1142  
  1143  		disk {
  1144  			image = "debian-8-jessie-v20160803"
  1145  			disk_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="
  1146  		}
  1147  
  1148  		disk {
  1149  			disk = "${google_compute_disk.foobar.name}"
  1150  		}
  1151  
  1152  		network_interface {
  1153  			network = "default"
  1154  		}
  1155  
  1156  		metadata {
  1157  			foo = "bar"
  1158  		}
  1159  	}`, disk, instance)
  1160  }
  1161  
  1162  func testAccComputeInstance_attachedDisk(disk, instance string) string {
  1163  	return fmt.Sprintf(`
  1164  	resource "google_compute_disk" "foobar" {
  1165  		name = "%s"
  1166  		size = 10
  1167  		type = "pd-ssd"
  1168  		zone = "us-central1-a"
  1169  	}
  1170  
  1171  	resource "google_compute_instance" "foobar" {
  1172  		name = "%s"
  1173  		machine_type = "n1-standard-1"
  1174  		zone = "us-central1-a"
  1175  
  1176  		attached_disk {
  1177  			source = "${google_compute_disk.foobar.self_link}"
  1178  		}
  1179  
  1180  		network_interface {
  1181  			network = "default"
  1182  		}
  1183  
  1184  		metadata {
  1185  			foo = "bar"
  1186  		}
  1187  	}`, disk, instance)
  1188  }
  1189  
  1190  func testAccComputeInstance_noDisk(instance string) string {
  1191  	return fmt.Sprintf(`
  1192  	resource "google_compute_instance" "foobar" {
  1193  		name = "%s"
  1194  		machine_type = "n1-standard-1"
  1195  		zone = "us-central1-a"
  1196  
  1197  		network_interface {
  1198  			network = "default"
  1199  		}
  1200  
  1201  		metadata {
  1202  			foo = "bar"
  1203  		}
  1204  	}`, instance)
  1205  }
  1206  
  1207  func testAccComputeInstance_local_ssd(instance string) string {
  1208  	return fmt.Sprintf(`
  1209  	resource "google_compute_instance" "local-ssd" {
  1210  		name = "%s"
  1211  		machine_type = "n1-standard-1"
  1212  		zone = "us-central1-a"
  1213  
  1214  		disk {
  1215  			image = "debian-8-jessie-v20160803"
  1216  		}
  1217  
  1218  		disk {
  1219  			type = "local-ssd"
  1220  			scratch = true
  1221  		}
  1222  
  1223  		network_interface {
  1224  			network = "default"
  1225  		}
  1226  
  1227  	}`, instance)
  1228  }
  1229  
  1230  func testAccComputeInstance_service_account(instance string) string {
  1231  	return fmt.Sprintf(`
  1232  	resource "google_compute_instance" "foobar" {
  1233  		name = "%s"
  1234  		machine_type = "n1-standard-1"
  1235  		zone = "us-central1-a"
  1236  
  1237  		disk {
  1238  			image = "debian-8-jessie-v20160803"
  1239  		}
  1240  
  1241  		network_interface {
  1242  			network = "default"
  1243  		}
  1244  
  1245  		service_account {
  1246  			scopes = [
  1247  				"userinfo-email",
  1248  				"compute-ro",
  1249  				"storage-ro",
  1250  			]
  1251  		}
  1252  	}`, instance)
  1253  }
  1254  
  1255  func testAccComputeInstance_scheduling(instance string) string {
  1256  	return fmt.Sprintf(`
  1257  	resource "google_compute_instance" "foobar" {
  1258  		name = "%s"
  1259  		machine_type = "n1-standard-1"
  1260  		zone = "us-central1-a"
  1261  
  1262  		disk {
  1263  			image = "debian-8-jessie-v20160803"
  1264  		}
  1265  
  1266  		network_interface {
  1267  			network = "default"
  1268  		}
  1269  
  1270  		scheduling {
  1271  		}
  1272  	}`, instance)
  1273  }
  1274  
  1275  func testAccComputeInstance_subnet_auto(instance string) string {
  1276  	return fmt.Sprintf(`
  1277  	resource "google_compute_network" "inst-test-network" {
  1278  		name = "inst-test-network-%s"
  1279  		auto_create_subnetworks = true
  1280  	}
  1281  
  1282  	resource "google_compute_instance" "foobar" {
  1283  		name = "%s"
  1284  		machine_type = "n1-standard-1"
  1285  		zone = "us-central1-a"
  1286  
  1287  		disk {
  1288  			image = "debian-8-jessie-v20160803"
  1289  		}
  1290  
  1291  		network_interface {
  1292  			network = "${google_compute_network.inst-test-network.name}"
  1293  			access_config {	}
  1294  		}
  1295  
  1296  	}`, acctest.RandString(10), instance)
  1297  }
  1298  
  1299  func testAccComputeInstance_subnet_custom(instance string) string {
  1300  	return fmt.Sprintf(`
  1301  	resource "google_compute_network" "inst-test-network" {
  1302  		name = "inst-test-network-%s"
  1303  		auto_create_subnetworks = false
  1304  	}
  1305  
  1306  	resource "google_compute_subnetwork" "inst-test-subnetwork" {
  1307  		name = "inst-test-subnetwork-%s"
  1308  		ip_cidr_range = "10.0.0.0/16"
  1309  		region = "us-central1"
  1310  		network = "${google_compute_network.inst-test-network.self_link}"
  1311  	}
  1312  
  1313  	resource "google_compute_instance" "foobar" {
  1314  		name = "%s"
  1315  		machine_type = "n1-standard-1"
  1316  		zone = "us-central1-a"
  1317  
  1318  		disk {
  1319  			image = "debian-8-jessie-v20160803"
  1320  		}
  1321  
  1322  		network_interface {
  1323  			subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
  1324  			access_config {	}
  1325  		}
  1326  
  1327  	}`, acctest.RandString(10), acctest.RandString(10), instance)
  1328  }
  1329  
  1330  func testAccComputeInstance_subnet_xpn(instance, xpn_host string) string {
  1331  	return fmt.Sprintf(`
  1332  	resource "google_compute_network" "inst-test-network" {
  1333  		name = "inst-test-network-%s"
  1334  		auto_create_subnetworks = false
  1335  		project = "%s"
  1336  	}
  1337  
  1338  	resource "google_compute_subnetwork" "inst-test-subnetwork" {
  1339  		name = "inst-test-subnetwork-%s"
  1340  		ip_cidr_range = "10.0.0.0/16"
  1341  		region = "us-central1"
  1342  		network = "${google_compute_network.inst-test-network.self_link}"
  1343  		project = "%s"
  1344  	}
  1345  
  1346  	resource "google_compute_instance" "foobar" {
  1347  		name = "%s"
  1348  		machine_type = "n1-standard-1"
  1349  		zone = "us-central1-a"
  1350  
  1351  		disk {
  1352  			image = "debian-8-jessie-v20160803"
  1353  		}
  1354  
  1355  		network_interface {
  1356  			subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
  1357  			subnetwork_project = "${google_compute_subnetwork.inst-test-subnetwork.project}"
  1358  			access_config {	}
  1359  		}
  1360  
  1361  	}`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, instance)
  1362  }
  1363  
  1364  func testAccComputeInstance_address_auto(instance string) string {
  1365  	return fmt.Sprintf(`
  1366  	resource "google_compute_network" "inst-test-network" {
  1367  		name = "inst-test-network-%s"
  1368  	}
  1369  	resource "google_compute_subnetwork" "inst-test-subnetwork" {
  1370  		name = "inst-test-subnetwork-%s"
  1371  		ip_cidr_range = "10.0.0.0/16"
  1372  		region = "us-central1"
  1373  		network = "${google_compute_network.inst-test-network.self_link}"
  1374  	}
  1375  	resource "google_compute_instance" "foobar" {
  1376  		name = "%s"
  1377  		machine_type = "n1-standard-1"
  1378  		zone = "us-central1-a"
  1379  
  1380  		disk {
  1381  			image = "debian-8-jessie-v20160803"
  1382  		}
  1383  
  1384  		network_interface {
  1385  			subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
  1386  			access_config {	}
  1387  		}
  1388  
  1389  	}`, acctest.RandString(10), acctest.RandString(10), instance)
  1390  }
  1391  
  1392  func testAccComputeInstance_address_custom(instance, address string) string {
  1393  	return fmt.Sprintf(`
  1394  	resource "google_compute_network" "inst-test-network" {
  1395  		name = "inst-test-network-%s"
  1396  	}
  1397  	resource "google_compute_subnetwork" "inst-test-subnetwork" {
  1398  		name = "inst-test-subnetwork-%s"
  1399  		ip_cidr_range = "10.0.0.0/16"
  1400  		region = "us-central1"
  1401  		network = "${google_compute_network.inst-test-network.self_link}"
  1402  	}
  1403  	resource "google_compute_instance" "foobar" {
  1404  		name = "%s"
  1405  		machine_type = "n1-standard-1"
  1406  		zone = "us-central1-a"
  1407  
  1408  		disk {
  1409  			image = "debian-8-jessie-v20160803"
  1410  		}
  1411  
  1412  		network_interface {
  1413  			subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
  1414  		    address = "%s"
  1415  			access_config {	}
  1416  		}
  1417  
  1418  	}`, acctest.RandString(10), acctest.RandString(10), instance, address)
  1419  }
  1420  
  1421  func testAccComputeInstance_private_image_family(disk, image, family, instance string) string {
  1422  	return fmt.Sprintf(`
  1423  		resource "google_compute_disk" "foobar" {
  1424  			name = "%s"
  1425  			zone = "us-central1-a"
  1426  			image = "debian-8-jessie-v20160803"
  1427  		}
  1428  
  1429  		resource "google_compute_image" "foobar" {
  1430  			name = "%s"
  1431  			source_disk = "${google_compute_disk.foobar.self_link}"
  1432  			family = "%s"
  1433  		}
  1434  
  1435  		resource "google_compute_instance" "foobar" {
  1436  			name = "%s"
  1437  			machine_type = "n1-standard-1"
  1438  			zone = "us-central1-a"
  1439  
  1440  			disk {
  1441  				image = "${google_compute_image.foobar.family}"
  1442  			}
  1443  
  1444  			network_interface {
  1445  				network = "default"
  1446  			}
  1447  
  1448  			metadata {
  1449  				foo = "bar"
  1450  			}
  1451  		}`, disk, image, family, instance)
  1452  }
  1453  
  1454  func testAccComputeInstance_invalid_disk(disk, instance string) string {
  1455  	return fmt.Sprintf(`
  1456  		resource "google_compute_instance" "foobar" {
  1457  		  name         = "%s"
  1458  		  machine_type = "f1-micro"
  1459  		  zone         = "us-central1-a"
  1460  
  1461  		  disk {
  1462  		    image = "ubuntu-os-cloud/ubuntu-1604-lts"
  1463  		    type  = "pd-standard"
  1464  		  }
  1465  
  1466  		  disk {
  1467  		    disk        = "${google_compute_disk.foobar.name}"
  1468  		    type        = "pd-standard"
  1469  		    device_name = "xvdb"
  1470  		  }
  1471  
  1472  		  network_interface {
  1473  		    network = "default"
  1474  		  }
  1475  		}
  1476  
  1477  		resource "google_compute_disk" "foobar" {
  1478  		  name = "%s"
  1479  		  zone = "us-central1-a"
  1480  		  type = "pd-standard"
  1481  		  size = "1"
  1482  		}`, instance, disk)
  1483  }