loadbalancerbackend.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 aws
  15. import (
  16. "context"
  17. "fmt"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. "yunion.io/x/pkg/util/netutils"
  22. )
  23. type SElbBackends struct {
  24. TargetHealthDescriptions []SElbBackend `xml:"TargetHealthDescriptions>member"`
  25. }
  26. type SElbBackend struct {
  27. multicloud.SResourceBase
  28. AwsTags
  29. group *SElbBackendGroup
  30. Target Target `json:"Target"`
  31. TargetHealth TargetHealth `json:"TargetHealth"`
  32. }
  33. type Target struct {
  34. Id string `json:"Id"`
  35. Port int `json:"Port"`
  36. }
  37. type TargetHealth struct {
  38. State string `json:"State"`
  39. Reason string `json:"Reason"`
  40. Description string `json:"Description"`
  41. }
  42. func (self *SElbBackend) GetId() string {
  43. return fmt.Sprintf("%s::%s::%d", self.group.GetId(), self.Target.Id, self.Target.Port)
  44. }
  45. func (self *SElbBackend) GetName() string {
  46. return self.GetId()
  47. }
  48. func (self *SElbBackend) GetGlobalId() string {
  49. return self.GetId()
  50. }
  51. func (self *SElbBackend) GetStatus() string {
  52. return api.LB_STATUS_ENABLED
  53. }
  54. func (self *SElbBackend) Refresh() error {
  55. return nil
  56. }
  57. func (self *SElbBackend) IsEmulated() bool {
  58. return false
  59. }
  60. func (self *SElbBackend) GetProjectId() string {
  61. return ""
  62. }
  63. func (self *SElbBackend) GetWeight() int {
  64. return 0
  65. }
  66. func (self *SElbBackend) GetPort() int {
  67. return self.Target.Port
  68. }
  69. func (self *SElbBackend) GetBackendType() string {
  70. return api.LB_BACKEND_GUEST
  71. }
  72. func (self *SElbBackend) GetBackendRole() string {
  73. return api.LB_BACKEND_ROLE_DEFAULT
  74. }
  75. func (self *SElbBackend) GetBackendId() string {
  76. return self.Target.Id
  77. }
  78. func (self *SElbBackend) Update(ctx context.Context, opts *cloudprovider.SLoadbalancerBackend) error {
  79. return self.group.lb.region.SyncElbBackend(self.GetId(), self.GetBackendId(), self.Target.Port, opts.Port)
  80. }
  81. func (self *SElbBackend) GetIpAddress() string {
  82. _, err := netutils.NewIPV4Addr(self.Target.Id)
  83. if err != nil {
  84. return ""
  85. }
  86. return self.Target.Id
  87. }
  88. func (self *SRegion) SyncElbBackend(backendId, serverId string, oldPort, newPort int) error {
  89. err := self.RemoveElbBackend(backendId, serverId, 0, oldPort)
  90. if err != nil {
  91. return err
  92. }
  93. _, err = self.AddElbBackend(backendId, serverId, 0, newPort)
  94. if err != nil {
  95. return err
  96. }
  97. return nil
  98. }
  99. func (self *SElbBackend) GetDescription() string {
  100. return self.AwsTags.GetDescription()
  101. }