github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_instance_test.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "testing" 6 7 "log" 8 9 "github.com/denverdino/aliyungo/common" 10 "github.com/denverdino/aliyungo/ecs" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/helper/schema" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAlicloudInstance_basic(t *testing.T) { 17 var instance ecs.InstanceAttributesType 18 19 testCheck := func(*terraform.State) error { 20 log.Printf("[WARN] instances: %#v", instance) 21 if instance.ZoneId == "" { 22 return fmt.Errorf("bad availability zone") 23 } 24 if len(instance.SecurityGroupIds.SecurityGroupId) == 0 { 25 return fmt.Errorf("no security group: %#v", instance.SecurityGroupIds.SecurityGroupId) 26 } 27 28 return nil 29 } 30 31 resource.Test(t, resource.TestCase{ 32 PreCheck: func() { 33 testAccPreCheck(t) 34 }, 35 36 // module name 37 IDRefreshName: "alicloud_instance.foo", 38 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckInstanceDestroy, 41 Steps: []resource.TestStep{ 42 resource.TestStep{ 43 Config: testAccInstanceConfig, 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckInstanceExists( 46 "alicloud_instance.foo", &instance), 47 testCheck, 48 resource.TestCheckResourceAttr( 49 "alicloud_instance.foo", 50 "image_id", 51 "ubuntu_140405_32_40G_cloudinit_20161115.vhd"), 52 resource.TestCheckResourceAttr( 53 "alicloud_instance.foo", 54 "instance_name", 55 "test_foo"), 56 resource.TestCheckResourceAttr( 57 "alicloud_instance.foo", 58 "internet_charge_type", 59 "PayByBandwidth"), 60 testAccCheckSystemDiskSize("alicloud_instance.foo", 80), 61 ), 62 }, 63 64 // test for multi steps 65 resource.TestStep{ 66 Config: testAccInstanceConfig, 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckInstanceExists( 69 "alicloud_instance.foo", &instance), 70 testCheck, 71 resource.TestCheckResourceAttr( 72 "alicloud_instance.foo", 73 "image_id", 74 "ubuntu_140405_32_40G_cloudinit_20161115.vhd"), 75 resource.TestCheckResourceAttr( 76 "alicloud_instance.foo", 77 "instance_name", 78 "test_foo"), 79 ), 80 }, 81 }, 82 }) 83 84 } 85 86 func TestAccAlicloudInstance_vpc(t *testing.T) { 87 var instance ecs.InstanceAttributesType 88 89 resource.Test(t, resource.TestCase{ 90 PreCheck: func() { 91 testAccPreCheck(t) 92 }, 93 IDRefreshName: "alicloud_instance.foo", 94 Providers: testAccProviders, 95 CheckDestroy: testAccCheckInstanceDestroy, 96 Steps: []resource.TestStep{ 97 resource.TestStep{ 98 Config: testAccInstanceConfigVPC, 99 Check: resource.ComposeTestCheckFunc( 100 testAccCheckInstanceExists( 101 "alicloud_instance.foo", &instance), 102 resource.TestCheckResourceAttr( 103 "alicloud_instance.foo", 104 "system_disk_category", 105 "cloud_efficiency"), 106 resource.TestCheckResourceAttr( 107 "alicloud_instance.foo", 108 "internet_charge_type", 109 "PayByTraffic"), 110 ), 111 }, 112 }, 113 }) 114 } 115 116 func TestAccAlicloudInstance_userData(t *testing.T) { 117 var instance ecs.InstanceAttributesType 118 119 resource.Test(t, resource.TestCase{ 120 PreCheck: func() { 121 testAccPreCheck(t) 122 }, 123 IDRefreshName: "alicloud_instance.foo", 124 Providers: testAccProviders, 125 CheckDestroy: testAccCheckInstanceDestroy, 126 Steps: []resource.TestStep{ 127 resource.TestStep{ 128 Config: testAccInstanceConfigUserData, 129 Check: resource.ComposeTestCheckFunc( 130 testAccCheckInstanceExists( 131 "alicloud_instance.foo", &instance), 132 resource.TestCheckResourceAttr( 133 "alicloud_instance.foo", 134 "system_disk_category", 135 "cloud_efficiency"), 136 resource.TestCheckResourceAttr( 137 "alicloud_instance.foo", 138 "internet_charge_type", 139 "PayByTraffic"), 140 resource.TestCheckResourceAttr( 141 "alicloud_instance.foo", 142 "user_data", 143 "echo 'net.ipv4.ip_forward=1'>> /etc/sysctl.conf"), 144 ), 145 }, 146 }, 147 }) 148 } 149 150 func TestAccAlicloudInstance_multipleRegions(t *testing.T) { 151 var instance ecs.InstanceAttributesType 152 153 // multi provideris 154 var providers []*schema.Provider 155 providerFactories := map[string]terraform.ResourceProviderFactory{ 156 "alicloud": func() (terraform.ResourceProvider, error) { 157 p := Provider() 158 providers = append(providers, p.(*schema.Provider)) 159 return p, nil 160 }, 161 } 162 163 resource.Test(t, resource.TestCase{ 164 PreCheck: func() { 165 testAccPreCheck(t) 166 }, 167 ProviderFactories: providerFactories, 168 CheckDestroy: testAccCheckInstanceDestroyWithProviders(&providers), 169 Steps: []resource.TestStep{ 170 resource.TestStep{ 171 Config: testAccInstanceConfigMultipleRegions, 172 Check: resource.ComposeTestCheckFunc( 173 testAccCheckInstanceExistsWithProviders( 174 "alicloud_instance.foo", &instance, &providers), 175 testAccCheckInstanceExistsWithProviders( 176 "alicloud_instance.bar", &instance, &providers), 177 ), 178 }, 179 }, 180 }) 181 } 182 183 func TestAccAlicloudInstance_multiSecurityGroup(t *testing.T) { 184 var instance ecs.InstanceAttributesType 185 186 testCheck := func(sgCount int) resource.TestCheckFunc { 187 return func(*terraform.State) error { 188 if len(instance.SecurityGroupIds.SecurityGroupId) < 0 { 189 return fmt.Errorf("no security group: %#v", instance.SecurityGroupIds.SecurityGroupId) 190 } 191 192 if len(instance.SecurityGroupIds.SecurityGroupId) < sgCount { 193 return fmt.Errorf("less security group: %#v", instance.SecurityGroupIds.SecurityGroupId) 194 } 195 196 return nil 197 } 198 } 199 200 resource.Test(t, resource.TestCase{ 201 PreCheck: func() { 202 testAccPreCheck(t) 203 }, 204 205 // module name 206 IDRefreshName: "alicloud_instance.foo", 207 208 Providers: testAccProviders, 209 CheckDestroy: testAccCheckInstanceDestroy, 210 Steps: []resource.TestStep{ 211 resource.TestStep{ 212 Config: testAccInstanceConfig_multiSecurityGroup, 213 Check: resource.ComposeTestCheckFunc( 214 testAccCheckInstanceExists( 215 "alicloud_instance.foo", &instance), 216 testCheck(2), 217 resource.TestCheckResourceAttr( 218 "alicloud_instance.foo", 219 "image_id", 220 "ubuntu_140405_32_40G_cloudinit_20161115.vhd"), 221 resource.TestCheckResourceAttr( 222 "alicloud_instance.foo", 223 "instance_name", 224 "test_foo"), 225 ), 226 }, 227 resource.TestStep{ 228 Config: testAccInstanceConfig_multiSecurityGroup_add, 229 Check: resource.ComposeTestCheckFunc( 230 testAccCheckInstanceExists( 231 "alicloud_instance.foo", &instance), 232 testCheck(3), 233 resource.TestCheckResourceAttr( 234 "alicloud_instance.foo", 235 "image_id", 236 "ubuntu_140405_32_40G_cloudinit_20161115.vhd"), 237 resource.TestCheckResourceAttr( 238 "alicloud_instance.foo", 239 "instance_name", 240 "test_foo"), 241 ), 242 }, 243 resource.TestStep{ 244 Config: testAccInstanceConfig_multiSecurityGroup_remove, 245 Check: resource.ComposeTestCheckFunc( 246 testAccCheckInstanceExists( 247 "alicloud_instance.foo", &instance), 248 testCheck(1), 249 resource.TestCheckResourceAttr( 250 "alicloud_instance.foo", 251 "image_id", 252 "ubuntu_140405_32_40G_cloudinit_20161115.vhd"), 253 resource.TestCheckResourceAttr( 254 "alicloud_instance.foo", 255 "instance_name", 256 "test_foo"), 257 ), 258 }, 259 }, 260 }) 261 262 } 263 264 func TestAccAlicloudInstance_multiSecurityGroupByCount(t *testing.T) { 265 var instance ecs.InstanceAttributesType 266 267 testCheck := func(sgCount int) resource.TestCheckFunc { 268 return func(*terraform.State) error { 269 if len(instance.SecurityGroupIds.SecurityGroupId) < 0 { 270 return fmt.Errorf("no security group: %#v", instance.SecurityGroupIds.SecurityGroupId) 271 } 272 273 if len(instance.SecurityGroupIds.SecurityGroupId) < sgCount { 274 return fmt.Errorf("less security group: %#v", instance.SecurityGroupIds.SecurityGroupId) 275 } 276 277 return nil 278 } 279 } 280 281 resource.Test(t, resource.TestCase{ 282 PreCheck: func() { 283 testAccPreCheck(t) 284 }, 285 286 // module name 287 IDRefreshName: "alicloud_instance.foo", 288 289 Providers: testAccProviders, 290 CheckDestroy: testAccCheckInstanceDestroy, 291 Steps: []resource.TestStep{ 292 resource.TestStep{ 293 Config: testAccInstanceConfig_multiSecurityGroupByCount, 294 Check: resource.ComposeTestCheckFunc( 295 testAccCheckInstanceExists( 296 "alicloud_instance.foo", &instance), 297 testCheck(2), 298 resource.TestCheckResourceAttr( 299 "alicloud_instance.foo", 300 "image_id", 301 "ubuntu_140405_32_40G_cloudinit_20161115.vhd"), 302 resource.TestCheckResourceAttr( 303 "alicloud_instance.foo", 304 "instance_name", 305 "test_foo"), 306 ), 307 }, 308 }, 309 }) 310 311 } 312 313 func TestAccAlicloudInstance_NetworkInstanceSecurityGroups(t *testing.T) { 314 var instance ecs.InstanceAttributesType 315 316 resource.Test(t, resource.TestCase{ 317 PreCheck: func() { 318 testAccPreCheck(t) 319 }, 320 IDRefreshName: "alicloud_instance.foo", 321 Providers: testAccProviders, 322 CheckDestroy: testAccCheckInstanceDestroy, 323 Steps: []resource.TestStep{ 324 resource.TestStep{ 325 Config: testAccInstanceNetworkInstanceSecurityGroups, 326 Check: resource.ComposeTestCheckFunc( 327 testAccCheckInstanceExists( 328 "alicloud_instance.foo", &instance), 329 ), 330 }, 331 }, 332 }) 333 } 334 335 func TestAccAlicloudInstance_tags(t *testing.T) { 336 var instance ecs.InstanceAttributesType 337 338 resource.Test(t, resource.TestCase{ 339 PreCheck: func() { 340 testAccPreCheck(t) 341 }, 342 Providers: testAccProviders, 343 CheckDestroy: testAccCheckInstanceDestroy, 344 Steps: []resource.TestStep{ 345 resource.TestStep{ 346 Config: testAccCheckInstanceConfigTags, 347 Check: resource.ComposeTestCheckFunc( 348 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 349 resource.TestCheckResourceAttr( 350 "alicloud_instance.foo", 351 "tags.foo", 352 "bar"), 353 ), 354 }, 355 356 resource.TestStep{ 357 Config: testAccCheckInstanceConfigTagsUpdate, 358 Check: resource.ComposeTestCheckFunc( 359 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 360 resource.TestCheckResourceAttr( 361 "alicloud_instance.foo", 362 "tags.bar", 363 "zzz"), 364 ), 365 }, 366 }, 367 }) 368 } 369 370 func TestAccAlicloudInstance_update(t *testing.T) { 371 var instance ecs.InstanceAttributesType 372 373 resource.Test(t, resource.TestCase{ 374 PreCheck: func() { 375 testAccPreCheck(t) 376 }, 377 Providers: testAccProviders, 378 CheckDestroy: testAccCheckInstanceDestroy, 379 Steps: []resource.TestStep{ 380 resource.TestStep{ 381 Config: testAccCheckInstanceConfigOrigin, 382 Check: resource.ComposeTestCheckFunc( 383 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 384 resource.TestCheckResourceAttr( 385 "alicloud_instance.foo", 386 "instance_name", 387 "instance_foo"), 388 resource.TestCheckResourceAttr( 389 "alicloud_instance.foo", 390 "host_name", 391 "host-foo"), 392 ), 393 }, 394 395 resource.TestStep{ 396 Config: testAccCheckInstanceConfigOriginUpdate, 397 Check: resource.ComposeTestCheckFunc( 398 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 399 resource.TestCheckResourceAttr( 400 "alicloud_instance.foo", 401 "instance_name", 402 "instance_bar"), 403 resource.TestCheckResourceAttr( 404 "alicloud_instance.foo", 405 "host_name", 406 "host-bar"), 407 ), 408 }, 409 }, 410 }) 411 } 412 413 func TestAccAlicloudInstance_privateIP(t *testing.T) { 414 var instance ecs.InstanceAttributesType 415 416 testCheckPrivateIP := func() resource.TestCheckFunc { 417 return func(*terraform.State) error { 418 privateIP := instance.VpcAttributes.PrivateIpAddress.IpAddress[0] 419 if privateIP == "" { 420 return fmt.Errorf("can't get private IP") 421 } 422 423 return nil 424 } 425 } 426 427 resource.Test(t, resource.TestCase{ 428 PreCheck: func() { 429 testAccPreCheck(t) 430 }, 431 IDRefreshName: "alicloud_instance.foo", 432 Providers: testAccProviders, 433 CheckDestroy: testAccCheckInstanceDestroy, 434 Steps: []resource.TestStep{ 435 resource.TestStep{ 436 Config: testAccInstanceConfigPrivateIP, 437 Check: resource.ComposeTestCheckFunc( 438 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 439 testCheckPrivateIP(), 440 ), 441 }, 442 }, 443 }) 444 } 445 446 func TestAccAlicloudInstance_associatePublicIP(t *testing.T) { 447 var instance ecs.InstanceAttributesType 448 449 testCheckPrivateIP := func() resource.TestCheckFunc { 450 return func(*terraform.State) error { 451 privateIP := instance.VpcAttributes.PrivateIpAddress.IpAddress[0] 452 if privateIP == "" { 453 return fmt.Errorf("can't get private IP") 454 } 455 456 return nil 457 } 458 } 459 460 testCheckPublicIP := func() resource.TestCheckFunc { 461 return func(*terraform.State) error { 462 publicIP := instance.PublicIpAddress.IpAddress[0] 463 if publicIP == "" { 464 return fmt.Errorf("can't get public IP") 465 } 466 467 return nil 468 } 469 } 470 471 resource.Test(t, resource.TestCase{ 472 PreCheck: func() { 473 testAccPreCheck(t) 474 }, 475 IDRefreshName: "alicloud_instance.foo", 476 Providers: testAccProviders, 477 CheckDestroy: testAccCheckInstanceDestroy, 478 Steps: []resource.TestStep{ 479 resource.TestStep{ 480 Config: testAccInstanceConfigAssociatePublicIP, 481 Check: resource.ComposeTestCheckFunc( 482 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 483 testCheckPrivateIP(), 484 testCheckPublicIP(), 485 ), 486 }, 487 }, 488 }) 489 } 490 491 func TestAccAlicloudInstance_vpcRule(t *testing.T) { 492 var instance ecs.InstanceAttributesType 493 494 resource.Test(t, resource.TestCase{ 495 PreCheck: func() { 496 testAccPreCheck(t) 497 }, 498 IDRefreshName: "alicloud_instance.foo", 499 Providers: testAccProviders, 500 CheckDestroy: testAccCheckInstanceDestroy, 501 Steps: []resource.TestStep{ 502 resource.TestStep{ 503 Config: testAccVpcInstanceWithSecurityRule, 504 Check: resource.ComposeTestCheckFunc( 505 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 506 resource.TestCheckResourceAttr( 507 "alicloud_instance.foo", 508 "internet_charge_type", 509 "PayByBandwidth"), 510 resource.TestCheckResourceAttr( 511 "alicloud_instance.foo", 512 "internet_max_bandwidth_out", 513 "5"), 514 ), 515 }, 516 }, 517 }) 518 } 519 520 func testAccCheckInstanceExists(n string, i *ecs.InstanceAttributesType) resource.TestCheckFunc { 521 providers := []*schema.Provider{testAccProvider} 522 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 523 } 524 525 func testAccCheckInstanceExistsWithProviders(n string, i *ecs.InstanceAttributesType, providers *[]*schema.Provider) resource.TestCheckFunc { 526 return func(s *terraform.State) error { 527 rs, ok := s.RootModule().Resources[n] 528 if !ok { 529 return fmt.Errorf("Not found: %s", n) 530 } 531 532 if rs.Primary.ID == "" { 533 return fmt.Errorf("No ID is set") 534 } 535 for _, provider := range *providers { 536 // Ignore if Meta is empty, this can happen for validation providers 537 if provider.Meta() == nil { 538 continue 539 } 540 541 client := provider.Meta().(*AliyunClient) 542 instance, err := client.QueryInstancesById(rs.Primary.ID) 543 log.Printf("[WARN]get ecs instance %#v", instance) 544 if err == nil && instance != nil { 545 *i = *instance 546 return nil 547 } 548 549 // Verify the error is what we want 550 e, _ := err.(*common.Error) 551 if e.ErrorResponse.Message == InstanceNotfound { 552 continue 553 } 554 if err != nil { 555 return err 556 557 } 558 } 559 560 return fmt.Errorf("Instance not found") 561 } 562 } 563 564 func testAccCheckInstanceDestroy(s *terraform.State) error { 565 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 566 } 567 568 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 569 return func(s *terraform.State) error { 570 for _, provider := range *providers { 571 if provider.Meta() == nil { 572 continue 573 } 574 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 575 return err 576 } 577 } 578 return nil 579 } 580 } 581 582 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 583 client := provider.Meta().(*AliyunClient) 584 585 for _, rs := range s.RootModule().Resources { 586 if rs.Type != "alicloud_instance" { 587 continue 588 } 589 590 // Try to find the resource 591 instance, err := client.QueryInstancesById(rs.Primary.ID) 592 if err == nil { 593 if instance.Status != "" && instance.Status != "Stopped" { 594 return fmt.Errorf("Found unstopped instance: %s", instance.InstanceId) 595 } 596 } 597 598 // Verify the error is what we want 599 e, _ := err.(*common.Error) 600 if e.ErrorResponse.Message == InstanceNotfound { 601 continue 602 } 603 604 return err 605 } 606 607 return nil 608 } 609 610 func testAccCheckSystemDiskSize(n string, size int) resource.TestCheckFunc { 611 return func(s *terraform.State) error { 612 providers := []*schema.Provider{testAccProvider} 613 rs, ok := s.RootModule().Resources[n] 614 615 if !ok { 616 return fmt.Errorf("Not found: %s", n) 617 } 618 619 for _, provider := range providers { 620 if provider.Meta() == nil { 621 continue 622 } 623 client := provider.Meta().(*AliyunClient) 624 systemDisk, err := client.QueryInstanceSystemDisk(rs.Primary.ID) 625 if err != nil { 626 log.Printf("[ERROR]get system disk size error: %#v", err) 627 return err 628 } 629 630 if systemDisk.Size != size { 631 return fmt.Errorf("system disk size not equal %d, the instance system size is %d", 632 size, systemDisk.Size) 633 } 634 } 635 636 return nil 637 } 638 } 639 640 const testAccInstanceConfig = ` 641 resource "alicloud_security_group" "tf_test_foo" { 642 name = "tf_test_foo" 643 description = "foo" 644 } 645 646 resource "alicloud_security_group" "tf_test_bar" { 647 name = "tf_test_bar" 648 description = "bar" 649 } 650 651 resource "alicloud_instance" "foo" { 652 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 653 654 system_disk_category = "cloud_ssd" 655 system_disk_size = 80 656 657 instance_type = "ecs.n1.small" 658 internet_charge_type = "PayByBandwidth" 659 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 660 instance_name = "test_foo" 661 io_optimized = "optimized" 662 663 tags { 664 foo = "bar" 665 work = "test" 666 } 667 } 668 ` 669 const testAccInstanceConfigVPC = ` 670 data "alicloud_zones" "default" { 671 "available_disk_category"= "cloud_efficiency" 672 "available_resource_creation"= "VSwitch" 673 } 674 675 resource "alicloud_vpc" "foo" { 676 name = "tf_test_foo" 677 cidr_block = "172.16.0.0/12" 678 } 679 680 resource "alicloud_vswitch" "foo" { 681 vpc_id = "${alicloud_vpc.foo.id}" 682 cidr_block = "172.16.0.0/21" 683 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 684 } 685 686 resource "alicloud_security_group" "tf_test_foo" { 687 name = "tf_test_foo" 688 description = "foo" 689 vpc_id = "${alicloud_vpc.foo.id}" 690 } 691 692 resource "alicloud_instance" "foo" { 693 # cn-beijing 694 vswitch_id = "${alicloud_vswitch.foo.id}" 695 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 696 697 # series II 698 instance_type = "ecs.n1.medium" 699 io_optimized = "optimized" 700 system_disk_category = "cloud_efficiency" 701 702 internet_charge_type = "PayByTraffic" 703 internet_max_bandwidth_out = 5 704 allocate_public_ip = true 705 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 706 instance_name = "test_foo" 707 } 708 709 ` 710 711 const testAccInstanceConfigUserData = ` 712 data "alicloud_zones" "default" { 713 "available_disk_category"= "cloud_efficiency" 714 "available_resource_creation"= "VSwitch" 715 } 716 717 resource "alicloud_vpc" "foo" { 718 name = "tf_test_foo" 719 cidr_block = "172.16.0.0/12" 720 } 721 722 resource "alicloud_vswitch" "foo" { 723 vpc_id = "${alicloud_vpc.foo.id}" 724 cidr_block = "172.16.0.0/21" 725 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 726 } 727 728 resource "alicloud_security_group" "tf_test_foo" { 729 name = "tf_test_foo" 730 description = "foo" 731 vpc_id = "${alicloud_vpc.foo.id}" 732 } 733 734 resource "alicloud_instance" "foo" { 735 # cn-beijing 736 vswitch_id = "${alicloud_vswitch.foo.id}" 737 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 738 # series II 739 instance_type = "ecs.n1.medium" 740 io_optimized = "optimized" 741 system_disk_category = "cloud_efficiency" 742 internet_charge_type = "PayByTraffic" 743 internet_max_bandwidth_out = 5 744 allocate_public_ip = true 745 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 746 instance_name = "test_foo" 747 user_data = "echo 'net.ipv4.ip_forward=1'>> /etc/sysctl.conf" 748 } 749 ` 750 751 const testAccInstanceConfigMultipleRegions = ` 752 provider "alicloud" { 753 alias = "beijing" 754 region = "cn-beijing" 755 } 756 757 provider "alicloud" { 758 alias = "shanghai" 759 region = "cn-shanghai" 760 } 761 762 resource "alicloud_security_group" "tf_test_foo" { 763 name = "tf_test_foo" 764 provider = "alicloud.beijing" 765 description = "foo" 766 } 767 768 resource "alicloud_security_group" "tf_test_bar" { 769 name = "tf_test_bar" 770 provider = "alicloud.shanghai" 771 description = "bar" 772 } 773 774 resource "alicloud_instance" "foo" { 775 # cn-beijing 776 provider = "alicloud.beijing" 777 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 778 779 internet_charge_type = "PayByBandwidth" 780 781 instance_type = "ecs.n1.medium" 782 io_optimized = "optimized" 783 system_disk_category = "cloud_efficiency" 784 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 785 instance_name = "test_foo" 786 } 787 788 resource "alicloud_instance" "bar" { 789 # cn-shanghai 790 provider = "alicloud.shanghai" 791 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 792 793 internet_charge_type = "PayByBandwidth" 794 795 instance_type = "ecs.n1.medium" 796 io_optimized = "optimized" 797 system_disk_category = "cloud_efficiency" 798 security_groups = ["${alicloud_security_group.tf_test_bar.id}"] 799 instance_name = "test_bar" 800 } 801 ` 802 803 const testAccInstanceConfig_multiSecurityGroup = ` 804 resource "alicloud_security_group" "tf_test_foo" { 805 name = "tf_test_foo" 806 description = "foo" 807 } 808 809 resource "alicloud_security_group" "tf_test_bar" { 810 name = "tf_test_bar" 811 description = "bar" 812 } 813 814 resource "alicloud_instance" "foo" { 815 # cn-beijing 816 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 817 818 instance_type = "ecs.s2.large" 819 internet_charge_type = "PayByBandwidth" 820 security_groups = ["${alicloud_security_group.tf_test_foo.id}", "${alicloud_security_group.tf_test_bar.id}"] 821 instance_name = "test_foo" 822 io_optimized = "optimized" 823 system_disk_category = "cloud_efficiency" 824 }` 825 826 const testAccInstanceConfig_multiSecurityGroup_add = ` 827 resource "alicloud_security_group" "tf_test_foo" { 828 name = "tf_test_foo" 829 description = "foo" 830 } 831 832 resource "alicloud_security_group" "tf_test_bar" { 833 name = "tf_test_bar" 834 description = "bar" 835 } 836 837 resource "alicloud_security_group" "tf_test_add_sg" { 838 name = "tf_test_add_sg" 839 description = "sg" 840 } 841 842 resource "alicloud_instance" "foo" { 843 # cn-beijing 844 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 845 846 instance_type = "ecs.s2.large" 847 internet_charge_type = "PayByBandwidth" 848 security_groups = ["${alicloud_security_group.tf_test_foo.id}", "${alicloud_security_group.tf_test_bar.id}", 849 "${alicloud_security_group.tf_test_add_sg.id}"] 850 instance_name = "test_foo" 851 io_optimized = "optimized" 852 system_disk_category = "cloud_efficiency" 853 } 854 ` 855 856 const testAccInstanceConfig_multiSecurityGroup_remove = ` 857 resource "alicloud_security_group" "tf_test_foo" { 858 name = "tf_test_foo" 859 description = "foo" 860 } 861 862 resource "alicloud_security_group_rule" "http-in" { 863 type = "ingress" 864 ip_protocol = "tcp" 865 nic_type = "internet" 866 policy = "accept" 867 port_range = "80/80" 868 priority = 1 869 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 870 cidr_ip = "0.0.0.0/0" 871 } 872 873 resource "alicloud_security_group_rule" "ssh-in" { 874 type = "ingress" 875 ip_protocol = "tcp" 876 nic_type = "internet" 877 policy = "accept" 878 port_range = "22/22" 879 priority = 1 880 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 881 cidr_ip = "0.0.0.0/0" 882 } 883 884 resource "alicloud_instance" "foo" { 885 # cn-beijing 886 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 887 888 instance_type = "ecs.s2.large" 889 internet_charge_type = "PayByBandwidth" 890 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 891 instance_name = "test_foo" 892 io_optimized = "optimized" 893 system_disk_category = "cloud_efficiency" 894 } 895 ` 896 897 const testAccInstanceConfig_multiSecurityGroupByCount = ` 898 resource "alicloud_security_group" "tf_test_foo" { 899 name = "tf_test_foo" 900 count = 2 901 description = "foo" 902 } 903 904 resource "alicloud_instance" "foo" { 905 # cn-beijing 906 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 907 908 instance_type = "ecs.s2.large" 909 internet_charge_type = "PayByBandwidth" 910 security_groups = ["${alicloud_security_group.tf_test_foo.*.id}"] 911 instance_name = "test_foo" 912 io_optimized = "optimized" 913 system_disk_category = "cloud_efficiency" 914 } 915 ` 916 917 const testAccInstanceNetworkInstanceSecurityGroups = ` 918 data "alicloud_zones" "default" { 919 "available_disk_category"= "cloud_efficiency" 920 "available_resource_creation"= "VSwitch" 921 } 922 923 resource "alicloud_vpc" "foo" { 924 name = "tf_test_foo" 925 cidr_block = "172.16.0.0/12" 926 } 927 928 resource "alicloud_vswitch" "foo" { 929 vpc_id = "${alicloud_vpc.foo.id}" 930 cidr_block = "172.16.0.0/21" 931 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 932 } 933 934 resource "alicloud_security_group" "tf_test_foo" { 935 name = "tf_test_foo" 936 description = "foo" 937 vpc_id = "${alicloud_vpc.foo.id}" 938 } 939 940 resource "alicloud_instance" "foo" { 941 # cn-beijing 942 vswitch_id = "${alicloud_vswitch.foo.id}" 943 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 944 945 # series II 946 instance_type = "ecs.n1.medium" 947 io_optimized = "optimized" 948 system_disk_category = "cloud_efficiency" 949 950 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 951 instance_name = "test_foo" 952 953 internet_max_bandwidth_out = 5 954 allocate_public_ip = "true" 955 internet_charge_type = "PayByBandwidth" 956 } 957 ` 958 const testAccCheckInstanceConfigTags = ` 959 resource "alicloud_security_group" "tf_test_foo" { 960 name = "tf_test_foo" 961 description = "foo" 962 } 963 964 resource "alicloud_instance" "foo" { 965 # cn-beijing 966 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 967 968 # series II 969 instance_type = "ecs.n1.medium" 970 io_optimized = "optimized" 971 internet_charge_type = "PayByBandwidth" 972 system_disk_category = "cloud_efficiency" 973 974 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 975 instance_name = "test_foo" 976 977 tags { 978 foo = "bar" 979 } 980 } 981 ` 982 983 const testAccCheckInstanceConfigTagsUpdate = ` 984 resource "alicloud_security_group" "tf_test_foo" { 985 name = "tf_test_foo" 986 description = "foo" 987 } 988 989 resource "alicloud_instance" "foo" { 990 # cn-beijing 991 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 992 993 # series II 994 instance_type = "ecs.n1.medium" 995 io_optimized = "optimized" 996 internet_charge_type = "PayByBandwidth" 997 system_disk_category = "cloud_efficiency" 998 999 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1000 instance_name = "test_foo" 1001 1002 tags { 1003 bar = "zzz" 1004 } 1005 } 1006 ` 1007 const testAccCheckInstanceConfigOrigin = ` 1008 resource "alicloud_security_group" "tf_test_foo" { 1009 name = "tf_test_foo" 1010 description = "foo" 1011 } 1012 1013 resource "alicloud_security_group_rule" "http-in" { 1014 type = "ingress" 1015 ip_protocol = "tcp" 1016 nic_type = "internet" 1017 policy = "accept" 1018 port_range = "80/80" 1019 priority = 1 1020 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1021 cidr_ip = "0.0.0.0/0" 1022 } 1023 1024 resource "alicloud_security_group_rule" "ssh-in" { 1025 type = "ingress" 1026 ip_protocol = "tcp" 1027 nic_type = "internet" 1028 policy = "accept" 1029 port_range = "22/22" 1030 priority = 1 1031 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1032 cidr_ip = "0.0.0.0/0" 1033 } 1034 1035 resource "alicloud_instance" "foo" { 1036 # cn-beijing 1037 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1038 1039 # series II 1040 instance_type = "ecs.n1.medium" 1041 io_optimized = "optimized" 1042 internet_charge_type = "PayByBandwidth" 1043 system_disk_category = "cloud_efficiency" 1044 1045 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1046 1047 instance_name = "instance_foo" 1048 host_name = "host-foo" 1049 } 1050 ` 1051 1052 const testAccCheckInstanceConfigOriginUpdate = ` 1053 resource "alicloud_security_group" "tf_test_foo" { 1054 name = "tf_test_foo" 1055 description = "foo" 1056 } 1057 1058 resource "alicloud_security_group_rule" "http-in" { 1059 type = "ingress" 1060 ip_protocol = "tcp" 1061 nic_type = "internet" 1062 policy = "accept" 1063 port_range = "80/80" 1064 priority = 1 1065 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1066 cidr_ip = "0.0.0.0/0" 1067 } 1068 1069 resource "alicloud_security_group_rule" "ssh-in" { 1070 type = "ingress" 1071 ip_protocol = "tcp" 1072 nic_type = "internet" 1073 policy = "accept" 1074 port_range = "22/22" 1075 priority = 1 1076 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1077 cidr_ip = "0.0.0.0/0" 1078 } 1079 1080 resource "alicloud_instance" "foo" { 1081 # cn-beijing 1082 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1083 1084 # series II 1085 instance_type = "ecs.n1.medium" 1086 io_optimized = "optimized" 1087 internet_charge_type = "PayByBandwidth" 1088 system_disk_category = "cloud_efficiency" 1089 1090 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1091 1092 instance_name = "instance_bar" 1093 host_name = "host-bar" 1094 } 1095 ` 1096 1097 const testAccInstanceConfigPrivateIP = ` 1098 data "alicloud_zones" "default" { 1099 "available_disk_category"= "cloud_efficiency" 1100 "available_resource_creation"= "VSwitch" 1101 } 1102 1103 resource "alicloud_vpc" "foo" { 1104 name = "tf_test_foo" 1105 cidr_block = "172.16.0.0/12" 1106 } 1107 1108 resource "alicloud_vswitch" "foo" { 1109 vpc_id = "${alicloud_vpc.foo.id}" 1110 cidr_block = "172.16.0.0/24" 1111 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 1112 } 1113 1114 resource "alicloud_security_group" "tf_test_foo" { 1115 name = "tf_test_foo" 1116 description = "foo" 1117 vpc_id = "${alicloud_vpc.foo.id}" 1118 } 1119 1120 resource "alicloud_instance" "foo" { 1121 # cn-beijing 1122 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1123 1124 vswitch_id = "${alicloud_vswitch.foo.id}" 1125 1126 # series II 1127 instance_type = "ecs.n1.medium" 1128 io_optimized = "optimized" 1129 system_disk_category = "cloud_efficiency" 1130 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1131 instance_name = "test_foo" 1132 } 1133 ` 1134 const testAccInstanceConfigAssociatePublicIP = ` 1135 data "alicloud_zones" "default" { 1136 "available_disk_category"= "cloud_efficiency" 1137 "available_resource_creation"= "VSwitch" 1138 } 1139 1140 resource "alicloud_vpc" "foo" { 1141 name = "tf_test_foo" 1142 cidr_block = "172.16.0.0/12" 1143 } 1144 1145 resource "alicloud_vswitch" "foo" { 1146 vpc_id = "${alicloud_vpc.foo.id}" 1147 cidr_block = "172.16.0.0/24" 1148 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 1149 } 1150 1151 resource "alicloud_security_group" "tf_test_foo" { 1152 name = "tf_test_foo" 1153 description = "foo" 1154 vpc_id = "${alicloud_vpc.foo.id}" 1155 } 1156 1157 resource "alicloud_instance" "foo" { 1158 # cn-beijing 1159 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1160 1161 vswitch_id = "${alicloud_vswitch.foo.id}" 1162 allocate_public_ip = "true" 1163 internet_max_bandwidth_out = 5 1164 internet_charge_type = "PayByBandwidth" 1165 1166 # series II 1167 instance_type = "ecs.n1.medium" 1168 io_optimized = "optimized" 1169 system_disk_category = "cloud_efficiency" 1170 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1171 instance_name = "test_foo" 1172 } 1173 ` 1174 const testAccVpcInstanceWithSecurityRule = ` 1175 data "alicloud_zones" "default" { 1176 "available_disk_category"= "cloud_efficiency" 1177 "available_resource_creation"= "VSwitch" 1178 } 1179 1180 resource "alicloud_vpc" "foo" { 1181 name = "tf_test_foo" 1182 cidr_block = "10.1.0.0/21" 1183 } 1184 1185 resource "alicloud_vswitch" "foo" { 1186 vpc_id = "${alicloud_vpc.foo.id}" 1187 cidr_block = "10.1.1.0/24" 1188 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 1189 } 1190 1191 resource "alicloud_security_group" "tf_test_foo" { 1192 name = "tf_test_foo" 1193 description = "foo" 1194 vpc_id = "${alicloud_vpc.foo.id}" 1195 } 1196 1197 resource "alicloud_security_group_rule" "ingress" { 1198 type = "ingress" 1199 ip_protocol = "tcp" 1200 nic_type = "intranet" 1201 policy = "accept" 1202 port_range = "22/22" 1203 priority = 1 1204 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1205 cidr_ip = "0.0.0.0/0" 1206 } 1207 1208 resource "alicloud_instance" "foo" { 1209 # cn-beijing 1210 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1211 1212 vswitch_id = "${alicloud_vswitch.foo.id}" 1213 allocate_public_ip = true 1214 1215 # series II 1216 instance_charge_type = "PostPaid" 1217 instance_type = "ecs.n1.small" 1218 internet_charge_type = "PayByBandwidth" 1219 internet_max_bandwidth_out = 5 1220 1221 system_disk_category = "cloud_efficiency" 1222 image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd" 1223 instance_name = "test_foo" 1224 io_optimized = "optimized" 1225 } 1226 1227 `