github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/l7policies/requests.go (about) 1 package l7policies 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // CreateOptsBuilder allows extensions to add additional parameters to the 11 // Create request. 12 type CreateOptsBuilder interface { 13 ToL7PolicyCreateMap() (map[string]any, error) 14 } 15 16 type Action string 17 type RuleType string 18 type CompareType string 19 20 const ( 21 ActionRedirectPrefix Action = "REDIRECT_PREFIX" 22 ActionRedirectToPool Action = "REDIRECT_TO_POOL" 23 ActionRedirectToURL Action = "REDIRECT_TO_URL" 24 ActionReject Action = "REJECT" 25 26 TypeCookie RuleType = "COOKIE" 27 TypeFileType RuleType = "FILE_TYPE" 28 TypeHeader RuleType = "HEADER" 29 TypeHostName RuleType = "HOST_NAME" 30 TypePath RuleType = "PATH" 31 TypeSSLConnHasCert RuleType = "SSL_CONN_HAS_CERT" 32 TypeSSLVerifyResult RuleType = "SSL_VERIFY_RESULT" 33 TypeSSLDNField RuleType = "SSL_DN_FIELD" 34 35 CompareTypeContains CompareType = "CONTAINS" 36 CompareTypeEndWith CompareType = "ENDS_WITH" 37 CompareTypeEqual CompareType = "EQUAL_TO" 38 CompareTypeRegex CompareType = "REGEX" 39 CompareTypeStartWith CompareType = "STARTS_WITH" 40 ) 41 42 // CreateOpts is the common options struct used in this package's Create 43 // operation. 44 type CreateOpts struct { 45 // Name of the L7 policy. 46 Name string `json:"name,omitempty"` 47 48 // The ID of the listener. 49 ListenerID string `json:"listener_id,omitempty"` 50 51 // The L7 policy action. One of REDIRECT_PREFIX, REDIRECT_TO_POOL, REDIRECT_TO_URL, or REJECT. 52 Action Action `json:"action" required:"true"` 53 54 // The position of this policy on the listener. 55 Position int32 `json:"position,omitempty"` 56 57 // A human-readable description for the resource. 58 Description string `json:"description,omitempty"` 59 60 // ProjectID is the UUID of the project who owns the L7 policy in octavia. 61 // Only administrative users can specify a project UUID other than their own. 62 ProjectID string `json:"project_id,omitempty"` 63 64 // Requests matching this policy will be redirected to this Prefix URL. 65 // Only valid if action is REDIRECT_PREFIX. 66 RedirectPrefix string `json:"redirect_prefix,omitempty"` 67 68 // Requests matching this policy will be redirected to the pool with this ID. 69 // Only valid if action is REDIRECT_TO_POOL. 70 RedirectPoolID string `json:"redirect_pool_id,omitempty"` 71 72 // Requests matching this policy will be redirected to this URL. 73 // Only valid if action is REDIRECT_TO_URL. 74 RedirectURL string `json:"redirect_url,omitempty"` 75 76 // Requests matching this policy will be redirected to the specified URL or Prefix URL 77 // with the HTTP response code. Valid if action is REDIRECT_TO_URL or REDIRECT_PREFIX. 78 // Valid options are: 301, 302, 303, 307, or 308. Default is 302. Requires version 2.9 79 RedirectHttpCode int32 `json:"redirect_http_code,omitempty"` 80 81 // The administrative state of the Loadbalancer. A valid value is true (UP) 82 // or false (DOWN). 83 AdminStateUp *bool `json:"admin_state_up,omitempty"` 84 85 // Rules is a slice of CreateRuleOpts which allows a set of rules 86 // to be created at the same time the policy is created. 87 // 88 // This is only possible to use when creating a fully populated 89 // Loadbalancer. 90 Rules []CreateRuleOpts `json:"rules,omitempty"` 91 92 // Tags is a set of resource tags. Requires version 2.5. 93 Tags []string `json:"tags,omitempty"` 94 } 95 96 // ToL7PolicyCreateMap builds a request body from CreateOpts. 97 func (opts CreateOpts) ToL7PolicyCreateMap() (map[string]any, error) { 98 return gophercloud.BuildRequestBody(opts, "l7policy") 99 } 100 101 // Create accepts a CreateOpts struct and uses the values to create a new l7policy. 102 func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 103 b, err := opts.ToL7PolicyCreateMap() 104 if err != nil { 105 r.Err = err 106 return 107 } 108 resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil) 109 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 110 return 111 } 112 113 // ListOptsBuilder allows extensions to add additional parameters to the 114 // List request. 115 type ListOptsBuilder interface { 116 ToL7PolicyListQuery() (string, error) 117 } 118 119 // ListOpts allows the filtering and sorting of paginated collections through 120 // the API. 121 type ListOpts struct { 122 Name string `q:"name"` 123 Description string `q:"description"` 124 ListenerID string `q:"listener_id"` 125 Action string `q:"action"` 126 ProjectID string `q:"project_id"` 127 RedirectPoolID string `q:"redirect_pool_id"` 128 RedirectURL string `q:"redirect_url"` 129 Position int32 `q:"position"` 130 AdminStateUp bool `q:"admin_state_up"` 131 ID string `q:"id"` 132 Limit int `q:"limit"` 133 Marker string `q:"marker"` 134 SortKey string `q:"sort_key"` 135 SortDir string `q:"sort_dir"` 136 } 137 138 // ToL7PolicyListQuery formats a ListOpts into a query string. 139 func (opts ListOpts) ToL7PolicyListQuery() (string, error) { 140 q, err := gophercloud.BuildQueryString(opts) 141 return q.String(), err 142 } 143 144 // List returns a Pager which allows you to iterate over a collection of 145 // l7policies. It accepts a ListOpts struct, which allows you to filter and sort 146 // the returned collection for greater efficiency. 147 // 148 // Default policy settings return only those l7policies that are owned by the 149 // project who submits the request, unless an admin user submits the request. 150 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 151 url := rootURL(c) 152 if opts != nil { 153 query, err := opts.ToL7PolicyListQuery() 154 if err != nil { 155 return pagination.Pager{Err: err} 156 } 157 url += query 158 } 159 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 160 return L7PolicyPage{pagination.LinkedPageBase{PageResult: r}} 161 }) 162 } 163 164 // Get retrieves a particular l7policy based on its unique ID. 165 func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) { 166 resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil) 167 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 168 return 169 } 170 171 // Delete will permanently delete a particular l7policy based on its unique ID. 172 func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) { 173 resp, err := c.Delete(ctx, resourceURL(c, id), nil) 174 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 175 return 176 } 177 178 // UpdateOptsBuilder allows extensions to add additional parameters to the 179 // Update request. 180 type UpdateOptsBuilder interface { 181 ToL7PolicyUpdateMap() (map[string]any, error) 182 } 183 184 // UpdateOpts is the common options struct used in this package's Update 185 // operation. 186 type UpdateOpts struct { 187 // Name of the L7 policy, empty string is allowed. 188 Name *string `json:"name,omitempty"` 189 190 // The L7 policy action. One of REDIRECT_PREFIX, REDIRECT_TO_POOL, REDIRECT_TO_URL, or REJECT. 191 Action Action `json:"action,omitempty"` 192 193 // The position of this policy on the listener. 194 Position int32 `json:"position,omitempty"` 195 196 // A human-readable description for the resource, empty string is allowed. 197 Description *string `json:"description,omitempty"` 198 199 // Requests matching this policy will be redirected to this Prefix URL. 200 // Only valid if action is REDIRECT_PREFIX. 201 RedirectPrefix *string `json:"redirect_prefix,omitempty"` 202 203 // Requests matching this policy will be redirected to the pool with this ID. 204 // Only valid if action is REDIRECT_TO_POOL. 205 RedirectPoolID *string `json:"redirect_pool_id,omitempty"` 206 207 // Requests matching this policy will be redirected to this URL. 208 // Only valid if action is REDIRECT_TO_URL. 209 RedirectURL *string `json:"redirect_url,omitempty"` 210 211 // Requests matching this policy will be redirected to the specified URL or Prefix URL 212 // with the HTTP response code. Valid if action is REDIRECT_TO_URL or REDIRECT_PREFIX. 213 // Valid options are: 301, 302, 303, 307, or 308. Default is 302. Requires version 2.9 214 RedirectHttpCode int32 `json:"redirect_http_code,omitempty"` 215 216 // The administrative state of the Loadbalancer. A valid value is true (UP) 217 // or false (DOWN). 218 AdminStateUp *bool `json:"admin_state_up,omitempty"` 219 220 // Tags is a set of resource tags. Requires version 2.5. 221 Tags *[]string `json:"tags,omitempty"` 222 } 223 224 // ToL7PolicyUpdateMap builds a request body from UpdateOpts. 225 func (opts UpdateOpts) ToL7PolicyUpdateMap() (map[string]any, error) { 226 b, err := gophercloud.BuildRequestBody(opts, "l7policy") 227 if err != nil { 228 return nil, err 229 } 230 231 m := b["l7policy"].(map[string]any) 232 233 if m["redirect_pool_id"] == "" { 234 m["redirect_pool_id"] = nil 235 } 236 237 if m["redirect_url"] == "" { 238 m["redirect_url"] = nil 239 } 240 241 if m["redirect_prefix"] == "" { 242 m["redirect_prefix"] = nil 243 } 244 245 if m["redirect_http_code"] == 0 { 246 m["redirect_http_code"] = nil 247 } 248 249 return b, nil 250 } 251 252 // Update allows l7policy to be updated. 253 func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 254 b, err := opts.ToL7PolicyUpdateMap() 255 if err != nil { 256 r.Err = err 257 return 258 } 259 resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 260 OkCodes: []int{200}, 261 }) 262 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 263 return 264 } 265 266 // CreateRuleOpts is the common options struct used in this package's CreateRule 267 // operation. 268 type CreateRuleOpts struct { 269 // The L7 rule type. One of COOKIE, FILE_TYPE, HEADER, HOST_NAME, or PATH. 270 RuleType RuleType `json:"type" required:"true"` 271 272 // The comparison type for the L7 rule. One of CONTAINS, ENDS_WITH, EQUAL_TO, REGEX, or STARTS_WITH. 273 CompareType CompareType `json:"compare_type" required:"true"` 274 275 // The value to use for the comparison. For example, the file type to compare. 276 Value string `json:"value" required:"true"` 277 278 // ProjectID is the UUID of the project who owns the rule in octavia. 279 // Only administrative users can specify a project UUID other than their own. 280 ProjectID string `json:"project_id,omitempty"` 281 282 // The key to use for the comparison. For example, the name of the cookie to evaluate. 283 Key string `json:"key,omitempty"` 284 285 // When true the logic of the rule is inverted. For example, with invert true, 286 // equal to would become not equal to. Default is false. 287 Invert bool `json:"invert,omitempty"` 288 289 // The administrative state of the Loadbalancer. A valid value is true (UP) 290 // or false (DOWN). 291 AdminStateUp *bool `json:"admin_state_up,omitempty"` 292 293 // Tags is a set of resource tags. Requires version 2.5. 294 Tags []string `json:"tags,omitempty"` 295 } 296 297 // ToRuleCreateMap builds a request body from CreateRuleOpts. 298 func (opts CreateRuleOpts) ToRuleCreateMap() (map[string]any, error) { 299 return gophercloud.BuildRequestBody(opts, "rule") 300 } 301 302 // CreateRule will create and associate a Rule with a particular L7Policy. 303 func CreateRule(ctx context.Context, c *gophercloud.ServiceClient, policyID string, opts CreateRuleOpts) (r CreateRuleResult) { 304 b, err := opts.ToRuleCreateMap() 305 if err != nil { 306 r.Err = err 307 return 308 } 309 resp, err := c.Post(ctx, ruleRootURL(c, policyID), b, &r.Body, nil) 310 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 311 return 312 } 313 314 // ListRulesOptsBuilder allows extensions to add additional parameters to the 315 // ListRules request. 316 type ListRulesOptsBuilder interface { 317 ToRulesListQuery() (string, error) 318 } 319 320 // ListRulesOpts allows the filtering and sorting of paginated collections 321 // through the API. 322 type ListRulesOpts struct { 323 RuleType RuleType `q:"type"` 324 ProjectID string `q:"project_id"` 325 CompareType CompareType `q:"compare_type"` 326 Value string `q:"value"` 327 Key string `q:"key"` 328 Invert bool `q:"invert"` 329 AdminStateUp bool `q:"admin_state_up"` 330 ID string `q:"id"` 331 Limit int `q:"limit"` 332 Marker string `q:"marker"` 333 SortKey string `q:"sort_key"` 334 SortDir string `q:"sort_dir"` 335 } 336 337 // ToRulesListQuery formats a ListOpts into a query string. 338 func (opts ListRulesOpts) ToRulesListQuery() (string, error) { 339 q, err := gophercloud.BuildQueryString(opts) 340 return q.String(), err 341 } 342 343 // ListRules returns a Pager which allows you to iterate over a collection of 344 // rules. It accepts a ListRulesOptsBuilder, which allows you to filter and 345 // sort the returned collection for greater efficiency. 346 // 347 // Default policy settings return only those rules that are owned by the 348 // project who submits the request, unless an admin user submits the request. 349 func ListRules(c *gophercloud.ServiceClient, policyID string, opts ListRulesOptsBuilder) pagination.Pager { 350 url := ruleRootURL(c, policyID) 351 if opts != nil { 352 query, err := opts.ToRulesListQuery() 353 if err != nil { 354 return pagination.Pager{Err: err} 355 } 356 url += query 357 } 358 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 359 return RulePage{pagination.LinkedPageBase{PageResult: r}} 360 }) 361 } 362 363 // GetRule retrieves a particular L7Policy Rule based on its unique ID. 364 func GetRule(ctx context.Context, c *gophercloud.ServiceClient, policyID string, ruleID string) (r GetRuleResult) { 365 resp, err := c.Get(ctx, ruleResourceURL(c, policyID, ruleID), &r.Body, nil) 366 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 367 return 368 } 369 370 // DeleteRule will remove a Rule from a particular L7Policy. 371 func DeleteRule(ctx context.Context, c *gophercloud.ServiceClient, policyID string, ruleID string) (r DeleteRuleResult) { 372 resp, err := c.Delete(ctx, ruleResourceURL(c, policyID, ruleID), nil) 373 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 374 return 375 } 376 377 // UpdateRuleOptsBuilder allows to add additional parameters to the PUT request. 378 type UpdateRuleOptsBuilder interface { 379 ToRuleUpdateMap() (map[string]any, error) 380 } 381 382 // UpdateRuleOpts is the common options struct used in this package's Update 383 // operation. 384 type UpdateRuleOpts struct { 385 // The L7 rule type. One of COOKIE, FILE_TYPE, HEADER, HOST_NAME, or PATH. 386 RuleType RuleType `json:"type,omitempty"` 387 388 // The comparison type for the L7 rule. One of CONTAINS, ENDS_WITH, EQUAL_TO, REGEX, or STARTS_WITH. 389 CompareType CompareType `json:"compare_type,omitempty"` 390 391 // The value to use for the comparison. For example, the file type to compare. 392 Value string `json:"value,omitempty"` 393 394 // The key to use for the comparison. For example, the name of the cookie to evaluate. 395 Key *string `json:"key,omitempty"` 396 397 // When true the logic of the rule is inverted. For example, with invert true, 398 // equal to would become not equal to. Default is false. 399 Invert *bool `json:"invert,omitempty"` 400 401 // The administrative state of the Loadbalancer. A valid value is true (UP) 402 // or false (DOWN). 403 AdminStateUp *bool `json:"admin_state_up,omitempty"` 404 405 // Tags is a set of resource tags. Requires version 2.5. 406 Tags *[]string `json:"tags,omitempty"` 407 } 408 409 // ToRuleUpdateMap builds a request body from UpdateRuleOpts. 410 func (opts UpdateRuleOpts) ToRuleUpdateMap() (map[string]any, error) { 411 b, err := gophercloud.BuildRequestBody(opts, "rule") 412 if err != nil { 413 return nil, err 414 } 415 416 if m := b["rule"].(map[string]any); m["key"] == "" { 417 m["key"] = nil 418 } 419 420 return b, nil 421 } 422 423 // UpdateRule allows Rule to be updated. 424 func UpdateRule(ctx context.Context, c *gophercloud.ServiceClient, policyID string, ruleID string, opts UpdateRuleOptsBuilder) (r UpdateRuleResult) { 425 b, err := opts.ToRuleUpdateMap() 426 if err != nil { 427 r.Err = err 428 return 429 } 430 resp, err := c.Put(ctx, ruleResourceURL(c, policyID, ruleID), b, &r.Body, &gophercloud.RequestOpts{ 431 OkCodes: []int{200, 201, 202}, 432 }) 433 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 434 return 435 }