github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_eip_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 "log" 11 ) 12 13 func TestAccAlicloudEIP_basic(t *testing.T) { 14 var eip ecs.EipAddressSetType 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { 18 testAccPreCheck(t) 19 }, 20 21 // module name 22 IDRefreshName: "alicloud_eip.foo", 23 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckEIPDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccEIPConfig, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckEIPExists( 31 "alicloud_eip.foo", &eip), 32 testAccCheckEIPAttributes(&eip), 33 ), 34 }, 35 resource.TestStep{ 36 Config: testAccEIPConfigTwo, 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckEIPExists( 39 "alicloud_eip.foo", &eip), 40 testAccCheckEIPAttributes(&eip), 41 resource.TestCheckResourceAttr( 42 "alicloud_eip.foo", 43 "bandwidth", 44 "10"), 45 ), 46 }, 47 }, 48 }) 49 50 } 51 52 func testAccCheckEIPExists(n string, eip *ecs.EipAddressSetType) resource.TestCheckFunc { 53 return func(s *terraform.State) error { 54 rs, ok := s.RootModule().Resources[n] 55 if !ok { 56 return fmt.Errorf("Not found: %s", n) 57 } 58 59 if rs.Primary.ID == "" { 60 return fmt.Errorf("No EIP ID is set") 61 } 62 63 client := testAccProvider.Meta().(*AliyunClient) 64 d, err := client.DescribeEipAddress(rs.Primary.ID) 65 66 log.Printf("[WARN] eip id %#v", rs.Primary.ID) 67 68 if err != nil { 69 return err 70 } 71 72 if d == nil || d.IpAddress == "" { 73 return fmt.Errorf("EIP not found") 74 } 75 76 *eip = *d 77 return nil 78 } 79 } 80 81 func testAccCheckEIPAttributes(eip *ecs.EipAddressSetType) resource.TestCheckFunc { 82 return func(s *terraform.State) error { 83 if eip.IpAddress == "" { 84 return fmt.Errorf("Empty Ip address") 85 } 86 87 return nil 88 } 89 } 90 91 func testAccCheckEIPDestroy(s *terraform.State) error { 92 client := testAccProvider.Meta().(*AliyunClient) 93 94 for _, rs := range s.RootModule().Resources { 95 if rs.Type != "alicloud_eip" { 96 continue 97 } 98 99 // Try to find the EIP 100 conn := client.ecsconn 101 102 args := &ecs.DescribeEipAddressesArgs{ 103 RegionId: client.Region, 104 AllocationId: rs.Primary.ID, 105 } 106 d, _, err := conn.DescribeEipAddresses(args) 107 108 if d != nil && len(d) > 0 { 109 return fmt.Errorf("Error EIP still exist") 110 } 111 112 // Verify the error is what we want 113 if err != nil { 114 return err 115 } 116 } 117 118 return nil 119 } 120 121 const testAccEIPConfig = ` 122 resource "alicloud_eip" "foo" { 123 } 124 ` 125 126 const testAccEIPConfigTwo = ` 127 resource "alicloud_eip" "foo" { 128 bandwidth = "10" 129 internet_charge_type = "PayByBandwidth" 130 } 131 `