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