github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_eip_association_test.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/denverdino/aliyungo/ecs" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "time" 11 ) 12 13 func TestAccAlicloudEIPAssociation(t *testing.T) { 14 var asso ecs.EipAddressSetType 15 var inst ecs.InstanceAttributesType 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { 19 testAccPreCheck(t) 20 }, 21 22 // module name 23 IDRefreshName: "alicloud_eip_association.foo", 24 25 Providers: testAccProviders, 26 CheckDestroy: testAccCheckEIPAssociationDestroy, 27 Steps: []resource.TestStep{ 28 resource.TestStep{ 29 Config: testAccEIPAssociationConfig, 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckInstanceExists( 32 "alicloud_instance.instance", &inst), 33 testAccCheckEIPExists( 34 "alicloud_eip.eip", &asso), 35 testAccCheckEIPAssociationExists( 36 "alicloud_eip_association.foo", &inst, &asso), 37 ), 38 }, 39 }, 40 }) 41 42 } 43 44 func testAccCheckEIPAssociationExists(n string, instance *ecs.InstanceAttributesType, eip *ecs.EipAddressSetType) resource.TestCheckFunc { 45 return func(s *terraform.State) error { 46 rs, ok := s.RootModule().Resources[n] 47 if !ok { 48 return fmt.Errorf("Not found: %s", n) 49 } 50 51 if rs.Primary.ID == "" { 52 return fmt.Errorf("No EIP Association ID is set") 53 } 54 55 client := testAccProvider.Meta().(*AliyunClient) 56 return resource.Retry(3*time.Minute, func() *resource.RetryError { 57 d, err := client.DescribeEipAddress(rs.Primary.Attributes["allocation_id"]) 58 59 if err != nil { 60 return resource.NonRetryableError(err) 61 } 62 63 if d != nil { 64 if d.Status != ecs.EipStatusInUse { 65 return resource.RetryableError(fmt.Errorf("Eip is in associating - trying again while it associates")) 66 } else if d.InstanceId == instance.InstanceId { 67 *eip = *d 68 return nil 69 } 70 } 71 72 return resource.NonRetryableError(fmt.Errorf("EIP Association not found")) 73 }) 74 } 75 } 76 77 func testAccCheckEIPAssociationDestroy(s *terraform.State) error { 78 client := testAccProvider.Meta().(*AliyunClient) 79 80 for _, rs := range s.RootModule().Resources { 81 if rs.Type != "alicloud_eip_association" { 82 continue 83 } 84 85 if rs.Primary.ID == "" { 86 return fmt.Errorf("No EIP Association ID is set") 87 } 88 89 // Try to find the EIP 90 eips, _, err := client.ecsconn.DescribeEipAddresses(&ecs.DescribeEipAddressesArgs{ 91 RegionId: client.Region, 92 AllocationId: rs.Primary.Attributes["allocation_id"], 93 }) 94 95 for _, eip := range eips { 96 if eip.Status != ecs.EipStatusAvailable { 97 return fmt.Errorf("Error EIP Association still exist") 98 } 99 } 100 101 // Verify the error is what we want 102 if err != nil { 103 return err 104 } 105 } 106 107 return nil 108 } 109 110 const testAccEIPAssociationConfig = ` 111 resource "alicloud_vpc" "main" { 112 cidr_block = "10.1.0.0/21" 113 } 114 115 resource "alicloud_vswitch" "main" { 116 vpc_id = "${alicloud_vpc.main.id}" 117 cidr_block = "10.1.1.0/24" 118 availability_zone = "cn-beijing-a" 119 depends_on = [ 120 "alicloud_vpc.main"] 121 } 122 123 resource "alicloud_instance" "instance" { 124 image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd" 125 instance_type = "ecs.s1.small" 126 availability_zone = "cn-beijing-a" 127 security_groups = ["${alicloud_security_group.group.id}"] 128 vswitch_id = "${alicloud_vswitch.main.id}" 129 instance_name = "hello" 130 io_optimized = "none" 131 132 tags { 133 Name = "TerraformTest-instance" 134 } 135 } 136 137 resource "alicloud_eip" "eip" { 138 } 139 140 resource "alicloud_eip_association" "foo" { 141 allocation_id = "${alicloud_eip.eip.id}" 142 instance_id = "${alicloud_instance.instance.id}" 143 } 144 145 resource "alicloud_security_group" "group" { 146 name = "terraform-test-group" 147 description = "New security group" 148 vpc_id = "${alicloud_vpc.main.id}" 149 } 150 `