loadbalancerbackendgroup.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 azure
  15. import (
  16. "context"
  17. "strings"
  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/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. )
  24. type SLoadbalancerBackendGroup struct {
  25. multicloud.SLoadbalancerBackendGroupBase
  26. AzureTags
  27. lb *SLoadbalancer
  28. Id string
  29. Properties *SLoadbalancerBackend
  30. BackendIPConfigurations []struct {
  31. Id string
  32. }
  33. }
  34. func (self *SLoadbalancerBackendGroup) GetId() string {
  35. return self.Id
  36. }
  37. func (self *SLoadbalancerBackendGroup) GetName() string {
  38. info := strings.Split(self.Id, "/")
  39. if len(info) > 0 {
  40. return info[len(info)-1]
  41. }
  42. return ""
  43. }
  44. func (self *SLoadbalancerBackendGroup) GetGlobalId() string {
  45. return strings.ToLower(self.GetId())
  46. }
  47. func (self *SLoadbalancerBackendGroup) GetStatus() string {
  48. return api.LB_STATUS_ENABLED
  49. }
  50. func (self *SLoadbalancerBackendGroup) Refresh() error {
  51. lbbg, err := self.lb.GetILoadBalancerBackendGroupById(self.GetId())
  52. if err != nil {
  53. return errors.Wrap(err, "GetILoadBalancerBackendGroupById")
  54. }
  55. return jsonutils.Update(self, lbbg)
  56. }
  57. func (self *SLoadbalancerBackendGroup) GetProjectId() string {
  58. return getResourceGroup(self.GetId())
  59. }
  60. func (self *SLoadbalancerBackendGroup) IsDefault() bool {
  61. return false
  62. }
  63. func (self *SLoadbalancerBackendGroup) GetType() string {
  64. return api.LB_BACKENDGROUP_TYPE_NORMAL
  65. }
  66. func (self *SLoadbalancerBackendGroup) GetLoadbalancerId() string {
  67. return self.lb.GetId()
  68. }
  69. func (self *SLoadbalancerBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) {
  70. ret := []cloudprovider.ICloudLoadbalancerBackend{}
  71. if len(self.BackendIPConfigurations) > 0 {
  72. for _, ipConf := range self.BackendIPConfigurations {
  73. apiVerion := "2024-03-01"
  74. if strings.Contains(strings.ToLower(ipConf.Id), "microsoft.network/networkinterfaces") {
  75. apiVerion = "2023-11-01"
  76. }
  77. resp, err := self.lb.region.show(ipConf.Id, apiVerion)
  78. if err != nil {
  79. return nil, err
  80. }
  81. backend := &SLoadbalancerBackend{
  82. lbbg: self,
  83. }
  84. err = resp.Unmarshal(backend, "properties")
  85. if err != nil {
  86. return nil, errors.Wrapf(err, "Unmarshal")
  87. }
  88. ret = append(ret, backend)
  89. }
  90. return ret, nil
  91. }
  92. resp, err := self.lb.region.show(self.Id, "2021-02-01")
  93. if err != nil {
  94. return nil, err
  95. }
  96. err = resp.Unmarshal(self)
  97. if err != nil {
  98. return nil, err
  99. }
  100. if self.Properties != nil && (len(self.Properties.PrivateIPAddress) > 0 || len(self.Properties.LoadBalancerBackendAddresses) > 0) {
  101. self.Properties.lbbg = self
  102. ret = append(ret, self.Properties)
  103. }
  104. return ret, nil
  105. }
  106. func (self *SLoadbalancerBackendGroup) GetILoadbalancerBackendById(backendId string) (cloudprovider.ICloudLoadbalancerBackend, error) {
  107. lbbs, err := self.GetILoadbalancerBackends()
  108. if err != nil {
  109. return nil, errors.Wrap(err, "GetILoadbalancerBackends")
  110. }
  111. for i := range lbbs {
  112. if lbbs[i].GetId() == backendId {
  113. return lbbs[i], nil
  114. }
  115. }
  116. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", backendId)
  117. }
  118. func (self *SLoadbalancerBackendGroup) GetProtocolType() string {
  119. return ""
  120. }
  121. func (self *SLoadbalancerBackendGroup) GetScheduler() string {
  122. return ""
  123. }
  124. func (self *SLoadbalancerBackendGroup) GetHealthCheck() (*cloudprovider.SLoadbalancerHealthCheck, error) {
  125. return nil, nil
  126. }
  127. func (self *SLoadbalancerBackendGroup) GetStickySession() (*cloudprovider.SLoadbalancerStickySession, error) {
  128. return nil, nil
  129. }
  130. func (self *SLoadbalancerBackendGroup) AddBackendServer(opts *cloudprovider.SLoadbalancerBackend) (cloudprovider.ICloudLoadbalancerBackend, error) {
  131. return nil, errors.Wrap(cloudprovider.ErrNotImplemented, "AddBackendServer")
  132. }
  133. func (self *SLoadbalancerBackendGroup) RemoveBackendServer(opts *cloudprovider.SLoadbalancerBackend) error {
  134. return errors.Wrap(cloudprovider.ErrNotImplemented, "RemoveBackendServer")
  135. }
  136. func (self *SLoadbalancerBackendGroup) Delete(ctx context.Context) error {
  137. return errors.Wrap(cloudprovider.ErrNotImplemented, "Delete")
  138. }
  139. func (self *SLoadbalancerBackendGroup) Update(ctx context.Context, opts *cloudprovider.SLoadbalancerBackendGroup) error {
  140. return errors.Wrap(cloudprovider.ErrNotImplemented, "Update")
  141. }