5
5
"fmt"
6
6
"time"
7
7
8
- "github.com/libdns/libdns"
9
8
"google.golang.org/api/dns/v1"
10
- "google.golang.org/api/googleapi"
11
9
"google.golang.org/api/option"
12
10
)
13
11
@@ -30,94 +28,10 @@ func (p *Provider) newService(ctx context.Context) error {
30
28
return err
31
29
}
32
30
33
- // getCloudDNSRecords returns all the records for the specified zone. It breaks up a single Google Record
34
- // with multiple Values into separate libdns.Records.
35
- func (p * Provider ) getCloudDNSRecords (ctx context.Context , zone string ) ([]libdns.Record , error ) {
36
- p .mutex .Lock ()
37
- defer p .mutex .Unlock ()
38
- if err := p .newService (ctx ); err != nil {
39
- return nil , err
40
- }
41
-
42
- gcdZone , err := p .getCloudDNSZone (zone )
43
- if err != nil {
44
- return nil , err
45
- }
46
- rrsReq := p .service .ResourceRecordSets .List (p .Project , gcdZone )
47
- records := make ([]libdns.Record , 0 )
48
- if err := rrsReq .Pages (ctx , func (page * dns.ResourceRecordSetsListResponse ) error {
49
- for _ , googleRecord := range page .Rrsets {
50
- records = append (records , convertToLibDNS (googleRecord , zone )... )
51
- }
52
- return nil
53
- }); err != nil {
54
- return nil , err
55
- }
56
- return records , nil
57
- }
58
-
59
- // setCloudDNSRecord will attempt to create a new Google Cloud DNS record set based on the libdns.Records or patch an existing one if
60
- // it already exists and patch is true.
61
- func (p * Provider ) setCloudDNSRecord (ctx context.Context , zone string , values []libdns.Record , patch bool ) ([]libdns.Record , error ) {
62
- p .mutex .Lock ()
63
- defer p .mutex .Unlock ()
64
- if err := p .newService (ctx ); err != nil {
65
- return nil , err
66
- }
67
-
68
- gcdZone , err := p .getCloudDNSZone (zone )
69
- if err != nil {
70
- return nil , err
71
- }
72
- if len (values ) == 0 {
73
- return nil , fmt .Errorf ("no records available to add to zone %s" , zone )
74
- }
75
- name := values [0 ].Name
76
- fullName := libdns .AbsoluteName (name , zone )
77
- rrs := dns.ResourceRecordSet {
78
- Name : fullName ,
79
- Rrdatas : make ([]string , 0 ),
80
- Ttl : int64 (values [0 ].TTL / time .Second ),
81
- Type : values [0 ].Type ,
82
- }
83
- for _ , record := range values {
84
- rrs .Rrdatas = append (rrs .Rrdatas , record .Value )
85
- }
86
- googleRecord , err := p .service .ResourceRecordSets .Create (p .Project , gcdZone , & rrs ).Context (ctx ).Do ()
87
- if err != nil {
88
- if gErr , ok := err .(* googleapi.Error ); ! ok || (gErr .Code == 409 && ! patch ) {
89
- return nil , err
90
- }
91
- // Record exists and we'd really like to get this libdns.Record into the zone so how about we try patching it instead...
92
- googleRecord , err = p .service .ResourceRecordSets .Patch (p .Project , gcdZone , rrs .Name , rrs .Type , & rrs ).Context (ctx ).Do ()
93
- if err != nil {
94
- return nil , err
95
- }
96
- }
97
- return convertToLibDNS (googleRecord , zone ), nil
98
- }
99
-
100
- // deleteCloudDNSRecord will delete the specified record set.
101
- func (p * Provider ) deleteCloudDNSRecord (ctx context.Context , zone , name , dnsType string ) error {
102
- p .mutex .Lock ()
103
- defer p .mutex .Unlock ()
104
- if err := p .newService (ctx ); err != nil {
105
- return err
106
- }
107
-
108
- gcdZone , err := p .getCloudDNSZone (zone )
109
- if err != nil {
110
- return err
111
- }
112
- fullName := libdns .AbsoluteName (name , zone )
113
- _ , err = p .service .ResourceRecordSets .Delete (p .Project , gcdZone , fullName , dnsType ).Context (ctx ).Do ()
114
- return err
115
- }
116
-
117
31
// getCloudDNSZone will return the Google Cloud DNS zone name for the specified zone. The data is cached
118
32
// for five minutes to avoid repeated calls to the GCP API servers.
119
33
func (p * Provider ) getCloudDNSZone (zone string ) (string , error ) {
120
- if p .zoneMap == nil || time .Now (). Sub (p .zoneMapLastUpdated ) > zoneMapTTL {
34
+ if p .zoneMap == nil || time .Since (p .zoneMapLastUpdated ) > zoneMapTTL {
121
35
p .zoneMap = make (map [string ]string )
122
36
zonesLister := p .service .ManagedZones .List (p .Project )
123
37
err := zonesLister .Pages (context .Background (), func (response * dns.ManagedZonesListResponse ) error {
@@ -138,19 +52,3 @@ func (p *Provider) getCloudDNSZone(zone string) (string, error) {
138
52
}
139
53
return "" , fmt .Errorf ("unable to find Google managaged zone for domain %s" , zone )
140
54
}
141
-
142
- func convertToLibDNS (googleRecord * dns.ResourceRecordSet , zone string ) []libdns.Record {
143
- records := make ([]libdns.Record , 0 )
144
- for _ , value := range googleRecord .Rrdatas {
145
- // there can be multiple values per record so
146
- // let's treat each one as a separate libdns Record
147
- record := libdns.Record {
148
- Type : googleRecord .Type ,
149
- Name : libdns .RelativeName (googleRecord .Name , zone ),
150
- Value : value ,
151
- TTL : time .Duration (googleRecord .Ttl ) * time .Second ,
152
- }
153
- records = append (records , record )
154
- }
155
- return records
156
- }
0 commit comments