-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.go
90 lines (75 loc) · 1.71 KB
/
setup.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
package netmaker
import (
"strings"
"time"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
var netmakerPluginName = "netmaker"
var log = clog.NewWithPlugin(netmakerPluginName)
func init() {
plugin.Register(netmakerPluginName, setup)
}
func setup(c *caddy.Controller) error {
nm, err := netmakerParse(c)
if err != nil {
return plugin.Error(netmakerPluginName, err)
}
config := dnsserver.GetConfig(c)
nm.Zone = config.Zone
c.OnStartup(func() error {
go nm.runFetchEntries(nm.Entries)
return nil
})
config.AddPlugin(func(next plugin.Handler) plugin.Handler {
nm.Next = next
return nm
})
return nil
}
func netmakerParse(c *caddy.Controller) (*Netmaker, error) {
entries := make(DNSEntries)
nm := Netmaker{
RefreshDuration: time.Second * 5,
}
nm.Entries = &entries
var APIKey string
var APIURL string
for c.Next() {
for c.NextBlock() {
switch c.Val() {
case "api_url":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
APIURL = args[0]
case "api_key":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
APIKey = args[0]
case "refresh_duration":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
d, err := time.ParseDuration(strings.Join(args, " "))
if err != nil {
return nil, c.ArgErr()
}
nm.RefreshDuration = d
case "fallthrough":
nm.Fall.SetZonesFromArgs(c.RemainingArgs())
default:
return nil, c.Errf("unknown property '%s'", c.Val())
}
}
}
cl := NewClient(APIURL, APIKey)
nm.DNSClient = cl
return &nm, nil
}