1package vegadns2client
2
3import (
4	"encoding/json"
5	"errors"
6	"fmt"
7	"io/ioutil"
8	"net/http"
9	"strings"
10)
11
12// Record - struct representing a Record object
13type Record struct {
14	Name       string `json:"name"`
15	Value      string `json:"value"`
16	RecordType string `json:"record_type"`
17	TTL        int    `json:"ttl"`
18	RecordID   int    `json:"record_id"`
19	LocationID string `json:"location_id"`
20	DomainID   int    `json:"domain_id"`
21}
22
23// RecordsResponse - api response list of records
24type RecordsResponse struct {
25	Status  string   `json:"status"`
26	Total   int      `json:"total_records"`
27	Domain  Domain   `json:"domain"`
28	Records []Record `json:"records"`
29}
30
31// GetRecordID - helper to get the id of a record
32// Input: domainID, record, recordType
33// Output: int
34func (vega *VegaDNSClient) GetRecordID(domainID int, record string, recordType string) (int, error) {
35	params := make(map[string]string)
36	params["domain_id"] = fmt.Sprintf("%d", domainID)
37
38	resp, err := vega.Send("GET", "records", params)
39
40	if err != nil {
41		return -1, fmt.Errorf("Error sending GET to GetRecordID: %s", err)
42	}
43	defer resp.Body.Close()
44	body, err := ioutil.ReadAll(resp.Body)
45	if err != nil {
46		return -1, fmt.Errorf("Error reading response from GetRecordID: %s", err)
47	}
48	if resp.StatusCode != http.StatusOK {
49		return -1, fmt.Errorf("Got bad answer from VegaDNS on GetRecordID. Code: %d. Message: %s", resp.StatusCode, string(body))
50	}
51
52	answer := RecordsResponse{}
53	if err := json.Unmarshal(body, &answer); err != nil {
54		return -1, fmt.Errorf("Error unmarshalling body from GetRecordID: %s", err)
55	}
56
57	for _, r := range answer.Records {
58		if r.Name == record && r.RecordType == recordType {
59			return r.RecordID, nil
60		}
61	}
62
63	return -1, errors.New("Couldnt find record")
64}
65
66// CreateTXT - Creates a TXT record
67// Input: domainID, fqdn, value, ttl
68// Output: nil or error
69func (vega *VegaDNSClient) CreateTXT(domainID int, fqdn string, value string, ttl int) error {
70	params := make(map[string]string)
71
72	params["record_type"] = "TXT"
73	params["ttl"] = fmt.Sprintf("%d", ttl)
74	params["domain_id"] = fmt.Sprintf("%d", domainID)
75	params["name"] = strings.TrimSuffix(fqdn, ".")
76	params["value"] = value
77
78	resp, err := vega.Send("POST", "records", params)
79
80	if err != nil {
81		return fmt.Errorf("Send POST error in CreateTXT: %s", err)
82	}
83	defer resp.Body.Close()
84	body, err := ioutil.ReadAll(resp.Body)
85	if err != nil {
86		return fmt.Errorf("Error reading POST response in CreateTXT: %s", err)
87	}
88	if resp.StatusCode != http.StatusCreated {
89		return fmt.Errorf("Got bad answer from VegaDNS on CreateTXT. Code: %d. Message: %s", resp.StatusCode, string(body))
90	}
91
92	return nil
93}
94
95// DeleteRecord - deletes a record by id
96// Input: recordID
97// Output: nil or error
98func (vega *VegaDNSClient) DeleteRecord(recordID int) error {
99	resp, err := vega.Send("DELETE", fmt.Sprintf("records/%d", recordID), nil)
100	if err != nil {
101		return fmt.Errorf("Send DELETE error in DeleteTXT: %s", err)
102	}
103	defer resp.Body.Close()
104	body, err := ioutil.ReadAll(resp.Body)
105	if err != nil {
106		return fmt.Errorf("Error reading DELETE response in DeleteTXT: %s", err)
107	}
108	if resp.StatusCode != http.StatusOK {
109		return fmt.Errorf("Got bad answer from VegaDNS on DeleteTXT. Code: %d. Message: %s", resp.StatusCode, string(body))
110	}
111
112	return nil
113}
114