custom_hostname.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package cloudflare
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "strconv"
  6. "github.com/pkg/errors"
  7. )
  8. // CustomHostnameSSLSettings represents the SSL settings for a custom hostname.
  9. type CustomHostnameSSLSettings struct {
  10. HTTP2 string `json:"http2,omitempty"`
  11. TLS13 string `json:"tls_1_3,omitempty"`
  12. MinTLSVersion string `json:"min_tls_version,omitempty"`
  13. Ciphers []string `json:"ciphers,omitempty"`
  14. }
  15. // CustomHostnameSSL represents the SSL section in a given custom hostname.
  16. type CustomHostnameSSL struct {
  17. Status string `json:"status,omitempty"`
  18. Method string `json:"method,omitempty"`
  19. Type string `json:"type,omitempty"`
  20. CnameTarget string `json:"cname_target,omitempty"`
  21. CnameName string `json:"cname,omitempty"`
  22. Settings CustomHostnameSSLSettings `json:"settings,omitempty"`
  23. }
  24. // CustomMetadata defines custom metadata for the hostname. This requires logic to be implemented by Cloudflare to act on the data provided.
  25. type CustomMetadata map[string]interface{}
  26. // CustomHostname represents a custom hostname in a zone.
  27. type CustomHostname struct {
  28. ID string `json:"id,omitempty"`
  29. Hostname string `json:"hostname,omitempty"`
  30. CustomOriginServer string `json:"custom_origin_server,omitempty"`
  31. SSL CustomHostnameSSL `json:"ssl,omitempty"`
  32. CustomMetadata CustomMetadata `json:"custom_metadata,omitempty"`
  33. }
  34. // CustomHostnameResponse represents a response from the Custom Hostnames endpoints.
  35. type CustomHostnameResponse struct {
  36. Result CustomHostname `json:"result"`
  37. Response
  38. }
  39. // CustomHostnameListResponse represents a response from the Custom Hostnames endpoints.
  40. type CustomHostnameListResponse struct {
  41. Result []CustomHostname `json:"result"`
  42. Response
  43. ResultInfo `json:"result_info"`
  44. }
  45. // UpdateCustomHostnameSSL modifies SSL configuration for the given custom
  46. // hostname in the given zone.
  47. //
  48. // API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-update-custom-hostname-configuration
  49. func (api *API) UpdateCustomHostnameSSL(zoneID string, customHostnameID string, ssl CustomHostnameSSL) (CustomHostname, error) {
  50. return CustomHostname{}, errors.New("Not implemented")
  51. }
  52. // DeleteCustomHostname deletes a custom hostname (and any issued SSL
  53. // certificates).
  54. //
  55. // API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-delete-a-custom-hostname-and-any-issued-ssl-certificates-
  56. func (api *API) DeleteCustomHostname(zoneID string, customHostnameID string) error {
  57. uri := "/zones/" + zoneID + "/custom_hostnames/" + customHostnameID
  58. res, err := api.makeRequest("DELETE", uri, nil)
  59. if err != nil {
  60. return errors.Wrap(err, errMakeRequestError)
  61. }
  62. var response *CustomHostnameResponse
  63. err = json.Unmarshal(res, &response)
  64. if err != nil {
  65. return errors.Wrap(err, errUnmarshalError)
  66. }
  67. return nil
  68. }
  69. // CreateCustomHostname creates a new custom hostname and requests that an SSL certificate be issued for it.
  70. //
  71. // API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-create-custom-hostname
  72. func (api *API) CreateCustomHostname(zoneID string, ch CustomHostname) (*CustomHostnameResponse, error) {
  73. uri := "/zones/" + zoneID + "/custom_hostnames"
  74. res, err := api.makeRequest("POST", uri, ch)
  75. if err != nil {
  76. return nil, errors.Wrap(err, errMakeRequestError)
  77. }
  78. var response *CustomHostnameResponse
  79. err = json.Unmarshal(res, &response)
  80. if err != nil {
  81. return nil, errors.Wrap(err, errUnmarshalError)
  82. }
  83. return response, nil
  84. }
  85. // CustomHostnames fetches custom hostnames for the given zone,
  86. // by applying filter.Hostname if not empty and scoping the result to page'th 50 items.
  87. //
  88. // The returned ResultInfo can be used to implement pagination.
  89. //
  90. // API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-list-custom-hostnames
  91. func (api *API) CustomHostnames(zoneID string, page int, filter CustomHostname) ([]CustomHostname, ResultInfo, error) {
  92. v := url.Values{}
  93. v.Set("per_page", "50")
  94. v.Set("page", strconv.Itoa(page))
  95. if filter.Hostname != "" {
  96. v.Set("hostname", filter.Hostname)
  97. }
  98. query := "?" + v.Encode()
  99. uri := "/zones/" + zoneID + "/custom_hostnames" + query
  100. res, err := api.makeRequest("GET", uri, nil)
  101. if err != nil {
  102. return []CustomHostname{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
  103. }
  104. var customHostnameListResponse CustomHostnameListResponse
  105. err = json.Unmarshal(res, &customHostnameListResponse)
  106. if err != nil {
  107. return []CustomHostname{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
  108. }
  109. return customHostnameListResponse.Result, customHostnameListResponse.ResultInfo, nil
  110. }
  111. // CustomHostname inspects the given custom hostname in the given zone.
  112. //
  113. // API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-custom-hostname-configuration-details
  114. func (api *API) CustomHostname(zoneID string, customHostnameID string) (CustomHostname, error) {
  115. uri := "/zones/" + zoneID + "/custom_hostnames/" + customHostnameID
  116. res, err := api.makeRequest("GET", uri, nil)
  117. if err != nil {
  118. return CustomHostname{}, errors.Wrap(err, errMakeRequestError)
  119. }
  120. var response CustomHostnameResponse
  121. err = json.Unmarshal(res, &response)
  122. if err != nil {
  123. return CustomHostname{}, errors.Wrap(err, errUnmarshalError)
  124. }
  125. return response.Result, nil
  126. }
  127. // CustomHostnameIDByName retrieves the ID for the given hostname in the given zone.
  128. func (api *API) CustomHostnameIDByName(zoneID string, hostname string) (string, error) {
  129. customHostnames, _, err := api.CustomHostnames(zoneID, 1, CustomHostname{Hostname: hostname})
  130. if err != nil {
  131. return "", errors.Wrap(err, "CustomHostnames command failed")
  132. }
  133. for _, ch := range customHostnames {
  134. if ch.Hostname == hostname {
  135. return ch.ID, nil
  136. }
  137. }
  138. return "", errors.New("CustomHostname could not be found")
  139. }