Skip to content

Commit

Permalink
Merge pull request #173 from fcgravalos/get_host_by_name
Browse files Browse the repository at this point in the history
add getHostByName function to resolve dns names to ips
  • Loading branch information
mattfarina authored Sep 24, 2019
2 parents fb071e4 + 75f1829 commit 022461a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
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
```
6 changes: 6 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ var nonhermeticFunctions = []string{
// OS
"env",
"expandenv",

// Network
"getHostByName",
}

var genericMap = map[string]interface{}{
Expand Down Expand Up @@ -224,6 +227,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 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)
}

0 comments on commit 022461a

Please sign in to comment.