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 handlebars resolve hostname helper #192

Merged
merged 5 commits into from
Aug 1, 2016
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
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();
}
}
}