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