-
Notifications
You must be signed in to change notification settings - Fork 37
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 #192 from HubSpot/resolve_ip_helper
add handlebars resolve hostname helper
- Loading branch information
Showing
5 changed files
with
35 additions
and
3 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
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
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
25 changes: 25 additions & 0 deletions
25
...gentService/src/main/java/com/hubspot/baragon/agent/handlebars/ResolveHostnameHelper.java
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,25 @@ | ||
package com.hubspot.baragon.agent.handlebars; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
|
||
import com.github.jknack.handlebars.Helper; | ||
import com.github.jknack.handlebars.Options; | ||
import com.google.common.net.HostAndPort; | ||
|
||
public class ResolveHostnameHelper implements Helper<String> { | ||
|
||
public static final String NAME = "resolveHostname"; | ||
|
||
@Override | ||
public CharSequence apply(String address, Options options) throws UnknownHostException { | ||
if (address.contains(":")) { | ||
HostAndPort hostAndPort = HostAndPort.fromString(address); | ||
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(hostAndPort.getHostText()), hostAndPort.getPort()); | ||
return String.format("%s:%d", socketAddress.getAddress().getHostAddress(), socketAddress.getPort()); | ||
} else { | ||
return InetAddress.getByName(address).getHostAddress(); | ||
} | ||
} | ||
} |