requests.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package l7policies
  2. import (
  3. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  4. "devel.mephi.ru/iacherepanov/openstack-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. // ProjectID is the UUID of the project who owns the L7 policy in octavia.
  43. // Only administrative users can specify a project UUID other than their own.
  44. ProjectID string `json:"project_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. }
  52. // ToL7PolicyCreateMap builds a request body from CreateOpts.
  53. func (opts CreateOpts) ToL7PolicyCreateMap() (map[string]interface{}, error) {
  54. return gophercloud.BuildRequestBody(opts, "l7policy")
  55. }
  56. // Create accepts a CreateOpts struct and uses the values to create a new l7policy.
  57. func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
  58. b, err := opts.ToL7PolicyCreateMap()
  59. if err != nil {
  60. r.Err = err
  61. return
  62. }
  63. _, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
  64. return
  65. }
  66. // ListOptsBuilder allows extensions to add additional parameters to the
  67. // List request.
  68. type ListOptsBuilder interface {
  69. ToL7PolicyListQuery() (string, error)
  70. }
  71. // ListOpts allows the filtering and sorting of paginated collections through
  72. // the API.
  73. type ListOpts struct {
  74. Name string `q:"name"`
  75. ListenerID string `q:"listener_id"`
  76. Action string `q:"action"`
  77. ProjectID string `q:"project_id"`
  78. RedirectPoolID string `q:"redirect_pool_id"`
  79. RedirectURL string `q:"redirect_url"`
  80. ID string `q:"id"`
  81. Limit int `q:"limit"`
  82. Marker string `q:"marker"`
  83. SortKey string `q:"sort_key"`
  84. SortDir string `q:"sort_dir"`
  85. }
  86. // ToL7PolicyListQuery formats a ListOpts into a query string.
  87. func (opts ListOpts) ToL7PolicyListQuery() (string, error) {
  88. q, err := gophercloud.BuildQueryString(opts)
  89. return q.String(), err
  90. }
  91. // List returns a Pager which allows you to iterate over a collection of
  92. // l7policies. It accepts a ListOpts struct, which allows you to filter and sort
  93. // the returned collection for greater efficiency.
  94. //
  95. // Default policy settings return only those l7policies that are owned by the
  96. // project who submits the request, unless an admin user submits the request.
  97. func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
  98. url := rootURL(c)
  99. if opts != nil {
  100. query, err := opts.ToL7PolicyListQuery()
  101. if err != nil {
  102. return pagination.Pager{Err: err}
  103. }
  104. url += query
  105. }
  106. return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
  107. return L7PolicyPage{pagination.LinkedPageBase{PageResult: r}}
  108. })
  109. }
  110. // Get retrieves a particular l7policy based on its unique ID.
  111. func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
  112. _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
  113. return
  114. }
  115. // Delete will permanently delete a particular l7policy based on its unique ID.
  116. func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
  117. _, r.Err = c.Delete(resourceURL(c, id), nil)
  118. return
  119. }
  120. // UpdateOptsBuilder allows extensions to add additional parameters to the
  121. // Update request.
  122. type UpdateOptsBuilder interface {
  123. ToL7PolicyUpdateMap() (map[string]interface{}, error)
  124. }
  125. // UpdateOpts is the common options struct used in this package's Update
  126. // operation.
  127. type UpdateOpts struct {
  128. // Name of the L7 policy, empty string is allowed.
  129. Name *string `json:"name,omitempty"`
  130. // The L7 policy action. One of REDIRECT_TO_POOL, REDIRECT_TO_URL, or REJECT.
  131. Action Action `json:"action,omitempty"`
  132. // The position of this policy on the listener.
  133. Position int32 `json:"position,omitempty"`
  134. // A human-readable description for the resource, empty string is allowed.
  135. Description *string `json:"description,omitempty"`
  136. // Requests matching this policy will be redirected to the pool with this ID.
  137. // Only valid if action is REDIRECT_TO_POOL.
  138. RedirectPoolID string `json:"redirect_pool_id,omitempty"`
  139. // Requests matching this policy will be redirected to this URL.
  140. // Only valid if action is REDIRECT_TO_URL.
  141. RedirectURL string `json:"redirect_url,omitempty"`
  142. }
  143. // ToL7PolicyUpdateMap builds a request body from UpdateOpts.
  144. func (opts UpdateOpts) ToL7PolicyUpdateMap() (map[string]interface{}, error) {
  145. return gophercloud.BuildRequestBody(opts, "l7policy")
  146. }
  147. // Update allows l7policy to be updated.
  148. func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
  149. b, err := opts.ToL7PolicyUpdateMap()
  150. if err != nil {
  151. r.Err = err
  152. return
  153. }
  154. _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
  155. OkCodes: []int{200},
  156. })
  157. return
  158. }
  159. // CreateRuleOpts is the common options struct used in this package's CreateRule
  160. // operation.
  161. type CreateRuleOpts struct {
  162. // The L7 rule type. One of COOKIE, FILE_TYPE, HEADER, HOST_NAME, or PATH.
  163. RuleType RuleType `json:"type" required:"true"`
  164. // The comparison type for the L7 rule. One of CONTAINS, ENDS_WITH, EQUAL_TO, REGEX, or STARTS_WITH.
  165. CompareType CompareType `json:"compare_type" required:"true"`
  166. // The value to use for the comparison. For example, the file type to compare.
  167. Value string `json:"value" required:"true"`
  168. // ProjectID is the UUID of the project who owns the rule in octavia.
  169. // Only administrative users can specify a project UUID other than their own.
  170. ProjectID string `json:"project_id,omitempty"`
  171. // The key to use for the comparison. For example, the name of the cookie to evaluate.
  172. Key string `json:"key,omitempty"`
  173. // When true the logic of the rule is inverted. For example, with invert true,
  174. // equal to would become not equal to. Default is false.
  175. Invert bool `json:"invert,omitempty"`
  176. }
  177. // ToRuleCreateMap builds a request body from CreateRuleOpts.
  178. func (opts CreateRuleOpts) ToRuleCreateMap() (map[string]interface{}, error) {
  179. return gophercloud.BuildRequestBody(opts, "rule")
  180. }
  181. // CreateRule will create and associate a Rule with a particular L7Policy.
  182. func CreateRule(c *gophercloud.ServiceClient, policyID string, opts CreateRuleOpts) (r CreateRuleResult) {
  183. b, err := opts.ToRuleCreateMap()
  184. if err != nil {
  185. r.Err = err
  186. return
  187. }
  188. _, r.Err = c.Post(ruleRootURL(c, policyID), b, &r.Body, nil)
  189. return
  190. }
  191. // ListRulesOptsBuilder allows extensions to add additional parameters to the
  192. // ListRules request.
  193. type ListRulesOptsBuilder interface {
  194. ToRulesListQuery() (string, error)
  195. }
  196. // ListRulesOpts allows the filtering and sorting of paginated collections
  197. // through the API.
  198. type ListRulesOpts struct {
  199. RuleType RuleType `q:"type"`
  200. ProjectID string `q:"project_id"`
  201. ID string `q:"id"`
  202. Limit int `q:"limit"`
  203. Marker string `q:"marker"`
  204. SortKey string `q:"sort_key"`
  205. SortDir string `q:"sort_dir"`
  206. }
  207. // ToRulesListQuery formats a ListOpts into a query string.
  208. func (opts ListRulesOpts) ToRulesListQuery() (string, error) {
  209. q, err := gophercloud.BuildQueryString(opts)
  210. return q.String(), err
  211. }
  212. // ListRules returns a Pager which allows you to iterate over a collection of
  213. // rules. It accepts a ListRulesOptsBuilder, which allows you to filter and
  214. // sort the returned collection for greater efficiency.
  215. //
  216. // Default policy settings return only those rules that are owned by the
  217. // project who submits the request, unless an admin user submits the request.
  218. func ListRules(c *gophercloud.ServiceClient, policyID string, opts ListRulesOptsBuilder) pagination.Pager {
  219. url := ruleRootURL(c, policyID)
  220. if opts != nil {
  221. query, err := opts.ToRulesListQuery()
  222. if err != nil {
  223. return pagination.Pager{Err: err}
  224. }
  225. url += query
  226. }
  227. return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
  228. return RulePage{pagination.LinkedPageBase{PageResult: r}}
  229. })
  230. }
  231. // GetRule retrieves a particular L7Policy Rule based on its unique ID.
  232. func GetRule(c *gophercloud.ServiceClient, policyID string, ruleID string) (r GetRuleResult) {
  233. _, r.Err = c.Get(ruleResourceURL(c, policyID, ruleID), &r.Body, nil)
  234. return
  235. }
  236. // DeleteRule will remove a Rule from a particular L7Policy.
  237. func DeleteRule(c *gophercloud.ServiceClient, policyID string, ruleID string) (r DeleteRuleResult) {
  238. _, r.Err = c.Delete(ruleResourceURL(c, policyID, ruleID), nil)
  239. return
  240. }
  241. // UpdateRuleOptsBuilder allows to add additional parameters to the PUT request.
  242. type UpdateRuleOptsBuilder interface {
  243. ToRuleUpdateMap() (map[string]interface{}, error)
  244. }
  245. // UpdateRuleOpts is the common options struct used in this package's Update
  246. // operation.
  247. type UpdateRuleOpts struct {
  248. // The L7 rule type. One of COOKIE, FILE_TYPE, HEADER, HOST_NAME, or PATH.
  249. RuleType RuleType `json:"type,omitempty"`
  250. // The comparison type for the L7 rule. One of CONTAINS, ENDS_WITH, EQUAL_TO, REGEX, or STARTS_WITH.
  251. CompareType CompareType `json:"compare_type,omitempty"`
  252. // The value to use for the comparison. For example, the file type to compare.
  253. Value string `json:"value,omitempty"`
  254. // The key to use for the comparison. For example, the name of the cookie to evaluate.
  255. Key string `json:"key,omitempty"`
  256. // When true the logic of the rule is inverted. For example, with invert true,
  257. // equal to would become not equal to. Default is false.
  258. Invert bool `json:"invert,omitempty"`
  259. }
  260. // ToRuleUpdateMap builds a request body from UpdateRuleOpts.
  261. func (opts UpdateRuleOpts) ToRuleUpdateMap() (map[string]interface{}, error) {
  262. return gophercloud.BuildRequestBody(opts, "rule")
  263. }
  264. // UpdateRule allows Rule to be updated.
  265. func UpdateRule(c *gophercloud.ServiceClient, policyID string, ruleID string, opts UpdateRuleOptsBuilder) (r UpdateRuleResult) {
  266. b, err := opts.ToRuleUpdateMap()
  267. if err != nil {
  268. r.Err = err
  269. return
  270. }
  271. _, r.Err = c.Put(ruleResourceURL(c, policyID, ruleID), b, &r.Body, &gophercloud.RequestOpts{
  272. OkCodes: []int{200, 201, 202},
  273. })
  274. return
  275. }