/
provider.go
109 lines (95 loc) · 2.52 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
package linode
import (
"context"
"fmt"
"github.com/TimothyYe/godns/internal/settings"
"github.com/TimothyYe/godns/internal/utils"
"github.com/linode/linodego"
log "github.com/sirupsen/logrus"
)
type DNSProvider struct {
linodeClient *linodego.Client
}
func (provider *DNSProvider) Init(conf *settings.Settings) {
httpClient, err := CreateHTTPClient(conf)
if err != nil {
panic(err)
}
linodeAPIClient := linodego.NewClient(httpClient)
linodeAPIClient.SetDebug(conf.DebugInfo)
provider.linodeClient = &linodeAPIClient
}
func (provider *DNSProvider) UpdateIP(domain, subdomain, ip string) error {
if subdomain == utils.RootDomain {
subdomain = ""
}
domainID, err := provider.getDomainID(domain)
if err != nil {
return err
}
recordExists, recordID, err := provider.getDomainRecordID(domainID, subdomain)
if err != nil {
return err
}
if !recordExists {
recordID, _ = provider.createDomainRecord(domainID, subdomain)
}
err = provider.updateDomainRecord(domainID, recordID, ip)
if err != nil {
return err
}
return nil
}
func (provider *DNSProvider) getDomainID(name string) (int, error) {
f := linodego.Filter{}
f.AddField(linodego.Eq, "domain", name)
fStr, err := f.MarshalJSON()
if err != nil {
log.Fatal(err)
}
opts := linodego.NewListOptions(0, string(fStr))
res, err := provider.linodeClient.ListDomains(context.Background(), opts)
if err != nil {
return 0, err
}
if len(res) == 0 {
return 0, fmt.Errorf("No domains found for name %s", name)
}
return res[0].ID, nil
}
func (provider *DNSProvider) getDomainRecordID(domainID int, name string) (bool, int, error) {
res, err := provider.linodeClient.ListDomainRecords(context.Background(), domainID, nil)
if err != nil {
return false, 0, err
}
if len(res) == 0 {
return false, 0, nil
}
for _, record := range res {
if record.Name == name {
return true, record.ID, nil
}
}
return false, 0, nil
}
func (provider *DNSProvider) createDomainRecord(domainID int, name string) (int, error) {
opts := &linodego.DomainRecordCreateOptions{
Type: "A",
Name: name,
Target: "127.0.0.1",
TTLSec: 30,
}
record, err := provider.linodeClient.CreateDomainRecord(context.Background(), domainID, *opts)
if err != nil {
return 0, err
}
return record.ID, nil
}
func (provider *DNSProvider) updateDomainRecord(domainID int, id int, ip string) error {
opts := &linodego.DomainRecordUpdateOptions{Target: ip}
_, err := provider.linodeClient.UpdateDomainRecord(context.Background(), domainID, id, *opts)
if err != nil {
return err
}
return nil
}