custom_pages.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package cloudflare
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/pkg/errors"
  7. )
  8. // CustomPage represents a custom page configuration.
  9. type CustomPage struct {
  10. CreatedOn time.Time `json:"created_on"`
  11. ModifiedOn time.Time `json:"modified_on"`
  12. URL interface{} `json:"url"`
  13. State string `json:"state"`
  14. RequiredTokens []string `json:"required_tokens"`
  15. PreviewTarget string `json:"preview_target"`
  16. Description string `json:"description"`
  17. ID string `json:"id"`
  18. }
  19. // CustomPageResponse represents the response from the custom pages endpoint.
  20. type CustomPageResponse struct {
  21. Response
  22. Result []CustomPage `json:"result"`
  23. }
  24. // CustomPageDetailResponse represents the response from the custom page endpoint.
  25. type CustomPageDetailResponse struct {
  26. Response
  27. Result CustomPage `json:"result"`
  28. }
  29. // CustomPageOptions is used to determine whether or not the operation
  30. // should take place on an account or zone level based on which is
  31. // provided to the function.
  32. //
  33. // A non-empty value denotes desired use.
  34. type CustomPageOptions struct {
  35. AccountID string
  36. ZoneID string
  37. }
  38. // CustomPageParameters is used to update a particular custom page with
  39. // the values provided.
  40. type CustomPageParameters struct {
  41. URL interface{} `json:"url"`
  42. State string `json:"state"`
  43. }
  44. // CustomPages lists custom pages for a zone or account.
  45. //
  46. // Zone API reference: https://api.cloudflare.com/#custom-pages-for-a-zone-list-available-custom-pages
  47. // Account API reference: https://api.cloudflare.com/#custom-pages-account--list-custom-pages
  48. func (api *API) CustomPages(options *CustomPageOptions) ([]CustomPage, error) {
  49. var (
  50. pageType, identifier string
  51. )
  52. if options.AccountID == "" && options.ZoneID == "" {
  53. return nil, errors.New("either account ID or zone ID must be provided")
  54. }
  55. if options.AccountID != "" && options.ZoneID != "" {
  56. return nil, errors.New("account ID and zone ID are mutually exclusive")
  57. }
  58. // Should the account ID be defined, treat this as an account level operation.
  59. if options.AccountID != "" {
  60. pageType = "accounts"
  61. identifier = options.AccountID
  62. } else {
  63. pageType = "zones"
  64. identifier = options.ZoneID
  65. }
  66. uri := fmt.Sprintf("/%s/%s/custom_pages", pageType, identifier)
  67. res, err := api.makeRequest("GET", uri, nil)
  68. if err != nil {
  69. return nil, errors.Wrap(err, errMakeRequestError)
  70. }
  71. var customPageResponse CustomPageResponse
  72. err = json.Unmarshal(res, &customPageResponse)
  73. if err != nil {
  74. return nil, errors.Wrap(err, errUnmarshalError)
  75. }
  76. return customPageResponse.Result, nil
  77. }
  78. // CustomPage lists a single custom page based on the ID.
  79. //
  80. // Zone API reference: https://api.cloudflare.com/#custom-pages-for-a-zone-custom-page-details
  81. // Account API reference: https://api.cloudflare.com/#custom-pages-account--custom-page-details
  82. func (api *API) CustomPage(options *CustomPageOptions, customPageID string) (CustomPage, error) {
  83. var (
  84. pageType, identifier string
  85. )
  86. if options.AccountID == "" && options.ZoneID == "" {
  87. return CustomPage{}, errors.New("either account ID or zone ID must be provided")
  88. }
  89. if options.AccountID != "" && options.ZoneID != "" {
  90. return CustomPage{}, errors.New("account ID and zone ID are mutually exclusive")
  91. }
  92. // Should the account ID be defined, treat this as an account level operation.
  93. if options.AccountID != "" {
  94. pageType = "accounts"
  95. identifier = options.AccountID
  96. } else {
  97. pageType = "zones"
  98. identifier = options.ZoneID
  99. }
  100. uri := fmt.Sprintf("/%s/%s/custom_pages/%s", pageType, identifier, customPageID)
  101. res, err := api.makeRequest("GET", uri, nil)
  102. if err != nil {
  103. return CustomPage{}, errors.Wrap(err, errMakeRequestError)
  104. }
  105. var customPageResponse CustomPageDetailResponse
  106. err = json.Unmarshal(res, &customPageResponse)
  107. if err != nil {
  108. return CustomPage{}, errors.Wrap(err, errUnmarshalError)
  109. }
  110. return customPageResponse.Result, nil
  111. }
  112. // UpdateCustomPage updates a single custom page setting.
  113. //
  114. // Zone API reference: https://api.cloudflare.com/#custom-pages-for-a-zone-update-custom-page-url
  115. // Account API reference: https://api.cloudflare.com/#custom-pages-account--update-custom-page
  116. func (api *API) UpdateCustomPage(options *CustomPageOptions, customPageID string, pageParameters CustomPageParameters) (CustomPage, error) {
  117. var (
  118. pageType, identifier string
  119. )
  120. if options.AccountID == "" && options.ZoneID == "" {
  121. return CustomPage{}, errors.New("either account ID or zone ID must be provided")
  122. }
  123. if options.AccountID != "" && options.ZoneID != "" {
  124. return CustomPage{}, errors.New("account ID and zone ID are mutually exclusive")
  125. }
  126. // Should the account ID be defined, treat this as an account level operation.
  127. if options.AccountID != "" {
  128. pageType = "accounts"
  129. identifier = options.AccountID
  130. } else {
  131. pageType = "zones"
  132. identifier = options.ZoneID
  133. }
  134. uri := fmt.Sprintf("/%s/%s/custom_pages/%s", pageType, identifier, customPageID)
  135. res, err := api.makeRequest("PUT", uri, pageParameters)
  136. if err != nil {
  137. return CustomPage{}, errors.Wrap(err, errMakeRequestError)
  138. }
  139. var customPageResponse CustomPageDetailResponse
  140. err = json.Unmarshal(res, &customPageResponse)
  141. if err != nil {
  142. return CustomPage{}, errors.Wrap(err, errUnmarshalError)
  143. }
  144. return customPageResponse.Result, nil
  145. }