/vendor/github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/l7policies/requests.go

https://github.com/openshift/installer · Go · 384 lines · 233 code · 63 blank · 88 comment · 26 complexity · f8b9de3d55f51311f2233d39c19f355e MD5 · raw file

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