-
Notifications
You must be signed in to change notification settings - Fork 7
/
record.go
150 lines (135 loc) · 5.04 KB
/
record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package godaddygo
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/oze4/godaddygo/internal/exception"
)
type records struct {
config *Config
}
func newRecords(config *Config) records {
return records{config}
}
func (r records) List(ctx context.Context) ([]Record, error) {
url := "/domains/" + r.config.domainName + "/records"
result, err := makeDo(ctx, r.config, http.MethodGet, url, nil, 200)
if err != nil {
return nil, exception.ListingRecords(err, r.config.domainName)
}
return readRecordListResponse(result)
}
func (r records) Add(ctx context.Context, rec []Record) error {
url := "/domains/" + r.config.domainName + "/records"
body, err := buildUpdateRecordRequest(rec)
if err != nil {
return exception.AddingRecords(err, r.config.domainName, rec[0].Name)
}
if _, err := makeDo(ctx, r.config, http.MethodPatch, url, body, 200); err != nil {
return exception.AddingRecords(err, r.config.domainName, rec[0].Name)
}
return nil
}
func (r records) FindByType(ctx context.Context, t RecordType) ([]Record, error) {
if !t.IsValid() {
err := fmt.Errorf("error FindByType : invalid record type")
return nil, exception.FindingRecordsByType(err, r.config.domainName, t.String())
}
url := "/domains/" + r.config.domainName + "/records/" + t.String()
result, err := makeDo(ctx, r.config, http.MethodGet, url, nil, 200)
if err != nil {
return nil, exception.FindingRecordsByType(err, r.config.domainName, t.String())
}
return readRecordListResponse(result)
}
func (r records) FindByTypeAndName(ctx context.Context, t RecordType, n string) ([]Record, error) {
if !t.IsValid() {
err := fmt.Errorf("error FindByTypeAndName : invalid record type")
return nil, exception.FindingRecordsByTypeAndName(err, r.config.domainName, t.String(), n)
}
url := "/domains/" + r.config.domainName + "/records/" + t.String() + "/" + n
result, err := makeDo(ctx, r.config, http.MethodGet, url, nil, 200)
if err != nil {
return nil, exception.FindingRecordsByTypeAndName(err, r.config.domainName, t.String(), n)
}
return readRecordListResponse(result)
}
func (r records) ReplaceByType(ctx context.Context, t RecordType, rec []Record) error {
url := "/domains/" + r.config.domainName + "/records/" + t.String()
body, err := buildUpdateRecordRequest(rec)
if err != nil {
return exception.AddingRecords(err, r.config.domainName, rec[0].Name)
}
if _, err := makeDo(ctx, r.config, http.MethodPut, url, body, 200); err != nil {
return exception.AddingRecords(err, r.config.domainName, rec[0].Name)
}
return nil
}
func (r records) ReplaceByTypeAndName(ctx context.Context, t RecordType, n string, newRecord Record) error {
if !t.IsValid() {
err := fmt.Errorf("error ReplaceByTypeAndNane : invalid record type")
return exception.AddingRecords(err, r.config.domainName, newRecord.Name)
}
url := "/domains/" + r.config.domainName + "/records/" + t.String() + "/" + n
body, err := buildUpdateRecordRequest([]Record{newRecord})
if err != nil {
return exception.AddingRecords(err, r.config.domainName, newRecord.Name)
}
if _, err := makeDo(ctx, r.config, http.MethodPut, url, body, 200); err != nil {
return exception.AddingRecords(err, r.config.domainName, newRecord.Name)
}
return nil
}
func (r records) Update(ctx context.Context, rec []Record) error {
return fmt.Errorf("not implemented : please use ReplaceByTypeAndName instead")
/*
url := "/domains/" + r.config.domainName + "/records"
body, err := buildUpdateRecordRequest(rec)
if err != nil {
return exception.UpdatingRecord(err, r.config.domainName, rec[0].Name)
}
if _, err = makeDo(ctx, r.config, http.MethodPut, url, body, 200); err != nil {
return exception.UpdatingRecord(err, r.config.domainName, rec[0].Name)
}
return nil
*/
}
func (r records) Delete(ctx context.Context, rec Record) error {
if !rec.Type.IsValid() {
err := fmt.Errorf("invalid record type")
return exception.DeletingRecord(err, r.config.domainName, rec.Name, rec.Type.String())
}
if !rec.Type.IsDeletable() {
err := fmt.Errorf("unsupported DNS Record Type for deleting")
return exception.DeletingRecord(err, r.config.domainName, rec.Name, rec.Type.String())
}
url := "/domains/" + r.config.domainName + "/records/" + rec.Type.String() + "/" + rec.Name
if _, err := makeDo(ctx, r.config, http.MethodDelete, url, nil, 204); err != nil {
return exception.DeletingRecord(err, r.config.domainName, rec.Name, rec.Type.String())
}
return nil
}
func readRecordListResponse(r []byte) ([]Record, error) {
var zone []Record
if err := json.Unmarshal(r, &zone); err != nil {
return []Record{}, exception.InvalidJSONResponse(err)
}
return zone, nil
}
// buildUpdateRecordRequest gives us our dns record as io.Reader
func buildUpdateRecordRequest(rec []Record) (io.Reader, error) {
b, e := json.Marshal(rec)
if e != nil {
return nil, fmt.Errorf("ErrorCannotMarshalRecords : %w", e)
}
return bytes.NewBuffer(b), nil
}
func readRecordResponse(result io.ReadCloser) (Record, error) {
//TODO..
defer result.Close()
// return Record{}, nil
return Record{}, fmt.Errorf("readRecordResponse not implemented")
}