route_tables.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package ecs
  2. import (
  3. "time"
  4. "github.com/denverdino/aliyungo/common"
  5. "github.com/denverdino/aliyungo/util"
  6. )
  7. type DescribeRouteTablesArgs struct {
  8. VRouterId string
  9. RouteTableId string
  10. common.Pagination
  11. }
  12. type RouteTableType string
  13. const (
  14. RouteTableSystem = RouteTableType("System")
  15. RouteTableCustom = RouteTableType("Custom")
  16. )
  17. type RouteEntryStatus string
  18. const (
  19. RouteEntryStatusPending = RouteEntryStatus("Pending")
  20. RouteEntryStatusAvailable = RouteEntryStatus("Available")
  21. RouteEntryStatusModifying = RouteEntryStatus("Modifying")
  22. )
  23. //
  24. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&routeentrysettype
  25. type RouteEntrySetType struct {
  26. RouteTableId string
  27. DestinationCidrBlock string
  28. Type RouteTableType
  29. InstanceId string
  30. Status RouteEntryStatus // enum Pending | Available | Modifying
  31. }
  32. //
  33. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&routetablesettype
  34. type RouteTableSetType struct {
  35. VRouterId string
  36. RouteTableId string
  37. RouteEntrys struct {
  38. RouteEntry []RouteEntrySetType
  39. }
  40. RouteTableType RouteTableType
  41. CreationTime util.ISO6801Time
  42. }
  43. type DescribeRouteTablesResponse struct {
  44. common.Response
  45. common.PaginationResult
  46. RouteTables struct {
  47. RouteTable []RouteTableSetType
  48. }
  49. }
  50. // DescribeRouteTables describes Virtual Routers
  51. //
  52. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/routertable&describeroutetables
  53. func (client *Client) DescribeRouteTables(args *DescribeRouteTablesArgs) (routeTables []RouteTableSetType, pagination *common.PaginationResult, err error) {
  54. args.Validate()
  55. response := DescribeRouteTablesResponse{}
  56. err = client.Invoke("DescribeRouteTables", args, &response)
  57. if err == nil {
  58. return response.RouteTables.RouteTable, &response.PaginationResult, nil
  59. }
  60. return nil, nil, err
  61. }
  62. type NextHopType string
  63. const (
  64. NextHopIntance = NextHopType("Instance") //Default
  65. NextHopTunnel = NextHopType("Tunnel")
  66. )
  67. type CreateRouteEntryArgs struct {
  68. RouteTableId string
  69. DestinationCidrBlock string
  70. NextHopType NextHopType
  71. NextHopId string
  72. ClientToken string
  73. }
  74. type CreateRouteEntryResponse struct {
  75. common.Response
  76. }
  77. // CreateRouteEntry creates route entry
  78. //
  79. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/routertable&createrouteentry
  80. func (client *Client) CreateRouteEntry(args *CreateRouteEntryArgs) error {
  81. response := CreateRouteEntryResponse{}
  82. return client.Invoke("CreateRouteEntry", args, &response)
  83. }
  84. type DeleteRouteEntryArgs struct {
  85. RouteTableId string
  86. DestinationCidrBlock string
  87. NextHopId string
  88. }
  89. type DeleteRouteEntryResponse struct {
  90. common.Response
  91. }
  92. // DeleteRouteEntry deletes route entry
  93. //
  94. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/routertable&deleterouteentry
  95. func (client *Client) DeleteRouteEntry(args *DeleteRouteEntryArgs) error {
  96. response := DeleteRouteEntryResponse{}
  97. return client.Invoke("DeleteRouteEntry", args, &response)
  98. }
  99. // WaitForAllRouteEntriesAvailable waits for all route entries to Available status
  100. func (client *Client) WaitForAllRouteEntriesAvailable(vrouterId string, routeTableId string, timeout int) error {
  101. if timeout <= 0 {
  102. timeout = DefaultTimeout
  103. }
  104. args := DescribeRouteTablesArgs{
  105. VRouterId: vrouterId,
  106. RouteTableId: routeTableId,
  107. }
  108. for {
  109. routeTables, _, err := client.DescribeRouteTables(&args)
  110. if err != nil {
  111. return err
  112. }
  113. if len(routeTables) == 0 {
  114. return common.GetClientErrorFromString("Not found")
  115. }
  116. success := true
  117. loop:
  118. for _, routeTable := range routeTables {
  119. for _, routeEntry := range routeTable.RouteEntrys.RouteEntry {
  120. if routeEntry.Status != RouteEntryStatusAvailable {
  121. success = false
  122. break loop
  123. }
  124. }
  125. }
  126. if success {
  127. break
  128. }
  129. timeout = timeout - DefaultWaitForInterval
  130. if timeout <= 0 {
  131. return common.GetClientErrorFromString("Timeout")
  132. }
  133. time.Sleep(DefaultWaitForInterval * time.Second)
  134. }
  135. return nil
  136. }