loadbalanceracl.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "unicode"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/gotypes"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. )
  25. type LoadbalancerAclListInput struct {
  26. apis.SharableVirtualResourceListInput
  27. apis.ExternalizedResourceBaseListInput
  28. ManagedResourceListInput
  29. RegionalFilterListInput
  30. }
  31. type LoadbalancerAclDetails struct {
  32. apis.SharableVirtualResourceDetails
  33. ManagedResourceInfo
  34. CloudregionResourceInfo
  35. SLoadbalancerAcl
  36. LoadbalancerAclUsage
  37. }
  38. type LoadbalancerAclUsage struct {
  39. ListenerCount int `json:"lb_listener_count"`
  40. }
  41. type LoadbalancerAclResourceInfo struct {
  42. // 负载均衡ACL名称
  43. Acl string `json:"acl"`
  44. }
  45. type LoadbalancerAclResourceInput struct {
  46. // ACL名称或ID
  47. AclId string `json:"acl_id"`
  48. // swagger:ignore
  49. // Deprecated
  50. Acl string `json:"acl" yunion-deprecated-by:"acl_id"`
  51. }
  52. type LoadbalancerAclFilterListInput struct {
  53. LoadbalancerAclResourceInput
  54. // 以ACL名称排序
  55. OrderByAcl string `json:"order_by_acl"`
  56. }
  57. type SAclEntry struct {
  58. Cidr string `json:"cidr"`
  59. Comment string `json:"comment"`
  60. }
  61. type SAclEntries []SAclEntry
  62. func (self SAclEntries) String() string {
  63. return jsonutils.Marshal(self).String()
  64. }
  65. func (self SAclEntries) IsZero() bool {
  66. return len(self) == 0
  67. }
  68. func (aclEntry *SAclEntry) Validate() error {
  69. if strings.Index(aclEntry.Cidr, "/") > 0 {
  70. _, ipNet, err := net.ParseCIDR(aclEntry.Cidr)
  71. if err != nil {
  72. return err
  73. }
  74. // normalize from 192.168.1.3/24 to 192.168.1.0/24
  75. aclEntry.Cidr = ipNet.String()
  76. } else {
  77. ip := net.ParseIP(aclEntry.Cidr).To4()
  78. if ip == nil {
  79. return httperrors.NewInputParameterError("invalid addr %s", aclEntry.Cidr)
  80. }
  81. }
  82. if commentLimit := 128; len(aclEntry.Comment) > commentLimit {
  83. return httperrors.NewInputParameterError("comment too long (%d>=%d)",
  84. len(aclEntry.Comment), commentLimit)
  85. }
  86. for _, r := range aclEntry.Comment {
  87. if !unicode.IsPrint(r) {
  88. return httperrors.NewInputParameterError("comment contains non-printable char: %v", r)
  89. }
  90. }
  91. return nil
  92. }
  93. type LoadbalancerAclCreateInput struct {
  94. apis.SharableVirtualResourceCreateInput
  95. CloudregionResourceInput
  96. CloudproviderResourceInput
  97. AclEntries SAclEntries `json:"acl_entries"`
  98. }
  99. func (self *LoadbalancerAclCreateInput) Validate() error {
  100. if len(self.AclEntries) == 0 {
  101. return httperrors.NewMissingParameterError("acl_entries")
  102. }
  103. found := map[string]bool{}
  104. for _, acl := range self.AclEntries {
  105. err := acl.Validate()
  106. if err != nil {
  107. return err
  108. }
  109. if _, ok := found[acl.Cidr]; ok {
  110. // error so that the user has a chance to deal with comments
  111. return httperrors.NewInputParameterError("acl cidr duplicate %s", acl.Cidr)
  112. }
  113. found[acl.Cidr] = true
  114. }
  115. return nil
  116. }
  117. type LoadbalancerAclUpdateInput struct {
  118. apis.SharableVirtualResourceBaseUpdateInput
  119. AclEntries SAclEntries `json:"acl_entries"`
  120. }
  121. func (self *LoadbalancerAclUpdateInput) Validate() error {
  122. if len(self.AclEntries) > 0 {
  123. found := map[string]bool{}
  124. for _, acl := range self.AclEntries {
  125. err := acl.Validate()
  126. if err != nil {
  127. return err
  128. }
  129. if _, ok := found[acl.Cidr]; ok {
  130. // error so that the user has a chance to deal with comments
  131. return httperrors.NewInputParameterError("acl cidr duplicate %s", acl.Cidr)
  132. }
  133. found[acl.Cidr] = true
  134. }
  135. }
  136. return nil
  137. }
  138. type LoadbalancerAclPatchInput struct {
  139. Adds []SAclEntry `json:"adds"`
  140. Dels []SAclEntry `json:"dels"`
  141. }
  142. func init() {
  143. gotypes.RegisterSerializable(reflect.TypeOf(&SAclEntries{}), func() gotypes.ISerializable {
  144. return &SAclEntries{}
  145. })
  146. }