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