forked from cloudfoundry/guardian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec_parser.go
43 lines (32 loc) · 1.02 KB
/
spec_parser.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
package kawasaki
import (
"net"
"strings"
"code.cloudfoundry.org/guardian/kawasaki/subnets"
"code.cloudfoundry.org/lager"
)
type SpecParserFunc func(spec string) (subnets.SubnetSelector, subnets.IPSelector, error)
func (fn SpecParserFunc) Parse(log lager.Logger, spec string) (subnets.SubnetSelector, subnets.IPSelector, error) {
return fn(spec)
}
func ParseSpec(spec string) (subnets.SubnetSelector, subnets.IPSelector, error) {
var ipSelector subnets.IPSelector = subnets.DynamicIPSelector
var subnetSelector subnets.SubnetSelector = subnets.DynamicSubnetSelector
if spec != "" {
specifiedIP, ipn, err := net.ParseCIDR(suffixIfNeeded(spec))
if err != nil {
return nil, nil, err
}
subnetSelector = subnets.StaticSubnetSelector{IPNet: ipn}
if !specifiedIP.Equal(subnets.NetworkIP(ipn)) {
ipSelector = subnets.StaticIPSelector{IP: specifiedIP}
}
}
return subnetSelector, ipSelector, nil
}
func suffixIfNeeded(spec string) string {
if !strings.Contains(spec, "/") {
spec = spec + "/30"
}
return spec
}