|
| 1 | +package cloudflare |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/pkg/errors" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const baseuri = "https://api.cloudflare.com/client/v4/" |
| 14 | + |
| 15 | +type listRecordSResponse struct { |
| 16 | + Success bool `json:"success"` |
| 17 | + Errors []string `json:"errors"` |
| 18 | + Messages []string `json:"messages"` |
| 19 | + Result []struct { |
| 20 | + ID string `json:"id"` |
| 21 | + Name string `json:"name"` |
| 22 | + Content string `json:"content"` |
| 23 | + TTL int `json:"ttl"` |
| 24 | + } `json:"result"` |
| 25 | +} |
| 26 | + |
| 27 | +//GetRecordDetails Gets record's id and ttl |
| 28 | +func GetRecordDetails(email, apikey, zonedID, name string) (string, string, int, error) { |
| 29 | + client := &http.Client{Timeout: time.Second * 2} |
| 30 | + req, err := http.NewRequest("GET", fmt.Sprintf("%szones/%s/dns_records", baseuri, zonedID), nil) |
| 31 | + if err != nil { |
| 32 | + return "", "", 0, errors.Wrap(err, "Error creating GET request") |
| 33 | + } |
| 34 | + req.Header.Set("X-Auth-Email", email) |
| 35 | + req.Header.Set("X-Auth-Key", apikey) |
| 36 | + |
| 37 | + res, err := client.Do(req) |
| 38 | + if err != nil { |
| 39 | + return "", "", 0, errors.Wrap(err, "Error doing request to Cloudflare") |
| 40 | + } |
| 41 | + defer res.Body.Close() |
| 42 | + |
| 43 | + body, err := ioutil.ReadAll(res.Body) |
| 44 | + if err != nil { |
| 45 | + return "", "", 0, errors.Wrap(err, "Error reading response body") |
| 46 | + } |
| 47 | + |
| 48 | + var r listRecordSResponse |
| 49 | + json.Unmarshal(body, &r) |
| 50 | + if !r.Success { |
| 51 | + return "", "", 0, fmt.Errorf("Cloudflare Errors: %+v - Cloudflare Messages: %+v", r.Errors, r.Messages) |
| 52 | + } |
| 53 | + var id, content string |
| 54 | + var ttl int |
| 55 | + for _, record := range r.Result { |
| 56 | + if record.Name == name { |
| 57 | + id = record.ID |
| 58 | + content = record.Content |
| 59 | + ttl = record.TTL |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return id, content, ttl, nil |
| 65 | +} |
| 66 | + |
| 67 | +//SetRecord sets record |
| 68 | +func SetRecord(email, apikey, zonedID, recordID, recordType, name, content string, ttl int) error { |
| 69 | + client := &http.Client{Timeout: time.Second * 2} |
| 70 | + |
| 71 | + data := fmt.Sprintf(`{"type":"%s","name":"%s","content":"%s","ttl":%d}`, recordType, name, content, ttl) |
| 72 | + req, err := http.NewRequest("PUT", fmt.Sprintf("%szones/%s/dns_records/%s", baseuri, zonedID, recordID), strings.NewReader(data)) |
| 73 | + if err != nil { |
| 74 | + return errors.Wrap(err, "Error creating PUT request") |
| 75 | + } |
| 76 | + |
| 77 | + req.ContentLength = int64(len(data)) |
| 78 | + req.Header.Set("X-Auth-Email", email) |
| 79 | + req.Header.Set("X-Auth-Key", apikey) |
| 80 | + req.Header.Set("Content-Type", "application/json") |
| 81 | + res, err := client.Do(req) |
| 82 | + if err != nil { |
| 83 | + return errors.Wrap(err, "Error doing request to Cloudflare") |
| 84 | + } |
| 85 | + defer res.Body.Close() |
| 86 | + |
| 87 | + body, err := ioutil.ReadAll(res.Body) |
| 88 | + if err != nil { |
| 89 | + return errors.Wrap(err, "Error reading response body") |
| 90 | + } |
| 91 | + |
| 92 | + var resMap map[string]interface{} |
| 93 | + json.Unmarshal(body, &resMap) |
| 94 | + if !resMap["success"].(bool) { |
| 95 | + return fmt.Errorf("Cloudflare Errors: %+v - Cloudflare Messages: %+v", resMap["errors"], resMap["messages"]) |
| 96 | + } |
| 97 | + return nil |
| 98 | +} |
0 commit comments