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