yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/loadbalancerlistenerrule.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 aliyun
    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 SLoadbalancerListenerRule struct {
    29  	multicloud.SResourceBase
    30  	multicloud.SLoadbalancerRedirectBase
    31  	AliyunTags
    32  	httpListener  *SLoadbalancerHTTPListener
    33  	httpsListener *SLoadbalancerHTTPSListener
    34  
    35  	Domain         string `json:"Domain"`
    36  	ListenerSync   string
    37  	RuleId         string
    38  	RuleName       string `json:"RuleName"`
    39  	Url            string `json:"Url"`
    40  	VServerGroupId string `json:"VServerGroupId"`
    41  }
    42  
    43  func (lbr *SLoadbalancerListenerRule) GetName() string {
    44  	return lbr.RuleName
    45  }
    46  
    47  func (lbr *SLoadbalancerListenerRule) GetId() string {
    48  	return lbr.RuleId
    49  }
    50  
    51  func (lbr *SLoadbalancerListenerRule) GetGlobalId() string {
    52  	return lbr.RuleId
    53  }
    54  
    55  func (lbr *SLoadbalancerListenerRule) GetStatus() string {
    56  	return api.LB_STATUS_ENABLED
    57  }
    58  
    59  func (self *SLoadbalancerListenerRule) IsDefault() bool {
    60  	return false
    61  }
    62  
    63  func (lbr *SLoadbalancerListenerRule) IsEmulated() bool {
    64  	return false
    65  }
    66  
    67  func (lbr *SLoadbalancerListenerRule) getRegion() *SRegion {
    68  	if lbr.httpListener != nil {
    69  		return lbr.httpListener.lb.region
    70  	} else if lbr.httpsListener != nil {
    71  		return lbr.httpsListener.lb.region
    72  	}
    73  	return nil
    74  }
    75  
    76  func (lbr *SLoadbalancerListenerRule) Refresh() error {
    77  	region := lbr.getRegion()
    78  	if region == nil {
    79  		return fmt.Errorf("failed to find listener for rule %s", lbr.RuleName)
    80  	}
    81  	rule, err := region.GetLoadbalancerListenerRule(lbr.RuleId)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	return jsonutils.Update(lbr, rule)
    86  }
    87  
    88  func (lbr *SLoadbalancerListenerRule) GetCondition() string {
    89  	return ""
    90  }
    91  
    92  func (lbr *SLoadbalancerListenerRule) GetDomain() string {
    93  	return lbr.Domain
    94  }
    95  
    96  func (lbr *SLoadbalancerListenerRule) GetPath() string {
    97  	return lbr.Url
    98  }
    99  
   100  func (lbr *SLoadbalancerListenerRule) GetProjectId() string {
   101  	if lbr.httpListener != nil {
   102  		return lbr.httpListener.GetProjectId()
   103  	} else if lbr.httpsListener != nil {
   104  		return lbr.httpsListener.GetProjectId()
   105  	}
   106  
   107  	return ""
   108  }
   109  
   110  func (lbr *SLoadbalancerListenerRule) GetBackendGroupId() string {
   111  	return lbr.VServerGroupId
   112  }
   113  
   114  func (region *SRegion) GetLoadbalancerListenerRules(loadbalancerId string, listenerPort int) ([]SLoadbalancerListenerRule, error) {
   115  	params := map[string]string{}
   116  	params["RegionId"] = region.RegionId
   117  	params["LoadBalancerId"] = loadbalancerId
   118  	params["ListenerPort"] = fmt.Sprintf("%d", listenerPort)
   119  	body, err := region.lbRequest("DescribeRules", params)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  	rules := []SLoadbalancerListenerRule{}
   124  	return rules, body.Unmarshal(&rules, "Rules", "Rule")
   125  }
   126  
   127  func (lbr *SLoadbalancerListenerRule) Delete(ctx context.Context) error {
   128  	if lbr.httpListener != nil {
   129  		return lbr.httpListener.lb.region.DeleteLoadbalancerListenerRule(lbr.RuleId)
   130  	}
   131  	if lbr.httpsListener != nil {
   132  		return lbr.httpsListener.lb.region.DeleteLoadbalancerListenerRule(lbr.RuleId)
   133  	}
   134  	return fmt.Errorf("failed to find listener for listener rule %s", lbr.RuleName)
   135  }
   136  
   137  func (region *SRegion) DeleteLoadbalancerListenerRule(ruleId string) error {
   138  	params := map[string]string{}
   139  	params["RegionId"] = region.RegionId
   140  	params["RuleIds"] = fmt.Sprintf(`["%s"]`, ruleId)
   141  	_, err := region.lbRequest("DeleteRules", params)
   142  	return err
   143  }
   144  
   145  func (region *SRegion) CreateLoadbalancerListenerRule(listenerPort int, loadbalancerId string, _rule *SLoadbalancerListenerRule) (*SLoadbalancerListenerRule, error) {
   146  	params := map[string]string{}
   147  	params["RegionId"] = region.RegionId
   148  	params["ListenerPort"] = fmt.Sprintf("%d", listenerPort)
   149  	params["LoadBalancerId"] = loadbalancerId
   150  	_rules := jsonutils.NewArray()
   151  	_rules.Add(jsonutils.Marshal(_rule))
   152  	params["RuleList"] = _rules.String()
   153  	body, err := region.lbRequest("CreateRules", params)
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  	rules := []SLoadbalancerListenerRule{}
   158  	if err := body.Unmarshal(&rules, "Rules", "Rule"); err != nil {
   159  		return nil, err
   160  	}
   161  	for _, rule := range rules {
   162  		if rule.RuleName == _rule.RuleName {
   163  			return region.GetLoadbalancerListenerRule(rule.RuleId)
   164  		}
   165  	}
   166  	return nil, cloudprovider.ErrNotFound
   167  }
   168  
   169  func (region *SRegion) GetLoadbalancerListenerRule(ruleId string) (*SLoadbalancerListenerRule, error) {
   170  	if len(ruleId) == 0 {
   171  		return nil, cloudprovider.ErrNotFound
   172  	}
   173  	params := map[string]string{}
   174  	params["RegionId"] = region.RegionId
   175  	params["RuleId"] = ruleId
   176  	body, err := region.lbRequest("DescribeRuleAttribute", params)
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  	rule := &SLoadbalancerListenerRule{RuleId: ruleId}
   181  	return rule, body.Unmarshal(rule)
   182  }