loadbalanceracls.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. type AclEntry struct {
  22. Cidr string
  23. Comment string
  24. }
  25. type AclEntries []*AclEntry
  26. func NewAclEntry(s string) *AclEntry {
  27. tu := strings.SplitN(s, "#", 2)
  28. cidr := strings.TrimSpace(tu[0])
  29. comment := ""
  30. if len(tu) > 1 {
  31. comment = tu[1]
  32. }
  33. aclEntry := &AclEntry{
  34. Cidr: cidr,
  35. Comment: comment,
  36. }
  37. return aclEntry
  38. }
  39. func NewAclEntries(ss []string) AclEntries {
  40. aclEntries := AclEntries{}
  41. for _, s := range ss {
  42. aclEntry := NewAclEntry(s)
  43. aclEntries = append(aclEntries, aclEntry)
  44. }
  45. return aclEntries
  46. }
  47. func (entry *AclEntry) String() string {
  48. cidr := entry.Cidr
  49. comment := entry.Comment
  50. if comment != "" {
  51. comment = " # " + comment
  52. }
  53. return fmt.Sprintf("%-15s%s", cidr, comment)
  54. }
  55. func (entries AclEntries) String() string {
  56. ss := []string{}
  57. for _, entry := range entries {
  58. ss = append(ss, entry.String())
  59. }
  60. lines := strings.Join(ss, "\n")
  61. return lines
  62. }
  63. type LoadbalancerAclCreateOptions struct {
  64. options.SharableProjectizedResourceBaseCreateInput
  65. NAME string
  66. AclEntry []string `help:"acl entry with cidr and comment separated by #, e.g. 10.9.0.0/16#no comment" json:"-"`
  67. Manager string `json:"manager_id"`
  68. Region string `json:"cloudregion"`
  69. }
  70. type LoadbalancerAclListOptions struct {
  71. options.BaseListOptions
  72. Cloudregion string
  73. }
  74. func (opts *LoadbalancerAclListOptions) Params() (jsonutils.JSONObject, error) {
  75. return options.ListStructToParams(opts)
  76. }
  77. type LoadbalancerAclUpdateOptions struct {
  78. LoadbalancerAclIdOptions
  79. Name string
  80. AclEntry []string `help:"acl entry with cidr and comment separated by #, e.g. 10.9.0.0/16#no comment" json:"-"`
  81. }
  82. type LoadbalancerAclActionPatchOptions struct {
  83. LoadbalancerAclIdOptions
  84. Add []string `help:"acl entry with cidr and comment separated by #, e.g. 10.9.0.0/16#no comment" json:"-"`
  85. Del []string `help:"acl entry with cidr and comment separated by #, e.g. 10.9.0.0/16#no comment" json:"-"`
  86. }
  87. type LoadbalancerAclPublicOptions struct {
  88. options.SharableResourcePublicBaseOptions
  89. LoadbalancerAclIdOptions
  90. }
  91. func (opts *LoadbalancerAclPublicOptions) Params() (jsonutils.JSONObject, error) {
  92. return jsonutils.Marshal(opts.SharableResourcePublicBaseOptions), nil
  93. }
  94. type LoadbalancerAclIdOptions struct {
  95. ID string `json:"-"`
  96. }
  97. func (opts *LoadbalancerAclIdOptions) GetId() string {
  98. return opts.ID
  99. }
  100. func (opts *LoadbalancerAclIdOptions) Params() (jsonutils.JSONObject, error) {
  101. return nil, nil
  102. }
  103. func (opts *LoadbalancerAclCreateOptions) Params() (jsonutils.JSONObject, error) {
  104. params, err := options.StructToParams(opts)
  105. if err != nil {
  106. return nil, err
  107. }
  108. sp, err := opts.SharableProjectizedResourceBaseCreateInput.Params()
  109. if err != nil {
  110. return nil, err
  111. }
  112. params.Update(sp)
  113. aclEntries := NewAclEntries(opts.AclEntry)
  114. aclEntriesJson := jsonutils.Marshal(aclEntries)
  115. params.Set("acl_entries", aclEntriesJson)
  116. return params, nil
  117. }
  118. func (opts *LoadbalancerAclUpdateOptions) Params() (jsonutils.JSONObject, error) {
  119. params, err := options.StructToParams(opts)
  120. if err != nil {
  121. return nil, err
  122. }
  123. // - when it's nil, we leave it alone without updating
  124. // - when it's non-nil, we update it as a whole
  125. if opts.AclEntry != nil {
  126. aclEntries := NewAclEntries(opts.AclEntry)
  127. aclEntriesJson := jsonutils.Marshal(aclEntries)
  128. params.Set("acl_entries", aclEntriesJson)
  129. }
  130. return params, nil
  131. }
  132. func (opts *LoadbalancerAclActionPatchOptions) Params() (jsonutils.JSONObject, error) {
  133. params, err := options.StructToParams(opts)
  134. if err != nil {
  135. return nil, err
  136. }
  137. m := map[string][]string{
  138. "adds": opts.Add,
  139. "dels": opts.Del,
  140. }
  141. for k, ss := range m {
  142. aclEntries := NewAclEntries(ss)
  143. aclEntriesJson := jsonutils.Marshal(aclEntries)
  144. params.Set(k, aclEntriesJson)
  145. }
  146. return params, nil
  147. }
  148. type CachedLoadbalancerAclListOptions struct {
  149. LoadbalancerAclListOptions
  150. AclId string `help:"local acl id" `
  151. }
  152. func (opts *CachedLoadbalancerAclListOptions) Params() (jsonutils.JSONObject, error) {
  153. return options.StructToParams(opts)
  154. }
  155. type LoadbalancerCachedAclCreateOptions struct {
  156. CLOUDPROVIDER string `help:"cloud provider id"`
  157. CLOUDREGION string `help:"cloud region id"`
  158. ACL string `help:"acl id"`
  159. Listener string `help:"cloud listener id, required by huawei"`
  160. }
  161. func (opts *LoadbalancerCachedAclCreateOptions) Params() (jsonutils.JSONObject, error) {
  162. return options.StructToParams(opts)
  163. }