-
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 #280 from HubSpot/aliases
Add ability to handle aliases for groups/domains/edgeCacheDomains
- Loading branch information
Showing
9 changed files
with
231 additions
and
4 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
74 changes: 74 additions & 0 deletions
74
BaragonCore/src/main/java/com/hubspot/baragon/models/BaragonGroupAlias.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,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 + | ||
'}'; | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
BaragonData/src/main/java/com/hubspot/baragon/data/BaragonAliasDatastore.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,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); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
BaragonService/src/main/java/com/hubspot/baragon/service/resources/AliasesResource.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,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); | ||
} | ||
} |
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