github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/elb/v3/policies/requests.go (about) 1 package policies 2 3 import ( 4 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/openstack/elb/v3/rules" 6 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 7 ) 8 9 type Action string 10 11 const ( 12 ActionRedirectToPool Action = "REDIRECT_TO_POOL" 13 ActionRedirectToListener Action = "REDIRECT_TO_LISTENER" 14 ActionFixedResponse Action = "FIXED_RESPONSE" 15 ActionUrlRedirect Action = "REDIRECT_TO_URL" 16 ) 17 18 type Rule struct { 19 // Specifies the match content. The value can be one of the following: 20 // 21 // HOST_NAME: A domain name will be used for matching. 22 // PATH: A URL will be used for matching. 23 // If type is set to HOST_NAME, PATH, METHOD, or SOURCE_IP, only one forwarding rule can be created for each type. 24 Type rules.RuleType `json:"type" required:"true"` 25 26 // Specifies how requests are matched with the domain name or URL. 27 // 28 // If type is set to HOST_NAME, this parameter can only be set to EQUAL_TO (exact match). 29 // 30 // If type is set to PATH, this parameter can be set to REGEX (regular expression match), STARTS_WITH (prefix match), or EQUAL_TO (exact match). 31 CompareType rules.CompareType `json:"compare_type" required:"true"` 32 33 // Specifies the value of the match item. For example, if a domain name is used for matching, value is the domain name. 34 // 35 // If type is set to HOST_NAME, the value can contain letters, digits, hyphens (-), and periods (.) and must start with a letter or digit. 36 // If you want to use a wildcard domain name, enter an asterisk (*) as the leftmost label of the domain name. 37 // 38 // If type is set to PATH and compare_type to STARTS_WITH or EQUAL_TO, the value must start with a slash (/) 39 // and can contain only letters, digits, and special characters _~';@^-%#&$.*+?,=!:|/()[]{} 40 Value string `json:"value" required:"true"` 41 } 42 43 type RedirectPoolOptions struct { 44 // Specifies the ID of the backend server group. 45 PoolId string `json:"pool_id" required:"true"` 46 47 // Specifies the weight of the backend server group. The value ranges from 0 to 100. 48 Weight string `json:"weight" required:"true"` 49 } 50 51 type FixedResponseOptions struct { 52 // Specifies the fixed HTTP status code configured in the forwarding rule. 53 // The value can be any integer in the range of 200-299, 400-499, or 500-599. 54 StatusCode string `json:"status_code" required:"true"` 55 56 // Specifies the format of the response body. 57 // 58 // The value can be text/plain, text/css, text/html, application/javascript, or application/json. 59 ContentType string `json:"content_type"` 60 61 // Specifies the content of the response message body. 62 MessageBody string `json:"message_body"` 63 } 64 65 type RedirectUrlOptions struct { 66 // Specifies the protocol for redirection. 67 // The value can be HTTP, HTTPS, or ${protocol}. The default value is ${protocol}, indicating that the protocol of the request will be used. 68 Protocol string `json:"protocol"` 69 70 // Specifies the host name that requests are redirected to. 71 // The value can contain only letters, digits, hyphens (-), and periods (.) and must start with a letter or digit. 72 // The default value is ${host}, indicating that the host of the request will be used. 73 Host string `json:"host"` 74 75 // Specifies the port that requests are redirected to. 76 // The default value is ${port}, indicating that the port of the request will be used. 77 Port string `json:"port"` 78 79 // Specifies the path that requests are redirected to. The default value is ${path}, indicating that the path of the request will be used. 80 // 81 // The value can contain only letters, digits, and special characters _~';@^- %#&$.*+?,=!:|/()[]{} and must start with a slash (/). 82 Path string `json:"path"` 83 84 // Specifies the query string set in the URL for redirection. The default value is ${query}, indicating that the query string of the request will be used. 85 // 86 // For example, in the URL https://www.xxx.com:8080/elb?type=loadbalancer, ${query} indicates type=loadbalancer. 87 // If this parameter is set to ${query}&name=my_name, the URL will be redirected to https://www.xxx.com:8080/elb?type=loadbalancer&name=my_name. 88 // 89 // The value is case-sensitive and can contain only letters, digits, and special characters !$&'()*+,-./:;=?@^_` 90 Query string `json:"query"` 91 92 // Specifies the status code returned after the requests are redirected. 93 // 94 // The value can be 301, 302, 303, 307, or 308. 95 StatusCode string `json:"status_code" required:"true"` 96 } 97 98 type CreateOpts struct { 99 // Specifies where requests will be forwarded. The value can be one of the following: 100 // 101 // REDIRECT_TO_POOL: Requests will be forwarded to another backend server group. 102 // REDIRECT_TO_LISTENER: Requests will be redirected to an HTTPS listener. 103 Action Action `json:"action" required:"true"` 104 105 // Specifies the conditions contained in a forwarding rule. 106 // This parameter will take effect when enhance_l7policy_enable is set to true. 107 // If conditions is specified, key and value will not take effect, 108 // and the value of this parameter will contain all conditions configured for the forwarding rule. 109 // The keys in the list must be the same, whereas each value must be unique. 110 Conditions []rules.Condition `json:"conditions,omitempty"` 111 112 // Provides supplementary information about the forwarding policy. 113 Description string `json:"description,omitempty"` 114 115 // Specifies the configuration of the page that will be returned. 116 // This parameter will take effect when 117 FixedResponseConfig *FixedResponseOptions `json:"fixed_response_config,omitempty"` 118 119 // Specifies the ID of the listener to which the forwarding policy is added. 120 ListenerID string `json:"listener_id" required:"true"` 121 122 // Specifies the forwarding policy name. 123 Name string `json:"name,omitempty"` 124 125 // Specifies the forwarding policy priority. The value cannot be updated. 126 Position int `json:"position,omitempty"` 127 128 // Specifies the forwarding policy priority. The value cannot be updated. 129 Priority int `json:"priority,omitempty"` 130 131 // Specifies the ID of the project where the forwarding policy is used. 132 ProjectID string `json:"project_id,omitempty"` 133 134 // Specifies the ID of the listener to which requests are redirected. 135 // This parameter is mandatory when action is set to REDIRECT_TO_LISTENER. 136 RedirectListenerID string `json:"redirect_listener_id,omitempty"` 137 138 // Specifies the ID of the backend server group that requests are forwarded to. 139 // 140 // This parameter is valid and mandatory only when action is set to REDIRECT_TO_POOL. 141 // The specified backend server group cannot be the default one associated with the listener, 142 // or any backend server group associated with the forwarding policies of other listeners. 143 RedirectPoolID string `json:"redirect_pool_id,omitempty"` 144 145 // Specifies the URL to which requests are forwarded. 146 // 147 // Format: protocol://host:port/path?query 148 RedirectUrl string `json:"redirect_url,omitempty"` 149 150 // Specifies the URL to which requests are forwarded. 151 // This parameter is mandatory when action is set to REDIRECT_TO_URL. 152 // It cannot be specified if the value of action is not REDIRECT_TO_URL. 153 RedirectUrlConfig *RedirectUrlOptions `json:"redirect_url_config,omitempty"` 154 155 // Lists the forwarding rules in the forwarding policy. 156 // The list can contain a maximum of 10 forwarding rules (if conditions is specified, a condition is considered as a rule). 157 Rules []Rule `json:"rules,omitempty"` 158 159 // Specifies the configuration of the backend server group that the requests are forwarded to. This parameter is valid only when action is set to REDIRECT_TO_POOL. 160 RedirectPoolsConfig []RedirectPoolOptions `json:"redirect_pools_config,omitempty"` 161 } 162 163 type CreateOptsBuilder interface { 164 ToPolicyCreateMap() (map[string]interface{}, error) 165 } 166 167 func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) { 168 return golangsdk.BuildRequestBody(opts, "l7policy") 169 } 170 171 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 172 b, err := opts.ToPolicyCreateMap() 173 if err != nil { 174 r.Err = err 175 return 176 } 177 _, r.Err = client.Post(baseURL(client), b, &r.Body, nil) 178 return 179 } 180 181 type ListOptsBuilder interface { 182 ToPolicyListQuery() (string, error) 183 } 184 185 type ListOpts struct { 186 Marker string `q:"marker"` 187 Limit int `q:"limit"` 188 PageReverse bool `q:"page_reverse"` 189 ID []string `q:"id"` 190 Name []string `q:"name"` 191 Description []string `q:"description"` 192 ListenerID []string `q:"listener_id"` 193 Action []string `q:"action"` 194 RedirectPoolID []string `q:"redirect_pool_id"` 195 RedirectListenerID []string `q:"redirect_listener_id"` 196 ProvisioningStatus []string `q:"provisioning_status"` 197 DisplayAllRules bool `q:"display_all_rules"` 198 } 199 200 func (opts ListOpts) ToPolicyListQuery() (string, error) { 201 q, err := golangsdk.BuildQueryString(opts) 202 if err != nil { 203 return "", err 204 } 205 return q.String(), nil 206 } 207 208 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 209 url := baseURL(client) 210 if opts != nil { 211 q, err := opts.ToPolicyListQuery() 212 if err != nil { 213 return pagination.Pager{Err: err} 214 } 215 url += q 216 } 217 218 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 219 return PolicyPage{PageWithInfo: pagination.NewPageWithInfo(r)} 220 }) 221 } 222 223 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 224 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) 225 return 226 } 227 228 type UpdateOpts struct { 229 Name *string `json:"name,omitempty"` 230 Description *string `json:"description,omitempty"` 231 RedirectListenerID string `json:"redirect_listener_id,omitempty"` 232 RedirectPoolID string `json:"redirect_pool_id,omitempty"` 233 Rules []Rule `json:"rules,omitempty"` 234 RedirectUrlConfig *RedirectUrlOptions `json:"redirect_url_config,omitempty"` 235 FixedResponseConfig *FixedResponseOptions `json:"fixed_response_config,omitempty"` 236 Priority int `json:"priority,omitempty"` 237 RedirectPoolsConfig []RedirectPoolOptions `json:"redirect_pools_config,omitempty"` 238 } 239 240 type UpdateOptsBuilder interface { 241 ToPolicyUpdateMap() (map[string]interface{}, error) 242 } 243 244 func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) { 245 return golangsdk.BuildRequestBody(opts, "l7policy") 246 } 247 248 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 249 b, err := opts.ToPolicyUpdateMap() 250 if err != nil { 251 r.Err = err 252 return 253 } 254 _, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 255 OkCodes: []int{200, 201}, 256 }) 257 return 258 } 259 260 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 261 _, r.Err = client.Delete(resourceURL(client, id), nil) 262 return 263 }