forked from libdns/googleclouddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
96 lines (85 loc) · 3.16 KB
/
provider.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
// Package googleclouddns implements a DNS record management client compatible
// with the libdns interfaces for Google Cloud DNS.
package googleclouddns
import (
"context"
"sync"
"time"
"github.com/libdns/libdns"
"google.golang.org/api/dns/v1"
)
// Provider facilitates DNS record manipulation with Google Cloud DNS.
type Provider struct {
Project string `json:"gcp_project,omitempty"`
ServiceAccountJSON string `json:"gcp_application_default,omitempty"`
service *dns.Service
zoneMap map[string]string
zoneMapLastUpdated time.Time
mutex sync.Mutex
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
return p.getCloudDNSRecords(ctx, zone)
}
// AppendRecords adds records to the zone. It returns the records that were added.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
return p.applyRecords(ctx, zone, records, false)
}
// SetRecords sets the records in the zone, either by updating existing records or creating new ones.
// It returns the updated records.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
return p.applyRecords(ctx, zone, records, true)
}
func (p *Provider) applyRecords(ctx context.Context, zone string, records []libdns.Record, update bool) ([]libdns.Record, error) {
googleDNSRecords := groupRecords(records)
setRecords := make([]libdns.Record, 0)
for _, recordData := range googleDNSRecords {
sr, err := p.setCloudDNSRecord(ctx, zone, recordData.values, update)
if err != nil {
return setRecords, err
}
setRecords = append(setRecords, sr...)
}
return setRecords, nil
}
// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
googleDNSRecords := groupRecords(records)
recordsToReturn := make([]libdns.Record, 0)
for recordName, recordData := range googleDNSRecords {
err := p.deleteCloudDNSRecord(ctx, zone, recordName, recordData.recordType)
if err != nil {
return recordsToReturn, err
}
recordsToReturn = append(recordsToReturn, recordData.values...)
}
return recordsToReturn, nil
}
type googleDNSRecord struct {
recordType string
values []libdns.Record
}
// groupRecords combines libdns.Record entries into a single googleDNSRecord to ensure
// the values are sent at the same time to Google Cloud.
func groupRecords(records []libdns.Record) map[string]googleDNSRecord {
gdrs := make(map[string]googleDNSRecord)
for _, record := range records {
if gdr, ok := gdrs[record.Name]; !ok {
gdrs[record.Name] = googleDNSRecord{
recordType: record.Type,
values: []libdns.Record{record},
}
} else {
gdr.values = append(gdr.values, record)
gdrs[record.Name] = gdr
}
}
return gdrs
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)