Skip to content

Commit

Permalink
Merge pull request #192 from HubSpot/resolve_ip_helper
Browse files Browse the repository at this point in the history
add handlebars resolve hostname helper
  • Loading branch information
ssalinas authored Aug 1, 2016
2 parents 8ac94cd + bc97c50 commit feda140
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.hubspot.baragon.agent.handlebars.FormatTimestampHelper;
import com.hubspot.baragon.agent.handlebars.IfEqualHelperSource;
import com.hubspot.baragon.agent.handlebars.PreferSameRackWeightingHelper;
import com.hubspot.baragon.agent.handlebars.ResolveHostnameHelper;
import com.hubspot.baragon.agent.listeners.ResyncListener;
import com.hubspot.baragon.agent.models.FilePathFormatType;
import com.hubspot.baragon.agent.models.LbConfigTemplate;
Expand Down Expand Up @@ -75,9 +76,10 @@ protected void configure() {
public Handlebars providesHandlebars(BaragonAgentConfiguration config, BaragonAgentMetadata agentMetadata) {
final Handlebars handlebars = new Handlebars();

handlebars.registerHelper("formatTimestamp", new FormatTimestampHelper(config.getDefaultDateFormat()));
handlebars.registerHelper("firstOf", new FirstOfHelper(""));
handlebars.registerHelper("currentRackIsPresent", new CurrentRackIsPresentHelper(agentMetadata.getEc2().getAvailabilityZone()));
handlebars.registerHelper(FormatTimestampHelper.NAME, new FormatTimestampHelper(config.getDefaultDateFormat()));
handlebars.registerHelper(FirstOfHelper.NAME, new FirstOfHelper(""));
handlebars.registerHelper(CurrentRackIsPresentHelper.NAME, new CurrentRackIsPresentHelper(agentMetadata.getEc2().getAvailabilityZone()));
handlebars.registerHelper(ResolveHostnameHelper.NAME, new ResolveHostnameHelper());
handlebars.registerHelpers(new PreferSameRackWeightingHelper(config, agentMetadata));
handlebars.registerHelpers(IfEqualHelperSource.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.hubspot.baragon.models.UpstreamInfo;

public class CurrentRackIsPresentHelper implements Helper<Collection<UpstreamInfo>> {
public static final String NAME = "currentRackIsPresent";

private final Optional<String> currentRackId;

public CurrentRackIsPresentHelper(Optional<String> currentRackId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.google.common.base.Strings;

public class FirstOfHelper implements Helper<Object> {
public static final String NAME = "firstOf";

private final Object fallback;

public FirstOfHelper(Object fallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public class FormatTimestampHelper implements Helper<Number> {
private static final Logger LOG = LoggerFactory.getLogger(FormatTimestampHelper.class);
public static final String NAME = "formatTimestamp";

private final String defaultFormatString;

Expand Down
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();
}
}
}

0 comments on commit feda140

Please sign in to comment.