yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/loadbalancerl7rule.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 openstack 16 17 import ( 18 "context" 19 "fmt" 20 "net/url" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/pkg/errors" 24 25 api "yunion.io/x/cloudmux/pkg/apis/compute" 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 "yunion.io/x/cloudmux/pkg/multicloud" 28 ) 29 30 type SLoadbalancerL7RuleCreateParams struct { 31 CompareType string `json:"compare_type"` 32 Invert *bool `json:"invert"` 33 Type string `json:"type"` 34 Value string `json:"value"` 35 AdminStateUp bool `json:"admin_state_up"` 36 Tags []string `json:"tags"` 37 } 38 39 type SLoadbalancerL7Rule struct { 40 multicloud.SResourceBase 41 multicloud.SLoadbalancerRedirectBase 42 OpenStackTags 43 policy *SLoadbalancerL7Policy 44 CreatedAt string `json:"created_at"` 45 CompareType string `json:"compare_type"` 46 ProvisioningStatus string `json:"provisioning_status"` 47 Invert bool `json:"invert"` 48 AdminStateUp bool `json:"admin_state_up"` 49 UpdatedAt string `json:"updated_at"` 50 Value string `json:"value"` 51 Key string `json:"key"` 52 ProjectID string `json:"project_id"` 53 Type string `json:"type"` 54 ID string `json:"id"` 55 OperatingStatus string `json:"operating_status"` 56 Tags []string `json:"tags"` 57 } 58 59 func (region *SRegion) CreateLoadbalancerL7Rule(l7policyId string, rule *cloudprovider.SLoadbalancerListenerRule) (*SLoadbalancerL7Rule, error) { 60 type Params struct { 61 L7Rule SLoadbalancerL7RuleCreateParams `json:"rule"` 62 } 63 l7ruleParams := Params{} 64 l7ruleParams.L7Rule.AdminStateUp = true 65 l7ruleParams.L7Rule.Type = "PATH" 66 l7ruleParams.L7Rule.Value = rule.Path 67 l7ruleParams.L7Rule.CompareType = "REGEX" 68 body, err := region.lbPost(fmt.Sprintf("/v2/lbaas/l7policies/%s/rules", l7policyId), jsonutils.Marshal(l7ruleParams)) 69 if err != nil { 70 return nil, errors.Wrapf(err, `region.lbPost(/v2/lbaas/l7policies/%s/rules), jsonutils.Marshal(l7ruleParams))`, l7policyId) 71 } 72 l7rule := SLoadbalancerL7Rule{} 73 err = body.Unmarshal(&l7rule, "rule") 74 if err != nil { 75 return nil, errors.Wrap(err, `body.Unmarshal(&l7rule, "rule")`) 76 } 77 return &l7rule, nil 78 } 79 80 func (policy *SLoadbalancerL7Policy) fetchLoadbalancerL7Rules() error { 81 l7rules, err := policy.region.GetLoadbalancerL7Rules(policy.ID) 82 if err != nil { 83 return errors.Wrapf(err, "policy.region.GetLoadbalancerL7Rules(%s)", policy.ID) 84 } 85 for i := 0; i < len(l7rules); i++ { 86 l7rules[i].policy = policy 87 } 88 policy.l7rules = l7rules 89 return nil 90 } 91 92 func (region *SRegion) GetLoadbalancerL7Rules(policyId string) ([]SLoadbalancerL7Rule, error) { 93 l7rules := []SLoadbalancerL7Rule{} 94 resource := fmt.Sprintf("/v2/lbaas/l7policies/%s/rules", policyId) 95 query := url.Values{} 96 for { 97 resp, err := region.lbList(resource, query) 98 if err != nil { 99 return nil, errors.Wrap(err, "lbList") 100 } 101 part := struct { 102 Rules []SLoadbalancerL7Rule 103 RulesLinks SNextLinks 104 }{} 105 err = resp.Unmarshal(&part) 106 if err != nil { 107 return nil, errors.Wrap(err, "resp.Unmarshal") 108 } 109 l7rules = append(l7rules, part.Rules...) 110 marker := part.RulesLinks.GetNextMark() 111 if len(marker) == 0 { 112 break 113 } 114 query.Set("marker", marker) 115 } 116 117 return l7rules, nil 118 } 119 120 func (region *SRegion) GetLoadbalancerL7RulebyId(policyId string, l7ruleId string) (*SLoadbalancerL7Rule, error) { 121 body, err := region.lbGet(fmt.Sprintf("/v2/lbaas/l7policies/%s/rules/%s", policyId, l7ruleId)) 122 if err != nil { 123 return nil, errors.Wrapf(err, `region.lbGet(/v2/lbaas/l7policies/%s/rules/%s )`, policyId, l7ruleId) 124 } 125 l7rule := SLoadbalancerL7Rule{} 126 return &l7rule, body.Unmarshal(&l7rule, "rule") 127 } 128 129 func (l7r *SLoadbalancerL7Rule) GetName() string { 130 return l7r.policy.Name + l7r.ID 131 } 132 133 func (l7r *SLoadbalancerL7Rule) GetId() string { 134 return l7r.ID 135 } 136 137 func (l7r *SLoadbalancerL7Rule) GetGlobalId() string { 138 return l7r.ID 139 } 140 141 func (l7r *SLoadbalancerL7Rule) GetStatus() string { 142 switch l7r.ProvisioningStatus { 143 case "ACTIVE": 144 return api.LB_STATUS_ENABLED 145 case "PENDING_CREATE": 146 return api.LB_CREATING 147 case "PENDING_UPDATE": 148 return api.LB_SYNC_CONF 149 case "PENDING_DELETE": 150 return api.LB_STATUS_DELETING 151 case "DELETED": 152 return api.LB_STATUS_DELETED 153 default: 154 return api.LB_STATUS_UNKNOWN 155 } 156 } 157 158 func (self *SLoadbalancerL7Rule) IsDefault() bool { 159 return false 160 } 161 162 func (l7r *SLoadbalancerL7Rule) IsEmulated() bool { 163 return false 164 } 165 166 func (l7r *SLoadbalancerL7Rule) Refresh() error { 167 newL7r, err := l7r.policy.region.GetLoadbalancerL7RulebyId(l7r.policy.ID, l7r.ID) 168 if err != nil { 169 return err 170 } 171 return jsonutils.Update(l7r, newL7r) 172 } 173 174 func (l7r *SLoadbalancerL7Rule) GetCondition() string { 175 return "" 176 } 177 178 func (l7r *SLoadbalancerL7Rule) GetDomain() string { 179 return "" 180 } 181 182 func (l7r *SLoadbalancerL7Rule) GetPath() string { 183 return l7r.Value 184 } 185 186 func (l7r *SLoadbalancerL7Rule) GetProjectId() string { 187 return "" 188 } 189 190 func (l7r *SLoadbalancerL7Rule) GetBackendGroupId() string { 191 return l7r.policy.RedirectPoolID 192 } 193 194 func (l7r *SLoadbalancerL7Rule) Delete(ctx context.Context) error { 195 return l7r.policy.region.DeleteLoadbalancerListenerL7policy(l7r.policy.ID) 196 }