github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_instance_template_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 "google.golang.org/api/compute/v1" 13 ) 14 15 func TestAccComputeInstanceTemplate_basic(t *testing.T) { 16 var instanceTemplate compute.InstanceTemplate 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccComputeInstanceTemplate_basic, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckComputeInstanceTemplateExists( 27 "google_compute_instance_template.foobar", &instanceTemplate), 28 testAccCheckComputeInstanceTemplateTag(&instanceTemplate, "foo"), 29 testAccCheckComputeInstanceTemplateMetadata(&instanceTemplate, "foo", "bar"), 30 testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160803", true, true), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccComputeInstanceTemplate_IP(t *testing.T) { 38 var instanceTemplate compute.InstanceTemplate 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: testAccComputeInstanceTemplate_ip, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckComputeInstanceTemplateExists( 49 "google_compute_instance_template.foobar", &instanceTemplate), 50 testAccCheckComputeInstanceTemplateNetwork(&instanceTemplate), 51 ), 52 }, 53 }, 54 }) 55 } 56 57 func TestAccComputeInstanceTemplate_disks(t *testing.T) { 58 var instanceTemplate compute.InstanceTemplate 59 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 64 Steps: []resource.TestStep{ 65 resource.TestStep{ 66 Config: testAccComputeInstanceTemplate_disks, 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckComputeInstanceTemplateExists( 69 "google_compute_instance_template.foobar", &instanceTemplate), 70 testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160803", true, true), 71 testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "terraform-test-foobar", false, false), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func TestAccComputeInstanceTemplate_subnet_auto(t *testing.T) { 79 var instanceTemplate compute.InstanceTemplate 80 network := "network-" + acctest.RandString(10) 81 82 resource.Test(t, resource.TestCase{ 83 PreCheck: func() { testAccPreCheck(t) }, 84 Providers: testAccProviders, 85 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 86 Steps: []resource.TestStep{ 87 resource.TestStep{ 88 Config: testAccComputeInstanceTemplate_subnet_auto(network), 89 Check: resource.ComposeTestCheckFunc( 90 testAccCheckComputeInstanceTemplateExists( 91 "google_compute_instance_template.foobar", &instanceTemplate), 92 testAccCheckComputeInstanceTemplateNetworkName(&instanceTemplate, network), 93 ), 94 }, 95 }, 96 }) 97 } 98 99 func TestAccComputeInstanceTemplate_subnet_custom(t *testing.T) { 100 var instanceTemplate compute.InstanceTemplate 101 102 resource.Test(t, resource.TestCase{ 103 PreCheck: func() { testAccPreCheck(t) }, 104 Providers: testAccProviders, 105 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 106 Steps: []resource.TestStep{ 107 resource.TestStep{ 108 Config: testAccComputeInstanceTemplate_subnet_custom, 109 Check: resource.ComposeTestCheckFunc( 110 testAccCheckComputeInstanceTemplateExists( 111 "google_compute_instance_template.foobar", &instanceTemplate), 112 testAccCheckComputeInstanceTemplateSubnetwork(&instanceTemplate), 113 ), 114 }, 115 }, 116 }) 117 } 118 119 func TestAccComputeInstanceTemplate_subnet_xpn(t *testing.T) { 120 var instanceTemplate compute.InstanceTemplate 121 var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT") 122 123 resource.Test(t, resource.TestCase{ 124 PreCheck: func() { testAccPreCheck(t) }, 125 Providers: testAccProviders, 126 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 127 Steps: []resource.TestStep{ 128 resource.TestStep{ 129 Config: testAccComputeInstanceTemplate_subnet_xpn(xpn_host), 130 Check: resource.ComposeTestCheckFunc( 131 testAccCheckComputeInstanceTemplateExists( 132 "google_compute_instance_template.foobar", &instanceTemplate), 133 testAccCheckComputeInstanceTemplateSubnetwork(&instanceTemplate), 134 ), 135 }, 136 }, 137 }) 138 } 139 140 func TestAccComputeInstanceTemplate_metadata_startup_script(t *testing.T) { 141 var instanceTemplate compute.InstanceTemplate 142 143 resource.Test(t, resource.TestCase{ 144 PreCheck: func() { testAccPreCheck(t) }, 145 Providers: testAccProviders, 146 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 147 Steps: []resource.TestStep{ 148 resource.TestStep{ 149 Config: testAccComputeInstanceTemplate_startup_script, 150 Check: resource.ComposeTestCheckFunc( 151 testAccCheckComputeInstanceTemplateExists( 152 "google_compute_instance_template.foobar", &instanceTemplate), 153 testAccCheckComputeInstanceTemplateStartupScript(&instanceTemplate, "echo 'Hello'"), 154 ), 155 }, 156 }, 157 }) 158 } 159 160 func testAccCheckComputeInstanceTemplateDestroy(s *terraform.State) error { 161 config := testAccProvider.Meta().(*Config) 162 163 for _, rs := range s.RootModule().Resources { 164 if rs.Type != "google_compute_instance_template" { 165 continue 166 } 167 168 _, err := config.clientCompute.InstanceTemplates.Get( 169 config.Project, rs.Primary.ID).Do() 170 if err == nil { 171 return fmt.Errorf("Instance template still exists") 172 } 173 } 174 175 return nil 176 } 177 178 func testAccCheckComputeInstanceTemplateExists(n string, instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc { 179 return func(s *terraform.State) error { 180 rs, ok := s.RootModule().Resources[n] 181 if !ok { 182 return fmt.Errorf("Not found: %s", n) 183 } 184 185 if rs.Primary.ID == "" { 186 return fmt.Errorf("No ID is set") 187 } 188 189 config := testAccProvider.Meta().(*Config) 190 191 found, err := config.clientCompute.InstanceTemplates.Get( 192 config.Project, rs.Primary.ID).Do() 193 if err != nil { 194 return err 195 } 196 197 if found.Name != rs.Primary.ID { 198 return fmt.Errorf("Instance template not found") 199 } 200 201 *instanceTemplate = *found 202 203 return nil 204 } 205 } 206 207 func testAccCheckComputeInstanceTemplateMetadata( 208 instanceTemplate *compute.InstanceTemplate, 209 k string, v string) resource.TestCheckFunc { 210 return func(s *terraform.State) error { 211 if instanceTemplate.Properties.Metadata == nil { 212 return fmt.Errorf("no metadata") 213 } 214 215 for _, item := range instanceTemplate.Properties.Metadata.Items { 216 if k != item.Key { 217 continue 218 } 219 220 if item.Value != nil && v == *item.Value { 221 return nil 222 } 223 224 return fmt.Errorf("bad value for %s: %s", k, *item.Value) 225 } 226 227 return fmt.Errorf("metadata not found: %s", k) 228 } 229 } 230 231 func testAccCheckComputeInstanceTemplateNetwork(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc { 232 return func(s *terraform.State) error { 233 for _, i := range instanceTemplate.Properties.NetworkInterfaces { 234 for _, c := range i.AccessConfigs { 235 if c.NatIP == "" { 236 return fmt.Errorf("no NAT IP") 237 } 238 } 239 } 240 241 return nil 242 } 243 } 244 245 func testAccCheckComputeInstanceTemplateNetworkName(instanceTemplate *compute.InstanceTemplate, network string) resource.TestCheckFunc { 246 return func(s *terraform.State) error { 247 for _, i := range instanceTemplate.Properties.NetworkInterfaces { 248 if !strings.Contains(i.Network, network) { 249 return fmt.Errorf("Network doesn't match expected value, Expected: %s Actual: %s", network, i.Network[strings.LastIndex("/", i.Network)+1:]) 250 } 251 } 252 253 return nil 254 } 255 } 256 257 func testAccCheckComputeInstanceTemplateDisk(instanceTemplate *compute.InstanceTemplate, source string, delete bool, boot bool) resource.TestCheckFunc { 258 return func(s *terraform.State) error { 259 if instanceTemplate.Properties.Disks == nil { 260 return fmt.Errorf("no disks") 261 } 262 263 for _, disk := range instanceTemplate.Properties.Disks { 264 if disk.InitializeParams == nil { 265 // Check disk source 266 if disk.Source == source { 267 if disk.AutoDelete == delete && disk.Boot == boot { 268 return nil 269 } 270 } 271 } else { 272 // Check source image 273 if disk.InitializeParams.SourceImage == source { 274 if disk.AutoDelete == delete && disk.Boot == boot { 275 return nil 276 } 277 } 278 } 279 } 280 281 return fmt.Errorf("Disk not found: %s", source) 282 } 283 } 284 285 func testAccCheckComputeInstanceTemplateSubnetwork(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc { 286 return func(s *terraform.State) error { 287 for _, i := range instanceTemplate.Properties.NetworkInterfaces { 288 if i.Subnetwork == "" { 289 return fmt.Errorf("no subnet") 290 } 291 } 292 293 return nil 294 } 295 } 296 297 func testAccCheckComputeInstanceTemplateTag(instanceTemplate *compute.InstanceTemplate, n string) resource.TestCheckFunc { 298 return func(s *terraform.State) error { 299 if instanceTemplate.Properties.Tags == nil { 300 return fmt.Errorf("no tags") 301 } 302 303 for _, k := range instanceTemplate.Properties.Tags.Items { 304 if k == n { 305 return nil 306 } 307 } 308 309 return fmt.Errorf("tag not found: %s", n) 310 } 311 } 312 313 func testAccCheckComputeInstanceTemplateStartupScript(instanceTemplate *compute.InstanceTemplate, n string) resource.TestCheckFunc { 314 return func(s *terraform.State) error { 315 if instanceTemplate.Properties.Metadata == nil && n == "" { 316 return nil 317 } else if instanceTemplate.Properties.Metadata == nil && n != "" { 318 return fmt.Errorf("Expected metadata.startup-script to be '%s', metadata wasn't set at all", n) 319 } 320 for _, item := range instanceTemplate.Properties.Metadata.Items { 321 if item.Key != "startup-script" { 322 continue 323 } 324 if item.Value != nil && *item.Value == n { 325 return nil 326 } else if item.Value == nil && n == "" { 327 return nil 328 } else if item.Value == nil && n != "" { 329 return fmt.Errorf("Expected metadata.startup-script to be '%s', wasn't set", n) 330 } else if *item.Value != n { 331 return fmt.Errorf("Expected metadata.startup-script to be '%s', got '%s'", n, *item.Value) 332 } 333 } 334 return fmt.Errorf("This should never be reached.") 335 } 336 } 337 338 var testAccComputeInstanceTemplate_basic = fmt.Sprintf(` 339 resource "google_compute_instance_template" "foobar" { 340 name = "instancet-test-%s" 341 machine_type = "n1-standard-1" 342 can_ip_forward = false 343 tags = ["foo", "bar"] 344 345 disk { 346 source_image = "debian-8-jessie-v20160803" 347 auto_delete = true 348 boot = true 349 } 350 351 network_interface { 352 network = "default" 353 } 354 355 scheduling { 356 preemptible = false 357 automatic_restart = true 358 } 359 360 metadata { 361 foo = "bar" 362 } 363 364 service_account { 365 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 366 } 367 }`, acctest.RandString(10)) 368 369 var testAccComputeInstanceTemplate_ip = fmt.Sprintf(` 370 resource "google_compute_address" "foo" { 371 name = "instancet-test-%s" 372 } 373 374 resource "google_compute_instance_template" "foobar" { 375 name = "instancet-test-%s" 376 machine_type = "n1-standard-1" 377 tags = ["foo", "bar"] 378 379 disk { 380 source_image = "debian-8-jessie-v20160803" 381 } 382 383 network_interface { 384 network = "default" 385 access_config { 386 nat_ip = "${google_compute_address.foo.address}" 387 } 388 } 389 390 metadata { 391 foo = "bar" 392 } 393 }`, acctest.RandString(10), acctest.RandString(10)) 394 395 var testAccComputeInstanceTemplate_disks = fmt.Sprintf(` 396 resource "google_compute_disk" "foobar" { 397 name = "instancet-test-%s" 398 image = "debian-8-jessie-v20160803" 399 size = 10 400 type = "pd-ssd" 401 zone = "us-central1-a" 402 } 403 404 resource "google_compute_instance_template" "foobar" { 405 name = "instancet-test-%s" 406 machine_type = "n1-standard-1" 407 408 disk { 409 source_image = "debian-8-jessie-v20160803" 410 auto_delete = true 411 disk_size_gb = 100 412 boot = true 413 } 414 415 disk { 416 source = "terraform-test-foobar" 417 auto_delete = false 418 boot = false 419 } 420 421 network_interface { 422 network = "default" 423 } 424 425 metadata { 426 foo = "bar" 427 } 428 }`, acctest.RandString(10), acctest.RandString(10)) 429 430 func testAccComputeInstanceTemplate_subnet_auto(network string) string { 431 return fmt.Sprintf(` 432 resource "google_compute_network" "auto-network" { 433 name = "%s" 434 auto_create_subnetworks = true 435 } 436 437 resource "google_compute_instance_template" "foobar" { 438 name = "instance-tpl-%s" 439 machine_type = "n1-standard-1" 440 441 disk { 442 source_image = "debian-8-jessie-v20160803" 443 auto_delete = true 444 disk_size_gb = 10 445 boot = true 446 } 447 448 network_interface { 449 network = "${google_compute_network.auto-network.name}" 450 } 451 452 metadata { 453 foo = "bar" 454 } 455 }`, network, acctest.RandString(10)) 456 } 457 458 var testAccComputeInstanceTemplate_subnet_custom = fmt.Sprintf(` 459 resource "google_compute_network" "network" { 460 name = "network-%s" 461 auto_create_subnetworks = false 462 } 463 464 resource "google_compute_subnetwork" "subnetwork" { 465 name = "subnetwork-%s" 466 ip_cidr_range = "10.0.0.0/24" 467 region = "us-central1" 468 network = "${google_compute_network.network.self_link}" 469 } 470 471 resource "google_compute_instance_template" "foobar" { 472 name = "instance-test-%s" 473 machine_type = "n1-standard-1" 474 region = "us-central1" 475 476 disk { 477 source_image = "debian-8-jessie-v20160803" 478 auto_delete = true 479 disk_size_gb = 10 480 boot = true 481 } 482 483 network_interface { 484 subnetwork = "${google_compute_subnetwork.subnetwork.name}" 485 } 486 487 metadata { 488 foo = "bar" 489 } 490 }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10)) 491 492 func testAccComputeInstanceTemplate_subnet_xpn(xpn_host string) string { 493 return fmt.Sprintf(` 494 resource "google_compute_network" "network" { 495 name = "network-%s" 496 auto_create_subnetworks = false 497 project = "%s" 498 } 499 500 resource "google_compute_subnetwork" "subnetwork" { 501 name = "subnetwork-%s" 502 ip_cidr_range = "10.0.0.0/24" 503 region = "us-central1" 504 network = "${google_compute_network.network.self_link}" 505 project = "%s" 506 } 507 508 resource "google_compute_instance_template" "foobar" { 509 name = "instance-test-%s" 510 machine_type = "n1-standard-1" 511 region = "us-central1" 512 513 disk { 514 source_image = "debian-8-jessie-v20160803" 515 auto_delete = true 516 disk_size_gb = 10 517 boot = true 518 } 519 520 network_interface { 521 subnetwork = "${google_compute_subnetwork.subnetwork.name}" 522 subnetwork_project = "${google_compute_subnetwork.subnetwork.project}" 523 } 524 525 metadata { 526 foo = "bar" 527 } 528 }`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, acctest.RandString(10)) 529 } 530 531 var testAccComputeInstanceTemplate_startup_script = fmt.Sprintf(` 532 resource "google_compute_instance_template" "foobar" { 533 name = "instance-test-%s" 534 machine_type = "n1-standard-1" 535 536 disk { 537 source_image = "debian-8-jessie-v20160803" 538 auto_delete = true 539 disk_size_gb = 10 540 boot = true 541 } 542 543 metadata { 544 foo = "bar" 545 } 546 547 network_interface{ 548 network = "default" 549 } 550 551 metadata_startup_script = "echo 'Hello'" 552 }`, acctest.RandString(10))