loadbalanceragents.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "time"
  19. "yunion.io/x/jsonutils"
  20. compute_apis "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/mcclient/options"
  22. "yunion.io/x/onecloud/pkg/util/ansible"
  23. )
  24. type LoadbalancerAgentParamsOptions struct {
  25. KeepalivedConfTmpl string
  26. HaproxyConfTmpl string
  27. TelegrafConfTmpl string
  28. VrrpPriority *int // required
  29. VrrpVirtualRouterId *int // required
  30. VrrpGarpMasterRefresh *int
  31. VrrpPreempt string `choices:"true|false"`
  32. VrrpInterface string // required
  33. VrrpAdvertInt *int
  34. VrrpPass string
  35. HaproxyGlobalLog string
  36. HaproxyGlobalNbthread *int `help:"enable experimental multi-threading support available since haproxy 1.8"`
  37. HaproxyLogHttp string `choices:"true|false"`
  38. HaproxyLogTcp string `choices:"true|false"`
  39. HaproxyLogNormal string `choices:"true|false"`
  40. HaproxyTuneHttpMaxhdr *int `help:"max number of http headers allowed"`
  41. TelegrafInfluxDbOutputUrl string
  42. TelegrafInfluxDbOutputName string
  43. TelegrafInfluxDbOutputUnsafeSsl *bool
  44. TelegrafHaproxyInputInterval int
  45. }
  46. func (opts *LoadbalancerAgentParamsOptions) setPrefixedParams(params *jsonutils.JSONDict, pref string) {
  47. pref_ := pref + "_"
  48. pref_len := len(pref_)
  49. pp := jsonutils.NewDict()
  50. pMap, _ := params.GetMap()
  51. for k, v := range pMap {
  52. if strings.HasPrefix(k, pref_) && !strings.HasSuffix(k, "_conf_tmpl") {
  53. params.Remove(k)
  54. pp.Set(k[pref_len:], v)
  55. }
  56. }
  57. if pp.Length() > 0 {
  58. params.Set(pref, pp)
  59. }
  60. }
  61. func (opts *LoadbalancerAgentParamsOptions) Params() (*jsonutils.JSONDict, error) {
  62. params, err := options.StructToParams(opts)
  63. if err != nil {
  64. return nil, err
  65. }
  66. opts.setPrefixedParams(params, "vrrp")
  67. opts.setPrefixedParams(params, "haproxy")
  68. opts.setPrefixedParams(params, "telegraf")
  69. return params, nil
  70. }
  71. type LoadbalancerAgentCreateOptions struct {
  72. NAME string
  73. IP string `help:"access ip of lbagent"`
  74. INTERFACE string `help:"access interface of lbagent"`
  75. VERSION string `help:"version of lbagent"`
  76. HbTimeout *int
  77. // Cluster string `required:"true"`
  78. LoadbalancerAgentParamsOptions
  79. }
  80. func (opts *LoadbalancerAgentCreateOptions) Params() (*jsonutils.JSONDict, error) {
  81. params, err := options.StructToParams(opts)
  82. if err != nil {
  83. return nil, err
  84. }
  85. paramsSub, err := opts.LoadbalancerAgentParamsOptions.Params()
  86. if err != nil {
  87. return nil, err
  88. }
  89. params.Set("params", paramsSub)
  90. return params, nil
  91. }
  92. type LoadbalancerAgentListOptions struct {
  93. options.BaseListOptions
  94. Cluster string
  95. }
  96. type LoadbalancerAgentGetOptions struct {
  97. ID string `json:"-"`
  98. }
  99. type LoadbalancerAgentUpdateOptions struct {
  100. ID string `json:"-"`
  101. Name string
  102. HbTimeout *int
  103. Loadbalancers *time.Time
  104. LoadbalancerListeners *time.Time
  105. LoadbalancerListenerRules *time.Time
  106. LoadbalancerBackendGroups *time.Time
  107. LoadbalancerBackends *time.Time
  108. LoadbalancerAcls *time.Time
  109. LoadbalancerCertificates *time.Time
  110. }
  111. func (opts *LoadbalancerAgentUpdateOptions) Params() (jsonutils.JSONObject, error) {
  112. return options.StructToParams(opts)
  113. }
  114. type LoadbalancerAgentDeleteOptions struct {
  115. ID string `json:"-"`
  116. }
  117. type LoadbalancerAgentActionHbOptions struct {
  118. ID string `json:"-"`
  119. Version string
  120. IP string
  121. HaState string
  122. }
  123. func (opts *LoadbalancerAgentActionHbOptions) Params() (*jsonutils.JSONDict, error) {
  124. return options.StructToParams(opts)
  125. }
  126. type LoadbalancerAgentActionPatchParamsOptions struct {
  127. ID string `json:"-"`
  128. LoadbalancerAgentParamsOptions
  129. }
  130. func (opts *LoadbalancerAgentActionPatchParamsOptions) Params() (*jsonutils.JSONDict, error) {
  131. return opts.LoadbalancerAgentParamsOptions.Params()
  132. }
  133. type LoadbalancerAgentActionDeployOptions struct {
  134. ID string `json:"-"`
  135. Host string `help:"name or id of the server in format '<[server:]id|host:id>|ipaddr var=val'" json:"-"`
  136. DeployMethod string `help:"use yum repo or use file copy" choices:"yum|copy" default:"yum"`
  137. }
  138. func (opts *LoadbalancerAgentActionDeployOptions) Params() (*jsonutils.JSONDict, error) {
  139. host, err := ansible.ParseHostLine(opts.Host)
  140. if err != nil {
  141. return nil, fmt.Errorf("parse host: %v", err)
  142. }
  143. input := &compute_apis.LoadbalancerAgentDeployInput{
  144. Host: host,
  145. DeployMethod: opts.DeployMethod,
  146. }
  147. params := input.JSON(input)
  148. return params, nil
  149. }
  150. type LoadbalancerAgentActionUndeployOptions struct {
  151. ID string `json:"-"`
  152. }
  153. type LoadbalancerAgentDefaultParamsOptions struct {
  154. Cluster string
  155. }
  156. type LoadbalancerAgentJoinClusterOptions struct {
  157. ID string `json:"-"`
  158. compute_apis.LoadbalancerAgentJoinClusterInput
  159. }
  160. func (opts *LoadbalancerAgentJoinClusterOptions) Params() (*jsonutils.JSONDict, error) {
  161. return jsonutils.Marshal(opts).(*jsonutils.JSONDict), nil
  162. }
  163. type LoadbalancerAgentLeaveClusterOptions struct {
  164. ID string `json:"-"`
  165. compute_apis.LoadbalancerAgentLeaveClusterInput
  166. }
  167. func (opts *LoadbalancerAgentLeaveClusterOptions) Params() (*jsonutils.JSONDict, error) {
  168. return jsonutils.Marshal(opts).(*jsonutils.JSONDict), nil
  169. }