| 1 |
package ecs |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/denverdino/aliyungo/common" |
| 8 |
) |
| 9 |
|
| 10 |
type CreateNetworkInterfaceArgs struct { |
| 11 |
RegionId common.Region |
| 12 |
VSwitchId string |
| 13 |
PrimaryIpAddress string |
| 14 |
SecurityGroupId string |
| 15 |
NetworkInterfaceName string |
| 16 |
Description string |
| 17 |
ClientToken string |
| 18 |
} |
| 19 |
|
| 20 |
type CreateNetworkInterfaceResponse struct { |
| 21 |
common.Response |
| 22 |
NetworkInterfaceId string |
| 23 |
} |
| 24 |
type DeleteNetworkInterfaceArgs struct { |
| 25 |
RegionId common.Region |
| 26 |
NetworkInterfaceId string |
| 27 |
} |
| 28 |
|
| 29 |
type DeleteNetworkInterfaceResponse struct { |
| 30 |
common.Response |
| 31 |
} |
| 32 |
|
| 33 |
type DescribeNetworkInterfacesArgs struct { |
| 34 |
RegionId common.Region |
| 35 |
VSwitchId string |
| 36 |
PrimaryIpAddress string |
| 37 |
SecurityGroupId string |
| 38 |
NetworkInterfaceName string |
| 39 |
Type string |
| 40 |
InstanceId string |
| 41 |
NetworkInterfaceId []string `query:"list"` |
| 42 |
PageNumber int |
| 43 |
PageSize int |
| 44 |
} |
| 45 |
type NetworkInterfaceType struct { |
| 46 |
NetworkInterfaceId string |
| 47 |
NetworkInterfaceName string |
| 48 |
PrimaryIpAddress string |
| 49 |
MacAddress string |
| 50 |
Status string |
| 51 |
PrivateIpAddress string |
| 52 |
} |
| 53 |
|
| 54 |
type DescribeNetworkInterfacesResponse struct { |
| 55 |
common.Response |
| 56 |
NetworkInterfaceSets struct { |
| 57 |
NetworkInterfaceSet []NetworkInterfaceType |
| 58 |
} |
| 59 |
TotalCount int |
| 60 |
PageNumber int |
| 61 |
PageSize int |
| 62 |
} |
| 63 |
type AttachNetworkInterfaceArgs struct { |
| 64 |
RegionId common.Region |
| 65 |
NetworkInterfaceId string |
| 66 |
InstanceId string |
| 67 |
} |
| 68 |
|
| 69 |
type AttachNetworkInterfaceResponse common.Response |
| 70 |
|
| 71 |
type DetachNetworkInterfaceArgs AttachNetworkInterfaceArgs |
| 72 |
|
| 73 |
type DetachNetworkInterfaceResponse common.Response |
| 74 |
|
| 75 |
type ModifyNetworkInterfaceAttributeArgs struct { |
| 76 |
RegionId common.Region |
| 77 |
NetworkInterfaceId string |
| 78 |
SecurityGroupId []string |
| 79 |
NetworkInterfaceName string |
| 80 |
Description string |
| 81 |
} |
| 82 |
type ModifyNetworkInterfaceAttributeResponse common.Response |
| 83 |
|
| 84 |
func (client *Client) CreateNetworkInterface(args *CreateNetworkInterfaceArgs) (resp *CreateNetworkInterfaceResponse, err error) { |
| 85 |
resp = &CreateNetworkInterfaceResponse{} |
| 86 |
err = client.Invoke("CreateNetworkInterface", args, resp) |
| 87 |
return resp, err |
| 88 |
} |
| 89 |
|
| 90 |
func (client *Client) DeleteNetworkInterface(args *DeleteNetworkInterfaceArgs) (resp *DeleteNetworkInterfaceResponse, err error) { |
| 91 |
resp = &DeleteNetworkInterfaceResponse{} |
| 92 |
err = client.Invoke("DeleteNetworkInterface", args, resp) |
| 93 |
return resp, err |
| 94 |
} |
| 95 |
|
| 96 |
func (client *Client) DescribeNetworkInterfaces(args *DescribeNetworkInterfacesArgs) (resp *DescribeNetworkInterfacesResponse, err error) { |
| 97 |
resp = &DescribeNetworkInterfacesResponse{} |
| 98 |
err = client.Invoke("DescribeNetworkInterfaces", args, resp) |
| 99 |
return resp, err |
| 100 |
} |
| 101 |
|
| 102 |
func (client *Client) AttachNetworkInterface(args *AttachNetworkInterfaceArgs) error { |
| 103 |
resp := &AttachNetworkInterfaceResponse{} |
| 104 |
err := client.Invoke("AttachNetworkInterface", args, resp) |
| 105 |
return err |
| 106 |
} |
| 107 |
|
| 108 |
func (client *Client) DetachNetworkInterface(args *DetachNetworkInterfaceArgs) (resp *DetachNetworkInterfaceResponse, err error) { |
| 109 |
resp = &DetachNetworkInterfaceResponse{} |
| 110 |
err = client.Invoke("DetachNetworkInterface", args, resp) |
| 111 |
return resp, err |
| 112 |
} |
| 113 |
|
| 114 |
func (client *Client) ModifyNetworkInterfaceAttribute(args *ModifyNetworkInterfaceAttributeArgs) (resp *ModifyNetworkInterfaceAttributeResponse, err error) { |
| 115 |
resp = &ModifyNetworkInterfaceAttributeResponse{} |
| 116 |
err = client.Invoke("ModifyNetworkInterfaceAttribute", args, resp) |
| 117 |
return resp, err |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
const NetworkInterfacesDefaultTimeout = 120 |
| 122 |
|
| 123 |
|
| 124 |
func (client *Client) WaitForNetworkInterface(regionId common.Region, eniID string, status string, timeout int) error { |
| 125 |
if timeout <= 0 { |
| 126 |
timeout = NetworkInterfacesDefaultTimeout |
| 127 |
} |
| 128 |
for { |
| 129 |
|
| 130 |
eniIds := []string{eniID} |
| 131 |
|
| 132 |
describeNetworkInterfacesArgs := DescribeNetworkInterfacesArgs{ |
| 133 |
RegionId: regionId, |
| 134 |
NetworkInterfaceId: eniIds, |
| 135 |
} |
| 136 |
|
| 137 |
nisResponse, err := client.DescribeNetworkInterfaces(&describeNetworkInterfacesArgs) |
| 138 |
if err != nil { |
| 139 |
return fmt.Errorf("Failed to describe network interface %v: %v", eniID, err) |
| 140 |
} |
| 141 |
|
| 142 |
if len(nisResponse.NetworkInterfaceSets.NetworkInterfaceSet) > 0 && nisResponse.NetworkInterfaceSets.NetworkInterfaceSet[0].Status == status { |
| 143 |
break |
| 144 |
} |
| 145 |
|
| 146 |
timeout = timeout - DefaultWaitForInterval |
| 147 |
if timeout <= 0 { |
| 148 |
return fmt.Errorf("Timeout for waiting available status for network interfaces") |
| 149 |
} |
| 150 |
time.Sleep(DefaultWaitForInterval * time.Second) |
| 151 |
|
| 152 |
} |
| 153 |
return nil |
| 154 |
} |