loadbalancerbackendgroups.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "strconv"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/utils"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. )
  24. type LoadbalancerBackendGroupCreateOptions struct {
  25. NAME string
  26. Loadbalancer string
  27. Type string `choices:"default|normal|master_slave"`
  28. ProtocolType string `help:"Huawei backendgroup protocol type" choices:"tcp|udp|http"`
  29. Scheduler string `help:"Huawei backendgroup scheduler algorithm" choices:"rr|sch|wlc"`
  30. Backend []string `help:"backends with separated by ',' e.g. weight:80,port:443,id:01e9d393-d2b8-4d2e-85fb-023b83889070,backend_type:guest" json:"-"`
  31. }
  32. type Backends []*SBackend
  33. type SBackend struct {
  34. Index int
  35. Weight int
  36. Port int
  37. ID string
  38. BackendType string
  39. }
  40. func NewBackend(s string, index int) (*SBackend, error) {
  41. backend := &SBackend{Index: index}
  42. for _, part := range strings.Split(s, ",") {
  43. value := strings.Split(part, ":")
  44. if len(value) != 2 {
  45. return nil, fmt.Errorf("invalid input params %s eg: weight:80,port:443,id:01e9d393-d2b8-4d2e-85fb-023b83889070,backend_type:guest", part)
  46. }
  47. switch value[0] {
  48. case "weight":
  49. weight, err := strconv.Atoi(value[1])
  50. if err != nil {
  51. return nil, fmt.Errorf("invalid weight %s error: %v", value[1], err)
  52. }
  53. if weight < 0 || weight > 256 {
  54. return nil, fmt.Errorf("invalid weight range, only support 0 ~ 256")
  55. }
  56. backend.Weight = weight
  57. case "port":
  58. port, err := strconv.Atoi(value[1])
  59. if err != nil {
  60. return nil, fmt.Errorf("invalid port %s error: %v", value[1], err)
  61. }
  62. if port < 1 || port > 65535 {
  63. return nil, fmt.Errorf("invalid port range, only support 1 ~ 65535")
  64. }
  65. backend.Port = port
  66. case "backend_type":
  67. if utils.IsInStringArray(value[1], []string{api.LB_BACKEND_GUEST, api.LB_BACKEND_HOST}) {
  68. return nil, fmt.Errorf("invalid backend type %s only support %s %s", value[1], api.LB_BACKEND_GUEST, api.LB_BACKEND_HOST)
  69. }
  70. backend.BackendType = value[1]
  71. case "id":
  72. backend.ID = value[1]
  73. default:
  74. return nil, fmt.Errorf("invalid input type %s", value[0])
  75. }
  76. }
  77. return backend, nil
  78. }
  79. func NewBackends(ss []string) (Backends, error) {
  80. backends := Backends{}
  81. for index, s := range ss {
  82. backend, err := NewBackend(s, index)
  83. if err != nil {
  84. return nil, err
  85. }
  86. backends = append(backends, backend)
  87. }
  88. return backends, nil
  89. }
  90. func (opts *LoadbalancerBackendGroupCreateOptions) Params() (*jsonutils.JSONDict, error) {
  91. params, err := options.StructToParams(opts)
  92. if err != nil {
  93. return nil, err
  94. }
  95. backends, err := NewBackends(opts.Backend)
  96. if err != nil {
  97. return nil, err
  98. }
  99. backendJSON := jsonutils.Marshal(backends)
  100. params.Set("backends", backendJSON)
  101. return params, nil
  102. }
  103. type LoadbalancerBackendGroupGetOptions struct {
  104. ID string `json:"-"`
  105. }
  106. type LoadbalancerBackendGroupUpdateOptions struct {
  107. ID string `json:"-"`
  108. Name string
  109. }
  110. func (opts *LoadbalancerBackendGroupUpdateOptions) Params() (jsonutils.JSONObject, error) {
  111. return options.StructToParams(opts)
  112. }
  113. type LoadbalancerBackendGroupDeleteOptions struct {
  114. ID string `json:"-"`
  115. }
  116. type LoadbalancerBackendGroupIDOptions struct {
  117. ID string `json:"-"`
  118. }
  119. type LoadbalancerBackendGroupListOptions struct {
  120. options.BaseListOptions
  121. Loadbalancer string
  122. Cloudregion string
  123. NoRef bool
  124. }
  125. func (opts *LoadbalancerBackendGroupListOptions) Params() (jsonutils.JSONObject, error) {
  126. return options.ListStructToParams(opts)
  127. }