-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from fcgravalos/get_host_by_name
add getHostByName function to resolve dns names to ips
- Loading branch information
Showing
4 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |