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