yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/loadbalancerl7policy.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 "fmt" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/pkg/errors" 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 SLoadbalancerL7PolicyCreateParams struct { 29 Description string `json:"description,omitempty"` 30 AdminStateUp bool `json:"admin_state_up,omitempty"` 31 ListenerID string `json:"listener_id,omitempty"` 32 RedirectPrefix string `json:"redirect_prefix,omitempty"` 33 RedirectURL string `json:"redirect_url,omitempty"` 34 RedirectPoolID string `json:"redirect_pool_id,omitempty"` 35 RedirectHTTPCode *int `json:"redirect_http_code,omitempty"` 36 Name string `json:"name,omitempty"` 37 Action string `json:"action,omitempty"` 38 Position *int `json:"position,omitempty"` 39 Tags []string `json:"tags,omitempty"` 40 } 41 42 type SLoadbalancerL7Policy struct { 43 multicloud.SResourceBase 44 OpenStackTags 45 region *SRegion 46 l7rules []SLoadbalancerL7Rule 47 ListenerID string `json:"listener_id"` 48 Description string `json:"description"` 49 AdminStateUp bool `json:"admin_state_up"` 50 RuleIds []SL7RuleID `json:"rules"` 51 CreatedAt string `json:"created_at"` 52 ProvisioningStatus string `json:"provisioning_status"` 53 UpdatedAt string `json:"updated_at"` 54 RedirectHTTPCode int `json:"redirect_http_code"` 55 RedirectPoolID string `json:"redirect_pool_id"` 56 RedirectPrefix string `json:"redirect_prefix"` 57 RedirectURL string `json:"redirect_url"` 58 Action string `json:"action"` 59 Position int `json:"position"` 60 ProjectID string `json:"project_id"` 61 ID string `json:"id"` 62 OperatingStatus string `json:"operating_status"` 63 Name string `json:"name"` 64 Tags []string `json:"tags"` 65 } 66 67 func (region *SRegion) GetLoadbalancerL7PolicybyId(policieId string) (*SLoadbalancerL7Policy, error) { 68 body, err := region.lbGet(fmt.Sprintf("/v2/lbaas/l7policies/%s", policieId)) 69 if err != nil { 70 return nil, errors.Wrapf(err, `region.lbGet(/v2/lbaas/l7policies/%s)`, policieId) 71 } 72 l7policy := SLoadbalancerL7Policy{} 73 err = body.Unmarshal(&l7policy, "l7policy") 74 if err != nil { 75 return nil, errors.Wrap(err, "body.Unmarshal(&l7policy, policy)") 76 } 77 l7policy.region = region 78 err = l7policy.fetchLoadbalancerL7Rules() 79 if err != nil { 80 return nil, errors.Wrap(err, "l7policy.fetchLoadbalancerL7Rules()") 81 } 82 return &l7policy, nil 83 } 84 85 func (region *SRegion) CreateLoadbalancerL7Policy(listenerId string, rule *cloudprovider.SLoadbalancerListenerRule) (*SLoadbalancerL7Policy, error) { 86 type Params struct { 87 L7policy SLoadbalancerL7PolicyCreateParams `json:"l7policy"` 88 } 89 l7policyParams := Params{} 90 91 l7policyParams.L7policy.AdminStateUp = true 92 l7policyParams.L7policy.ListenerID = listenerId 93 l7policyParams.L7policy.Name = rule.Name 94 95 if rule.Redirect == api.LB_REDIRECT_RAW { 96 l7policyParams.L7policy.RedirectHTTPCode = &rule.RedirectCode 97 if len(rule.RedirectPath) < 1 { 98 l7policyParams.L7policy.Action = "REDIRECT_PREFIX" 99 l7policyParams.L7policy.RedirectPrefix = rule.RedirectScheme + "://" + rule.RedirectHost 100 } else { 101 l7policyParams.L7policy.Action = "REDIRECT_TO_URL" 102 l7policyParams.L7policy.RedirectURL = rule.RedirectScheme + "://" + rule.RedirectHost + rule.RedirectPath 103 } 104 } else { 105 l7policyParams.L7policy.Action = "REDIRECT_TO_POOL" 106 l7policyParams.L7policy.RedirectPoolID = rule.BackendGroupID 107 } 108 109 body, err := region.lbPost("/v2/lbaas/l7policies", jsonutils.Marshal(l7policyParams)) 110 if err != nil { 111 return nil, errors.Wrap(err, `region.lbPost("/v2/lbaas/l7policies", jsonutils.Marshal(l7policyParams))`) 112 } 113 l7policy := SLoadbalancerL7Policy{} 114 l7policy.region = region 115 err = body.Unmarshal(&l7policy, "l7policy") 116 if err != nil { 117 return nil, errors.Wrap(err, "body.Unmarshal(&l7policy, policy)") 118 } 119 return &l7policy, nil 120 } 121 122 func (region *SRegion) DeleteLoadbalancerListenerL7policy(policyId string) error { 123 _, err := region.lbDelete(fmt.Sprintf("/v2/lbaas/l7policies/%s", policyId)) 124 if err != nil { 125 return errors.Wrapf(err, `region.lbDelete(/v2/lbaas/l7policies/%s)`, policyId) 126 } 127 return nil 128 } 129 130 func (L7policy *SLoadbalancerL7Policy) GetName() string { 131 return L7policy.Name 132 } 133 134 func (L7policy *SLoadbalancerL7Policy) GetId() string { 135 return L7policy.ID 136 } 137 138 func (L7policy *SLoadbalancerL7Policy) GetGlobalId() string { 139 return L7policy.ID 140 } 141 142 func (L7policy *SLoadbalancerL7Policy) GetStatus() string { 143 switch L7policy.ProvisioningStatus { 144 case "ACTIVE": 145 return api.LB_STATUS_ENABLED 146 case "PENDING_CREATE": 147 return api.LB_CREATING 148 case "PENDING_UPDATE": 149 return api.LB_SYNC_CONF 150 case "PENDING_DELETE": 151 return api.LB_STATUS_DELETING 152 case "DELETED": 153 return api.LB_STATUS_DELETED 154 default: 155 return api.LB_STATUS_UNKNOWN 156 } 157 } 158 159 func (L7policy *SLoadbalancerL7Policy) Refresh() error { 160 newL7policie, err := L7policy.region.GetLoadbalancerL7PolicybyId(L7policy.ID) 161 if err != nil { 162 return err 163 } 164 return jsonutils.Update(L7policy, newL7policie) 165 } 166 167 func (L7policy *SLoadbalancerL7Policy) IsEmulated() bool { 168 return false 169 }