yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/loadbalancer_backend.go (about) 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 15 package hcs 16 17 import ( 18 "context" 19 "fmt" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/log" 23 "yunion.io/x/pkg/errors" 24 25 api "yunion.io/x/cloudmux/pkg/apis/compute" 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 "yunion.io/x/cloudmux/pkg/multicloud" 28 "yunion.io/x/cloudmux/pkg/multicloud/huawei" 29 ) 30 31 type SElbBackend struct { 32 multicloud.SResourceBase 33 huawei.HuaweiTags 34 region *SRegion 35 lb *SLoadbalancer 36 backendGroup *SElbBackendGroup 37 38 Name string `json:"name"` 39 Weight int `json:"weight"` 40 AdminStateUp bool `json:"admin_state_up"` 41 SubnetId string `json:"subnet_id"` 42 TenantId string `json:"tenant_id"` 43 ProjectId string `json:"project_id"` 44 Address string `json:"address"` 45 ProtocolPort int `json:"protocol_port"` 46 OperatingStatus string `json:"operating_status"` 47 Id string `json:"id"` 48 } 49 50 func (self *SElbBackend) GetId() string { 51 return self.Id 52 } 53 54 func (self *SElbBackend) GetName() string { 55 return self.Name 56 } 57 58 func (self *SElbBackend) GetGlobalId() string { 59 return self.GetId() 60 } 61 62 func (self *SElbBackend) GetStatus() string { 63 return api.LB_STATUS_ENABLED 64 } 65 66 func (self *SElbBackend) Refresh() error { 67 backend, err := self.region.GetElbBackend(self.backendGroup.GetId(), self.Id) 68 if err != nil { 69 return err 70 } 71 return jsonutils.Update(self, backend) 72 } 73 74 func (self *SElbBackend) IsEmulated() bool { 75 return false 76 } 77 78 func (self *SElbBackend) GetProjectId() string { 79 return "" 80 } 81 82 func (self *SElbBackend) GetWeight() int { 83 return self.Weight 84 } 85 86 func (self *SElbBackend) GetPort() int { 87 return self.ProtocolPort 88 } 89 90 func (self *SElbBackend) GetBackendType() string { 91 return api.LB_BACKEND_GUEST 92 } 93 94 func (self *SElbBackend) GetBackendRole() string { 95 return api.LB_BACKEND_ROLE_DEFAULT 96 } 97 98 func (self *SElbBackend) GetBackendId() string { 99 i, err := self.lb.region.getInstanceByIP(self.Address) 100 if err != nil { 101 log.Errorf("ElbBackend GetBackendId %s", err) 102 } 103 104 if i != nil { 105 return i.GetId() 106 } 107 108 return "" 109 } 110 111 func (self *SElbBackend) GetIpAddress() string { 112 return "" 113 } 114 115 func (self *SElbBackend) SyncConf(ctx context.Context, port, weight int) error { 116 if port > 0 { 117 log.Warningf("Elb backend SyncConf unsupport modify port") 118 } 119 120 params := map[string]interface{}{ 121 "weight": weight, 122 } 123 res := fmt.Sprintf("lbaas/pools/%s/members/%s", self.backendGroup.GetId(), self.Id) 124 return self.region.lbUpdate(res, map[string]interface{}{"member": params}) 125 } 126 127 func (self *SRegion) getInstanceByIP(ip string) (*SInstance, error) { 128 instances, err := self.GetInstances(ip) 129 if err != nil { 130 return nil, errors.Wrapf(err, "GetInstance(%s)", ip) 131 } 132 for i := range instances { 133 return &instances[i], nil 134 } 135 return nil, cloudprovider.ErrNotFound 136 } 137 138 func (self *SRegion) GetElbBackend(pool, id string) (*SElbBackend, error) { 139 res := fmt.Sprintf("lbaas/pools/%s/members/%s", pool, id) 140 ret := &SElbBackend{region: self} 141 return ret, self.lbGet(res, ret) 142 }