github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/lbaas_v2/l7policies/requests.go (about) 1 package l7policies 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/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 ActionRedirectToURL Action = "REDIRECT_TO_URL" 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 URL. 63 // Only valid if action is REDIRECT_TO_URL. 64 RedirectURL string `json:"redirect_url,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 gophercloud.BuildRequestBody(opts, "l7policy") 74 } 75 76 // Create accepts a CreateOpts struct and uses the values to create a new l7policy. 77 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 78 b, err := opts.ToL7PolicyCreateMap() 79 if err != nil { 80 r.Err = err 81 return 82 } 83 resp, err := c.Post(rootURL(c), b, &r.Body, nil) 84 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 85 return 86 } 87 88 // ListOptsBuilder allows extensions to add additional parameters to the 89 // List request. 90 type ListOptsBuilder interface { 91 ToL7PolicyListQuery() (string, error) 92 } 93 94 // ListOpts allows the filtering and sorting of paginated collections through 95 // the API. 96 type ListOpts struct { 97 Name string `q:"name"` 98 Description string `q:"description"` 99 ListenerID string `q:"listener_id"` 100 Action string `q:"action"` 101 TenantID string `q:"tenant_id"` 102 RedirectPoolID string `q:"redirect_pool_id"` 103 RedirectURL string `q:"redirect_url"` 104 Position int32 `q:"position"` 105 AdminStateUp bool `q:"admin_state_up"` 106 ID string `q:"id"` 107 Limit int `q:"limit"` 108 Marker string `q:"marker"` 109 SortKey string `q:"sort_key"` 110 SortDir string `q:"sort_dir"` 111 } 112 113 // ToL7PolicyListQuery formats a ListOpts into a query string. 114 func (opts ListOpts) ToL7PolicyListQuery() (string, error) { 115 q, err := gophercloud.BuildQueryString(opts) 116 return q.String(), err 117 } 118 119 // List returns a Pager which allows you to iterate over a collection of 120 // l7policies. It accepts a ListOpts struct, which allows you to filter and sort 121 // the returned collection for greater efficiency. 122 // 123 // Default policy settings return only those l7policies that are owned by the 124 // project who submits the request, unless an admin user submits the request. 125 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 126 url := rootURL(c) 127 if opts != nil { 128 query, err := opts.ToL7PolicyListQuery() 129 if err != nil { 130 return pagination.Pager{Err: err} 131 } 132 url += query 133 } 134 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 135 return L7PolicyPage{pagination.LinkedPageBase{PageResult: r}} 136 }) 137 } 138 139 // Get retrieves a particular l7policy based on its unique ID. 140 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 141 resp, err := c.Get(resourceURL(c, id), &r.Body, nil) 142 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 143 return 144 } 145 146 // Delete will permanently delete a particular l7policy based on its unique ID. 147 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 148 resp, err := c.Delete(resourceURL(c, id), nil) 149 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 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 URL. 179 // Only valid if action is REDIRECT_TO_URL. 180 RedirectURL *string `json:"redirect_url,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 := gophercloud.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 *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 209 b, err := opts.ToL7PolicyUpdateMap() 210 if err != nil { 211 r.Err = err 212 return 213 } 214 resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 215 OkCodes: []int{200}, 216 }) 217 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 218 return 219 } 220 221 // CreateRuleOpts is the common options struct used in this package's CreateRule 222 // operation. 223 type CreateRuleOpts struct { 224 // The L7 rule type. One of COOKIE, FILE_TYPE, HEADER, HOST_NAME, or PATH. 225 RuleType RuleType `json:"type" required:"true"` 226 227 // The comparison type for the L7 rule. One of CONTAINS, ENDS_WITH, EQUAL_TO, REGEX, or STARTS_WITH. 228 CompareType CompareType `json:"compare_type" required:"true"` 229 230 // The value to use for the comparison. For example, the file type to compare. 231 Value string `json:"value" required:"true"` 232 233 // TenantID is the UUID of the tenant who owns the rule in octavia. 234 // Only administrative users can specify a project UUID other than their own. 235 TenantID string `json:"tenant_id,omitempty"` 236 237 // The key to use for the comparison. For example, the name of the cookie to evaluate. 238 Key string `json:"key,omitempty"` 239 240 // When true the logic of the rule is inverted. For example, with invert true, 241 // equal to would become not equal to. Default is false. 242 Invert bool `json:"invert,omitempty"` 243 244 // The administrative state of the Loadbalancer. A valid value is true (UP) 245 // or false (DOWN). 246 AdminStateUp *bool `json:"admin_state_up,omitempty"` 247 } 248 249 // ToRuleCreateMap builds a request body from CreateRuleOpts. 250 func (opts CreateRuleOpts) ToRuleCreateMap() (map[string]interface{}, error) { 251 return gophercloud.BuildRequestBody(opts, "rule") 252 } 253 254 // CreateRule will create and associate a Rule with a particular L7Policy. 255 func CreateRule(c *gophercloud.ServiceClient, policyID string, opts CreateRuleOpts) (r CreateRuleResult) { 256 b, err := opts.ToRuleCreateMap() 257 if err != nil { 258 r.Err = err 259 return 260 } 261 resp, err := c.Post(ruleRootURL(c, policyID), b, &r.Body, nil) 262 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 263 return 264 } 265 266 // ListRulesOptsBuilder allows extensions to add additional parameters to the 267 // ListRules request. 268 type ListRulesOptsBuilder interface { 269 ToRulesListQuery() (string, error) 270 } 271 272 // ListRulesOpts allows the filtering and sorting of paginated collections 273 // through the API. 274 type ListRulesOpts struct { 275 RuleType RuleType `q:"type"` 276 TenantID string `q:"tenant_id"` 277 CompareType CompareType `q:"compare_type"` 278 Value string `q:"value"` 279 Key string `q:"key"` 280 Invert bool `q:"invert"` 281 AdminStateUp bool `q:"admin_state_up"` 282 ID string `q:"id"` 283 Limit int `q:"limit"` 284 Marker string `q:"marker"` 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 := gophercloud.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 *gophercloud.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 *gophercloud.ServiceClient, policyID string, ruleID string) (r GetRuleResult) { 317 resp, err := c.Get(ruleResourceURL(c, policyID, ruleID), &r.Body, nil) 318 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 319 return 320 } 321 322 // DeleteRule will remove a Rule from a particular L7Policy. 323 func DeleteRule(c *gophercloud.ServiceClient, policyID string, ruleID string) (r DeleteRuleResult) { 324 resp, err := c.Delete(ruleResourceURL(c, policyID, ruleID), nil) 325 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 326 return 327 } 328 329 // UpdateRuleOptsBuilder allows to add additional parameters to the PUT request. 330 type UpdateRuleOptsBuilder interface { 331 ToRuleUpdateMap() (map[string]interface{}, error) 332 } 333 334 // UpdateRuleOpts is the common options struct used in this package's Update 335 // operation. 336 type UpdateRuleOpts struct { 337 // The L7 rule type. One of COOKIE, FILE_TYPE, HEADER, HOST_NAME, or PATH. 338 RuleType RuleType `json:"type,omitempty"` 339 340 // The comparison type for the L7 rule. One of CONTAINS, ENDS_WITH, EQUAL_TO, REGEX, or STARTS_WITH. 341 CompareType CompareType `json:"compare_type,omitempty"` 342 343 // The value to use for the comparison. For example, the file type to compare. 344 Value string `json:"value,omitempty"` 345 346 // The key to use for the comparison. For example, the name of the cookie to evaluate. 347 Key *string `json:"key,omitempty"` 348 349 // When true the logic of the rule is inverted. For example, with invert true, 350 // equal to would become not equal to. Default is false. 351 Invert *bool `json:"invert,omitempty"` 352 353 // The administrative state of the Loadbalancer. A valid value is true (UP) 354 // or false (DOWN). 355 AdminStateUp *bool `json:"admin_state_up,omitempty"` 356 } 357 358 // ToRuleUpdateMap builds a request body from UpdateRuleOpts. 359 func (opts UpdateRuleOpts) ToRuleUpdateMap() (map[string]interface{}, error) { 360 b, err := gophercloud.BuildRequestBody(opts, "rule") 361 if err != nil { 362 return nil, err 363 } 364 365 if m := b["rule"].(map[string]interface{}); m["key"] == "" { 366 m["key"] = nil 367 } 368 369 return b, nil 370 } 371 372 // UpdateRule allows Rule to be updated. 373 func UpdateRule(c *gophercloud.ServiceClient, policyID string, ruleID string, opts UpdateRuleOptsBuilder) (r UpdateRuleResult) { 374 b, err := opts.ToRuleUpdateMap() 375 if err != nil { 376 r.Err = err 377 return 378 } 379 resp, err := c.Put(ruleResourceURL(c, policyID, ruleID), b, &r.Body, &gophercloud.RequestOpts{ 380 OkCodes: []int{200, 201, 202}, 381 }) 382 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 383 return 384 }