Skip to content

Add influxDB java client auto-configuration #9066

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@
<artifactId>narayana-jts-integration</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<optional>true</optional>
</dependency>
<!-- Annotation processing -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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;
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDBAutoConfiguration

# Failure analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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();
}
}
6 changes: 6 additions & 0 deletions spring-boot-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
<webjars-locator.version>0.32-1</webjars-locator.version>
<wsdl4j.version>1.6.3</wsdl4j.version>
<xml-apis.version>1.4.01</xml-apis.version>
<influxdb-java.version>2.5</influxdb-java.version>
<!-- Plugin versions -->
<build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
Expand Down Expand Up @@ -2347,6 +2348,11 @@
<artifactId>xml-apis</artifactId>
<version>${xml-apis.version}</version>
</dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>${influxdb-java.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
Expand Down