page_rules.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package cloudflare
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/pkg/errors"
  6. )
  7. // PageRuleTarget is the target to evaluate on a request.
  8. //
  9. // Currently Target must always be "url" and Operator must be "matches". Value
  10. // is the URL pattern to match against.
  11. type PageRuleTarget struct {
  12. Target string `json:"target"`
  13. Constraint struct {
  14. Operator string `json:"operator"`
  15. Value string `json:"value"`
  16. } `json:"constraint"`
  17. }
  18. /*
  19. PageRuleAction is the action to take when the target is matched.
  20. Valid IDs are:
  21. always_online
  22. always_use_https
  23. automatic_https_rewrites
  24. browser_cache_ttl
  25. browser_check
  26. bypass_cache_on_cookie
  27. cache_by_device_type
  28. cache_deception_armor
  29. cache_level
  30. cache_on_cookie
  31. disable_apps
  32. disable_performance
  33. disable_railgun
  34. disable_security
  35. edge_cache_ttl
  36. email_obfuscation
  37. explicit_cache_control
  38. forwarding_url
  39. host_header_override
  40. ip_geolocation
  41. minify
  42. mirage
  43. opportunistic_encryption
  44. origin_error_page_pass_thru
  45. polish
  46. resolve_override
  47. respect_strong_etag
  48. response_buffering
  49. rocket_loader
  50. security_level
  51. server_side_exclude
  52. sort_query_string_for_cache
  53. ssl
  54. true_client_ip_header
  55. waf
  56. */
  57. type PageRuleAction struct {
  58. ID string `json:"id"`
  59. Value interface{} `json:"value"`
  60. }
  61. // PageRuleActions maps API action IDs to human-readable strings.
  62. var PageRuleActions = map[string]string{
  63. "always_online": "Always Online", // Value of type string
  64. "always_use_https": "Always Use HTTPS", // Value of type interface{}
  65. "automatic_https_rewrites": "Automatic HTTPS Rewrites", // Value of type string
  66. "browser_cache_ttl": "Browser Cache TTL", // Value of type int
  67. "browser_check": "Browser Integrity Check", // Value of type string
  68. "bypass_cache_on_cookie": "Bypass Cache on Cookie", // Value of type string
  69. "cache_by_device_type": "Cache By Device Type", // Value of type string
  70. "cache_deception_armor": "Cache Deception Armor", // Value of type string
  71. "cache_level": "Cache Level", // Value of type string
  72. "cache_on_cookie": "Cache On Cookie", // Value of type string
  73. "disable_apps": "Disable Apps", // Value of type interface{}
  74. "disable_performance": "Disable Performance", // Value of type interface{}
  75. "disable_railgun": "Disable Railgun", // Value of type string
  76. "disable_security": "Disable Security", // Value of type interface{}
  77. "edge_cache_ttl": "Edge Cache TTL", // Value of type int
  78. "email_obfuscation": "Email Obfuscation", // Value of type string
  79. "explicit_cache_control": "Origin Cache Control", // Value of type string
  80. "forwarding_url": "Forwarding URL", // Value of type map[string]interface
  81. "host_header_override": "Host Header Override", // Value of type string
  82. "ip_geolocation": "IP Geolocation Header", // Value of type string
  83. "minify": "Minify", // Value of type map[string]interface
  84. "mirage": "Mirage", // Value of type string
  85. "opportunistic_encryption": "Opportunistic Encryption", // Value of type string
  86. "origin_error_page_pass_thru": "Origin Error Page Pass-thru", // Value of type string
  87. "polish": "Polish", // Value of type string
  88. "resolve_override": "Resolve Override", // Value of type string
  89. "respect_strong_etag": "Respect Strong ETags", // Value of type string
  90. "response_buffering": "Response Buffering", // Value of type string
  91. "rocket_loader": "Rocker Loader", // Value of type string
  92. "security_level": "Security Level", // Value of type string
  93. "server_side_exclude": "Server Side Excludes", // Value of type string
  94. "sort_query_string_for_cache": "Query String Sort", // Value of type string
  95. "ssl": "SSL", // Value of type string
  96. "true_client_ip_header": "True Client IP Header", // Value of type string
  97. "waf": "Web Application Firewall", // Value of type string
  98. }
  99. // PageRule describes a Page Rule.
  100. type PageRule struct {
  101. ID string `json:"id,omitempty"`
  102. Targets []PageRuleTarget `json:"targets"`
  103. Actions []PageRuleAction `json:"actions"`
  104. Priority int `json:"priority"`
  105. Status string `json:"status"` // can be: active, paused
  106. ModifiedOn time.Time `json:"modified_on,omitempty"`
  107. CreatedOn time.Time `json:"created_on,omitempty"`
  108. }
  109. // PageRuleDetailResponse is the API response, containing a single PageRule.
  110. type PageRuleDetailResponse struct {
  111. Success bool `json:"success"`
  112. Errors []string `json:"errors"`
  113. Messages []string `json:"messages"`
  114. Result PageRule `json:"result"`
  115. }
  116. // PageRulesResponse is the API response, containing an array of PageRules.
  117. type PageRulesResponse struct {
  118. Success bool `json:"success"`
  119. Errors []string `json:"errors"`
  120. Messages []string `json:"messages"`
  121. Result []PageRule `json:"result"`
  122. }
  123. // CreatePageRule creates a new Page Rule for a zone.
  124. //
  125. // API reference: https://api.cloudflare.com/#page-rules-for-a-zone-create-a-page-rule
  126. func (api *API) CreatePageRule(zoneID string, rule PageRule) (*PageRule, error) {
  127. uri := "/zones/" + zoneID + "/pagerules"
  128. res, err := api.makeRequest("POST", uri, rule)
  129. if err != nil {
  130. return nil, errors.Wrap(err, errMakeRequestError)
  131. }
  132. var r PageRuleDetailResponse
  133. err = json.Unmarshal(res, &r)
  134. if err != nil {
  135. return nil, errors.Wrap(err, errUnmarshalError)
  136. }
  137. return &r.Result, nil
  138. }
  139. // ListPageRules returns all Page Rules for a zone.
  140. //
  141. // API reference: https://api.cloudflare.com/#page-rules-for-a-zone-list-page-rules
  142. func (api *API) ListPageRules(zoneID string) ([]PageRule, error) {
  143. uri := "/zones/" + zoneID + "/pagerules"
  144. res, err := api.makeRequest("GET", uri, nil)
  145. if err != nil {
  146. return []PageRule{}, errors.Wrap(err, errMakeRequestError)
  147. }
  148. var r PageRulesResponse
  149. err = json.Unmarshal(res, &r)
  150. if err != nil {
  151. return []PageRule{}, errors.Wrap(err, errUnmarshalError)
  152. }
  153. return r.Result, nil
  154. }
  155. // PageRule fetches detail about one Page Rule for a zone.
  156. //
  157. // API reference: https://api.cloudflare.com/#page-rules-for-a-zone-page-rule-details
  158. func (api *API) PageRule(zoneID, ruleID string) (PageRule, error) {
  159. uri := "/zones/" + zoneID + "/pagerules/" + ruleID
  160. res, err := api.makeRequest("GET", uri, nil)
  161. if err != nil {
  162. return PageRule{}, errors.Wrap(err, errMakeRequestError)
  163. }
  164. var r PageRuleDetailResponse
  165. err = json.Unmarshal(res, &r)
  166. if err != nil {
  167. return PageRule{}, errors.Wrap(err, errUnmarshalError)
  168. }
  169. return r.Result, nil
  170. }
  171. // ChangePageRule lets you change individual settings for a Page Rule. This is
  172. // in contrast to UpdatePageRule which replaces the entire Page Rule.
  173. //
  174. // API reference: https://api.cloudflare.com/#page-rules-for-a-zone-change-a-page-rule
  175. func (api *API) ChangePageRule(zoneID, ruleID string, rule PageRule) error {
  176. uri := "/zones/" + zoneID + "/pagerules/" + ruleID
  177. res, err := api.makeRequest("PATCH", uri, rule)
  178. if err != nil {
  179. return errors.Wrap(err, errMakeRequestError)
  180. }
  181. var r PageRuleDetailResponse
  182. err = json.Unmarshal(res, &r)
  183. if err != nil {
  184. return errors.Wrap(err, errUnmarshalError)
  185. }
  186. return nil
  187. }
  188. // UpdatePageRule lets you replace a Page Rule. This is in contrast to
  189. // ChangePageRule which lets you change individual settings.
  190. //
  191. // API reference: https://api.cloudflare.com/#page-rules-for-a-zone-update-a-page-rule
  192. func (api *API) UpdatePageRule(zoneID, ruleID string, rule PageRule) error {
  193. uri := "/zones/" + zoneID + "/pagerules/" + ruleID
  194. res, err := api.makeRequest("PUT", uri, rule)
  195. if err != nil {
  196. return errors.Wrap(err, errMakeRequestError)
  197. }
  198. var r PageRuleDetailResponse
  199. err = json.Unmarshal(res, &r)
  200. if err != nil {
  201. return errors.Wrap(err, errUnmarshalError)
  202. }
  203. return nil
  204. }
  205. // DeletePageRule deletes a Page Rule for a zone.
  206. //
  207. // API reference: https://api.cloudflare.com/#page-rules-for-a-zone-delete-a-page-rule
  208. func (api *API) DeletePageRule(zoneID, ruleID string) error {
  209. uri := "/zones/" + zoneID + "/pagerules/" + ruleID
  210. res, err := api.makeRequest("DELETE", uri, nil)
  211. if err != nil {
  212. return errors.Wrap(err, errMakeRequestError)
  213. }
  214. var r PageRuleDetailResponse
  215. err = json.Unmarshal(res, &r)
  216. if err != nil {
  217. return errors.Wrap(err, errUnmarshalError)
  218. }
  219. return nil
  220. }