Skip to content

Commit

Permalink
Merge pull request #280 from HubSpot/aliases
Browse files Browse the repository at this point in the history
Add ability to handle aliases for groups/domains/edgeCacheDomains
  • Loading branch information
ssalinas authored Nov 6, 2018
2 parents 58da671 + c6bd43d commit 2806040
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableMap;
import com.hubspot.baragon.models.BaragonAgentMetadata;
import com.hubspot.baragon.models.BaragonGroup;
import com.hubspot.baragon.models.BaragonGroupAlias;
import com.hubspot.baragon.models.BaragonRequest;
import com.hubspot.baragon.models.BaragonResponse;
import com.hubspot.baragon.models.BaragonService;
Expand Down Expand Up @@ -56,6 +57,8 @@ public class BaragonServiceClient {

private static final String STATUS_FORMAT = "%s/status";

private static final String ALIASES_FORMAT = "%s/aliases/%s";

private static final TypeReference<Collection<String>> STRING_COLLECTION = new TypeReference<Collection<String>>() {};
private static final TypeReference<Collection<BaragonGroup>> BARAGON_GROUP_COLLECTION = new TypeReference<Collection<BaragonGroup>>() {};
private static final TypeReference<Collection<BaragonAgentMetadata>> BARAGON_AGENTS_COLLECTION = new TypeReference<Collection<BaragonAgentMetadata>>() {};
Expand Down Expand Up @@ -359,4 +362,21 @@ public Collection<QueuedRequestId> getQueuedRequests() {
final String uri = String.format(REQUEST_FORMAT, getBaseUrl());
return getCollection(uri, "queued requests", QUEUED_REQUEST_COLLECTION);
}

// Aliases

public Optional<BaragonGroupAlias> getAlias(String name) {
final String uri = String.format(ALIASES_FORMAT, getBaseUrl(), name);
return getSingle(uri, "alias", name, BaragonGroupAlias.class);
}

public void createAlias(String name, BaragonGroupAlias alias) {
final String uri = String.format(ALIASES_FORMAT, getBaseUrl(), name);
post(uri, "alias", Optional.of(alias), Optional.absent());
}

public void deleteAlias(String name) {
final String uri = String.format(ALIASES_FORMAT, getBaseUrl(), name);
delete(uri, "alias", name, Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.hubspot.baragon.models;

import java.util.Collections;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class BaragonGroupAlias {
private final Set<String> groups;
private final Set<String> domains;
private final Set<String> edgeCacheDomains;

@JsonCreator

public BaragonGroupAlias(@JsonProperty("groups") Set<String> groups,
@JsonProperty("domains") Set<String> domains,
@JsonProperty("edgeCacheDomains") Set<String> edgeCacheDomains) {
this.groups = groups == null ? Collections.emptySet() : groups;
this.domains = domains == null ? Collections.emptySet() : domains;
this.edgeCacheDomains = edgeCacheDomains == null ? Collections.emptySet() : edgeCacheDomains;
}

public Set<String> getGroups() {
return groups;
}

public Set<String> getDomains() {
return domains;
}

public Set<String> getEdgeCacheDomains() {
return edgeCacheDomains;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

BaragonGroupAlias that = (BaragonGroupAlias) o;

if (groups != null ? !groups.equals(that.groups) : that.groups != null) {
return false;
}
if (domains != null ? !domains.equals(that.domains) : that.domains != null) {
return false;
}
return edgeCacheDomains != null ? edgeCacheDomains.equals(that.edgeCacheDomains) : that.edgeCacheDomains == null;
}

@Override
public int hashCode() {
int result = groups != null ? groups.hashCode() : 0;
result = 31 * result + (domains != null ? domains.hashCode() : 0);
result = 31 * result + (edgeCacheDomains != null ? edgeCacheDomains.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "BaragonGroupAlias{" +
"groups='" + groups + '\'' +
", domains=" + domains +
", edgeCacheDomains=" + edgeCacheDomains +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public BaragonRequest(String loadBalancerRequestId, BaragonService loadBalancerS
this(loadBalancerRequestId, loadBalancerService, addUpstreams, removeUpstreams, Collections.<UpstreamInfo>emptyList(), replaceServiceId, Optional.of(RequestAction.UPDATE), false, false);
}

public BaragonRequest withUpdatedGroups(BaragonGroupAlias updatedFromAlias) {
return new BaragonRequest(loadBalancerRequestId, loadBalancerService.withUpdatedGroups(updatedFromAlias), addUpstreams, removeUpstreams, replaceUpstreams, replaceServiceId, action, noValidate, noReload);
}

public String getLoadBalancerRequestId() {
return loadBalancerRequestId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public BaragonService(String serviceId, Collection<String> owners, String servic
this(serviceId, owners, serviceBasePath, Collections.<String>emptyList(), loadBalancerGroups, options, Optional.<String>absent(), Collections.<String>emptySet(), Optional.absent(), Collections.emptySet());
}

public BaragonService withUpdatedGroups(BaragonGroupAlias updatedFromAlias) {
return new BaragonService(serviceId, owners, serviceBasePath, additionalPaths, updatedFromAlias.getGroups(), options, templateName, updatedFromAlias.getDomains(), updatedFromAlias.getEdgeCacheDomains());
}

public String getServiceId() {
return serviceId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.hubspot.baragon.cache.BaragonStateCache;
import com.hubspot.baragon.config.AuthConfiguration;
import com.hubspot.baragon.data.BaragonAgentResponseDatastore;
import com.hubspot.baragon.data.BaragonAliasDatastore;
import com.hubspot.baragon.data.BaragonAuthDatastore;
import com.hubspot.baragon.data.BaragonConnectionStateListener;
import com.hubspot.baragon.data.BaragonKnownAgentsDatastore;
Expand Down Expand Up @@ -76,6 +77,7 @@ protected void configure() {
bind(BaragonRequestDatastore.class).in(Scopes.SINGLETON);
bind(BaragonResponseHistoryDatastore.class).in(Scopes.SINGLETON);
bind(BaragonZkMetaDatastore.class).in(Scopes.SINGLETON);
bind(BaragonAliasDatastore.class).in(Scopes.SINGLETON);

bind(ZkParallelFetcher.class).in(Scopes.SINGLETON);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.hubspot.baragon.data;

import java.util.HashSet;
import java.util.Set;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.ZKPaths;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.hubspot.baragon.config.ZooKeeperConfiguration;
import com.hubspot.baragon.models.BaragonGroupAlias;
import com.hubspot.baragon.models.BaragonRequest;

public class BaragonAliasDatastore extends AbstractDataStore {

public static final String ALIASES_ROOT = "/aliases";

@Inject
public BaragonAliasDatastore(CuratorFramework curatorFramework, ObjectMapper objectMapper, ZooKeeperConfiguration zooKeeperConfiguration) {
super(curatorFramework, objectMapper, zooKeeperConfiguration);
}

private String getAliasPath(String name) {
return ZKPaths.makePath(ALIASES_ROOT, name);
}

public void saveAlias(String name, BaragonGroupAlias alias) {
writeToZk(getAliasPath(name), alias);
}

public Optional<BaragonGroupAlias> getAlias(String name) {
return readFromZk(getAliasPath(name), BaragonGroupAlias.class);
}

public void deleteAlias(String name) {
deleteNode(getAliasPath(name));
}

public BaragonRequest updateForAliases(BaragonRequest original) {
Set<String> edgeCacheDomains = new HashSet<>(original.getLoadBalancerService().getEdgeCacheDomains());
if (original.getLoadBalancerService().getEdgeCacheDNS().isPresent()) {
edgeCacheDomains.add(original.getLoadBalancerService().getEdgeCacheDNS().get());
}
return original.withUpdatedGroups(
processAliases(
original.getLoadBalancerService().getLoadBalancerGroups(),
original.getLoadBalancerService().getDomains(),
edgeCacheDomains
)
);
}

public BaragonGroupAlias processAliases(Set<String> groups, Set<String> domains, Set<String> edgeCacheDomains) {
Set<String> allGroups = new HashSet<>();
Set<String> allDomains = new HashSet<>(domains);
Set<String> allEdgeCacheDomains = new HashSet<>(edgeCacheDomains);
for (String group : groups) {
Optional<BaragonGroupAlias> maybeAlias = getAlias(group);
if (maybeAlias.isPresent()) {
allGroups.addAll(maybeAlias.get().getGroups());
allDomains.addAll(maybeAlias.get().getDomains());
allEdgeCacheDomains.addAll(maybeAlias.get().getEdgeCacheDomains());
} else {
allGroups.add(group);
}
}
return new BaragonGroupAlias(allGroups, allDomains, allEdgeCacheDomains);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.hubspot.baragon.service.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.hubspot.baragon.data.BaragonAliasDatastore;
import com.hubspot.baragon.models.BaragonGroupAlias;

@Path("/aliases")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class AliasesResource {
private final BaragonAliasDatastore aliasDatastore;

@Inject
public AliasesResource(BaragonAliasDatastore aliasDatastore) {
this.aliasDatastore = aliasDatastore;
}

@POST
@Path("/{name}")
public BaragonGroupAlias createAlias(@PathParam("name") String name, BaragonGroupAlias alias) {
aliasDatastore.saveAlias(name, alias);
return alias;
}

@GET
@Path("/{name}")
public Optional<BaragonGroupAlias> getAlias(@PathParam("name") String name) {
return aliasDatastore.getAlias(name);
}

@DELETE
@Path("/{name}")
public void deleteAlias(@PathParam("name") String name) {
aliasDatastore.deleteAlias(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ protected void configure() {
bind(StatusResource.class);
bind(UIResource.class);
bind(WorkerResource.class);
bind(AliasesResource.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.hubspot.baragon.auth.NoAuth;
import com.hubspot.baragon.data.BaragonAliasDatastore;
import com.hubspot.baragon.models.BaragonRequest;
import com.hubspot.baragon.models.BaragonResponse;
import com.hubspot.baragon.models.QueuedRequestId;
import com.hubspot.baragon.service.managers.RequestManager;
import com.hubspot.baragon.service.worker.BaragonRequestWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Path("/request")
@Consumes({MediaType.APPLICATION_JSON})
Expand All @@ -32,11 +34,13 @@ public class RequestResource {

private final RequestManager manager;
private final ObjectMapper objectMapper;
private final BaragonAliasDatastore aliasDatastore;

@Inject
public RequestResource(RequestManager manager, ObjectMapper objectMapper) {
public RequestResource(RequestManager manager, ObjectMapper objectMapper, BaragonAliasDatastore aliasDatastore) {
this.manager = manager;
this.objectMapper = objectMapper;
this.aliasDatastore = aliasDatastore;
}

@GET
Expand All @@ -49,8 +53,9 @@ public Optional<BaragonResponse> getResponse(@PathParam("requestId") String requ
@POST
public BaragonResponse enqueueRequest(@Valid BaragonRequest request) {
try {
BaragonRequest updatedForAliases = aliasDatastore.updateForAliases(request);
LOG.info(String.format("Received request: %s", objectMapper.writeValueAsString(request)));
return manager.enqueueRequest(request);
return manager.enqueueRequest(updatedForAliases);
} catch (Exception e) {
LOG.error(String.format("Caught exception for %s", request.getLoadBalancerRequestId()), e);
return BaragonResponse.failure(request.getLoadBalancerRequestId(), e.getMessage());
Expand Down

0 comments on commit 2806040

Please sign in to comment.