Skip to content

Commit

Permalink
JENKINS-52697: Configuration as Code (jenkinsci#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwinter69 authored and jakub-bochenski committed Apr 16, 2019
1 parent 6123a61 commit 4bf1216
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
<version>2.15</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>1.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws
save();
return true;
}

configuring = true;

// when we bind the stapler request we get a new instance of logstashIndexer.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/jenkins/plugins/logstash/PluginImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
import hudson.Plugin;
import hudson.model.Descriptor;
import jenkins.plugins.logstash.configuration.LogstashIndexer;
import jenkins.plugins.logstash.utils.URIConverter;

import java.net.URI;
import java.util.logging.Logger;

import org.kohsuke.stapler.Stapler;

public class PluginImpl extends Plugin {
private final static Logger LOG = Logger.getLogger(PluginImpl.class.getName());

Expand All @@ -40,6 +44,9 @@ public class PluginImpl extends Plugin {
@Override
public void start() throws Exception {
LOG.info("Logstash: a logstash agent to send jenkins logs to a logstash indexer.");

// Register a converter for URI, as nither Stapler nor apache commons beanutils have it
Stapler.CONVERT_UTILS.register(new URIConverter(), URI.class);
}

public DescriptorExtensionList<LogstashIndexer<?>, Descriptor<LogstashIndexer<?>>> getAllIndexers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.security.cert.CertificateException;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void setUri(URL url) throws URISyntaxException
this.uri = url.toURI();
}

public void setUri(URI uri) throws URISyntaxException
public void setUri(URI uri)
{
this.uri = uri;
}
Expand Down Expand Up @@ -221,6 +222,7 @@ private StandardCertificateCredentials getCredentials(String credentials)
}

@Extension
@Symbol("elasticSearch")
public static class ElasticSearchDescriptor extends LogstashIndexerDescriptor
{
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jenkins.plugins.logstash.configuration;

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
Expand All @@ -20,6 +21,7 @@ protected LogstashDao createIndexerInstance()
}

@Extension
@Symbol("logstash")
public static class Descriptor extends LogstashIndexerDescriptor
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.charset.Charset;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -174,6 +175,7 @@ public RabbitMqDao createIndexerInstance()
}

@Extension
@Symbol("rabbitMq")
public static class RabbitMqDescriptor extends LogstashIndexerDescriptor
{
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jenkins.plugins.logstash.configuration;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -88,6 +89,7 @@ public RedisDao createIndexerInstance()
}

@Extension
@Symbol("redis")
public static class RedisDescriptor extends LogstashIndexerDescriptor
{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jenkins.plugins.logstash.configuration;

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

Expand Down Expand Up @@ -76,6 +77,7 @@ public SyslogDao createIndexerInstance()
}

@Extension
@Symbol("syslog")
public static class SyslogDescriptor extends LogstashIndexerDescriptor
{

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/jenkins/plugins/logstash/utils/URIConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jenkins.plugins.logstash.utils;

import java.net.URI;

import org.apache.commons.beanutils.converters.AbstractConverter;

public class URIConverter extends AbstractConverter
{

@Override
protected Object convertToType(Class type, Object value) throws Throwable
{
return new URI(value.toString());
}

@Override
protected Class getDefaultType()
{
return URI.class;
}

}
82 changes: 82 additions & 0 deletions src/test/java/jenkins/plugins/logstash/ConfigAsCodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package jenkins.plugins.logstash;

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import io.jenkins.plugins.casc.ConfigurationAsCode;
import io.jenkins.plugins.casc.ConfiguratorException;
import jenkins.plugins.logstash.configuration.ElasticSearch;
import jenkins.plugins.logstash.configuration.Logstash;
import jenkins.plugins.logstash.configuration.RabbitMq;
import jenkins.plugins.logstash.configuration.Redis;

public class ConfigAsCodeTest
{

@Rule public JenkinsRule r = new JenkinsRule();

@Test
public void elasticSearch() throws ConfiguratorException
{
ConfigurationAsCode.get().configure(ConfigAsCodeTest.class.getResource("/jcasc/elasticSearch.yaml").toString());
LogstashConfiguration c = LogstashConfiguration.getInstance();
assertThat(c.isEnabled(), is(true));
assertThat(c.isEnableGlobally(), is(true));
assertThat(c.isMilliSecondTimestamps(), is(true));
assertThat(c.getLogstashIndexer(), is(instanceOf(ElasticSearch.class)));
ElasticSearch es = (ElasticSearch) c.getLogstashIndexer();
assertThat(es.getUri().toString(), is("http://localhost:9200/jenkins/test"));
assertThat(es.getMimeType(), is("application/json"));
assertThat(es.getUsername(), is("es"));
}

@Test
public void logstash() throws ConfiguratorException
{
ConfigurationAsCode.get().configure(ConfigAsCodeTest.class.getResource("/jcasc/logstash.yaml").toString());
LogstashConfiguration c = LogstashConfiguration.getInstance();
assertThat(c.isEnabled(), is(true));
assertThat(c.isEnableGlobally(), is(true));
assertThat(c.isMilliSecondTimestamps(), is(true));
assertThat(c.getLogstashIndexer(), is(instanceOf(Logstash.class)));
Logstash logstash = (Logstash) c.getLogstashIndexer();
assertThat(logstash.getHost(), is("localhost"));
assertThat(logstash.getPort(), is(9200));
}

@Test
public void rabbitMq() throws ConfiguratorException
{
ConfigurationAsCode.get().configure(ConfigAsCodeTest.class.getResource("/jcasc/rabbitmq.yaml").toString());
LogstashConfiguration c = LogstashConfiguration.getInstance();
assertThat(c.isEnabled(), is(true));
assertThat(c.isEnableGlobally(), is(true));
assertThat(c.isMilliSecondTimestamps(), is(true));
assertThat(c.getLogstashIndexer(), is(instanceOf(RabbitMq.class)));
RabbitMq rabbitMq = (RabbitMq) c.getLogstashIndexer();
assertThat(rabbitMq.getHost(), is("localhost"));
assertThat(rabbitMq.getPort(), is(9200));
assertThat(rabbitMq.getVirtualHost(), is("/vhost"));
assertThat(rabbitMq.getUsername(), is("rabbit"));
}

@Test
public void redis() throws ConfiguratorException
{
ConfigurationAsCode.get().configure(ConfigAsCodeTest.class.getResource("/jcasc/redis.yaml").toString());
LogstashConfiguration c = LogstashConfiguration.getInstance();
assertThat(c.isEnabled(), is(true));
assertThat(c.isEnableGlobally(), is(true));
assertThat(c.isMilliSecondTimestamps(), is(true));
assertThat(c.getLogstashIndexer(), is(instanceOf(Redis.class)));
Redis redis = (Redis) c.getLogstashIndexer();
assertThat(redis.getHost(), is("localhost"));
assertThat(redis.getPort(), is(9200));
assertThat(redis.getKey(), is("redis"));
}
}
26 changes: 26 additions & 0 deletions src/test/resources/jcasc/elasticSearch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
jenkins:
agentProtocols:
- "JNLP4-connect"
- "Ping"
disableRememberMe: false
mode: NORMAL
numExecutors: 2
primaryView:
all:
name: "all"
quietPeriod: 5
scmCheckoutRetryCount: 0
slaveAgentPort: 0
views:
- all:
name: "all"
unclassified:
logstashConfiguration:
enableGlobally: true
enabled: true
logstashIndexer:
elasticSearch:
mimeType: "application/json"
uri: "http://localhost:9200/jenkins/test"
username: "es"
milliSecondTimestamps: true
25 changes: 25 additions & 0 deletions src/test/resources/jcasc/logstash.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
jenkins:
agentProtocols:
- "JNLP4-connect"
- "Ping"
disableRememberMe: false
mode: NORMAL
numExecutors: 2
primaryView:
all:
name: "all"
quietPeriod: 5
scmCheckoutRetryCount: 0
slaveAgentPort: 0
views:
- all:
name: "all"
unclassified:
logstashConfiguration:
enableGlobally: true
enabled: true
logstashIndexer:
logstash:
host: "localhost"
port: 9200
milliSecondTimestamps: true
27 changes: 27 additions & 0 deletions src/test/resources/jcasc/rabbitmq.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
jenkins:
agentProtocols:
- "JNLP4-connect"
- "Ping"
disableRememberMe: false
mode: NORMAL
numExecutors: 2
primaryView:
all:
name: "all"
quietPeriod: 5
scmCheckoutRetryCount: 0
slaveAgentPort: 0
views:
- all:
name: "all"
unclassified:
logstashConfiguration:
enableGlobally: true
enabled: true
logstashIndexer:
rabbitMq:
host: "localhost"
port: 9200
virtualHost: "/vhost"
username: "rabbit"
milliSecondTimestamps: true
26 changes: 26 additions & 0 deletions src/test/resources/jcasc/redis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
jenkins:
agentProtocols:
- "JNLP4-connect"
- "Ping"
disableRememberMe: false
mode: NORMAL
numExecutors: 2
primaryView:
all:
name: "all"
quietPeriod: 5
scmCheckoutRetryCount: 0
slaveAgentPort: 0
views:
- all:
name: "all"
unclassified:
logstashConfiguration:
enableGlobally: true
enabled: true
logstashIndexer:
redis:
host: "localhost"
port: 9200
key: "redis"
milliSecondTimestamps: true

0 comments on commit 4bf1216

Please sign in to comment.