yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/loadbalancermasterslavebackendgroup.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 SLoadbalancerMasterSlaveBackendGroup struct {
    29  	multicloud.SResourceBase
    30  	ApsaraTags
    31  	lb *SLoadbalancer
    32  
    33  	MasterSlaveServerGroupId   string
    34  	MasterSlaveServerGroupName string
    35  	DepartmentInfo
    36  }
    37  
    38  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetLoadbalancerId() string {
    39  	return backendgroup.lb.GetId()
    40  }
    41  
    42  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetProtocolType() string {
    43  	return ""
    44  }
    45  
    46  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetScheduler() string {
    47  	return ""
    48  }
    49  
    50  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetHealthCheck() (*cloudprovider.SLoadbalancerHealthCheck, error) {
    51  	return nil, nil
    52  }
    53  
    54  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetStickySession() (*cloudprovider.SLoadbalancerStickySession, error) {
    55  	return nil, nil
    56  }
    57  
    58  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetName() string {
    59  	return backendgroup.MasterSlaveServerGroupName
    60  }
    61  
    62  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetId() string {
    63  	return backendgroup.MasterSlaveServerGroupId
    64  }
    65  
    66  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetGlobalId() string {
    67  	return backendgroup.GetId()
    68  }
    69  
    70  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetStatus() string {
    71  	return api.LB_STATUS_ENABLED
    72  }
    73  
    74  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) IsEmulated() bool {
    75  	return false
    76  }
    77  
    78  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) Refresh() error {
    79  	return nil
    80  }
    81  
    82  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) IsDefault() bool {
    83  	return false
    84  }
    85  
    86  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetType() string {
    87  	return api.LB_BACKENDGROUP_TYPE_MASTER_SLAVE
    88  }
    89  
    90  func (region *SRegion) GetLoadbalancerMasterSlaveBackendgroups(loadbalancerId string) ([]SLoadbalancerMasterSlaveBackendGroup, error) {
    91  	params := map[string]string{}
    92  	params["RegionId"] = region.RegionId
    93  	params["LoadBalancerId"] = loadbalancerId
    94  	body, err := region.lbRequest("DescribeMasterSlaveServerGroups", params)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	backendgroups := []SLoadbalancerMasterSlaveBackendGroup{}
    99  	return backendgroups, body.Unmarshal(&backendgroups, "MasterSlaveServerGroups", "MasterSlaveServerGroup")
   100  }
   101  
   102  func (region *SRegion) GetLoadbalancerMasterSlaveBackends(backendgroupId string) ([]SLoadbalancerMasterSlaveBackend, error) {
   103  	params := map[string]string{}
   104  	params["RegionId"] = region.RegionId
   105  	params["MasterSlaveServerGroupId"] = backendgroupId
   106  	body, err := region.lbRequest("DescribeMasterSlaveServerGroupAttribute", params)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	backends := []SLoadbalancerMasterSlaveBackend{}
   111  	return backends, body.Unmarshal(&backends, "MasterSlaveBackendServers", "MasterSlaveBackendServer")
   112  }
   113  
   114  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) {
   115  	backends, err := backendgroup.lb.region.GetLoadbalancerMasterSlaveBackends(backendgroup.MasterSlaveServerGroupId)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	ibackends := []cloudprovider.ICloudLoadbalancerBackend{}
   120  	for i := 0; i < len(backends); i++ {
   121  		backends[i].lbbg = backendgroup
   122  		ibackends = append(ibackends, &backends[i])
   123  	}
   124  	return ibackends, nil
   125  }
   126  
   127  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetILoadbalancerBackendById(backendId string) (cloudprovider.ICloudLoadbalancerBackend, error) {
   128  	backends, err := backendgroup.GetILoadbalancerBackends()
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	for i := 0; i < len(backends); i++ {
   133  		if backends[i].GetGlobalId() == backendId {
   134  			return backends[i], nil
   135  		}
   136  	}
   137  	return nil, cloudprovider.ErrNotFound
   138  }
   139  
   140  func (region *SRegion) CreateLoadbalancerMasterSlaveBackendGroup(name, loadbalancerId string, backends []cloudprovider.SLoadbalancerBackend) (*SLoadbalancerMasterSlaveBackendGroup, error) {
   141  	params := map[string]string{}
   142  	params["RegionId"] = region.RegionId
   143  	params["MasterSlaveServerGroupName"] = name
   144  	params["LoadBalancerId"] = loadbalancerId
   145  	if len(backends) != 2 {
   146  		return nil, fmt.Errorf("master slave backendgorup must contain two backend")
   147  	}
   148  	servers := jsonutils.NewArray()
   149  	for _, backend := range backends {
   150  		serverType := "Slave"
   151  		if backend.Index == 0 {
   152  			serverType = "Master"
   153  		}
   154  		servers.Add(
   155  			jsonutils.Marshal(
   156  				map[string]string{
   157  					"ServerId":   backend.ExternalID,
   158  					"Port":       fmt.Sprintf("%d", backend.Port),
   159  					"Weight":     fmt.Sprintf("%d", backend.Weight),
   160  					"ServerType": serverType,
   161  				},
   162  			))
   163  	}
   164  	params["MasterSlaveBackendServers"] = servers.String()
   165  	body, err := region.lbRequest("CreateMasterSlaveServerGroup", params)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	groupId, err := body.GetString("MasterSlaveServerGroupId")
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	return region.GetLoadbalancerMasterSlaveBackendgroupById(groupId)
   174  }
   175  
   176  func (region *SRegion) GetLoadbalancerMasterSlaveBackendgroupById(groupId string) (*SLoadbalancerMasterSlaveBackendGroup, error) {
   177  	params := map[string]string{}
   178  	params["RegionId"] = region.RegionId
   179  	params["MasterSlaveServerGroupId"] = groupId
   180  	params["NeedInstanceDetail"] = "true"
   181  	body, err := region.lbRequest("DescribeMasterSlaveServerGroupAttribute", params)
   182  	if err != nil {
   183  		return nil, err
   184  	}
   185  	group := &SLoadbalancerMasterSlaveBackendGroup{}
   186  	return group, body.Unmarshal(group)
   187  }
   188  
   189  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) Sync(ctx context.Context, group *cloudprovider.SLoadbalancerBackendGroup) error {
   190  	return nil
   191  }
   192  
   193  func (region *SRegion) DeleteLoadbalancerMasterSlaveBackendgroup(groupId string) error {
   194  	params := map[string]string{}
   195  	params["RegionId"] = region.RegionId
   196  	params["MasterSlaveServerGroupId"] = groupId
   197  	_, err := region.lbRequest("DeleteMasterSlaveServerGroup", params)
   198  	return err
   199  }
   200  
   201  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) Delete(ctx context.Context) error {
   202  	return backendgroup.lb.region.DeleteLoadbalancerMasterSlaveBackendgroup(backendgroup.MasterSlaveServerGroupId)
   203  }
   204  
   205  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) AddBackendServer(serverId string, weight, port int) (cloudprovider.ICloudLoadbalancerBackend, error) {
   206  	return nil, cloudprovider.ErrNotSupported
   207  }
   208  
   209  func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) RemoveBackendServer(serverId string, weight, port int) error {
   210  	return cloudprovider.ErrNotSupported
   211  }