Skip to content

Commit

Permalink
Add LookupIPV4, LookupIPV6 template functions (kelseyhightower#684)
Browse files Browse the repository at this point in the history
Add LookupIPV4, LookupIPV6 template functions
  • Loading branch information
AndrewChubatiuk authored and okushchenko committed Mar 7, 2018
1 parent 9b0ce8e commit 5601717
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
22 changes: 22 additions & 0 deletions resource/template/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func newFuncMap() map[string]interface{} {
m["replace"] = strings.Replace
m["trimSuffix"] = strings.TrimSuffix
m["lookupIP"] = LookupIP
m["lookupIPV4"] = LookupIPV4
m["lookupIPV6"] = LookupIPV6
m["lookupSRV"] = LookupSRV
m["fileExists"] = isFileExist
m["base64Encode"] = Base64Encode
Expand Down Expand Up @@ -181,6 +183,26 @@ func LookupIP(data string) []string {
return ipStrings
}

func LookupIPV6(data string) []string {
var addresses []string
for _, ip := range LookupIP(data) {
if strings.Contains(ip, ":") {
addresses = append(addresses, ip)
}
}
return addresses
}

func LookupIPV4(data string) []string {
var addresses []string
for _, ip := range LookupIP(data) {
if strings.Contains(ip, ".") {
addresses = append(addresses, ip)
}
}
return addresses
}

type sortSRV []*net.SRV

func (s sortSRV) Len() int {
Expand Down
88 changes: 84 additions & 4 deletions resource/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type templateTest struct {
desc string // description of the test (for helpful errors)
toml string // toml file contents
tmpl string // template file contents
expected string // expected generated file contents
expected interface{} // expected generated file contents
updateStore func(*TemplateResource) // function for setting values in store
}

Expand Down Expand Up @@ -615,6 +615,62 @@ dir: /test/data
tr.store.Set("/test/data/def", "child")
},
},
templateTest{
desc: "ipv4 lookup test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/data",
"/test/data/abc",
]
`,
tmpl: `
{{range lookupIPV4 "localhost"}}
ip: {{.}}
{{end}}
`,
expected: `
ip: 127.0.0.1
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data", "parent")
tr.store.Set("/test/data/def", "child")
},
},
templateTest{
desc: "ipv6 lookup test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/data",
"/test/data/abc",
]
`,
tmpl: `
{{range lookupIPV6 "localhost"}}
ip: {{.}}
{{end}}
`,
expected: [...]string{
`
ip: ::1
`,
`
`,
},
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data", "parent")
tr.store.Set("/test/data/def", "child")
},
},
templateTest{
desc: "ip lookup test",
toml: `
Expand All @@ -631,11 +687,25 @@ keys = [
ip: {{.}}
{{end}}
`,
expected: `
expected: [...]string{
`
ip: 127.0.0.1
`,
`
ip: 127.0.0.1
ip: ::1
`,
`
ip: ::1
`,
},
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data", "parent")
tr.store.Set("/test/data/def", "child")
Expand Down Expand Up @@ -749,8 +819,18 @@ func ExecuteTestTemplate(tt templateTest, t *testing.T) {
if err != nil {
t.Errorf(tt.desc + ": failed to read StageFile: " + err.Error())
}
if string(actual) != tt.expected {
t.Errorf(fmt.Sprintf("%v: invalid StageFile. Expected %v, actual %v", tt.desc, tt.expected, string(actual)))
switch tt.expected.(type) {
case string:
if string(actual) != tt.expected.(string) {
t.Errorf(fmt.Sprintf("%v: invalid StageFile. Expected %v, actual %v", tt.desc, tt.expected, string(actual)))
}
case []string:
for _, expected := range tt.expected.([]string) {
if string(actual) == expected {
break
}
}
t.Errorf(fmt.Sprintf("%v: invalid StageFile. Possible expected values %v, actual %v", tt.desc, tt.expected, string(actual)))
}
}

Expand Down

0 comments on commit 5601717

Please sign in to comment.