-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move configuration from ToolInstallation to GlobalConfiguration #43
Changes from 3 commits
371e27b
67bc346
c252ac8
faf30cd
6b4c6d7
333ca41
ca4eca8
45a03ac
59d1a77
96deff1
379c5e4
1321104
4974276
227611b
d602a6e
7340194
6116410
ac70407
87c93cb
f021a43
fdc79cc
dac27f1
f2c4a24
add8146
d28cab4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package jenkins.plugins.logstash; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
|
@@ -10,6 +13,7 @@ | |
|
||
import com.cloudbees.syslog.MessageFormat; | ||
|
||
import org.apache.http.client.utils.URIBuilder; | ||
import hudson.Extension; | ||
import hudson.init.InitMilestone; | ||
import hudson.init.Initializer; | ||
|
@@ -75,6 +79,7 @@ public List<?> getIndexerTypes() | |
return LogstashIndexer.all(); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
@Initializer(after = InitMilestone.JOB_LOADED) | ||
public void migrateData() | ||
{ | ||
|
@@ -97,13 +102,21 @@ public void migrateData() | |
break; | ||
case ELASTICSEARCH: | ||
LOGGER.log(Level.INFO, "Migrating logstash configuration for Elastic Search"); | ||
ElasticSearch es = new ElasticSearch(); | ||
es.setHost(descriptor.getHost()); | ||
es.setPort(descriptor.getPort()); | ||
es.setKey(descriptor.getKey()); | ||
es.setUsername(descriptor.getUsername()); | ||
es.setPassword(descriptor.getPassword()); | ||
logstashIndexer = es; | ||
URI uri; | ||
try | ||
{ | ||
uri = (new URIBuilder(descriptor.getHost())) | ||
.setPort(descriptor.getPort()) | ||
.setPath("/" + descriptor.getKey()).build(); | ||
ElasticSearch es = new ElasticSearch(uri.toString()); | ||
es.setUsername(descriptor.getUsername()); | ||
es.setPassword(descriptor.getPassword()); | ||
logstashIndexer = es; | ||
} | ||
catch (URISyntaxException e) | ||
{ | ||
LOGGER.log(Level.INFO, "Migrating logstash configuration for Elastic Search failed: " + e.toString()); | ||
} | ||
break; | ||
case RABBIT_MQ: | ||
LOGGER.log(Level.INFO, "Migrating logstash configuration for RabbitMQ"); | ||
|
@@ -151,8 +164,7 @@ public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws | |
{ | ||
// when we bind the stapler request we get a new instance of logstashIndexer | ||
staplerRequest.bindJSON(this, json); | ||
|
||
if (logstashIndexer != null && !logstashIndexer.equals(activeIndexer)) | ||
if (!Objects.equals(logstashIndexer, activeIndexer)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add further comment explaining that we check for equality because |
||
{ | ||
activeIndexer = logstashIndexer; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package jenkins.plugins.logstash.configuration; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
@@ -16,24 +17,27 @@ | |
|
||
public class ElasticSearch extends LogstashIndexer<ElasticSearchDao> | ||
{ | ||
protected String key; | ||
protected String username; | ||
protected Secret password; | ||
private String username; | ||
private Secret password; | ||
private URI uri; | ||
|
||
@DataBoundConstructor | ||
public ElasticSearch() | ||
public ElasticSearch(String uri) throws URISyntaxException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we changing the constructor instead of using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the uri is not valid it will fail to save the configuration when it is checked in the constructor. |
||
{ | ||
this.uri = new URI(uri); | ||
validateUri(this.uri); | ||
} | ||
|
||
public String getKey() | ||
public URI getUri() | ||
{ | ||
return key; | ||
return uri; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setKey(String key) | ||
public void setUri(String value) throws URISyntaxException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor; for sake of consistency can we make setter accept URI? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With URI stapler is not able to autoconvert the String to an URI. I will switch to URL as persistence here. Then I can also use a DataBoundSetter that accepts a URL |
||
{ | ||
this.key = key; | ||
URI uri = new URI(value); | ||
validateUri(uri); | ||
this.uri = uri; | ||
} | ||
|
||
public String getUsername() | ||
|
@@ -61,23 +65,23 @@ public void setPassword(String password) | |
@Override | ||
public boolean equals(Object obj) | ||
{ | ||
if (obj == null) | ||
return false; | ||
if (this == obj) | ||
return true; | ||
if (!super.equals(obj)) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
ElasticSearch other = (ElasticSearch) obj; | ||
if (!Secret.toString(password).equals(other.getPassword())) | ||
{ | ||
return false; | ||
} | ||
if (key == null) | ||
if (uri == null) | ||
{ | ||
if (other.key != null) | ||
if (other.uri != null) | ||
return false; | ||
} | ||
else if (!key.equals(other.key)) | ||
else if (!uri.equals(other.uri)) | ||
{ | ||
return false; | ||
} | ||
|
@@ -98,7 +102,7 @@ public int hashCode() | |
{ | ||
final int prime = 31; | ||
int result = super.hashCode(); | ||
result = prime * result + ((key == null) ? 0 : key.hashCode()); | ||
result = prime * result + ((uri == null) ? 0 : uri.hashCode()); | ||
result = prime * result + ((username == null) ? 0 : username.hashCode()); | ||
result = prime * result + Secret.toString(password).hashCode(); | ||
return result; | ||
|
@@ -107,7 +111,7 @@ public int hashCode() | |
@Override | ||
public ElasticSearchDao createIndexerInstance() | ||
{ | ||
return new ElasticSearchDao(host, port, key, username, Secret.toString(password)); | ||
return new ElasticSearchDao(uri, username, Secret.toString(password)); | ||
} | ||
|
||
@Extension | ||
|
@@ -125,33 +129,48 @@ public int getDefaultPort() | |
return 9300; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. umm.. isn't ES default port 9200? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes but with URI, we don't need this at all. |
||
} | ||
|
||
@Override | ||
public FormValidation doCheckHost(@QueryParameter("value") String value) | ||
public FormValidation doCheckUri(@QueryParameter("value") String value) | ||
{ | ||
if (StringUtils.isBlank(value)) | ||
{ | ||
return FormValidation.warning(Messages.PleaseProvideHost()); | ||
} | ||
try | ||
{ | ||
new URL(value); | ||
URI uri = new URI(value); | ||
validateUri(uri); | ||
} | ||
catch (MalformedURLException e) | ||
catch (URISyntaxException | IllegalArgumentException e) | ||
{ | ||
return FormValidation.error(e.getMessage()); | ||
} | ||
return FormValidation.ok(); | ||
} | ||
} | ||
|
||
public FormValidation doCheckKey(@QueryParameter("value") String value) | ||
{ | ||
if (StringUtils.isBlank(value)) | ||
/** | ||
* Validates that the given uri has a scheme, a port and a path which is not empty or just "/" | ||
* | ||
* @param uri | ||
* @throws IllegalArgumentException when one of scheme, port or path | ||
*/ | ||
public static void validateUri(URI uri) throws IllegalArgumentException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we ignore those I'd add a check enforcing empty username and password (in authority part) |
||
{ | ||
try | ||
{ | ||
return FormValidation.error(Messages.ValueIsRequired()); | ||
uri.toURL(); | ||
} | ||
catch (MalformedURLException e) | ||
{ | ||
throw new IllegalArgumentException(e.getMessage()); | ||
} | ||
|
||
return FormValidation.ok(); | ||
if(uri.getPort() == -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not let users rely on default port? |
||
throw new IllegalArgumentException("Please specify a port."); | ||
} | ||
|
||
if(StringUtils.isBlank(uri.getPath()) || uri.getPath().trim().matches("^\\/+$")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have mixed feelings here. Can we make this into a warning instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. before it was also a must to have a non empty key |
||
throw new IllegalArgumentException("Please specify an elastic search key."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package jenkins.plugins.logstash.configuration; | ||
|
||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
import jenkins.plugins.logstash.persistence.AbstractLogstashIndexerDao; | ||
|
||
public abstract class HostBasedLogstashIndexer<T extends AbstractLogstashIndexerDao> extends LogstashIndexer<T> | ||
{ | ||
private String host; | ||
private int port; | ||
|
||
/** | ||
* Returns the host for connecting to the indexer. | ||
* | ||
* @return Host of the indexer | ||
*/ | ||
public String getHost() | ||
{ | ||
return host; | ||
} | ||
|
||
/** | ||
* Sets the host for connecting to the indexer. | ||
* | ||
* @param host | ||
* host to connect to. | ||
*/ | ||
@DataBoundSetter | ||
public void setHost(String host) | ||
{ | ||
this.host = host; | ||
} | ||
|
||
/** | ||
* Returns the port for connecting to the indexer. | ||
* | ||
* @return Port of the indexer | ||
*/ | ||
public int getPort() | ||
{ | ||
return port; | ||
} | ||
|
||
/** | ||
* Sets the port used for connecting to the indexer | ||
* | ||
* @param port | ||
* The port of the indexer | ||
*/ | ||
@DataBoundSetter | ||
public void setPort(int port) | ||
{ | ||
this.port = port; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((host == null) ? 0 : host.hashCode()); | ||
result = prime * result + port; | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
HostBasedLogstashIndexer<?> other = (HostBasedLogstashIndexer<?>) obj; | ||
if (host == null) { | ||
if (other.host != null) | ||
return false; | ||
} else if (!host.equals(other.host)) | ||
return false; | ||
if (port != other.port) | ||
return false; | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package jenkins.plugins.logstash.configuration; | ||
|
||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
public class HostConfiguration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't find any references to that class, am I missing something? |
||
{ | ||
|
||
protected String host; | ||
protected int port; | ||
|
||
@DataBoundConstructor | ||
public HostConfiguration() | ||
{ | ||
} | ||
|
||
public String getHost() | ||
{ | ||
return host; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setHost(String host) | ||
{ | ||
this.host = host; | ||
} | ||
|
||
public int getPort() | ||
{ | ||
return port; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setPort(int port) | ||
{ | ||
this.port = port; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((host == null) ? 0 : host.hashCode()); | ||
result = prime * result + port; | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
HostConfiguration other = (HostConfiguration) obj; | ||
if (host == null) { | ||
if (other.host != null) | ||
return false; | ||
} else if (!host.equals(other.host)) | ||
return false; | ||
if (port != other.port) | ||
return false; | ||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if this will work properly for keys that already start with
/
does this
UriBuilder
collapse consecutive/
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly the same behaviour as it was before in the ElasticSearchDao
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK then