routetable.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package compute
  15. import (
  16. "net"
  17. "reflect"
  18. "strings"
  19. "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/gotypes"
  23. "yunion.io/x/onecloud/pkg/apis"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. )
  26. const (
  27. ROUTE_TABLE_UPDATING = "updating"
  28. ROUTE_TABLE_UPDATEFAILED = "update_falied"
  29. ROUTE_TABLE_AVAILABLE = compute.ROUTE_TABLE_AVAILABLE
  30. ROUTE_TABLE_UNKNOWN = "unknown"
  31. )
  32. type RouteTableDetails struct {
  33. apis.StatusInfrasResourceBaseDetails
  34. VpcResourceInfo
  35. SRouteTable
  36. RouteSetCount int `json:"route_set_count"`
  37. AccociationCount int `json:"accociation_count"`
  38. }
  39. type SRoute struct {
  40. Type string `json:"type"`
  41. Cidr string `json:"cidr"`
  42. NextHopType string `json:"next_hop_type"`
  43. NextHopId string `json:"next_hop_id"`
  44. }
  45. func (route *SRoute) Validate() error {
  46. if strings.Index(route.Cidr, "/") > 0 {
  47. _, ipNet, err := net.ParseCIDR(route.Cidr)
  48. if err != nil {
  49. return errors.Wrapf(httperrors.ErrInputParameter, "net.ParseCIDR %s", err)
  50. }
  51. // normalize from 192.168.1.3/24 to 192.168.1.0/24
  52. route.Cidr = ipNet.String()
  53. } else {
  54. ip := net.ParseIP(route.Cidr).To4()
  55. if ip == nil {
  56. return errors.Wrapf(httperrors.ErrInputParameter, "invalid addr %s", route.Cidr)
  57. }
  58. }
  59. return nil
  60. }
  61. type SRoutes []*SRoute
  62. func (routes SRoutes) String() string {
  63. return jsonutils.Marshal(routes).String()
  64. }
  65. func (routes SRoutes) IsZero() bool {
  66. if len(routes) == 0 {
  67. return true
  68. }
  69. return false
  70. }
  71. func (routes *SRoutes) Validate() error {
  72. if routes == nil {
  73. *routes = SRoutes{}
  74. return nil
  75. }
  76. found := map[string]struct{}{}
  77. for _, route := range *routes {
  78. if err := route.Validate(); err != nil {
  79. return err
  80. }
  81. if _, ok := found[route.Cidr]; ok {
  82. // error so that the user has a chance to deal with comments
  83. return httperrors.NewInputParameterError("duplicate route cidr %s", route.Cidr)
  84. }
  85. // TODO aliyun: check overlap with System type route
  86. found[route.Cidr] = struct{}{}
  87. }
  88. return nil
  89. }
  90. type RouteTableCreateInput struct {
  91. apis.StatusInfrasResourceBaseCreateInput
  92. VpcResourceInput
  93. Type string `json:"type"`
  94. Routes *SRoutes `json:"routes"`
  95. }
  96. type RouteTableUpdateInput struct {
  97. apis.StatusInfrasResourceBaseUpdateInput
  98. Routes *SRoutes `json:"routes"`
  99. }
  100. type RouteTableFilterListBase struct {
  101. RouteTableId string `json:"RouteTableId"`
  102. }
  103. type RouteTableFilterList struct {
  104. RouteTableFilterListBase
  105. VpcFilterListInput
  106. }
  107. func init() {
  108. gotypes.RegisterSerializable(reflect.TypeOf(&SRoutes{}), func() gotypes.ISerializable {
  109. return &SRoutes{}
  110. })
  111. }