Commit 6a70b901 authored by Sergey Kuptsov's avatar Sergey Kuptsov Committed by Stephane Nicoll

Add influxDB java client auto-configuration

See gh-9066
parent 9053bad2
...@@ -661,6 +661,11 @@ ...@@ -661,6 +661,11 @@
<artifactId>quartz</artifactId> <artifactId>quartz</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<optional>true</optional>
</dependency>
<!-- Annotation processing --> <!-- Annotation processing -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
......
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.influx;
import com.google.common.base.Strings;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB.
*
* @author Sergey Kuptsov
*/
@Configuration
@ConditionalOnClass(InfluxDB.class)
@EnableConfigurationProperties(InfluxDBProperties.class)
public class InfluxDBAutoConfiguration {
private final InfluxDBProperties properties;
public InfluxDBAutoConfiguration(InfluxDBProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean
public InfluxDB influxDB() {
if (Strings.isNullOrEmpty(this.properties.getUser())) {
return InfluxDBFactory.connect(
this.properties.getUrl()
);
}
else {
return InfluxDBFactory.connect(
this.properties.getUrl(),
this.properties.getUser(),
this.properties.getPassword()
);
}
}
}
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.influx;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for InfluxDB.
*
* @author Sergey Kuptsov
*/
@ConfigurationProperties(prefix = "spring.data.influx")
public class InfluxDBProperties {
/**
* The url to connect to.
*/
private String url;
/**
* The username which is used to authorize against the influxDB instance.
*/
private String user;
/**
* The password for the username which is used to authorize against the influxDB.
*/
private String password;
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return this.user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Auto-configuration for InfluxDB.
*/
package org.springframework.boot.autoconfigure.influx;
...@@ -122,7 +122,8 @@ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ ...@@ -122,7 +122,8 @@ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDBAutoConfiguration
# Failure analyzers # Failure analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.diagnostics.FailureAnalyzer=\
......
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.influx;
import org.assertj.core.api.Java6Assertions;
import org.influxdb.InfluxDB;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Tests for {@link InfluxDBAutoConfiguration}.
*
* @author Sergey Kuptsov
*/
public class InfluxDBAutoConfigurationTest {
private AnnotationConfigApplicationContext context;
@Before
public void setUp() {
this.context = new AnnotationConfigApplicationContext();
}
@After
public void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void canEnableConfiguration() {
this.context.register(InfluxDBAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.influx.url=http://localhost");
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.influx.password:password");
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.influx.user:user");
this.context.refresh();
Java6Assertions.assertThat(this.context.getBeansOfType(InfluxDB.class)).isNotEmpty();
}
@Test
public void canEnableWithEmptyUserConfiguration() {
this.context.register(InfluxDBAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.influx.url=http://localhost");
this.context.refresh();
Java6Assertions.assertThat(this.context.getBeansOfType(InfluxDB.class)).isNotEmpty();
}
}
...@@ -190,6 +190,7 @@ ...@@ -190,6 +190,7 @@
<webjars-locator.version>0.32-1</webjars-locator.version> <webjars-locator.version>0.32-1</webjars-locator.version>
<wsdl4j.version>1.6.3</wsdl4j.version> <wsdl4j.version>1.6.3</wsdl4j.version>
<xml-apis.version>1.4.01</xml-apis.version> <xml-apis.version>1.4.01</xml-apis.version>
<influxdb-java.version>2.5</influxdb-java.version>
<!-- Plugin versions --> <!-- Plugin versions -->
<build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version> <build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version> <exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
...@@ -2472,6 +2473,11 @@ ...@@ -2472,6 +2473,11 @@
<artifactId>xml-apis</artifactId> <artifactId>xml-apis</artifactId>
<version>${xml-apis.version}</version> <version>${xml-apis.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>${influxdb-java.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<build> <build>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment