forked from libdns/googleclouddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
124 lines (116 loc) · 4.47 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
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
// 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"
"google.golang.org/api/googleapi"
)
// 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) {
p.mutex.Lock()
defer p.mutex.Unlock()
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) {
p.mutex.Lock()
defer p.mutex.Unlock()
processedRecords := make(libdnsRecords, 0)
recordsToAppend := libdnsRecords(records)
for recordData, recordsToPost := range recordsToAppend.groupRecordsByType() {
existingRecords, err := p.getCloudDNSRecord(ctx, zone, recordData.name, recordData.recordType)
if err != nil {
if gErr, ok := err.(*googleapi.Error); !ok || gErr.Code != 404 {
return processedRecords, err
}
}
verifiedNewRecords := make(libdnsRecords, 0)
for _, newRecord := range recordsToPost { // Make sure that we do not append a record that already exists
if existingRecords.doesNotHaveRecord(newRecord) {
verifiedNewRecords = append(verifiedNewRecords, newRecord)
}
}
if len(verifiedNewRecords) == 0 {
continue
}
submittedRecords, err := p.postCloudDNSRecord(ctx, zone, append(existingRecords, verifiedNewRecords...))
if err != nil {
return processedRecords, err
}
// Let's generate an exact list of appended records based on the returned results
for _, updatedRecord := range submittedRecords {
if verifiedNewRecords.hasRecord(updatedRecord) {
processedRecords = append(processedRecords, updatedRecord)
}
}
}
return processedRecords, nil
}
// 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) {
p.mutex.Lock()
defer p.mutex.Unlock()
processedRecords := make(libdnsRecords, 0)
recordsToSet := libdnsRecords(records)
for _, recordsToPost := range recordsToSet.groupRecordsByType() {
submittedRecords, err := p.postCloudDNSRecord(ctx, zone, recordsToPost)
if err != nil {
return processedRecords, err
}
processedRecords = append(processedRecords, submittedRecords...)
}
return processedRecords, 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) {
p.mutex.Lock()
defer p.mutex.Unlock()
recordsToDelete := libdnsRecords(records)
deletedRecords := make(libdnsRecords, 0)
for recordData, recordsToDelete := range recordsToDelete.groupRecordsByType() {
existingRecords, err := p.getCloudDNSRecord(ctx, zone, recordData.name, recordData.recordType)
if err != nil { // If the entry does not exist, nothing to delete so skip this set
if gErr, ok := err.(*googleapi.Error); !ok || gErr.Code != 404 {
return deletedRecords, err
}
continue
}
verifiedRecords := make(libdnsRecords, 0)
for _, recordToDelete := range recordsToDelete { // Make sure the requested records exist in the Cloud DNS record
if existingRecords.hasRecord(recordToDelete) {
verifiedRecords = append(verifiedRecords, recordToDelete)
}
}
if len(verifiedRecords) == 0 { // The Cloud DNS entry does not have these records so skip this set
continue
}
processedRecords, err := p.deleteCloudDNSRecord(
ctx, zone, recordData.name, recordData.recordType, verifiedRecords, existingRecords)
if err != nil {
return deletedRecords, err
}
deletedRecords = append(deletedRecords, processedRecords...)
}
return deletedRecords, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)