| 1 | package ecs |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/denverdino/aliyungo/common" |
| 7 | ) |
| 8 | |
| 9 | func TestAllocatePublicIpAddress(t *testing.T) { |
| 10 | |
| 11 | client := NewTestClient() |
| 12 | instance, err := client.DescribeInstanceAttribute(TestInstanceId) |
| 13 | if err != nil { |
| 14 | t.Fatalf("Failed to describe instance %s: %v", TestInstanceId, err) |
| 15 | } |
| 16 | t.Logf("Instance: %++v %v", instance, err) |
| 17 | ipAddr, err := client.AllocatePublicIpAddress(TestInstanceId) |
| 18 | if err != nil { |
| 19 | t.Fatalf("Failed to allocate public IP address for instance %s: %v", TestInstanceId, err) |
| 20 | } |
| 21 | t.Logf("Public IP address of instance %s: %s", TestInstanceId, ipAddr) |
| 22 | |
| 23 | } |
| 24 | |
| 25 | func testEipAddress(t *testing.T, client *Client, regionId common.Region, instanceId string) error { |
| 26 | |
| 27 | args := AllocateEipAddressArgs{ |
| 28 | RegionId: regionId, |
| 29 | Bandwidth: 5, |
| 30 | InternetChargeType: common.PayByTraffic, |
| 31 | ClientToken: client.GenerateClientToken(), |
| 32 | } |
| 33 | ipAddr, allocationId, err := client.AllocateEipAddress(&args) |
| 34 | if err != nil { |
| 35 | t.Errorf("Failed to allocate EIP address: %v", err) |
| 36 | return err |
| 37 | } |
| 38 | t.Logf("EIP address: %s, AllocationId: %s", ipAddr, allocationId) |
| 39 | |
| 40 | err = client.WaitForEip(regionId, allocationId, EipStatusAvailable, 0) |
| 41 | if err != nil { |
| 42 | t.Errorf("Failed to wait EIP %s: %v", allocationId, err) |
| 43 | } |
| 44 | |
| 45 | err = client.AssociateEipAddress(allocationId, instanceId) |
| 46 | if err != nil { |
| 47 | t.Errorf("Failed to associate EIP address: %v", err) |
| 48 | } |
| 49 | err = client.WaitForEip(regionId, allocationId, EipStatusInUse, 0) |
| 50 | if err != nil { |
| 51 | t.Errorf("Failed to wait EIP %s: %v", allocationId, err) |
| 52 | } |
| 53 | err = client.UnassociateEipAddress(allocationId, instanceId) |
| 54 | if err != nil { |
| 55 | t.Errorf("Failed to unassociate EIP address: %v", err) |
| 56 | } |
| 57 | err = client.WaitForEip(regionId, allocationId, EipStatusAvailable, 0) |
| 58 | if err != nil { |
| 59 | t.Errorf("Failed to wait EIP %s: %v", allocationId, err) |
| 60 | } |
| 61 | err = client.ReleaseEipAddress(allocationId) |
| 62 | if err != nil { |
| 63 | t.Errorf("Failed to release EIP address: %v", err) |
| 64 | } |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | func TestClient_AllocateEipAddress(t *testing.T) { |
| 69 | client := NewVPCClientWithSecurityToken(TestAccessKeyId, TestAccessKeySecret, "", TestRegionID) |
| 70 | client.SetDebug(true) |
| 71 | args := &AllocateEipAddressArgs{ |
| 72 | RegionId: TestRegionID, |
| 73 | Bandwidth: 5, |
| 74 | InternetChargeType: common.PayByTraffic, |
| 75 | ISP: "BGP_FinanceCloud", |
| 76 | ClientToken: client.GenerateClientToken(), |
| 77 | } |
| 78 | |
| 79 | eip, aid, err := client.AllocateEipAddress(args) |
| 80 | if err != nil { |
| 81 | t.Fatalf("Error %++v", err) |
| 82 | } else { |
| 83 | t.Logf("eip=%s,aid=%s", eip, aid) |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
| 88 | func TestClient_DescribeEipAddresses(t *testing.T) { |
| 89 | client := NewVpcTestClientForDebug() |
| 90 | args := &DescribeEipAddressesArgs{ |
| 91 | RegionId: TestRegionID, |
| 92 | AssociatedInstanceType: AssociatedInstanceTypeNat, |
| 93 | AssociatedInstanceId: TestInstanceId, |
| 94 | } |
| 95 | |
| 96 | eips, _, err := client.DescribeEipAddresses(args) |
| 97 | if err != nil { |
| 98 | t.Fatalf("Error %++v", err) |
| 99 | } else { |
| 100 | for index, item := range eips { |
| 101 | t.Logf("eips[%d]=%++v", index, item) |
| 102 | } |
| 103 | } |
| 104 | } |