bind.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package bind
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "time"
  10. )
  11. // Client contains an API client for a Bind DNS server wrapped
  12. // with a lightweight API
  13. type Client struct {
  14. apiKey string
  15. serverURL string
  16. httpClient *http.Client
  17. }
  18. // NewClient creates a new bind API client
  19. func NewClient(serverURL, apiKey string) *Client {
  20. httpClient := &http.Client{
  21. Timeout: time.Minute,
  22. }
  23. return &Client{apiKey, serverURL, httpClient}
  24. }
  25. // RecordData represents the data required to create or delete an A/CNAME record
  26. // for the nameserver
  27. type RecordData struct {
  28. Value string `json:"value"`
  29. Type string `json:"type"`
  30. Hostname string `json:"hostname"`
  31. }
  32. // CreateCNAMERecord creates a new CNAME record for the nameserver
  33. func (c *Client) CreateCNAMERecord(value, hostname string) error {
  34. return c.sendRequest("POST", &RecordData{
  35. Value: value,
  36. Type: "CNAME",
  37. Hostname: hostname,
  38. })
  39. }
  40. // CreateARecord creates a new A record for the nameserver
  41. func (c *Client) CreateARecord(value, hostname string) error {
  42. return c.sendRequest("POST", &RecordData{
  43. Value: value,
  44. Type: "A",
  45. Hostname: hostname,
  46. })
  47. }
  48. // DeleteARecord deletes a new A record for the nameserver
  49. func (c *Client) DeleteARecord(value, hostname string) error {
  50. return c.sendRequest("DELETE", &RecordData{
  51. Value: value,
  52. Type: "A",
  53. Hostname: hostname,
  54. })
  55. }
  56. func (c *Client) sendRequest(method string, data *RecordData) error {
  57. reqURL, err := url.Parse(c.serverURL)
  58. if err != nil {
  59. return nil
  60. }
  61. reqURL.Path = "/dns"
  62. strData, err := json.Marshal(data)
  63. if err != nil {
  64. return err
  65. }
  66. req, err := http.NewRequest(
  67. method,
  68. reqURL.String(),
  69. strings.NewReader(string(strData)),
  70. )
  71. if err != nil {
  72. return err
  73. }
  74. req.Header.Set("Content-Type", "application/json; charset=utf-8")
  75. req.Header.Set("Accept", "application/json; charset=utf-8")
  76. req.Header.Set("X-Api-Key", c.apiKey)
  77. res, err := c.httpClient.Do(req)
  78. if err != nil {
  79. return err
  80. }
  81. defer res.Body.Close()
  82. if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
  83. resBytes, err := ioutil.ReadAll(res.Body)
  84. if err != nil {
  85. return fmt.Errorf("request failed with status code %d, but could not read body (%s)\n", res.StatusCode, err.Error())
  86. }
  87. return fmt.Errorf("request failed with status code %d: %s\n", res.StatusCode, string(resBytes))
  88. }
  89. return nil
  90. }