networks.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // API on Network
  2. package ecs
  3. import (
  4. "time"
  5. "github.com/denverdino/aliyungo/common"
  6. "github.com/denverdino/aliyungo/util"
  7. )
  8. type AllocatePublicIpAddressArgs struct {
  9. InstanceId string
  10. }
  11. type AllocatePublicIpAddressResponse struct {
  12. common.Response
  13. IpAddress string
  14. }
  15. // AllocatePublicIpAddress allocates Public Ip Address
  16. //
  17. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&allocatepublicipaddress
  18. func (client *Client) AllocatePublicIpAddress(instanceId string) (ipAddress string, err error) {
  19. args := AllocatePublicIpAddressArgs{
  20. InstanceId: instanceId,
  21. }
  22. response := AllocatePublicIpAddressResponse{}
  23. err = client.Invoke("AllocatePublicIpAddress", &args, &response)
  24. if err != nil {
  25. return "", err
  26. }
  27. return response.IpAddress, nil
  28. }
  29. type ModifyInstanceNetworkSpec struct {
  30. InstanceId string
  31. InternetMaxBandwidthOut *int
  32. InternetMaxBandwidthIn *int
  33. }
  34. type ModifyInstanceNetworkSpecResponse struct {
  35. common.Response
  36. }
  37. // ModifyInstanceNetworkSpec modifies instance network spec
  38. //
  39. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&modifyinstancenetworkspec
  40. func (client *Client) ModifyInstanceNetworkSpec(args *ModifyInstanceNetworkSpec) error {
  41. response := ModifyInstanceNetworkSpecResponse{}
  42. return client.Invoke("ModifyInstanceNetworkSpec", args, &response)
  43. }
  44. type AllocateEipAddressArgs struct {
  45. RegionId common.Region
  46. Bandwidth int
  47. InternetChargeType common.InternetChargeType
  48. ClientToken string
  49. }
  50. type AllocateEipAddressResponse struct {
  51. common.Response
  52. EipAddress string
  53. AllocationId string
  54. }
  55. // AllocateEipAddress allocates Eip Address
  56. //
  57. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&allocateeipaddress
  58. func (client *Client) AllocateEipAddress(args *AllocateEipAddressArgs) (EipAddress string, AllocationId string, err error) {
  59. if args.Bandwidth == 0 {
  60. args.Bandwidth = 5
  61. }
  62. response := AllocateEipAddressResponse{}
  63. err = client.Invoke("AllocateEipAddress", args, &response)
  64. if err != nil {
  65. return "", "", err
  66. }
  67. return response.EipAddress, response.AllocationId, nil
  68. }
  69. type AssociateEipAddressArgs struct {
  70. AllocationId string
  71. InstanceId string
  72. }
  73. type AssociateEipAddressResponse struct {
  74. common.Response
  75. }
  76. // AssociateEipAddress associates EIP address to VM instance
  77. //
  78. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&associateeipaddress
  79. func (client *Client) AssociateEipAddress(allocationId string, instanceId string) error {
  80. args := AssociateEipAddressArgs{
  81. AllocationId: allocationId,
  82. InstanceId: instanceId,
  83. }
  84. response := ModifyInstanceNetworkSpecResponse{}
  85. return client.Invoke("AssociateEipAddress", &args, &response)
  86. }
  87. // Status of disks
  88. type EipStatus string
  89. const (
  90. EipStatusAssociating = EipStatus("Associating")
  91. EipStatusUnassociating = EipStatus("Unassociating")
  92. EipStatusInUse = EipStatus("InUse")
  93. EipStatusAvailable = EipStatus("Available")
  94. )
  95. type DescribeEipAddressesArgs struct {
  96. RegionId common.Region
  97. Status EipStatus //enum Associating | Unassociating | InUse | Available
  98. EipAddress string
  99. AllocationId string
  100. common.Pagination
  101. }
  102. //
  103. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&eipaddresssettype
  104. type EipAddressSetType struct {
  105. RegionId common.Region
  106. IpAddress string
  107. AllocationId string
  108. Status EipStatus
  109. InstanceId string
  110. Bandwidth string // Why string
  111. InternetChargeType common.InternetChargeType
  112. OperationLocks OperationLocksType
  113. AllocationTime util.ISO6801Time
  114. }
  115. type DescribeEipAddressesResponse struct {
  116. common.Response
  117. common.PaginationResult
  118. EipAddresses struct {
  119. EipAddress []EipAddressSetType
  120. }
  121. }
  122. // DescribeInstanceStatus describes instance status
  123. //
  124. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&describeeipaddresses
  125. func (client *Client) DescribeEipAddresses(args *DescribeEipAddressesArgs) (eipAddresses []EipAddressSetType, pagination *common.PaginationResult, err error) {
  126. args.Validate()
  127. response := DescribeEipAddressesResponse{}
  128. err = client.Invoke("DescribeEipAddresses", args, &response)
  129. if err == nil {
  130. return response.EipAddresses.EipAddress, &response.PaginationResult, nil
  131. }
  132. return nil, nil, err
  133. }
  134. type ModifyEipAddressAttributeArgs struct {
  135. AllocationId string
  136. Bandwidth int
  137. }
  138. type ModifyEipAddressAttributeResponse struct {
  139. common.Response
  140. }
  141. // ModifyEipAddressAttribute Modifies EIP attribute
  142. //
  143. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&modifyeipaddressattribute
  144. func (client *Client) ModifyEipAddressAttribute(allocationId string, bandwidth int) error {
  145. args := ModifyEipAddressAttributeArgs{
  146. AllocationId: allocationId,
  147. Bandwidth: bandwidth,
  148. }
  149. response := ModifyEipAddressAttributeResponse{}
  150. return client.Invoke("ModifyEipAddressAttribute", &args, &response)
  151. }
  152. type UnallocateEipAddressArgs struct {
  153. AllocationId string
  154. InstanceId string
  155. }
  156. type UnallocateEipAddressResponse struct {
  157. common.Response
  158. }
  159. // UnassociateEipAddress unallocates Eip Address from instance
  160. //
  161. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&unassociateeipaddress
  162. func (client *Client) UnassociateEipAddress(allocationId string, instanceId string) error {
  163. args := UnallocateEipAddressArgs{
  164. AllocationId: allocationId,
  165. InstanceId: instanceId,
  166. }
  167. response := UnallocateEipAddressResponse{}
  168. return client.Invoke("UnassociateEipAddress", &args, &response)
  169. }
  170. type ReleaseEipAddressArgs struct {
  171. AllocationId string
  172. }
  173. type ReleaseEipAddressResponse struct {
  174. common.Response
  175. }
  176. // ReleaseEipAddress releases Eip address
  177. //
  178. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&releaseeipaddress
  179. func (client *Client) ReleaseEipAddress(allocationId string) error {
  180. args := ReleaseEipAddressArgs{
  181. AllocationId: allocationId,
  182. }
  183. response := ReleaseEipAddressResponse{}
  184. return client.Invoke("ReleaseEipAddress", &args, &response)
  185. }
  186. // WaitForVSwitchAvailable waits for VSwitch to given status
  187. func (client *Client) WaitForEip(regionId common.Region, allocationId string, status EipStatus, timeout int) error {
  188. if timeout <= 0 {
  189. timeout = DefaultTimeout
  190. }
  191. args := DescribeEipAddressesArgs{
  192. RegionId: regionId,
  193. AllocationId: allocationId,
  194. }
  195. for {
  196. eips, _, err := client.DescribeEipAddresses(&args)
  197. if err != nil {
  198. return err
  199. }
  200. if len(eips) == 0 {
  201. return common.GetClientErrorFromString("Not found")
  202. }
  203. if eips[0].Status == status {
  204. break
  205. }
  206. timeout = timeout - DefaultWaitForInterval
  207. if timeout <= 0 {
  208. return common.GetClientErrorFromString("Timeout")
  209. }
  210. time.Sleep(DefaultWaitForInterval * time.Second)
  211. }
  212. return nil
  213. }