loadbalancerbackend.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 apsara
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SLoadbalancerBackend struct {
  24. multicloud.SResourceBase
  25. ApsaraTags
  26. lbbg *SLoadbalancerBackendGroup
  27. ServerId string
  28. Port int
  29. Weight int
  30. DepartmentInfo
  31. }
  32. func (backend *SLoadbalancerBackend) GetName() string {
  33. return backend.ServerId
  34. }
  35. func (backend *SLoadbalancerBackend) GetId() string {
  36. return fmt.Sprintf("%s/%s", backend.lbbg.VServerGroupId, backend.ServerId)
  37. }
  38. func (backend *SLoadbalancerBackend) GetGlobalId() string {
  39. return backend.GetId()
  40. }
  41. func (backend *SLoadbalancerBackend) GetStatus() string {
  42. return api.LB_STATUS_ENABLED
  43. }
  44. func (backend *SLoadbalancerBackend) IsEmulated() bool {
  45. return false
  46. }
  47. func (backend *SLoadbalancerBackend) Refresh() error {
  48. loadbalancerBackends, err := backend.lbbg.lb.region.GetLoadbalancerBackends(backend.lbbg.VServerGroupId)
  49. if err != nil {
  50. return err
  51. }
  52. for _, loadbalancerBackend := range loadbalancerBackends {
  53. if loadbalancerBackend.ServerId == backend.ServerId {
  54. return jsonutils.Update(backend, loadbalancerBackend)
  55. }
  56. }
  57. return cloudprovider.ErrNotFound
  58. }
  59. func (backend *SLoadbalancerBackend) GetWeight() int {
  60. return backend.Weight
  61. }
  62. func (backend *SLoadbalancerBackend) GetPort() int {
  63. return backend.Port
  64. }
  65. func (backend *SLoadbalancerBackend) GetBackendType() string {
  66. return api.LB_BACKEND_GUEST
  67. }
  68. func (backend *SLoadbalancerBackend) GetBackendRole() string {
  69. return api.LB_BACKEND_ROLE_DEFAULT
  70. }
  71. func (backend *SLoadbalancerBackend) GetBackendId() string {
  72. return backend.ServerId
  73. }
  74. func (backend *SLoadbalancerBackend) GetIpAddress() string {
  75. return ""
  76. }
  77. func (region *SRegion) GetLoadbalancerBackends(backendgroupId string) ([]SLoadbalancerBackend, error) {
  78. params := map[string]string{}
  79. params["RegionId"] = region.RegionId
  80. params["VServerGroupId"] = backendgroupId
  81. body, err := region.lbRequest("DescribeVServerGroupAttribute", params)
  82. if err != nil {
  83. return nil, err
  84. }
  85. backends := []SLoadbalancerBackend{}
  86. return backends, body.Unmarshal(&backends, "BackendServers", "BackendServer")
  87. }
  88. func (backend *SLoadbalancerBackend) Update(ctx context.Context, opts *cloudprovider.SLoadbalancerBackend) error {
  89. err := backend.lbbg.lb.region.RemoveBackendVServer(backend.lbbg.lb.LoadBalancerId, backend.lbbg.VServerGroupId, backend.ServerId, backend.Port)
  90. if err != nil {
  91. return err
  92. }
  93. return backend.lbbg.lb.region.AddBackendVServer(backend.lbbg.lb.LoadBalancerId, backend.lbbg.VServerGroupId, backend.ServerId, opts.Weight, opts.Port)
  94. }