yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/loadbalancer_listener_rule.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/multicloud" 25 "yunion.io/x/cloudmux/pkg/multicloud/huawei" 26 ) 27 28 type SElbListenerPolicy struct { 29 multicloud.SResourceBase 30 multicloud.SLoadbalancerRedirectBase 31 huawei.HuaweiTags 32 region *SRegion 33 lb *SLoadbalancer 34 listener *SElbListener 35 36 RedirectPoolID string `json:"redirect_pool_id"` 37 RedirectListenerID *string `json:"redirect_listener_id"` 38 Description string `json:"description"` 39 AdminStateUp bool `json:"admin_state_up"` 40 Rules []Rule `json:"rules"` 41 TenantID string `json:"tenant_id"` 42 ProjectID string `json:"project_id"` 43 ListenerID string `json:"listener_id"` 44 RedirectURL *string `json:"redirect_url"` 45 ProvisioningStatus string `json:"provisioning_status"` 46 Action string `json:"action"` 47 Position int64 `json:"position"` 48 ID string `json:"id"` 49 Name string `json:"name"` 50 } 51 52 type Rule struct { 53 ID string `json:"id"` 54 } 55 56 type SElbListenerPolicyRule struct { 57 region *SRegion 58 policy *SElbListenerPolicy 59 60 CompareType string `json:"compare_type"` 61 ProvisioningStatus string `json:"provisioning_status"` 62 AdminStateUp bool `json:"admin_state_up"` 63 TenantID string `json:"tenant_id"` 64 ProjectID string `json:"project_id"` 65 Invert bool `json:"invert"` 66 Value string `json:"value"` 67 Key interface{} `json:"key"` 68 Type string `json:"type"` 69 ID string `json:"id"` 70 } 71 72 func (self *SElbListenerPolicy) GetId() string { 73 return self.ID 74 } 75 76 func (self *SElbListenerPolicy) GetName() string { 77 return self.Name 78 } 79 80 func (self *SElbListenerPolicy) GetGlobalId() string { 81 return self.GetId() 82 } 83 84 // 负载均衡没有启用禁用操作 85 func (self *SElbListenerPolicy) GetStatus() string { 86 return api.LB_STATUS_ENABLED 87 } 88 89 func (self *SElbListenerPolicy) Refresh() error { 90 ret := &SElbListenerPolicy{} 91 err := DoGet(self.lb.region.ecsClient.ElbL7policies.Get, self.GetId(), nil, ret) 92 if err != nil { 93 return err 94 } 95 96 err = jsonutils.Update(self, ret) 97 if err != nil { 98 return err 99 } 100 101 return nil 102 } 103 104 func (self *SElbListenerPolicy) IsDefault() bool { 105 return false 106 } 107 108 func (self *SElbListenerPolicy) IsEmulated() bool { 109 return false 110 } 111 112 func (self *SElbListenerPolicy) GetProjectId() string { 113 return "" 114 } 115 116 func (self *SElbListenerPolicy) GetRules() ([]SElbListenerPolicyRule, error) { 117 ret, err := self.region.GetLoadBalancerPolicyRules(self.GetId()) 118 if err != nil { 119 return nil, err 120 } 121 122 for i := range ret { 123 ret[i].policy = self 124 } 125 126 return ret, nil 127 } 128 129 func (self *SElbListenerPolicy) GetDomain() string { 130 rules, err := self.GetRules() 131 if err != nil { 132 log.Errorf("loadbalancer rule GetDomain %s", err) 133 } 134 135 for i := range rules { 136 if rules[i].Type == "HOST_NAME" { 137 return rules[i].Value 138 } 139 } 140 141 return "" 142 } 143 144 func (self *SElbListenerPolicy) GetCondition() string { 145 return "" 146 } 147 148 func (self *SElbListenerPolicy) GetPath() string { 149 rules, err := self.GetRules() 150 if err != nil { 151 log.Errorf("loadbalancer rule GetPath %s", err) 152 } 153 154 for i := range rules { 155 if rules[i].Type == "PATH" { 156 return rules[i].Value 157 } 158 } 159 160 return "" 161 } 162 163 func (self *SElbListenerPolicy) GetBackendGroupId() string { 164 return self.RedirectPoolID 165 } 166 167 func (self *SElbListenerPolicy) Delete(ctx context.Context) error { 168 return self.region.DeleteLoadBalancerPolicy(self.GetId()) 169 } 170 171 func (self *SRegion) DeleteLoadBalancerPolicy(policyId string) error { 172 return DoDelete(self.ecsClient.ElbL7policies.Delete, policyId, nil, nil) 173 }