Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getHostByName function to resolve dns names to ips #173

Merged
merged 3 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/network.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Network Functions

Sprig network manipulation functions.

## getHostByName

The `getHostByName` receives a domain name and returns the ip address.

```
getHostByName "www.google.com" would return the corresponding ip address of www.google.com
```
56 changes: 31 additions & 25 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ var nonhermeticFunctions = []string{
// OS
"env",
"expandenv",

// Network
"getHostByName",
}

var genericMap = map[string]interface{}{
Expand Down Expand Up @@ -134,20 +137,20 @@ var genericMap = map[string]interface{}{
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
// Switch order so that "foobar" | contains "foo"
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
"quote": quote,
"squote": squote,
"cat": cat,
"indent": indent,
"nindent": nindent,
"replace": replace,
"plural": plural,
"sha1sum": sha1sum,
"sha256sum": sha256sum,
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
"quote": quote,
"squote": squote,
"cat": cat,
"indent": indent,
"nindent": nindent,
"replace": replace,
"plural": plural,
"sha1sum": sha1sum,
"sha256sum": sha256sum,
"adler32sum": adler32sum,
"toString": strval,
"toString": strval,

// Wrap Atoi to stop errors.
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
Expand Down Expand Up @@ -221,6 +224,9 @@ var genericMap = map[string]interface{}{
"env": func(s string) string { return os.Getenv(s) },
"expandenv": func(s string) string { return os.ExpandEnv(s) },

// Network:
"getHostByName": getHostByName,

// File Paths:
"base": path.Base,
"dir": path.Dir,
Expand All @@ -235,19 +241,19 @@ var genericMap = map[string]interface{}{
"b32dec": base32decode,

// Data Structures:
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
"list": list,
"dict": dict,
"set": set,
"unset": unset,
"hasKey": hasKey,
"pluck": pluck,
"keys": keys,
"pick": pick,
"omit": omit,
"merge": merge,
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
"list": list,
"dict": dict,
"set": set,
"unset": unset,
"hasKey": hasKey,
"pluck": pluck,
"keys": keys,
"pick": pick,
"omit": omit,
"merge": merge,
"mergeOverwrite": mergeOverwrite,
"values": values,
"values": values,

"append": push, "push": push,
"prepend": prepend,
Expand Down
12 changes: 12 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package sprig

import (
"math/rand"
"net"
)

func getHostByName(name string) string {
addrs, _ := net.LookupHost(name)
//TODO: add error handing when release v3 cames out
return addrs[rand.Intn(len(addrs))]
}
18 changes: 18 additions & 0 deletions network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package sprig

import (
"net"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetHostByName(t *testing.T) {
tpl := `{{"www.google.com" | getHostByName}}`

resolvedIP, _ := runRaw(tpl, nil)

ip := net.ParseIP(resolvedIP)
assert.NotNil(t, ip)
assert.NotEmpty(t, ip)
}