yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/loadbalancerbackendgroup.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 apsara
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"yunion.io/x/jsonutils"
    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  )
    27  
    28  type Rule struct {
    29  	RuleId   string
    30  	RuleName string
    31  	Domain   string
    32  	Url      string
    33  }
    34  
    35  type Rules struct {
    36  	Rule []Rule
    37  }
    38  
    39  type Listener struct {
    40  	Protocol string
    41  	Port     int
    42  }
    43  
    44  type Listeners struct {
    45  	Listener []Listener
    46  }
    47  
    48  type AssociatedObjects struct {
    49  	Rules     Rules
    50  	Listeners Listeners
    51  }
    52  
    53  type SLoadbalancerBackendGroup struct {
    54  	multicloud.SResourceBase
    55  	ApsaraTags
    56  	lb *SLoadbalancer
    57  
    58  	VServerGroupId    string
    59  	VServerGroupName  string
    60  	AssociatedObjects AssociatedObjects
    61  	DepartmentInfo
    62  }
    63  
    64  func (backendgroup *SLoadbalancerBackendGroup) GetILoadbalancer() cloudprovider.ICloudLoadbalancer {
    65  	return backendgroup.lb
    66  }
    67  
    68  func (backendgroup *SLoadbalancerBackendGroup) GetLoadbalancerId() string {
    69  	return backendgroup.lb.GetId()
    70  }
    71  
    72  func (backendgroup *SLoadbalancerBackendGroup) GetProtocolType() string {
    73  	return ""
    74  }
    75  
    76  func (backendgroup *SLoadbalancerBackendGroup) GetScheduler() string {
    77  	return ""
    78  }
    79  
    80  func (backendgroup *SLoadbalancerBackendGroup) GetHealthCheck() (*cloudprovider.SLoadbalancerHealthCheck, error) {
    81  	return nil, nil
    82  }
    83  
    84  func (backendgroup *SLoadbalancerBackendGroup) GetStickySession() (*cloudprovider.SLoadbalancerStickySession, error) {
    85  	return nil, nil
    86  }
    87  
    88  func (backendgroup *SLoadbalancerBackendGroup) GetName() string {
    89  	return backendgroup.VServerGroupName
    90  }
    91  
    92  func (backendgroup *SLoadbalancerBackendGroup) GetId() string {
    93  	return backendgroup.VServerGroupId
    94  }
    95  
    96  func (backendgroup *SLoadbalancerBackendGroup) GetGlobalId() string {
    97  	return backendgroup.VServerGroupId
    98  }
    99  
   100  func (backendgroup *SLoadbalancerBackendGroup) GetStatus() string {
   101  	return api.LB_STATUS_ENABLED
   102  }
   103  
   104  func (backendgroup *SLoadbalancerBackendGroup) IsDefault() bool {
   105  	return false
   106  }
   107  
   108  func (backendgroup *SLoadbalancerBackendGroup) GetType() string {
   109  	return api.LB_BACKENDGROUP_TYPE_NORMAL
   110  }
   111  
   112  func (backendgroup *SLoadbalancerBackendGroup) IsEmulated() bool {
   113  	return false
   114  }
   115  
   116  func (backendgroup *SLoadbalancerBackendGroup) Refresh() error {
   117  	loadbalancerBackendgroups, err := backendgroup.lb.region.GetLoadbalancerBackendgroups(backendgroup.lb.LoadBalancerId)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	for _, loadbalancerBackendgroup := range loadbalancerBackendgroups {
   122  		if loadbalancerBackendgroup.VServerGroupId == backendgroup.VServerGroupId {
   123  			return jsonutils.Update(backendgroup, loadbalancerBackendgroup)
   124  		}
   125  	}
   126  	return cloudprovider.ErrNotFound
   127  }
   128  
   129  func (region *SRegion) GetLoadbalancerBackendgroups(loadbalancerId string) ([]SLoadbalancerBackendGroup, error) {
   130  	params := map[string]string{}
   131  	params["RegionId"] = region.RegionId
   132  	params["LoadBalancerId"] = loadbalancerId
   133  	body, err := region.lbRequest("DescribeVServerGroups", params)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	backendgroups := []SLoadbalancerBackendGroup{}
   138  	return backendgroups, body.Unmarshal(&backendgroups, "VServerGroups", "VServerGroup")
   139  }
   140  
   141  func (backendgroup *SLoadbalancerBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) {
   142  	backends, err := backendgroup.lb.region.GetLoadbalancerBackends(backendgroup.VServerGroupId)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	ibackends := []cloudprovider.ICloudLoadbalancerBackend{}
   147  	for i := 0; i < len(backends); i++ {
   148  		backends[i].lbbg = backendgroup
   149  		ibackends = append(ibackends, &backends[i])
   150  	}
   151  	return ibackends, nil
   152  }
   153  
   154  func (backendgroup *SLoadbalancerBackendGroup) GetILoadbalancerBackendById(backendId string) (cloudprovider.ICloudLoadbalancerBackend, error) {
   155  	backends, err := backendgroup.GetILoadbalancerBackends()
   156  	if err != nil {
   157  		return nil, err
   158  	}
   159  	for i := 0; i < len(backends); i++ {
   160  		if backends[i].GetGlobalId() == backendId {
   161  			return backends[i], nil
   162  		}
   163  	}
   164  	return nil, cloudprovider.ErrNotFound
   165  }
   166  
   167  func (region *SRegion) CreateLoadbalancerBackendGroup(name, loadbalancerId string, backends []cloudprovider.SLoadbalancerBackend) (*SLoadbalancerBackendGroup, error) {
   168  	params := map[string]string{}
   169  	params["RegionId"] = region.RegionId
   170  	params["VServerGroupName"] = name
   171  	params["LoadBalancerId"] = loadbalancerId
   172  	if len(backends) > 0 {
   173  		servers := jsonutils.NewArray()
   174  		for _, backend := range backends {
   175  			servers.Add(
   176  				jsonutils.Marshal(
   177  					map[string]string{
   178  						"ServerId": backend.ExternalID,
   179  						"Port":     fmt.Sprintf("%d", backend.Port),
   180  						"Weight":   fmt.Sprintf("%d", backend.Weight),
   181  					},
   182  				))
   183  		}
   184  		params["BackendServers"] = servers.String()
   185  	}
   186  	body, err := region.lbRequest("CreateVServerGroup", params)
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	groupId, err := body.GetString("VServerGroupId")
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  	return region.GetLoadbalancerBackendgroupById(groupId)
   195  }
   196  
   197  func (region *SRegion) GetLoadbalancerBackendgroupById(groupId string) (*SLoadbalancerBackendGroup, error) {
   198  	params := map[string]string{}
   199  	params["RegionId"] = region.RegionId
   200  	params["VServerGroupId"] = groupId
   201  	body, err := region.lbRequest("DescribeVServerGroupAttribute", params)
   202  	if err != nil {
   203  		return nil, err
   204  	}
   205  	group := &SLoadbalancerBackendGroup{}
   206  	return group, body.Unmarshal(group)
   207  }
   208  
   209  func (region *SRegion) UpdateLoadBalancerBackendGroupName(name, groupId string) error {
   210  	params := map[string]string{}
   211  	params["RegionId"] = region.RegionId
   212  	params["VServerGroupId"] = groupId
   213  	params["VServerGroupName"] = name
   214  	_, err := region.lbRequest("SetVServerGroupAttribute", params)
   215  	return err
   216  }
   217  
   218  func (backendgroup *SLoadbalancerBackendGroup) Sync(ctx context.Context, group *cloudprovider.SLoadbalancerBackendGroup) error {
   219  	if group == nil {
   220  		return nil
   221  	}
   222  
   223  	if backendgroup.VServerGroupName != group.Name {
   224  		return backendgroup.lb.region.UpdateLoadBalancerBackendGroupName(backendgroup.VServerGroupId, group.Name)
   225  	}
   226  	return nil
   227  }
   228  
   229  func (region *SRegion) DeleteLoadBalancerBackendGroup(groupId string) error {
   230  	params := map[string]string{}
   231  	params["RegionId"] = region.RegionId
   232  	params["VServerGroupId"] = groupId
   233  	_, err := region.lbRequest("DeleteVServerGroup", params)
   234  	return err
   235  }
   236  
   237  func (backendgroup *SLoadbalancerBackendGroup) Delete(ctx context.Context) error {
   238  	return backendgroup.lb.region.DeleteLoadBalancerBackendGroup(backendgroup.VServerGroupId)
   239  }
   240  
   241  func (region *SRegion) AddBackendVServer(loadbalancerId, backendGroupId, serverId string, weight, port int) error {
   242  	params := map[string]string{}
   243  	params["RegionId"] = region.RegionId
   244  	params["LoadBalancerId"] = loadbalancerId
   245  	params["VServerGroupId"] = backendGroupId
   246  	servers := jsonutils.NewArray()
   247  	servers.Add(jsonutils.Marshal(map[string]string{"ServerId": serverId, "Weight": fmt.Sprintf("%d", weight), "Port": fmt.Sprintf("%d", port)}))
   248  	params["BackendServers"] = servers.String()
   249  	_, err := region.lbRequest("AddVServerGroupBackendServers", params)
   250  	return err
   251  }
   252  
   253  func (region *SRegion) RemoveBackendVServer(loadbalancerId, backendgroupId, serverId string, port int) error {
   254  	params := map[string]string{}
   255  	params["RegionId"] = region.RegionId
   256  	params["LoadBalancerId"] = loadbalancerId
   257  	params["VServerGroupId"] = backendgroupId
   258  	servers := jsonutils.NewArray()
   259  	servers.Add(jsonutils.Marshal(map[string]string{"ServerId": serverId, "Port": fmt.Sprintf("%d", port)}))
   260  	params["BackendServers"] = servers.String()
   261  	_, err := region.lbRequest("RemoveVServerGroupBackendServers", params)
   262  	return err
   263  }
   264  
   265  func (backendgroup *SLoadbalancerBackendGroup) AddBackendServer(serverId string, weight, port int) (cloudprovider.ICloudLoadbalancerBackend, error) {
   266  	if err := backendgroup.lb.region.AddBackendVServer(backendgroup.lb.LoadBalancerId, backendgroup.VServerGroupId, serverId, weight, port); err != nil {
   267  		return nil, err
   268  	}
   269  	return &SLoadbalancerBackend{lbbg: backendgroup, ServerId: serverId, Weight: weight, Port: port}, nil
   270  }
   271  
   272  func (backendgroup *SLoadbalancerBackendGroup) RemoveBackendServer(serverId string, weight, port int) error {
   273  	return backendgroup.lb.region.RemoveBackendVServer(backendgroup.lb.LoadBalancerId, backendgroup.VServerGroupId, serverId, port)
   274  }