github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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_local_ssd(t *testing.T) { 248 var instance compute.Instance 249 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 250 251 resource.Test(t, resource.TestCase{ 252 PreCheck: func() { testAccPreCheck(t) }, 253 Providers: testAccProviders, 254 CheckDestroy: testAccCheckComputeInstanceDestroy, 255 Steps: []resource.TestStep{ 256 resource.TestStep{ 257 Config: testAccComputeInstance_local_ssd(instanceName), 258 Check: resource.ComposeTestCheckFunc( 259 testAccCheckComputeInstanceExists( 260 "google_compute_instance.local-ssd", &instance), 261 testAccCheckComputeInstanceDisk(&instance, instanceName, true, true), 262 ), 263 }, 264 }, 265 }) 266 } 267 268 func TestAccComputeInstance_update_deprecated_network(t *testing.T) { 269 var instance compute.Instance 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_basic_deprecated_network(instanceName), 279 Check: resource.ComposeTestCheckFunc( 280 testAccCheckComputeInstanceExists( 281 "google_compute_instance.foobar", &instance), 282 ), 283 }, 284 resource.TestStep{ 285 Config: testAccComputeInstance_update_deprecated_network(instanceName), 286 Check: resource.ComposeTestCheckFunc( 287 testAccCheckComputeInstanceExists( 288 "google_compute_instance.foobar", &instance), 289 testAccCheckComputeInstanceMetadata( 290 &instance, "bar", "baz"), 291 testAccCheckComputeInstanceTag(&instance, "baz"), 292 ), 293 }, 294 }, 295 }) 296 } 297 298 func TestAccComputeInstance_forceNewAndChangeMetadata(t *testing.T) { 299 var instance compute.Instance 300 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 301 302 resource.Test(t, resource.TestCase{ 303 PreCheck: func() { testAccPreCheck(t) }, 304 Providers: testAccProviders, 305 CheckDestroy: testAccCheckComputeInstanceDestroy, 306 Steps: []resource.TestStep{ 307 resource.TestStep{ 308 Config: testAccComputeInstance_basic(instanceName), 309 Check: resource.ComposeTestCheckFunc( 310 testAccCheckComputeInstanceExists( 311 "google_compute_instance.foobar", &instance), 312 ), 313 }, 314 resource.TestStep{ 315 Config: testAccComputeInstance_forceNewAndChangeMetadata(instanceName), 316 Check: resource.ComposeTestCheckFunc( 317 testAccCheckComputeInstanceExists( 318 "google_compute_instance.foobar", &instance), 319 testAccCheckComputeInstanceMetadata( 320 &instance, "qux", "true"), 321 ), 322 }, 323 }, 324 }) 325 } 326 327 func TestAccComputeInstance_update(t *testing.T) { 328 var instance compute.Instance 329 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 330 331 resource.Test(t, resource.TestCase{ 332 PreCheck: func() { testAccPreCheck(t) }, 333 Providers: testAccProviders, 334 CheckDestroy: testAccCheckComputeInstanceDestroy, 335 Steps: []resource.TestStep{ 336 resource.TestStep{ 337 Config: testAccComputeInstance_basic(instanceName), 338 Check: resource.ComposeTestCheckFunc( 339 testAccCheckComputeInstanceExists( 340 "google_compute_instance.foobar", &instance), 341 ), 342 }, 343 resource.TestStep{ 344 Config: testAccComputeInstance_update(instanceName), 345 Check: resource.ComposeTestCheckFunc( 346 testAccCheckComputeInstanceExists( 347 "google_compute_instance.foobar", &instance), 348 testAccCheckComputeInstanceMetadata( 349 &instance, "bar", "baz"), 350 testAccCheckComputeInstanceTag(&instance, "baz"), 351 testAccCheckComputeInstanceAccessConfig(&instance), 352 ), 353 }, 354 }, 355 }) 356 } 357 358 func TestAccComputeInstance_service_account(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_service_account(instanceName), 369 Check: resource.ComposeTestCheckFunc( 370 testAccCheckComputeInstanceExists( 371 "google_compute_instance.foobar", &instance), 372 testAccCheckComputeInstanceServiceAccount(&instance, 373 "https://www.googleapis.com/auth/compute.readonly"), 374 testAccCheckComputeInstanceServiceAccount(&instance, 375 "https://www.googleapis.com/auth/devstorage.read_only"), 376 testAccCheckComputeInstanceServiceAccount(&instance, 377 "https://www.googleapis.com/auth/userinfo.email"), 378 ), 379 }, 380 }, 381 }) 382 } 383 384 func TestAccComputeInstance_scheduling(t *testing.T) { 385 var instance compute.Instance 386 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 387 388 resource.Test(t, resource.TestCase{ 389 PreCheck: func() { testAccPreCheck(t) }, 390 Providers: testAccProviders, 391 CheckDestroy: testAccCheckComputeInstanceDestroy, 392 Steps: []resource.TestStep{ 393 resource.TestStep{ 394 Config: testAccComputeInstance_scheduling(instanceName), 395 Check: resource.ComposeTestCheckFunc( 396 testAccCheckComputeInstanceExists( 397 "google_compute_instance.foobar", &instance), 398 ), 399 }, 400 }, 401 }) 402 } 403 404 func TestAccComputeInstance_subnet_auto(t *testing.T) { 405 var instance compute.Instance 406 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 407 408 resource.Test(t, resource.TestCase{ 409 PreCheck: func() { testAccPreCheck(t) }, 410 Providers: testAccProviders, 411 CheckDestroy: testAccCheckComputeInstanceDestroy, 412 Steps: []resource.TestStep{ 413 resource.TestStep{ 414 Config: testAccComputeInstance_subnet_auto(instanceName), 415 Check: resource.ComposeTestCheckFunc( 416 testAccCheckComputeInstanceExists( 417 "google_compute_instance.foobar", &instance), 418 testAccCheckComputeInstanceHasSubnet(&instance), 419 ), 420 }, 421 }, 422 }) 423 } 424 425 func TestAccComputeInstance_subnet_custom(t *testing.T) { 426 var instance compute.Instance 427 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 428 429 resource.Test(t, resource.TestCase{ 430 PreCheck: func() { testAccPreCheck(t) }, 431 Providers: testAccProviders, 432 CheckDestroy: testAccCheckComputeInstanceDestroy, 433 Steps: []resource.TestStep{ 434 resource.TestStep{ 435 Config: testAccComputeInstance_subnet_custom(instanceName), 436 Check: resource.ComposeTestCheckFunc( 437 testAccCheckComputeInstanceExists( 438 "google_compute_instance.foobar", &instance), 439 testAccCheckComputeInstanceHasSubnet(&instance), 440 ), 441 }, 442 }, 443 }) 444 } 445 446 func TestAccComputeInstance_subnet_xpn(t *testing.T) { 447 var instance compute.Instance 448 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 449 var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT") 450 451 resource.Test(t, resource.TestCase{ 452 PreCheck: func() { testAccPreCheck(t) }, 453 Providers: testAccProviders, 454 CheckDestroy: testAccCheckComputeInstanceDestroy, 455 Steps: []resource.TestStep{ 456 resource.TestStep{ 457 Config: testAccComputeInstance_subnet_xpn(instanceName, xpn_host), 458 Check: resource.ComposeTestCheckFunc( 459 testAccCheckComputeInstanceExists( 460 "google_compute_instance.foobar", &instance), 461 testAccCheckComputeInstanceHasSubnet(&instance), 462 ), 463 }, 464 }, 465 }) 466 } 467 468 func TestAccComputeInstance_address_auto(t *testing.T) { 469 var instance compute.Instance 470 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 471 472 resource.Test(t, resource.TestCase{ 473 PreCheck: func() { testAccPreCheck(t) }, 474 Providers: testAccProviders, 475 CheckDestroy: testAccCheckComputeInstanceDestroy, 476 Steps: []resource.TestStep{ 477 resource.TestStep{ 478 Config: testAccComputeInstance_address_auto(instanceName), 479 Check: resource.ComposeTestCheckFunc( 480 testAccCheckComputeInstanceExists( 481 "google_compute_instance.foobar", &instance), 482 testAccCheckComputeInstanceHasAnyAddress(&instance), 483 ), 484 }, 485 }, 486 }) 487 } 488 489 func TestAccComputeInstance_address_custom(t *testing.T) { 490 var instance compute.Instance 491 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 492 var address = "10.0.200.200" 493 resource.Test(t, resource.TestCase{ 494 PreCheck: func() { testAccPreCheck(t) }, 495 Providers: testAccProviders, 496 CheckDestroy: testAccCheckComputeInstanceDestroy, 497 Steps: []resource.TestStep{ 498 resource.TestStep{ 499 Config: testAccComputeInstance_address_custom(instanceName, address), 500 Check: resource.ComposeTestCheckFunc( 501 testAccCheckComputeInstanceExists( 502 "google_compute_instance.foobar", &instance), 503 testAccCheckComputeInstanceHasAddress(&instance, address), 504 ), 505 }, 506 }, 507 }) 508 } 509 510 func TestAccComputeInstance_private_image_family(t *testing.T) { 511 var instance compute.Instance 512 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 513 var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10)) 514 var imageName = fmt.Sprintf("instance-testi-%s", acctest.RandString(10)) 515 var familyName = fmt.Sprintf("instance-testf-%s", acctest.RandString(10)) 516 517 resource.Test(t, resource.TestCase{ 518 PreCheck: func() { testAccPreCheck(t) }, 519 Providers: testAccProviders, 520 CheckDestroy: testAccCheckComputeInstanceDestroy, 521 Steps: []resource.TestStep{ 522 resource.TestStep{ 523 Config: testAccComputeInstance_private_image_family(diskName, imageName, familyName, instanceName), 524 Check: resource.ComposeTestCheckFunc( 525 testAccCheckComputeInstanceExists( 526 "google_compute_instance.foobar", &instance), 527 ), 528 }, 529 }, 530 }) 531 } 532 533 func TestAccComputeInstance_invalid_disk(t *testing.T) { 534 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 535 var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10)) 536 537 resource.Test(t, resource.TestCase{ 538 PreCheck: func() { testAccPreCheck(t) }, 539 Providers: testAccProviders, 540 CheckDestroy: testAccCheckComputeInstanceDestroy, 541 Steps: []resource.TestStep{ 542 resource.TestStep{ 543 Config: testAccComputeInstance_invalid_disk(diskName, instanceName), 544 ExpectError: regexp.MustCompile("Error: cannot define both disk and type."), 545 }, 546 }, 547 }) 548 } 549 550 func TestAccComputeInstance_forceChangeMachineTypeManually(t *testing.T) { 551 var instance compute.Instance 552 var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) 553 554 resource.Test(t, resource.TestCase{ 555 PreCheck: func() { testAccPreCheck(t) }, 556 Providers: testAccProviders, 557 CheckDestroy: testAccCheckComputeInstanceDestroy, 558 Steps: []resource.TestStep{ 559 resource.TestStep{ 560 Config: testAccComputeInstance_basic(instanceName), 561 Check: resource.ComposeTestCheckFunc( 562 testAccCheckComputeInstanceExists("google_compute_instance.foobar", &instance), 563 testAccCheckComputeInstanceUpdateMachineType("google_compute_instance.foobar"), 564 ), 565 ExpectNonEmptyPlan: true, 566 }, 567 }, 568 }) 569 } 570 571 func testAccCheckComputeInstanceUpdateMachineType(n string) resource.TestCheckFunc { 572 return func(s *terraform.State) error { 573 rs, ok := s.RootModule().Resources[n] 574 if !ok { 575 return fmt.Errorf("Not found: %s", n) 576 } 577 578 if rs.Primary.ID == "" { 579 return fmt.Errorf("No ID is set") 580 } 581 582 config := testAccProvider.Meta().(*Config) 583 584 op, err := config.clientCompute.Instances.Stop(config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 585 if err != nil { 586 return fmt.Errorf("Could not stop instance: %s", err) 587 } 588 err = computeOperationWaitZone(config, op, config.Project, rs.Primary.Attributes["zone"], "Waiting on stop") 589 if err != nil { 590 return fmt.Errorf("Could not stop instance: %s", err) 591 } 592 593 machineType := compute.InstancesSetMachineTypeRequest{ 594 MachineType: "zones/us-central1-a/machineTypes/f1-micro", 595 } 596 597 op, err = config.clientCompute.Instances.SetMachineType( 598 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID, &machineType).Do() 599 if err != nil { 600 return fmt.Errorf("Could not change machine type: %s", err) 601 } 602 err = computeOperationWaitZone(config, op, config.Project, rs.Primary.Attributes["zone"], "Waiting machine type change") 603 if err != nil { 604 return fmt.Errorf("Could not change machine type: %s", err) 605 } 606 return nil 607 } 608 } 609 610 func testAccCheckComputeInstanceDestroy(s *terraform.State) error { 611 config := testAccProvider.Meta().(*Config) 612 613 for _, rs := range s.RootModule().Resources { 614 if rs.Type != "google_compute_instance" { 615 continue 616 } 617 618 _, err := config.clientCompute.Instances.Get( 619 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 620 if err == nil { 621 return fmt.Errorf("Instance still exists") 622 } 623 } 624 625 return nil 626 } 627 628 func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc { 629 return func(s *terraform.State) error { 630 rs, ok := s.RootModule().Resources[n] 631 if !ok { 632 return fmt.Errorf("Not found: %s", n) 633 } 634 635 if rs.Primary.ID == "" { 636 return fmt.Errorf("No ID is set") 637 } 638 639 config := testAccProvider.Meta().(*Config) 640 641 found, err := config.clientCompute.Instances.Get( 642 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 643 if err != nil { 644 return err 645 } 646 647 if found.Name != rs.Primary.ID { 648 return fmt.Errorf("Instance not found") 649 } 650 651 *instance = *found 652 653 return nil 654 } 655 } 656 657 func testAccCheckComputeInstanceMetadata( 658 instance *compute.Instance, 659 k string, v string) resource.TestCheckFunc { 660 return func(s *terraform.State) error { 661 if instance.Metadata == nil { 662 return fmt.Errorf("no metadata") 663 } 664 665 for _, item := range instance.Metadata.Items { 666 if k != item.Key { 667 continue 668 } 669 670 if item.Value != nil && v == *item.Value { 671 return nil 672 } 673 674 return fmt.Errorf("bad value for %s: %s", k, *item.Value) 675 } 676 677 return fmt.Errorf("metadata not found: %s", k) 678 } 679 } 680 681 func testAccCheckComputeInstanceAccessConfig(instance *compute.Instance) resource.TestCheckFunc { 682 return func(s *terraform.State) error { 683 for _, i := range instance.NetworkInterfaces { 684 if len(i.AccessConfigs) == 0 { 685 return fmt.Errorf("no access_config") 686 } 687 } 688 689 return nil 690 } 691 } 692 693 func testAccCheckComputeInstanceAccessConfigHasIP(instance *compute.Instance) resource.TestCheckFunc { 694 return func(s *terraform.State) error { 695 for _, i := range instance.NetworkInterfaces { 696 for _, c := range i.AccessConfigs { 697 if c.NatIP == "" { 698 return fmt.Errorf("no NAT IP") 699 } 700 } 701 } 702 703 return nil 704 } 705 } 706 707 func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc { 708 return func(s *terraform.State) error { 709 if instance.Disks == nil { 710 return fmt.Errorf("no disks") 711 } 712 713 for _, disk := range instance.Disks { 714 if strings.LastIndex(disk.Source, "/"+source) == len(disk.Source)-len(source)-1 && disk.AutoDelete == delete && disk.Boot == boot { 715 return nil 716 } 717 } 718 719 return fmt.Errorf("Disk not found: %s", source) 720 } 721 } 722 723 func testAccCheckComputeInstanceDiskEncryptionKey(n string, instance *compute.Instance) resource.TestCheckFunc { 724 return func(s *terraform.State) error { 725 rs, ok := s.RootModule().Resources[n] 726 if !ok { 727 return fmt.Errorf("Not found: %s", n) 728 } 729 730 for i, disk := range instance.Disks { 731 attr := rs.Primary.Attributes[fmt.Sprintf("disk.%d.disk_encryption_key_sha256", i)] 732 if disk.DiskEncryptionKey == nil && attr != "" { 733 return fmt.Errorf("Disk %d has mismatched encryption key.\nTF State: %+v\nGCP State: <empty>", i, attr) 734 } 735 if disk.DiskEncryptionKey != nil && attr != disk.DiskEncryptionKey.Sha256 { 736 return fmt.Errorf("Disk %d has mismatched encryption key.\nTF State: %+v\nGCP State: %+v", 737 i, attr, disk.DiskEncryptionKey.Sha256) 738 } 739 } 740 return nil 741 } 742 } 743 744 func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc { 745 return func(s *terraform.State) error { 746 if instance.Tags == nil { 747 return fmt.Errorf("no tags") 748 } 749 750 for _, k := range instance.Tags.Items { 751 if k == n { 752 return nil 753 } 754 } 755 756 return fmt.Errorf("tag not found: %s", n) 757 } 758 } 759 760 func testAccCheckComputeInstanceServiceAccount(instance *compute.Instance, scope string) resource.TestCheckFunc { 761 return func(s *terraform.State) error { 762 if count := len(instance.ServiceAccounts); count != 1 { 763 return fmt.Errorf("Wrong number of ServiceAccounts: expected 1, got %d", count) 764 } 765 766 for _, val := range instance.ServiceAccounts[0].Scopes { 767 if val == scope { 768 return nil 769 } 770 } 771 772 return fmt.Errorf("Scope not found: %s", scope) 773 } 774 } 775 776 func testAccCheckComputeInstanceHasSubnet(instance *compute.Instance) resource.TestCheckFunc { 777 return func(s *terraform.State) error { 778 for _, i := range instance.NetworkInterfaces { 779 if i.Subnetwork == "" { 780 return fmt.Errorf("no subnet") 781 } 782 } 783 784 return nil 785 } 786 } 787 788 func testAccCheckComputeInstanceHasAnyAddress(instance *compute.Instance) resource.TestCheckFunc { 789 return func(s *terraform.State) error { 790 for _, i := range instance.NetworkInterfaces { 791 if i.NetworkIP == "" { 792 return fmt.Errorf("no address") 793 } 794 } 795 796 return nil 797 } 798 } 799 800 func testAccCheckComputeInstanceHasAddress(instance *compute.Instance, address string) resource.TestCheckFunc { 801 return func(s *terraform.State) error { 802 for _, i := range instance.NetworkInterfaces { 803 if i.NetworkIP != address { 804 return fmt.Errorf("Wrong address found: expected %v, got %v", address, i.NetworkIP) 805 } 806 } 807 808 return nil 809 } 810 } 811 812 func testAccComputeInstance_basic_deprecated_network(instance string) string { 813 return fmt.Sprintf(` 814 resource "google_compute_instance" "foobar" { 815 name = "%s" 816 machine_type = "n1-standard-1" 817 zone = "us-central1-a" 818 can_ip_forward = false 819 tags = ["foo", "bar"] 820 821 disk { 822 image = "debian-8-jessie-v20160803" 823 } 824 825 network { 826 source = "default" 827 } 828 829 metadata { 830 foo = "bar" 831 } 832 }`, instance) 833 } 834 835 func testAccComputeInstance_update_deprecated_network(instance string) string { 836 return fmt.Sprintf(` 837 resource "google_compute_instance" "foobar" { 838 name = "%s" 839 machine_type = "n1-standard-1" 840 zone = "us-central1-a" 841 tags = ["baz"] 842 843 disk { 844 image = "debian-8-jessie-v20160803" 845 } 846 847 network { 848 source = "default" 849 } 850 851 metadata { 852 bar = "baz" 853 } 854 }`, instance) 855 } 856 857 func testAccComputeInstance_basic(instance string) string { 858 return fmt.Sprintf(` 859 resource "google_compute_instance" "foobar" { 860 name = "%s" 861 machine_type = "n1-standard-1" 862 zone = "us-central1-a" 863 can_ip_forward = false 864 tags = ["foo", "bar"] 865 866 disk { 867 image = "debian-8-jessie-v20160803" 868 } 869 870 network_interface { 871 network = "default" 872 } 873 874 metadata { 875 foo = "bar" 876 baz = "qux" 877 } 878 879 create_timeout = 5 880 881 metadata_startup_script = "echo Hello" 882 }`, instance) 883 } 884 885 func testAccComputeInstance_basic2(instance string) string { 886 return fmt.Sprintf(` 887 resource "google_compute_instance" "foobar" { 888 name = "%s" 889 machine_type = "n1-standard-1" 890 zone = "us-central1-a" 891 can_ip_forward = false 892 tags = ["foo", "bar"] 893 894 disk { 895 image = "debian-8" 896 } 897 898 network_interface { 899 network = "default" 900 } 901 902 metadata { 903 foo = "bar" 904 } 905 }`, instance) 906 } 907 908 func testAccComputeInstance_basic3(instance string) string { 909 return fmt.Sprintf(` 910 resource "google_compute_instance" "foobar" { 911 name = "%s" 912 machine_type = "n1-standard-1" 913 zone = "us-central1-a" 914 can_ip_forward = false 915 tags = ["foo", "bar"] 916 917 disk { 918 image = "debian-cloud/debian-8-jessie-v20160803" 919 } 920 921 network_interface { 922 network = "default" 923 } 924 925 926 metadata { 927 foo = "bar" 928 } 929 }`, instance) 930 } 931 932 func testAccComputeInstance_basic4(instance string) string { 933 return fmt.Sprintf(` 934 resource "google_compute_instance" "foobar" { 935 name = "%s" 936 machine_type = "n1-standard-1" 937 zone = "us-central1-a" 938 can_ip_forward = false 939 tags = ["foo", "bar"] 940 941 disk { 942 image = "debian-cloud/debian-8" 943 } 944 945 network_interface { 946 network = "default" 947 } 948 949 950 metadata { 951 foo = "bar" 952 } 953 }`, instance) 954 } 955 956 func testAccComputeInstance_basic5(instance string) string { 957 return fmt.Sprintf(` 958 resource "google_compute_instance" "foobar" { 959 name = "%s" 960 machine_type = "n1-standard-1" 961 zone = "us-central1-a" 962 can_ip_forward = false 963 tags = ["foo", "bar"] 964 965 disk { 966 image = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160803" 967 } 968 969 network_interface { 970 network = "default" 971 } 972 973 metadata { 974 foo = "bar" 975 } 976 }`, instance) 977 } 978 979 // Update zone to ForceNew, and change metadata k/v entirely 980 // Generates diff mismatch 981 func testAccComputeInstance_forceNewAndChangeMetadata(instance string) string { 982 return fmt.Sprintf(` 983 resource "google_compute_instance" "foobar" { 984 name = "%s" 985 machine_type = "n1-standard-1" 986 zone = "us-central1-a" 987 zone = "us-central1-b" 988 tags = ["baz"] 989 990 disk { 991 image = "debian-8-jessie-v20160803" 992 } 993 994 network_interface { 995 network = "default" 996 access_config { } 997 } 998 999 metadata { 1000 qux = "true" 1001 } 1002 }`, instance) 1003 } 1004 1005 // Update metadata, tags, and network_interface 1006 func testAccComputeInstance_update(instance string) string { 1007 return fmt.Sprintf(` 1008 resource "google_compute_instance" "foobar" { 1009 name = "%s" 1010 machine_type = "n1-standard-1" 1011 zone = "us-central1-a" 1012 tags = ["baz"] 1013 1014 disk { 1015 image = "debian-8-jessie-v20160803" 1016 } 1017 1018 network_interface { 1019 network = "default" 1020 access_config { } 1021 } 1022 1023 metadata { 1024 bar = "baz" 1025 } 1026 }`, instance) 1027 } 1028 1029 func testAccComputeInstance_ip(ip, instance string) string { 1030 return fmt.Sprintf(` 1031 resource "google_compute_address" "foo" { 1032 name = "%s" 1033 } 1034 1035 resource "google_compute_instance" "foobar" { 1036 name = "%s" 1037 machine_type = "n1-standard-1" 1038 zone = "us-central1-a" 1039 tags = ["foo", "bar"] 1040 1041 disk { 1042 image = "debian-8-jessie-v20160803" 1043 } 1044 1045 network_interface { 1046 network = "default" 1047 access_config { 1048 nat_ip = "${google_compute_address.foo.address}" 1049 } 1050 } 1051 1052 metadata { 1053 foo = "bar" 1054 } 1055 }`, ip, instance) 1056 } 1057 1058 func testAccComputeInstance_disks(disk, instance string, autodelete bool) string { 1059 return fmt.Sprintf(` 1060 resource "google_compute_disk" "foobar" { 1061 name = "%s" 1062 size = 10 1063 type = "pd-ssd" 1064 zone = "us-central1-a" 1065 } 1066 1067 resource "google_compute_instance" "foobar" { 1068 name = "%s" 1069 machine_type = "n1-standard-1" 1070 zone = "us-central1-a" 1071 1072 disk { 1073 image = "debian-8-jessie-v20160803" 1074 } 1075 1076 disk { 1077 disk = "${google_compute_disk.foobar.name}" 1078 auto_delete = %v 1079 } 1080 1081 network_interface { 1082 network = "default" 1083 } 1084 1085 metadata { 1086 foo = "bar" 1087 } 1088 }`, disk, instance, autodelete) 1089 } 1090 1091 func testAccComputeInstance_disks_encryption(disk, instance string) string { 1092 return fmt.Sprintf(` 1093 resource "google_compute_disk" "foobar" { 1094 name = "%s" 1095 size = 10 1096 type = "pd-ssd" 1097 zone = "us-central1-a" 1098 } 1099 1100 resource "google_compute_instance" "foobar" { 1101 name = "%s" 1102 machine_type = "n1-standard-1" 1103 zone = "us-central1-a" 1104 1105 disk { 1106 image = "debian-8-jessie-v20160803" 1107 disk_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0=" 1108 } 1109 1110 disk { 1111 disk = "${google_compute_disk.foobar.name}" 1112 } 1113 1114 network_interface { 1115 network = "default" 1116 } 1117 1118 metadata { 1119 foo = "bar" 1120 } 1121 }`, disk, instance) 1122 } 1123 1124 func testAccComputeInstance_local_ssd(instance string) string { 1125 return fmt.Sprintf(` 1126 resource "google_compute_instance" "local-ssd" { 1127 name = "%s" 1128 machine_type = "n1-standard-1" 1129 zone = "us-central1-a" 1130 1131 disk { 1132 image = "debian-8-jessie-v20160803" 1133 } 1134 1135 disk { 1136 type = "local-ssd" 1137 scratch = true 1138 } 1139 1140 network_interface { 1141 network = "default" 1142 } 1143 1144 }`, instance) 1145 } 1146 1147 func testAccComputeInstance_service_account(instance string) string { 1148 return fmt.Sprintf(` 1149 resource "google_compute_instance" "foobar" { 1150 name = "%s" 1151 machine_type = "n1-standard-1" 1152 zone = "us-central1-a" 1153 1154 disk { 1155 image = "debian-8-jessie-v20160803" 1156 } 1157 1158 network_interface { 1159 network = "default" 1160 } 1161 1162 service_account { 1163 scopes = [ 1164 "userinfo-email", 1165 "compute-ro", 1166 "storage-ro", 1167 ] 1168 } 1169 }`, instance) 1170 } 1171 1172 func testAccComputeInstance_scheduling(instance string) string { 1173 return fmt.Sprintf(` 1174 resource "google_compute_instance" "foobar" { 1175 name = "%s" 1176 machine_type = "n1-standard-1" 1177 zone = "us-central1-a" 1178 1179 disk { 1180 image = "debian-8-jessie-v20160803" 1181 } 1182 1183 network_interface { 1184 network = "default" 1185 } 1186 1187 scheduling { 1188 } 1189 }`, instance) 1190 } 1191 1192 func testAccComputeInstance_subnet_auto(instance string) string { 1193 return fmt.Sprintf(` 1194 resource "google_compute_network" "inst-test-network" { 1195 name = "inst-test-network-%s" 1196 auto_create_subnetworks = true 1197 } 1198 1199 resource "google_compute_instance" "foobar" { 1200 name = "%s" 1201 machine_type = "n1-standard-1" 1202 zone = "us-central1-a" 1203 1204 disk { 1205 image = "debian-8-jessie-v20160803" 1206 } 1207 1208 network_interface { 1209 network = "${google_compute_network.inst-test-network.name}" 1210 access_config { } 1211 } 1212 1213 }`, acctest.RandString(10), instance) 1214 } 1215 1216 func testAccComputeInstance_subnet_custom(instance string) string { 1217 return fmt.Sprintf(` 1218 resource "google_compute_network" "inst-test-network" { 1219 name = "inst-test-network-%s" 1220 auto_create_subnetworks = false 1221 } 1222 1223 resource "google_compute_subnetwork" "inst-test-subnetwork" { 1224 name = "inst-test-subnetwork-%s" 1225 ip_cidr_range = "10.0.0.0/16" 1226 region = "us-central1" 1227 network = "${google_compute_network.inst-test-network.self_link}" 1228 } 1229 1230 resource "google_compute_instance" "foobar" { 1231 name = "%s" 1232 machine_type = "n1-standard-1" 1233 zone = "us-central1-a" 1234 1235 disk { 1236 image = "debian-8-jessie-v20160803" 1237 } 1238 1239 network_interface { 1240 subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}" 1241 access_config { } 1242 } 1243 1244 }`, acctest.RandString(10), acctest.RandString(10), instance) 1245 } 1246 1247 func testAccComputeInstance_subnet_xpn(instance, xpn_host string) string { 1248 return fmt.Sprintf(` 1249 resource "google_compute_network" "inst-test-network" { 1250 name = "inst-test-network-%s" 1251 auto_create_subnetworks = false 1252 project = "%s" 1253 } 1254 1255 resource "google_compute_subnetwork" "inst-test-subnetwork" { 1256 name = "inst-test-subnetwork-%s" 1257 ip_cidr_range = "10.0.0.0/16" 1258 region = "us-central1" 1259 network = "${google_compute_network.inst-test-network.self_link}" 1260 project = "%s" 1261 } 1262 1263 resource "google_compute_instance" "foobar" { 1264 name = "%s" 1265 machine_type = "n1-standard-1" 1266 zone = "us-central1-a" 1267 1268 disk { 1269 image = "debian-8-jessie-v20160803" 1270 } 1271 1272 network_interface { 1273 subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}" 1274 subnetwork_project = "${google_compute_subnetwork.inst-test-subnetwork.project}" 1275 access_config { } 1276 } 1277 1278 }`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, instance) 1279 } 1280 1281 func testAccComputeInstance_address_auto(instance string) string { 1282 return fmt.Sprintf(` 1283 resource "google_compute_network" "inst-test-network" { 1284 name = "inst-test-network-%s" 1285 } 1286 resource "google_compute_subnetwork" "inst-test-subnetwork" { 1287 name = "inst-test-subnetwork-%s" 1288 ip_cidr_range = "10.0.0.0/16" 1289 region = "us-central1" 1290 network = "${google_compute_network.inst-test-network.self_link}" 1291 } 1292 resource "google_compute_instance" "foobar" { 1293 name = "%s" 1294 machine_type = "n1-standard-1" 1295 zone = "us-central1-a" 1296 1297 disk { 1298 image = "debian-8-jessie-v20160803" 1299 } 1300 1301 network_interface { 1302 subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}" 1303 access_config { } 1304 } 1305 1306 }`, acctest.RandString(10), acctest.RandString(10), instance) 1307 } 1308 1309 func testAccComputeInstance_address_custom(instance, address string) string { 1310 return fmt.Sprintf(` 1311 resource "google_compute_network" "inst-test-network" { 1312 name = "inst-test-network-%s" 1313 } 1314 resource "google_compute_subnetwork" "inst-test-subnetwork" { 1315 name = "inst-test-subnetwork-%s" 1316 ip_cidr_range = "10.0.0.0/16" 1317 region = "us-central1" 1318 network = "${google_compute_network.inst-test-network.self_link}" 1319 } 1320 resource "google_compute_instance" "foobar" { 1321 name = "%s" 1322 machine_type = "n1-standard-1" 1323 zone = "us-central1-a" 1324 1325 disk { 1326 image = "debian-8-jessie-v20160803" 1327 } 1328 1329 network_interface { 1330 subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}" 1331 address = "%s" 1332 access_config { } 1333 } 1334 1335 }`, acctest.RandString(10), acctest.RandString(10), instance, address) 1336 } 1337 1338 func testAccComputeInstance_private_image_family(disk, image, family, instance string) string { 1339 return fmt.Sprintf(` 1340 resource "google_compute_disk" "foobar" { 1341 name = "%s" 1342 zone = "us-central1-a" 1343 image = "debian-8-jessie-v20160803" 1344 } 1345 1346 resource "google_compute_image" "foobar" { 1347 name = "%s" 1348 source_disk = "${google_compute_disk.foobar.self_link}" 1349 family = "%s" 1350 } 1351 1352 resource "google_compute_instance" "foobar" { 1353 name = "%s" 1354 machine_type = "n1-standard-1" 1355 zone = "us-central1-a" 1356 1357 disk { 1358 image = "${google_compute_image.foobar.family}" 1359 } 1360 1361 network_interface { 1362 network = "default" 1363 } 1364 1365 metadata { 1366 foo = "bar" 1367 } 1368 }`, disk, image, family, instance) 1369 } 1370 1371 func testAccComputeInstance_invalid_disk(disk, instance string) string { 1372 return fmt.Sprintf(` 1373 resource "google_compute_instance" "foobar" { 1374 name = "%s" 1375 machine_type = "f1-micro" 1376 zone = "us-central1-a" 1377 1378 disk { 1379 image = "ubuntu-os-cloud/ubuntu-1604-lts" 1380 type = "pd-standard" 1381 } 1382 1383 disk { 1384 disk = "${google_compute_disk.foobar.name}" 1385 type = "pd-standard" 1386 device_name = "xvdb" 1387 } 1388 1389 network_interface { 1390 network = "default" 1391 } 1392 } 1393 1394 resource "google_compute_disk" "foobar" { 1395 name = "%s" 1396 zone = "us-central1-a" 1397 type = "pd-standard" 1398 size = "1" 1399 }`, instance, disk) 1400 }