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