Skip to content

Commit c205c35

Browse files
committed
Add method for converting cluster name to symbolic name valid for URLs
Also added getPartitionarName to ClusterInfo. ClusterResource uses these.
1 parent 3f69dbe commit c205c35

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

src/main/java/com/spotify/reaper/cassandra/ClusterInfo.java

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class ClusterInfo implements IClusterInfo {
99
private String seedHost;
1010
private int seedPort = 0;
1111
private String clusterName;
12+
private String partitionerName;
1213

1314
private List<String> tokens;
1415

@@ -26,6 +27,7 @@ public ClusterInfo init() throws ReaperException {
2627
seedPort == 0 ? JMXProxy.connect(seedHost) : JMXProxy.connect(seedHost, seedPort);
2728
tokens = jmx.getTokens();
2829
clusterName = jmx.getClusterName();
30+
partitionerName = jmx.getPartitionerName();
2931
jmx.close();
3032
return this;
3133
}
@@ -40,4 +42,18 @@ public String getClusterName() {
4042
return clusterName;
4143
}
4244

45+
public static String toSymbolicName(String s) {
46+
return s.toLowerCase().replaceAll("[^a-z0-9_]", "");
47+
}
48+
49+
@Override
50+
public String getSymbolicName() {
51+
return toSymbolicName(clusterName);
52+
}
53+
54+
@Override
55+
public String getPartitionerName() {
56+
return partitionerName;
57+
}
58+
4359
}

src/main/java/com/spotify/reaper/cassandra/IClusterInfo.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.util.List;
44

55
public interface IClusterInfo {
6-
7-
public List<String> getTokens();
8-
9-
public String getClusterName();
6+
List<String> getTokens();
7+
String getClusterName();
8+
String getSymbolicName();
9+
String getPartitionerName();
1010
}

src/main/java/com/spotify/reaper/cassandra/JMXProxy.java

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public String getClusterName() {
6363
return ssProxy.getClusterName();
6464
}
6565

66+
public String getPartitionerName() {
67+
return ssProxy.getPartitionerName();
68+
}
69+
6670
public void close() throws ReaperException {
6771
try {
6872
jmxConnector.close();

src/main/java/com/spotify/reaper/resources/ClusterResource.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.spotify.reaper.ReaperException;
66
import com.spotify.reaper.cassandra.ClusterInfo;
7+
import com.spotify.reaper.cassandra.IClusterInfo;
78
import com.spotify.reaper.core.Cluster;
89
import com.spotify.reaper.storage.IStorage;
910

@@ -12,6 +13,7 @@
1213

1314
import java.net.URI;
1415
import java.net.URL;
16+
import java.util.Collections;
1517

1618
import javax.ws.rs.GET;
1719
import javax.ws.rs.POST;
@@ -51,7 +53,7 @@ public Response addCluster(@Context UriInfo uriInfo, @QueryParam("host") Optiona
5153
}
5254
LOG.info("add cluster called with host: {}", host);
5355

54-
ClusterInfo clusterInfo;
56+
IClusterInfo clusterInfo;
5557
try {
5658
clusterInfo = ClusterInfo.getInstance(host.get());
5759
} catch (ReaperException e) {
@@ -61,14 +63,23 @@ public Response addCluster(@Context UriInfo uriInfo, @QueryParam("host") Optiona
6163
return Response.status(400).entity(errMsg).build();
6264
}
6365

66+
6467
URI createdURI = null;
6568
try {
66-
createdURI = (new URL(uriInfo.getAbsolutePath().toURL(), "1")).toURI();
69+
createdURI = (new URL(uriInfo.getAbsolutePath().toURL(), clusterInfo.getSymbolicName())).toURI();
6770
} catch (Exception e) {
68-
LOG.error("failed creating target URI: " + uriInfo.getAbsolutePath());
71+
String errMsg = "failed creating target for cluster: " + clusterInfo.getSymbolicName();
72+
LOG.error(errMsg);
6973
e.printStackTrace();
74+
return Response.status(400).entity(errMsg).build();
7075
}
7176

77+
storage.addCluster(new Cluster.Builder()
78+
.name(clusterInfo.getClusterName())
79+
.seedHosts(Collections.singleton(host.get()))
80+
.partitioner(clusterInfo.getPartitionerName())
81+
.build());
82+
7283
String replyMsg = "cluster with name \"" + clusterInfo.getClusterName() + "\" created";
7384
return Response.created(createdURI).entity(replyMsg).build();
7485
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.spotify.reaper.cassandra;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class ClusterInfoTest {
8+
@Test
9+
public void testGetSymbolicName() {
10+
assertEquals("example2cluster", ClusterInfo.toSymbolicName("Example2 Cluster"));
11+
assertEquals("example2_cluster", ClusterInfo.toSymbolicName("Example2_Cluster"));
12+
}
13+
}

0 commit comments

Comments
 (0)