loadbalancers.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. type LoadbalancerCreateOptions struct {
  23. NAME string
  24. Vpc string
  25. Network string
  26. Address string
  27. AddressType string `choices:"intranet|internet"`
  28. LoadbalancerSpec string `choices:"slb.s1.small|slb.s2.small|slb.s2.medium|slb.s3.small|slb.s3.medium|slb.s3.large|network"`
  29. ChargeType string `choices:"traffic|bandwidth"`
  30. Bandwidth int
  31. Zone string
  32. Zone1 string `json:"zone_1" help:"slave zone 1"`
  33. Cluster string `json:"cluster_id"`
  34. Manager string
  35. Tags []string `help:"Tags info,prefix with 'user:', eg: user:project=default" json:"-"`
  36. Eip string `json:"eip" help:"Id or name of EIP to associate with"`
  37. EipBw int `json:"eip_bw"`
  38. EipChargeType string `json:"eip_charge_type"`
  39. EipBgpType string `json:"eip_bgp_type"`
  40. EipAutoDellocate *bool `json:"eip_auto_dellocate"`
  41. }
  42. func (opts *LoadbalancerCreateOptions) Params() (jsonutils.JSONObject, error) {
  43. params, err := options.StructToParams(opts)
  44. if err != nil {
  45. return nil, err
  46. }
  47. Tagparams := jsonutils.NewDict()
  48. for _, tag := range opts.Tags {
  49. info := strings.Split(tag, "=")
  50. if len(info) == 2 {
  51. if len(info[0]) == 0 {
  52. return nil, fmt.Errorf("invalidate tag info %s", tag)
  53. }
  54. Tagparams.Add(jsonutils.NewString(info[1]), info[0])
  55. } else if len(info) == 1 {
  56. Tagparams.Add(jsonutils.NewString(info[0]), info[0])
  57. } else {
  58. return nil, fmt.Errorf("invalidate tag info %s", tag)
  59. }
  60. }
  61. params.Add(Tagparams, "__meta__")
  62. return params, nil
  63. }
  64. type LoadbalancerIdOptions struct {
  65. ID string `json:"-"`
  66. }
  67. func (opts *LoadbalancerIdOptions) GetId() string {
  68. return opts.ID
  69. }
  70. func (opts *LoadbalancerIdOptions) Params() (jsonutils.JSONObject, error) {
  71. return nil, nil
  72. }
  73. type LoadbalancerUpdateOptions struct {
  74. LoadbalancerIdOptions
  75. Name string
  76. Delete string `help:"Lock server to prevent from deleting" choices:"enable|disable" json:"-"`
  77. Cluster string `json:"cluster_id"`
  78. BackendGroup string
  79. }
  80. func (opts LoadbalancerUpdateOptions) Params() (jsonutils.JSONObject, error) {
  81. params := jsonutils.Marshal(opts).(*jsonutils.JSONDict)
  82. if len(opts.Delete) > 0 {
  83. if opts.Delete == "disable" {
  84. params.Set("disable_delete", jsonutils.JSONTrue)
  85. } else {
  86. params.Set("disable_delete", jsonutils.JSONFalse)
  87. }
  88. }
  89. return params, nil
  90. }
  91. type LoadbalancerDeleteOptions struct {
  92. ID string `json:"-"`
  93. }
  94. type LoadbalancerPurgeOptions struct {
  95. ID string `json:"-"`
  96. }
  97. type LoadbalancerListOptions struct {
  98. options.BaseListOptions
  99. Address string
  100. AddressType string `choices:"intranet|internet"`
  101. NetworkType string `choices:"classic|vpc"`
  102. Network string
  103. BackendGroup string
  104. Cloudregion string
  105. Zone string
  106. Cluster string `json:"cluster_id"`
  107. SecgroupId string
  108. }
  109. func (opts *LoadbalancerListOptions) Params() (jsonutils.JSONObject, error) {
  110. return options.ListStructToParams(opts)
  111. }
  112. type LoadbalancerActionStatusOptions struct {
  113. LoadbalancerIdOptions
  114. Status string `choices:"enabled|disabled"`
  115. }
  116. func (opts *LoadbalancerActionStatusOptions) Params() (jsonutils.JSONObject, error) {
  117. if len(opts.Status) == 0 {
  118. return nil, fmt.Errorf("empty status")
  119. }
  120. return jsonutils.Marshal(map[string]string{"status": opts.Status}), nil
  121. }
  122. type LoadbalancerActionSyncStatusOptions struct {
  123. ID string `json:"-"`
  124. }
  125. type LoadbalancerRemoteUpdateOptions struct {
  126. LoadbalancerIdOptions
  127. computeapi.LoadbalancerRemoteUpdateInput
  128. }
  129. func (opts *LoadbalancerRemoteUpdateOptions) Params() (jsonutils.JSONObject, error) {
  130. return jsonutils.Marshal(opts), nil
  131. }
  132. type LoadbalancerAssociateEipOptions struct {
  133. LoadbalancerIdOptions
  134. computeapi.LoadbalancerAssociateEipInput
  135. }
  136. func (opts *LoadbalancerAssociateEipOptions) Params() (jsonutils.JSONObject, error) {
  137. return jsonutils.Marshal(opts), nil
  138. }
  139. type LoadbalancerCreateEipOptions struct {
  140. LoadbalancerIdOptions
  141. computeapi.LoadbalancerCreateEipInput
  142. }
  143. func (opts *LoadbalancerCreateEipOptions) Params() (jsonutils.JSONObject, error) {
  144. return jsonutils.Marshal(opts), nil
  145. }
  146. type LoadbalancerDissociateEipOptions struct {
  147. LoadbalancerIdOptions
  148. computeapi.LoadbalancerDissociateEipInput
  149. }
  150. func (opts *LoadbalancerDissociateEipOptions) Params() (jsonutils.JSONObject, error) {
  151. return jsonutils.Marshal(opts), nil
  152. }