-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
81 lines (68 loc) · 1.94 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
package drovedns
import (
"fmt"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
var pluginName = "drove"
var log = clog.NewWithPlugin(pluginName)
// init registers this plugin.
func init() { plugin.Register(pluginName, setup) }
// setup is the function that gets called when the config parser see the token "example". Setup is responsible
// for parsing any extra options the example plugin may have. The first token this function sees is "example".
func setup(c *caddy.Controller) error {
handler, err := parseAndCreate(c)
if err != nil {
return err
}
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
handler.Next = next
return handler
})
return nil
}
func parseAndCreate(c *caddy.Controller) (*DroveHandler, error) {
c.Next() // Ignore "example" and give us the next token.
config := NewDroveConfig()
for c.NextBlock() {
switch c.Val() {
case "endpoint":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
config.Endpoint = args[0]
case "gateway":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
config.Gateway = args[0]
case "access_token":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
config.AuthConfig.AccessToken = args[0]
case "user_pass":
args := c.RemainingArgs()
if len(args) != 2 {
return nil, c.ArgErr()
}
config.AuthConfig.User, config.AuthConfig.Pass = args[0], args[1]
case "skip_ssl_check":
config.SkipSSL = true
default:
return nil, fmt.Errorf("Drove: Unknown argument %s found", c.Val())
}
}
if err := config.Validate(); err != nil {
return nil, err
}
drove_client := NewDroveClient(config)
drove_client.Init()
return NewDroveHandler(&drove_client), nil
}