diff --git a/src/main/java/jenkins/plugins/logstash/configuration/Redis.java b/src/main/java/jenkins/plugins/logstash/configuration/Redis.java index 373743be..be2b2ec3 100644 --- a/src/main/java/jenkins/plugins/logstash/configuration/Redis.java +++ b/src/main/java/jenkins/plugins/logstash/configuration/Redis.java @@ -17,6 +17,7 @@ public class Redis extends HostBasedLogstashIndexer protected String key; protected Secret password; + protected int dbindex; @DataBoundConstructor public Redis() @@ -45,6 +46,17 @@ public void setPassword(Secret password) this.password = password; } + public int getDbindex() + { + return dbindex; + } + + @DataBoundSetter + public void setDbindex(int dbindex) + { + this.dbindex = dbindex; + } + @Override public boolean equals(Object obj) { @@ -85,7 +97,7 @@ public int hashCode() @Override public RedisDao createIndexerInstance() { - return new RedisDao(getHost(), getPort(), key, Secret.toString(password)); + return new RedisDao(getHost(), getPort(), key, Secret.toString(password), getDbindex()); } @Extension @@ -105,6 +117,11 @@ public int getDefaultPort() return 6379; } + public int getDefaultDbindex() + { + return 0; + } + public FormValidation doCheckKey(@QueryParameter("value") String value) { if (StringUtils.isBlank(value)) diff --git a/src/main/java/jenkins/plugins/logstash/persistence/RedisDao.java b/src/main/java/jenkins/plugins/logstash/persistence/RedisDao.java index 63848b12..2a4cf04d 100644 --- a/src/main/java/jenkins/plugins/logstash/persistence/RedisDao.java +++ b/src/main/java/jenkins/plugins/logstash/persistence/RedisDao.java @@ -48,10 +48,11 @@ public class RedisDao extends HostBasedLogstashIndexerDao { private final String password; private final String key; + private final int dbindex; //primary constructor used by indexer factory - public RedisDao(String host, int port, String key, String password) { - this(null, host, port, key, password); + public RedisDao(String host, int port, String key, String password, int dbindex) { + this(null, host, port, key, password, dbindex); } /* @@ -59,11 +60,12 @@ public RedisDao(String host, int port, String key, String password) { * With Powermock we can intercept the creation of the JedisPool and replace with a mock * making this constructor obsolete */ - RedisDao(JedisPool factory, String host, int port, String key, String password) { + RedisDao(JedisPool factory, String host, int port, String key, String password, int dbindex) { super(host, port); this.key = key; this.password = password; + this.dbindex = dbindex; if (StringUtils.isBlank(key)) { throw new IllegalArgumentException("redis key is required"); @@ -90,6 +92,11 @@ public String getKey() return key; } + public int getDbindex() + { + return dbindex; + } + @Override public void push(String data) throws IOException { Jedis jedis = null; @@ -100,6 +107,9 @@ public void push(String data) throws IOException { if (!StringUtils.isBlank(password)) { jedis.auth(password); } + if (dbindex > 0) { + jedis.select(dbindex); + } jedis.connect(); long result = jedis.rpush(key, data); diff --git a/src/main/resources/jenkins/plugins/logstash/configuration/Redis/configure-advanced.jelly b/src/main/resources/jenkins/plugins/logstash/configuration/Redis/configure-advanced.jelly index 019cb016..82773491 100644 --- a/src/main/resources/jenkins/plugins/logstash/configuration/Redis/configure-advanced.jelly +++ b/src/main/resources/jenkins/plugins/logstash/configuration/Redis/configure-advanced.jelly @@ -1,5 +1,8 @@ + + + diff --git a/src/main/resources/jenkins/plugins/logstash/configuration/Redis/help-dbindex.html b/src/main/resources/jenkins/plugins/logstash/configuration/Redis/help-dbindex.html new file mode 100644 index 00000000..8c5fd02e --- /dev/null +++ b/src/main/resources/jenkins/plugins/logstash/configuration/Redis/help-dbindex.html @@ -0,0 +1,4 @@ +
+ The Redis database index to store data in. +
+ diff --git a/src/test/java/jenkins/plugins/logstash/persistence/RedisDaoTest.java b/src/test/java/jenkins/plugins/logstash/persistence/RedisDaoTest.java index a666a508..ad877ad0 100644 --- a/src/test/java/jenkins/plugins/logstash/persistence/RedisDaoTest.java +++ b/src/test/java/jenkins/plugins/logstash/persistence/RedisDaoTest.java @@ -26,14 +26,14 @@ public class RedisDaoTest { @Mock JedisPool mockPool; @Mock Jedis mockJedis; - RedisDao createDao(String host, int port, String key, String username, String password) { - return new RedisDao(mockPool, host, port, key, password); + RedisDao createDao(String host, int port, String key, String username, String password, int dbindex) { + return new RedisDao(mockPool, host, port, key, password, dbindex); } @Before public void before() throws Exception { int port = (int) (Math.random() * 1000); - dao = createDao("localhost", port, "logstash", "username", "password"); + dao = createDao("localhost", port, "logstash", "username", "password", 0); when(mockPool.getResource()).thenReturn(mockJedis); } @@ -47,7 +47,7 @@ public void after() throws Exception { @Test(expected = IllegalArgumentException.class) public void constructorFailNullHost() throws Exception { try { - createDao(null, 6379, "logstash", "username", "password"); + createDao(null, 6379, "logstash", "username", "password", 0); } catch (IllegalArgumentException e) { assertEquals("Wrong error message was thrown", "host name is required", e.getMessage()); throw e; @@ -57,7 +57,7 @@ public void constructorFailNullHost() throws Exception { @Test(expected = IllegalArgumentException.class) public void constructorFailEmptyHost() throws Exception { try { - createDao(" ", 6379, "logstash", "username", "password"); + createDao(" ", 6379, "logstash", "username", "password", 0); } catch (IllegalArgumentException e) { assertEquals("Wrong error message was thrown", "host name is required", e.getMessage()); throw e; @@ -67,7 +67,7 @@ public void constructorFailEmptyHost() throws Exception { @Test(expected = IllegalArgumentException.class) public void constructorFailNullKey() throws Exception { try { - createDao("localhost", 6379, null, "username", "password"); + createDao("localhost", 6379, null, "username", "password", 0); } catch (IllegalArgumentException e) { assertEquals("Wrong error message was thrown", "redis key is required", e.getMessage()); throw e; @@ -77,7 +77,7 @@ public void constructorFailNullKey() throws Exception { @Test(expected = IllegalArgumentException.class) public void constructorFailEmptyKey() throws Exception { try { - createDao("localhost", 6379, " ", "username", "password"); + createDao("localhost", 6379, " ", "username", "password", 0); } catch (IllegalArgumentException e) { assertEquals("Wrong error message was thrown", "redis key is required", e.getMessage()); throw e; @@ -87,7 +87,7 @@ public void constructorFailEmptyKey() throws Exception { @Test public void constructorSuccess() throws Exception { // Unit under test - dao = createDao("localhost", 6379, "logstash", "username", "password"); + dao = createDao("localhost", 6379, "logstash", "username", "password", 0); // Verify results assertEquals("Wrong host name", "localhost", dao.getHost()); @@ -182,7 +182,7 @@ public void pushSuccessNoAuth() throws Exception { String json = "{ 'foo': 'bar' }"; // Initialize mocks - dao = createDao("localhost", 6379, "logstash", null, null); + dao = createDao("localhost", 6379, "logstash", null, null, 0); when(mockJedis.rpush("logstash", json)).thenReturn(1L); // Unit under test