Skip to content

Commit d860d87

Browse files
wilkinsonamhalbritterphilwebb
committed
Add ConnectionDetail support to Influx auto-configuration
Update Influx auto-configuration so that `InfluxDbConnectionDetails` beans may be optionally used to provide connection details. See gh-34657 Co-Authored-By: Mortitz Halbritter <[email protected]> Co-Authored-By: Phillip Webb <[email protected]>
1 parent 4cc7958 commit d860d87

File tree

3 files changed

+164
-5
lines changed

3 files changed

+164
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,25 +16,34 @@
1616

1717
package org.springframework.boot.autoconfigure.influx;
1818

19+
import java.net.URI;
20+
1921
import okhttp3.OkHttpClient;
2022
import org.influxdb.InfluxDB;
2123
import org.influxdb.impl.InfluxDBImpl;
2224

2325
import org.springframework.beans.factory.ObjectProvider;
2426
import org.springframework.boot.autoconfigure.AutoConfiguration;
2527
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2630
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2731
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2832
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2933
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3034
import org.springframework.context.annotation.Bean;
35+
import org.springframework.context.annotation.Condition;
36+
import org.springframework.context.annotation.Conditional;
3137

3238
/**
3339
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB.
3440
*
3541
* @author Sergey Kuptsov
3642
* @author Stephane Nicoll
3743
* @author Eddú Meléndez
44+
* @author Moritz Halbritter
45+
* @author Andy Wilkinson
46+
* @author Phillip Webb
3847
* @since 2.0.0
3948
*/
4049
@AutoConfiguration
@@ -44,11 +53,14 @@ public class InfluxDbAutoConfiguration {
4453

4554
@Bean
4655
@ConditionalOnMissingBean
47-
@ConditionalOnProperty("spring.influx.url")
56+
@Conditional(InfluxDBCondition.class)
4857
public InfluxDB influxDb(InfluxDbProperties properties, ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder,
49-
ObjectProvider<InfluxDbCustomizer> customizers) {
50-
InfluxDB influxDb = new InfluxDBImpl(properties.getUrl(), properties.getUser(), properties.getPassword(),
51-
determineBuilder(builder.getIfAvailable()));
58+
ObjectProvider<InfluxDbCustomizer> customizers,
59+
ObjectProvider<InfluxDbConnectionDetails> connectionDetailsProvider) {
60+
InfluxDbConnectionDetails connectionDetails = connectionDetailsProvider
61+
.getIfAvailable(() -> new PropertiesInfluxDbConnectionDetails(properties));
62+
InfluxDB influxDb = new InfluxDBImpl(connectionDetails.getUrl().toString(), connectionDetails.getUsername(),
63+
connectionDetails.getPassword(), determineBuilder(builder.getIfAvailable()));
5264
customizers.orderedStream().forEach((customizer) -> customizer.customize(influxDb));
5365
return influxDb;
5466
}
@@ -60,4 +72,54 @@ private static OkHttpClient.Builder determineBuilder(InfluxDbOkHttpClientBuilder
6072
return new OkHttpClient.Builder();
6173
}
6274

75+
/**
76+
* {@link Condition} that matches when either {@code spring.influx.url} has been set
77+
* or there is an {@link InfluxDbConnectionDetails} bean.
78+
*/
79+
static final class InfluxDBCondition extends AnyNestedCondition {
80+
81+
InfluxDBCondition() {
82+
super(ConfigurationPhase.REGISTER_BEAN);
83+
}
84+
85+
@ConditionalOnProperty(prefix = "spring.influx", name = "url")
86+
private static final class InfluxUrlCondition {
87+
88+
}
89+
90+
@ConditionalOnBean(InfluxDbConnectionDetails.class)
91+
private static final class InfluxDbConnectionDetailsCondition {
92+
93+
}
94+
95+
}
96+
97+
/**
98+
* Adapts {@link InfluxDbProperties} to {@link InfluxDbConnectionDetails}.
99+
*/
100+
static class PropertiesInfluxDbConnectionDetails implements InfluxDbConnectionDetails {
101+
102+
private final InfluxDbProperties properties;
103+
104+
PropertiesInfluxDbConnectionDetails(InfluxDbProperties properties) {
105+
this.properties = properties;
106+
}
107+
108+
@Override
109+
public URI getUrl() {
110+
return URI.create(this.properties.getUrl());
111+
}
112+
113+
@Override
114+
public String getUsername() {
115+
return this.properties.getUser();
116+
}
117+
118+
@Override
119+
public String getPassword() {
120+
return this.properties.getPassword();
121+
}
122+
123+
}
124+
63125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.influx;
18+
19+
import java.net.URI;
20+
21+
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
22+
23+
/**
24+
* Details required to establish a connection to an InfluxDB service.
25+
*
26+
* @author Moritz Halbritter
27+
* @author Andy Wilkinson
28+
* @author Phillip Webb
29+
* @since 3.1.0
30+
*/
31+
public interface InfluxDbConnectionDetails extends ConnectionDetails {
32+
33+
/**
34+
* URL of the InfluxDB instance to which to connect.
35+
* @return the URL of the InfluxDB instance to which to connect
36+
*/
37+
URI getUrl();
38+
39+
/**
40+
* Login user.
41+
* @return the login user or {@code null}
42+
*/
43+
String getUsername();
44+
45+
/**
46+
* Login password.
47+
* @return the login password or {@code null}
48+
*/
49+
String getPassword();
50+
51+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.influx;
1818

19+
import java.net.URI;
1920
import java.util.concurrent.TimeUnit;
2021

2122
import okhttp3.OkHttpClient;
@@ -38,6 +39,9 @@
3839
* @author Sergey Kuptsov
3940
* @author Stephane Nicoll
4041
* @author Eddú Meléndez
42+
* @author Moritz Halbritter
43+
* @author Andy Wilkinson
44+
* @author Phillip Webb
4145
*/
4246
class InfluxDbAutoConfigurationTests {
4347

@@ -49,6 +53,27 @@ void influxDbRequiresUrl() {
4953
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(InfluxDB.class));
5054
}
5155

56+
@Test
57+
void shouldUseConnectionDetails() {
58+
this.contextRunner.withBean(InfluxDbConnectionDetails.class, this::influxDbConnectionDetails).run((context) -> {
59+
assertThat(context).hasSingleBean(InfluxDB.class);
60+
InfluxDB influxDb = context.getBean(InfluxDB.class);
61+
assertThat(influxDb).hasFieldOrPropertyWithValue("hostName", "localhost");
62+
});
63+
}
64+
65+
@Test
66+
void connectionDetailsOverwriteProperties() {
67+
this.contextRunner.withBean(InfluxDbConnectionDetails.class, this::influxDbConnectionDetails)
68+
.withPropertyValues("spring.influx.url=http://some-other-host", "spring.influx.user=user",
69+
"spring.influx.password=password")
70+
.run((context) -> {
71+
assertThat(context).hasSingleBean(InfluxDB.class);
72+
InfluxDB influxDb = context.getBean(InfluxDB.class);
73+
assertThat(influxDb).hasFieldOrPropertyWithValue("hostName", "localhost");
74+
});
75+
}
76+
5277
@Test
5378
void influxDbCanBeCustomized() {
5479
this.contextRunner
@@ -95,6 +120,27 @@ private int getReadTimeoutProperty(AssertableApplicationContext context) {
95120
return callFactory.readTimeoutMillis();
96121
}
97122

123+
private InfluxDbConnectionDetails influxDbConnectionDetails() {
124+
return new InfluxDbConnectionDetails() {
125+
126+
@Override
127+
public URI getUrl() {
128+
return URI.create("http://localhost");
129+
}
130+
131+
@Override
132+
public String getUsername() {
133+
return "user-1";
134+
}
135+
136+
@Override
137+
public String getPassword() {
138+
return "password-1";
139+
}
140+
141+
};
142+
}
143+
98144
@Configuration(proxyBeanMethods = false)
99145
static class CustomOkHttpClientBuilderProviderConfig {
100146

0 commit comments

Comments
 (0)