github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 TestAccAlicloudInstanceImage_update(t *testing.T) { 414 var instance ecs.InstanceAttributesType 415 resource.Test(t, resource.TestCase{ 416 PreCheck: func() { 417 testAccPreCheck(t) 418 }, 419 Providers: testAccProviders, 420 CheckDestroy: testAccCheckInstanceDestroy, 421 Steps: []resource.TestStep{ 422 resource.TestStep{ 423 Config: testAccCheckInstanceImageOrigin, 424 Check: resource.ComposeTestCheckFunc( 425 testAccCheckInstanceExists("alicloud_instance.update_image", &instance), 426 resource.TestCheckResourceAttr( 427 "alicloud_instance.update_image", 428 "system_disk_size", 429 "50"), 430 ), 431 }, 432 resource.TestStep{ 433 Config: testAccCheckInstanceImageUpdate, 434 Check: resource.ComposeTestCheckFunc( 435 testAccCheckInstanceExists("alicloud_instance.update_image", &instance), 436 resource.TestCheckResourceAttr( 437 "alicloud_instance.update_image", 438 "system_disk_size", 439 "60"), 440 ), 441 }, 442 }, 443 }) 444 } 445 446 func TestAccAlicloudInstance_privateIP(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 resource.Test(t, resource.TestCase{ 461 PreCheck: func() { 462 testAccPreCheck(t) 463 }, 464 IDRefreshName: "alicloud_instance.foo", 465 Providers: testAccProviders, 466 CheckDestroy: testAccCheckInstanceDestroy, 467 Steps: []resource.TestStep{ 468 resource.TestStep{ 469 Config: testAccInstanceConfigPrivateIP, 470 Check: resource.ComposeTestCheckFunc( 471 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 472 testCheckPrivateIP(), 473 ), 474 }, 475 }, 476 }) 477 } 478 479 func TestAccAlicloudInstance_associatePublicIP(t *testing.T) { 480 var instance ecs.InstanceAttributesType 481 482 testCheckPrivateIP := func() resource.TestCheckFunc { 483 return func(*terraform.State) error { 484 privateIP := instance.VpcAttributes.PrivateIpAddress.IpAddress[0] 485 if privateIP == "" { 486 return fmt.Errorf("can't get private IP") 487 } 488 489 return nil 490 } 491 } 492 493 testCheckPublicIP := func() resource.TestCheckFunc { 494 return func(*terraform.State) error { 495 publicIP := instance.PublicIpAddress.IpAddress[0] 496 if publicIP == "" { 497 return fmt.Errorf("can't get public IP") 498 } 499 500 return nil 501 } 502 } 503 504 resource.Test(t, resource.TestCase{ 505 PreCheck: func() { 506 testAccPreCheck(t) 507 }, 508 IDRefreshName: "alicloud_instance.foo", 509 Providers: testAccProviders, 510 CheckDestroy: testAccCheckInstanceDestroy, 511 Steps: []resource.TestStep{ 512 resource.TestStep{ 513 Config: testAccInstanceConfigAssociatePublicIP, 514 Check: resource.ComposeTestCheckFunc( 515 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 516 testCheckPrivateIP(), 517 testCheckPublicIP(), 518 ), 519 }, 520 }, 521 }) 522 } 523 524 func TestAccAlicloudInstance_vpcRule(t *testing.T) { 525 var instance ecs.InstanceAttributesType 526 527 resource.Test(t, resource.TestCase{ 528 PreCheck: func() { 529 testAccPreCheck(t) 530 }, 531 IDRefreshName: "alicloud_instance.foo", 532 Providers: testAccProviders, 533 CheckDestroy: testAccCheckInstanceDestroy, 534 Steps: []resource.TestStep{ 535 resource.TestStep{ 536 Config: testAccVpcInstanceWithSecurityRule, 537 Check: resource.ComposeTestCheckFunc( 538 testAccCheckInstanceExists("alicloud_instance.foo", &instance), 539 resource.TestCheckResourceAttr( 540 "alicloud_instance.foo", 541 "internet_charge_type", 542 "PayByBandwidth"), 543 resource.TestCheckResourceAttr( 544 "alicloud_instance.foo", 545 "internet_max_bandwidth_out", 546 "5"), 547 ), 548 }, 549 }, 550 }) 551 } 552 553 func testAccCheckInstanceExists(n string, i *ecs.InstanceAttributesType) resource.TestCheckFunc { 554 providers := []*schema.Provider{testAccProvider} 555 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 556 } 557 558 func testAccCheckInstanceExistsWithProviders(n string, i *ecs.InstanceAttributesType, providers *[]*schema.Provider) resource.TestCheckFunc { 559 return func(s *terraform.State) error { 560 rs, ok := s.RootModule().Resources[n] 561 if !ok { 562 return fmt.Errorf("Not found: %s", n) 563 } 564 565 if rs.Primary.ID == "" { 566 return fmt.Errorf("No ID is set") 567 } 568 for _, provider := range *providers { 569 // Ignore if Meta is empty, this can happen for validation providers 570 if provider.Meta() == nil { 571 continue 572 } 573 574 client := provider.Meta().(*AliyunClient) 575 instance, err := client.QueryInstancesById(rs.Primary.ID) 576 log.Printf("[WARN]get ecs instance %#v", instance) 577 if err == nil && instance != nil { 578 *i = *instance 579 return nil 580 } 581 582 // Verify the error is what we want 583 e, _ := err.(*common.Error) 584 if e.ErrorResponse.Message == InstanceNotfound { 585 continue 586 } 587 if err != nil { 588 return err 589 590 } 591 } 592 593 return fmt.Errorf("Instance not found") 594 } 595 } 596 597 func testAccCheckInstanceDestroy(s *terraform.State) error { 598 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 599 } 600 601 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 602 return func(s *terraform.State) error { 603 for _, provider := range *providers { 604 if provider.Meta() == nil { 605 continue 606 } 607 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 608 return err 609 } 610 } 611 return nil 612 } 613 } 614 615 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 616 client := provider.Meta().(*AliyunClient) 617 618 for _, rs := range s.RootModule().Resources { 619 if rs.Type != "alicloud_instance" { 620 continue 621 } 622 623 // Try to find the resource 624 instance, err := client.QueryInstancesById(rs.Primary.ID) 625 if err == nil { 626 if instance.Status != "" && instance.Status != "Stopped" { 627 return fmt.Errorf("Found unstopped instance: %s", instance.InstanceId) 628 } 629 } 630 631 // Verify the error is what we want 632 e, _ := err.(*common.Error) 633 if e.ErrorResponse.Message == InstanceNotfound { 634 continue 635 } 636 637 return err 638 } 639 640 return nil 641 } 642 643 func testAccCheckSystemDiskSize(n string, size int) resource.TestCheckFunc { 644 return func(s *terraform.State) error { 645 providers := []*schema.Provider{testAccProvider} 646 rs, ok := s.RootModule().Resources[n] 647 648 if !ok { 649 return fmt.Errorf("Not found: %s", n) 650 } 651 652 for _, provider := range providers { 653 if provider.Meta() == nil { 654 continue 655 } 656 client := provider.Meta().(*AliyunClient) 657 systemDisk, err := client.QueryInstanceSystemDisk(rs.Primary.ID) 658 if err != nil { 659 log.Printf("[ERROR]get system disk size error: %#v", err) 660 return err 661 } 662 663 if systemDisk.Size != size { 664 return fmt.Errorf("system disk size not equal %d, the instance system size is %d", 665 size, systemDisk.Size) 666 } 667 } 668 669 return nil 670 } 671 } 672 673 const testAccInstanceConfig = ` 674 resource "alicloud_security_group" "tf_test_foo" { 675 name = "tf_test_foo" 676 description = "foo" 677 } 678 679 resource "alicloud_security_group" "tf_test_bar" { 680 name = "tf_test_bar" 681 description = "bar" 682 } 683 684 resource "alicloud_instance" "foo" { 685 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 686 687 system_disk_category = "cloud_ssd" 688 system_disk_size = 80 689 690 instance_type = "ecs.n1.small" 691 internet_charge_type = "PayByBandwidth" 692 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 693 instance_name = "test_foo" 694 io_optimized = "optimized" 695 696 tags { 697 foo = "bar" 698 work = "test" 699 } 700 } 701 ` 702 const testAccInstanceConfigVPC = ` 703 data "alicloud_zones" "default" { 704 "available_disk_category"= "cloud_efficiency" 705 "available_resource_creation"= "VSwitch" 706 } 707 708 resource "alicloud_vpc" "foo" { 709 name = "tf_test_foo" 710 cidr_block = "172.16.0.0/12" 711 } 712 713 resource "alicloud_vswitch" "foo" { 714 vpc_id = "${alicloud_vpc.foo.id}" 715 cidr_block = "172.16.0.0/21" 716 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 717 } 718 719 resource "alicloud_security_group" "tf_test_foo" { 720 name = "tf_test_foo" 721 description = "foo" 722 vpc_id = "${alicloud_vpc.foo.id}" 723 } 724 725 resource "alicloud_instance" "foo" { 726 # cn-beijing 727 vswitch_id = "${alicloud_vswitch.foo.id}" 728 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 729 730 # series II 731 instance_type = "ecs.n1.medium" 732 io_optimized = "optimized" 733 system_disk_category = "cloud_efficiency" 734 735 internet_charge_type = "PayByTraffic" 736 internet_max_bandwidth_out = 5 737 allocate_public_ip = true 738 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 739 instance_name = "test_foo" 740 } 741 742 ` 743 744 const testAccInstanceConfigUserData = ` 745 data "alicloud_zones" "default" { 746 "available_disk_category"= "cloud_efficiency" 747 "available_resource_creation"= "VSwitch" 748 } 749 750 resource "alicloud_vpc" "foo" { 751 name = "tf_test_foo" 752 cidr_block = "172.16.0.0/12" 753 } 754 755 resource "alicloud_vswitch" "foo" { 756 vpc_id = "${alicloud_vpc.foo.id}" 757 cidr_block = "172.16.0.0/21" 758 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 759 } 760 761 resource "alicloud_security_group" "tf_test_foo" { 762 name = "tf_test_foo" 763 description = "foo" 764 vpc_id = "${alicloud_vpc.foo.id}" 765 } 766 767 resource "alicloud_instance" "foo" { 768 # cn-beijing 769 vswitch_id = "${alicloud_vswitch.foo.id}" 770 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 771 # series II 772 instance_type = "ecs.n1.medium" 773 io_optimized = "optimized" 774 system_disk_category = "cloud_efficiency" 775 internet_charge_type = "PayByTraffic" 776 internet_max_bandwidth_out = 5 777 allocate_public_ip = true 778 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 779 instance_name = "test_foo" 780 user_data = "echo 'net.ipv4.ip_forward=1'>> /etc/sysctl.conf" 781 } 782 ` 783 784 const testAccInstanceConfigMultipleRegions = ` 785 provider "alicloud" { 786 alias = "beijing" 787 region = "cn-beijing" 788 } 789 790 provider "alicloud" { 791 alias = "shanghai" 792 region = "cn-shanghai" 793 } 794 795 resource "alicloud_security_group" "tf_test_foo" { 796 name = "tf_test_foo" 797 provider = "alicloud.beijing" 798 description = "foo" 799 } 800 801 resource "alicloud_security_group" "tf_test_bar" { 802 name = "tf_test_bar" 803 provider = "alicloud.shanghai" 804 description = "bar" 805 } 806 807 resource "alicloud_instance" "foo" { 808 # cn-beijing 809 provider = "alicloud.beijing" 810 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 811 812 internet_charge_type = "PayByBandwidth" 813 814 instance_type = "ecs.n1.medium" 815 io_optimized = "optimized" 816 system_disk_category = "cloud_efficiency" 817 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 818 instance_name = "test_foo" 819 } 820 821 resource "alicloud_instance" "bar" { 822 # cn-shanghai 823 provider = "alicloud.shanghai" 824 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 825 826 internet_charge_type = "PayByBandwidth" 827 828 instance_type = "ecs.n1.medium" 829 io_optimized = "optimized" 830 system_disk_category = "cloud_efficiency" 831 security_groups = ["${alicloud_security_group.tf_test_bar.id}"] 832 instance_name = "test_bar" 833 } 834 ` 835 836 const testAccInstanceConfig_multiSecurityGroup = ` 837 resource "alicloud_security_group" "tf_test_foo" { 838 name = "tf_test_foo" 839 description = "foo" 840 } 841 842 resource "alicloud_security_group" "tf_test_bar" { 843 name = "tf_test_bar" 844 description = "bar" 845 } 846 847 resource "alicloud_instance" "foo" { 848 # cn-beijing 849 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 850 851 instance_type = "ecs.s2.large" 852 internet_charge_type = "PayByBandwidth" 853 security_groups = ["${alicloud_security_group.tf_test_foo.id}", "${alicloud_security_group.tf_test_bar.id}"] 854 instance_name = "test_foo" 855 io_optimized = "optimized" 856 system_disk_category = "cloud_efficiency" 857 }` 858 859 const testAccInstanceConfig_multiSecurityGroup_add = ` 860 resource "alicloud_security_group" "tf_test_foo" { 861 name = "tf_test_foo" 862 description = "foo" 863 } 864 865 resource "alicloud_security_group" "tf_test_bar" { 866 name = "tf_test_bar" 867 description = "bar" 868 } 869 870 resource "alicloud_security_group" "tf_test_add_sg" { 871 name = "tf_test_add_sg" 872 description = "sg" 873 } 874 875 resource "alicloud_instance" "foo" { 876 # cn-beijing 877 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 878 879 instance_type = "ecs.s2.large" 880 internet_charge_type = "PayByBandwidth" 881 security_groups = ["${alicloud_security_group.tf_test_foo.id}", "${alicloud_security_group.tf_test_bar.id}", 882 "${alicloud_security_group.tf_test_add_sg.id}"] 883 instance_name = "test_foo" 884 io_optimized = "optimized" 885 system_disk_category = "cloud_efficiency" 886 } 887 ` 888 889 const testAccInstanceConfig_multiSecurityGroup_remove = ` 890 resource "alicloud_security_group" "tf_test_foo" { 891 name = "tf_test_foo" 892 description = "foo" 893 } 894 895 resource "alicloud_security_group_rule" "http-in" { 896 type = "ingress" 897 ip_protocol = "tcp" 898 nic_type = "internet" 899 policy = "accept" 900 port_range = "80/80" 901 priority = 1 902 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 903 cidr_ip = "0.0.0.0/0" 904 } 905 906 resource "alicloud_security_group_rule" "ssh-in" { 907 type = "ingress" 908 ip_protocol = "tcp" 909 nic_type = "internet" 910 policy = "accept" 911 port_range = "22/22" 912 priority = 1 913 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 914 cidr_ip = "0.0.0.0/0" 915 } 916 917 resource "alicloud_instance" "foo" { 918 # cn-beijing 919 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 920 921 instance_type = "ecs.s2.large" 922 internet_charge_type = "PayByBandwidth" 923 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 924 instance_name = "test_foo" 925 io_optimized = "optimized" 926 system_disk_category = "cloud_efficiency" 927 } 928 ` 929 930 const testAccInstanceConfig_multiSecurityGroupByCount = ` 931 resource "alicloud_security_group" "tf_test_foo" { 932 name = "tf_test_foo" 933 count = 2 934 description = "foo" 935 } 936 937 resource "alicloud_instance" "foo" { 938 # cn-beijing 939 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 940 941 instance_type = "ecs.s2.large" 942 internet_charge_type = "PayByBandwidth" 943 security_groups = ["${alicloud_security_group.tf_test_foo.*.id}"] 944 instance_name = "test_foo" 945 io_optimized = "optimized" 946 system_disk_category = "cloud_efficiency" 947 } 948 ` 949 950 const testAccInstanceNetworkInstanceSecurityGroups = ` 951 data "alicloud_zones" "default" { 952 "available_disk_category"= "cloud_efficiency" 953 "available_resource_creation"= "VSwitch" 954 } 955 956 resource "alicloud_vpc" "foo" { 957 name = "tf_test_foo" 958 cidr_block = "172.16.0.0/12" 959 } 960 961 resource "alicloud_vswitch" "foo" { 962 vpc_id = "${alicloud_vpc.foo.id}" 963 cidr_block = "172.16.0.0/21" 964 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 965 } 966 967 resource "alicloud_security_group" "tf_test_foo" { 968 name = "tf_test_foo" 969 description = "foo" 970 vpc_id = "${alicloud_vpc.foo.id}" 971 } 972 973 resource "alicloud_instance" "foo" { 974 # cn-beijing 975 vswitch_id = "${alicloud_vswitch.foo.id}" 976 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 977 978 # series II 979 instance_type = "ecs.n1.medium" 980 io_optimized = "optimized" 981 system_disk_category = "cloud_efficiency" 982 983 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 984 instance_name = "test_foo" 985 986 internet_max_bandwidth_out = 5 987 allocate_public_ip = "true" 988 internet_charge_type = "PayByBandwidth" 989 } 990 ` 991 const testAccCheckInstanceConfigTags = ` 992 resource "alicloud_security_group" "tf_test_foo" { 993 name = "tf_test_foo" 994 description = "foo" 995 } 996 997 resource "alicloud_instance" "foo" { 998 # cn-beijing 999 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1000 1001 # series II 1002 instance_type = "ecs.n1.medium" 1003 io_optimized = "optimized" 1004 internet_charge_type = "PayByBandwidth" 1005 system_disk_category = "cloud_efficiency" 1006 1007 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1008 instance_name = "test_foo" 1009 1010 tags { 1011 foo = "bar" 1012 } 1013 } 1014 ` 1015 1016 const testAccCheckInstanceConfigTagsUpdate = ` 1017 resource "alicloud_security_group" "tf_test_foo" { 1018 name = "tf_test_foo" 1019 description = "foo" 1020 } 1021 1022 resource "alicloud_instance" "foo" { 1023 # cn-beijing 1024 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1025 1026 # series II 1027 instance_type = "ecs.n1.medium" 1028 io_optimized = "optimized" 1029 internet_charge_type = "PayByBandwidth" 1030 system_disk_category = "cloud_efficiency" 1031 1032 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1033 instance_name = "test_foo" 1034 1035 tags { 1036 bar = "zzz" 1037 } 1038 } 1039 ` 1040 const testAccCheckInstanceConfigOrigin = ` 1041 resource "alicloud_security_group" "tf_test_foo" { 1042 name = "tf_test_foo" 1043 description = "foo" 1044 } 1045 1046 resource "alicloud_security_group_rule" "http-in" { 1047 type = "ingress" 1048 ip_protocol = "tcp" 1049 nic_type = "internet" 1050 policy = "accept" 1051 port_range = "80/80" 1052 priority = 1 1053 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1054 cidr_ip = "0.0.0.0/0" 1055 } 1056 1057 resource "alicloud_security_group_rule" "ssh-in" { 1058 type = "ingress" 1059 ip_protocol = "tcp" 1060 nic_type = "internet" 1061 policy = "accept" 1062 port_range = "22/22" 1063 priority = 1 1064 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1065 cidr_ip = "0.0.0.0/0" 1066 } 1067 1068 resource "alicloud_instance" "foo" { 1069 # cn-beijing 1070 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1071 1072 # series II 1073 instance_type = "ecs.n1.medium" 1074 io_optimized = "optimized" 1075 internet_charge_type = "PayByBandwidth" 1076 system_disk_category = "cloud_efficiency" 1077 1078 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1079 1080 instance_name = "instance_foo" 1081 host_name = "host-foo" 1082 } 1083 ` 1084 1085 const testAccCheckInstanceConfigOriginUpdate = ` 1086 resource "alicloud_security_group" "tf_test_foo" { 1087 name = "tf_test_foo" 1088 description = "foo" 1089 } 1090 1091 resource "alicloud_security_group_rule" "http-in" { 1092 type = "ingress" 1093 ip_protocol = "tcp" 1094 nic_type = "internet" 1095 policy = "accept" 1096 port_range = "80/80" 1097 priority = 1 1098 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1099 cidr_ip = "0.0.0.0/0" 1100 } 1101 1102 resource "alicloud_security_group_rule" "ssh-in" { 1103 type = "ingress" 1104 ip_protocol = "tcp" 1105 nic_type = "internet" 1106 policy = "accept" 1107 port_range = "22/22" 1108 priority = 1 1109 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1110 cidr_ip = "0.0.0.0/0" 1111 } 1112 1113 resource "alicloud_instance" "foo" { 1114 # cn-beijing 1115 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1116 1117 # series II 1118 instance_type = "ecs.n1.medium" 1119 io_optimized = "optimized" 1120 internet_charge_type = "PayByBandwidth" 1121 system_disk_category = "cloud_efficiency" 1122 1123 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1124 1125 instance_name = "instance_bar" 1126 host_name = "host-bar" 1127 } 1128 ` 1129 1130 const testAccInstanceConfigPrivateIP = ` 1131 data "alicloud_zones" "default" { 1132 "available_disk_category"= "cloud_efficiency" 1133 "available_resource_creation"= "VSwitch" 1134 } 1135 1136 resource "alicloud_vpc" "foo" { 1137 name = "tf_test_foo" 1138 cidr_block = "172.16.0.0/12" 1139 } 1140 1141 resource "alicloud_vswitch" "foo" { 1142 vpc_id = "${alicloud_vpc.foo.id}" 1143 cidr_block = "172.16.0.0/24" 1144 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 1145 } 1146 1147 resource "alicloud_security_group" "tf_test_foo" { 1148 name = "tf_test_foo" 1149 description = "foo" 1150 vpc_id = "${alicloud_vpc.foo.id}" 1151 } 1152 1153 resource "alicloud_instance" "foo" { 1154 # cn-beijing 1155 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1156 1157 vswitch_id = "${alicloud_vswitch.foo.id}" 1158 1159 # series II 1160 instance_type = "ecs.n1.medium" 1161 io_optimized = "optimized" 1162 system_disk_category = "cloud_efficiency" 1163 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1164 instance_name = "test_foo" 1165 } 1166 ` 1167 const testAccInstanceConfigAssociatePublicIP = ` 1168 data "alicloud_zones" "default" { 1169 "available_disk_category"= "cloud_efficiency" 1170 "available_resource_creation"= "VSwitch" 1171 } 1172 1173 resource "alicloud_vpc" "foo" { 1174 name = "tf_test_foo" 1175 cidr_block = "172.16.0.0/12" 1176 } 1177 1178 resource "alicloud_vswitch" "foo" { 1179 vpc_id = "${alicloud_vpc.foo.id}" 1180 cidr_block = "172.16.0.0/24" 1181 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 1182 } 1183 1184 resource "alicloud_security_group" "tf_test_foo" { 1185 name = "tf_test_foo" 1186 description = "foo" 1187 vpc_id = "${alicloud_vpc.foo.id}" 1188 } 1189 1190 resource "alicloud_instance" "foo" { 1191 # cn-beijing 1192 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1193 1194 vswitch_id = "${alicloud_vswitch.foo.id}" 1195 allocate_public_ip = "true" 1196 internet_max_bandwidth_out = 5 1197 internet_charge_type = "PayByBandwidth" 1198 1199 # series II 1200 instance_type = "ecs.n1.medium" 1201 io_optimized = "optimized" 1202 system_disk_category = "cloud_efficiency" 1203 image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" 1204 instance_name = "test_foo" 1205 } 1206 ` 1207 const testAccVpcInstanceWithSecurityRule = ` 1208 data "alicloud_zones" "default" { 1209 "available_disk_category"= "cloud_efficiency" 1210 "available_resource_creation"= "VSwitch" 1211 } 1212 1213 resource "alicloud_vpc" "foo" { 1214 name = "tf_test_foo" 1215 cidr_block = "10.1.0.0/21" 1216 } 1217 1218 resource "alicloud_vswitch" "foo" { 1219 vpc_id = "${alicloud_vpc.foo.id}" 1220 cidr_block = "10.1.1.0/24" 1221 availability_zone = "${data.alicloud_zones.default.zones.0.id}" 1222 } 1223 1224 resource "alicloud_security_group" "tf_test_foo" { 1225 name = "tf_test_foo" 1226 description = "foo" 1227 vpc_id = "${alicloud_vpc.foo.id}" 1228 } 1229 1230 resource "alicloud_security_group_rule" "ingress" { 1231 type = "ingress" 1232 ip_protocol = "tcp" 1233 nic_type = "intranet" 1234 policy = "accept" 1235 port_range = "22/22" 1236 priority = 1 1237 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 1238 cidr_ip = "0.0.0.0/0" 1239 } 1240 1241 resource "alicloud_instance" "foo" { 1242 # cn-beijing 1243 security_groups = ["${alicloud_security_group.tf_test_foo.id}"] 1244 1245 vswitch_id = "${alicloud_vswitch.foo.id}" 1246 allocate_public_ip = true 1247 1248 # series II 1249 instance_charge_type = "PostPaid" 1250 instance_type = "ecs.n1.small" 1251 internet_charge_type = "PayByBandwidth" 1252 internet_max_bandwidth_out = 5 1253 1254 system_disk_category = "cloud_efficiency" 1255 image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd" 1256 instance_name = "test_foo" 1257 io_optimized = "optimized" 1258 } 1259 ` 1260 const testAccCheckInstanceImageOrigin = ` 1261 data "alicloud_images" "centos" { 1262 most_recent = true 1263 owners = "system" 1264 name_regex = "^centos_6\\w{1,5}[64]{1}.*" 1265 } 1266 1267 resource "alicloud_vpc" "foo" { 1268 name = "tf_test_image" 1269 cidr_block = "10.1.0.0/21" 1270 } 1271 1272 resource "alicloud_vswitch" "foo" { 1273 vpc_id = "${alicloud_vpc.foo.id}" 1274 cidr_block = "10.1.1.0/24" 1275 availability_zone = "cn-beijing-a" 1276 } 1277 1278 resource "alicloud_security_group" "tf_test_foo" { 1279 name = "tf_test_foo" 1280 description = "foo" 1281 vpc_id = "${alicloud_vpc.foo.id}" 1282 } 1283 1284 resource "alicloud_instance" "update_image" { 1285 image_id = "${data.alicloud_images.centos.images.0.id}" 1286 availability_zone = "cn-beijing-a" 1287 system_disk_category = "cloud_efficiency" 1288 system_disk_size = 50 1289 1290 instance_type = "ecs.n1.small" 1291 internet_charge_type = "PayByBandwidth" 1292 instance_name = "update_image" 1293 io_optimized = "optimized" 1294 password = "Test12345" 1295 } 1296 ` 1297 const testAccCheckInstanceImageUpdate = ` 1298 data "alicloud_images" "ubuntu" { 1299 most_recent = true 1300 owners = "system" 1301 name_regex = "^ubuntu_14\\w{1,5}[64]{1}.*" 1302 } 1303 1304 resource "alicloud_vpc" "foo" { 1305 name = "tf_test_image" 1306 cidr_block = "10.1.0.0/21" 1307 } 1308 1309 resource "alicloud_vswitch" "foo" { 1310 vpc_id = "${alicloud_vpc.foo.id}" 1311 cidr_block = "10.1.1.0/24" 1312 availability_zone = "cn-beijing-a" 1313 } 1314 1315 resource "alicloud_security_group" "tf_test_foo" { 1316 name = "tf_test_foo" 1317 description = "foo" 1318 vpc_id = "${alicloud_vpc.foo.id}" 1319 } 1320 1321 resource "alicloud_instance" "update_image" { 1322 image_id = "${data.alicloud_images.ubuntu.images.0.id}" 1323 availability_zone = "cn-beijing-a" 1324 system_disk_category = "cloud_efficiency" 1325 system_disk_size = 60 1326 1327 instance_type = "ecs.n1.small" 1328 internet_charge_type = "PayByBandwidth" 1329 instance_name = "update_image" 1330 io_optimized = "optimized" 1331 password = "Test12345" 1332 } 1333 `