-
Notifications
You must be signed in to change notification settings - Fork 3
/
dnsclient _fake.go
89 lines (78 loc) · 2 KB
/
dnsclient _fake.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
package dnsclient
import (
"context"
)
type Record struct {
ZoneID string
Name string
RecordType string
Rrdata string
Opts DNSRecordOptions
}
type FakeDNSClient struct {
Zones map[string]string
Records map[string]Record
}
// NewDNSClient creates a new dns client with a cloudflare api token.
func NewFakeDNSClient(
zones map[string]string,
records map[string]Record,
) *FakeDNSClient {
return &FakeDNSClient{
Zones: zones,
Records: records,
}
}
// GetManagedZones returns a map of all managed zone DNS names mapped to their IDs, composed of the project ID and
// their user assigned resource names.
func (c *FakeDNSClient) GetManagedZones(ctx context.Context) (map[string]string, error) {
zones := make(map[string]string)
for k, v := range c.Zones {
zones[k] = v
}
return zones, nil
}
func (c *FakeDNSClient) getRecords() map[string]Record {
r := map[string]Record{}
for k, v := range c.Records {
r[k] = v
}
return r
}
// CreateOrUpdateRecordSet creates or updates the resource recordset with the given name, record type, rrdatas, and ttl
// in the managed zone with the given name or ID.
func (c *FakeDNSClient) CreateOrUpdateRecordSet(
ctx context.Context,
zoneID,
name,
recordType string,
rrdatas []string,
opts DNSRecordOptions,
) error {
records := c.getRecords()
for _, rrdata := range rrdatas {
c.Records[rrdata] = Record{
ZoneID: zoneID,
Name: name,
RecordType: recordType,
Rrdata: rrdata,
Opts: opts,
}
delete(records, rrdata)
}
// delete undefined data
for rrdata := range records {
delete(c.Records, rrdata)
}
return nil
}
// DeleteRecordSet deletes the resource recordset with the given name and record type
// in the managed zone with the given name or ID.
func (c *FakeDNSClient) DeleteRecordSet(ctx context.Context, zoneID, name, recordType string) error {
for rrdata, record := range c.Records {
if record.ZoneID == zoneID && record.RecordType == recordType {
delete(c.Records, rrdata)
}
}
return nil
}