yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"context"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/log"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    27  )
    28  
    29  type SElbBackend struct {
    30  	multicloud.SResourceBase
    31  	huawei.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  	m := self.lb.region.ecsClient.ElbBackend
    66  	err := m.SetBackendGroupId(self.backendGroup.GetId())
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	backend := SElbBackend{}
    72  	err = DoGet(m.Get, self.GetId(), nil, &backend)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	backend.lb = self.lb
    78  	backend.backendGroup = self.backendGroup
    79  	err = jsonutils.Update(self, backend)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func (self *SElbBackend) IsEmulated() bool {
    88  	return false
    89  }
    90  
    91  func (self *SElbBackend) GetProjectId() string {
    92  	return ""
    93  }
    94  
    95  func (self *SElbBackend) GetWeight() int {
    96  	return self.Weight
    97  }
    98  
    99  func (self *SElbBackend) GetPort() int {
   100  	return self.ProtocolPort
   101  }
   102  
   103  func (self *SElbBackend) GetBackendType() string {
   104  	return api.LB_BACKEND_GUEST
   105  }
   106  
   107  func (self *SElbBackend) GetBackendRole() string {
   108  	return api.LB_BACKEND_ROLE_DEFAULT
   109  }
   110  
   111  func (self *SElbBackend) GetBackendId() string {
   112  	i, err := self.lb.region.getInstanceByIP(self.Address)
   113  	if err != nil {
   114  		log.Errorf("ElbBackend GetBackendId %s", err)
   115  	}
   116  
   117  	if i != nil {
   118  		return i.GetId()
   119  	}
   120  
   121  	return ""
   122  }
   123  
   124  func (self *SElbBackend) GetIpAddress() string {
   125  	return ""
   126  }
   127  
   128  func (self *SElbBackend) SyncConf(ctx context.Context, port, weight int) error {
   129  	if port > 0 {
   130  		log.Warningf("Elb backend SyncConf unsupport modify port")
   131  	}
   132  
   133  	params := jsonutils.NewDict()
   134  	memberObj := jsonutils.NewDict()
   135  	memberObj.Set("weight", jsonutils.NewInt(int64(weight)))
   136  	params.Set("member", memberObj)
   137  	err := self.lb.region.ecsClient.ElbBackend.SetBackendGroupId(self.backendGroup.GetId())
   138  	if err != nil {
   139  		return err
   140  	}
   141  	return DoUpdate(self.lb.region.ecsClient.ElbBackend.Update, self.GetId(), params, nil)
   142  }
   143  
   144  func (self *SRegion) getInstanceByIP(privateIP string) (*SInstance, error) {
   145  	queries := make(map[string]string)
   146  
   147  	if len(self.client.projectId) > 0 {
   148  		queries["project_id"] = self.client.projectId
   149  	}
   150  
   151  	if len(privateIP) > 0 {
   152  		queries["ip"] = privateIP
   153  	}
   154  
   155  	instances := make([]SInstance, 0)
   156  	err := doListAllWithOffset(self.ecsClient.Servers.List, queries, &instances)
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	if len(instances) == 1 {
   162  		return &instances[0], nil
   163  	} else if len(instances) > 1 {
   164  		log.Warningf("SRegion.getInstanceByIP %s result: multiple server find", privateIP)
   165  		return &instances[0], nil
   166  	}
   167  
   168  	return nil, cloudprovider.ErrNotFound
   169  }