Improve Cloud Foundry Java Client auto-configuration.
An organization and space are not required by all CF Java Client operations. Those properties should be optional to the client-autoconfiguration. The CF target URL should default to the CF an application is running on when it is deployed to CF.
This commit is contained in:
@@ -43,9 +43,10 @@ import org.springframework.context.annotation.Lazy;
|
||||
*
|
||||
* @author Josh Long
|
||||
* @author Ben Hale
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "spring.cloud.cloudfoundry", name = {"username", "password", "org", "space"})
|
||||
@ConditionalOnProperty(prefix = "spring.cloud.cloudfoundry", name = {"username", "password"})
|
||||
@ConditionalOnClass(name = {"reactor.core.publisher.Flux", "org.cloudfoundry.operations.DefaultCloudFoundryOperations",
|
||||
"org.cloudfoundry.reactor.client.ReactorCloudFoundryClient", "org.reactivestreams.Publisher"})
|
||||
@EnableConfigurationProperties(CloudFoundryProperties.class)
|
||||
@@ -64,16 +65,6 @@ public class CloudFoundryClientAutoConfiguration {
|
||||
return new CloudFoundryService(cloudFoundryOperations);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
@ConditionalOnMissingBean
|
||||
public ReactorCloudFoundryClient cloudFoundryClient(ConnectionContext connectionContext, TokenProvider tokenProvider) {
|
||||
return ReactorCloudFoundryClient.builder()
|
||||
.connectionContext(connectionContext)
|
||||
.tokenProvider(tokenProvider)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
@ConditionalOnMissingBean
|
||||
@@ -93,18 +84,13 @@ public class CloudFoundryClientAutoConfiguration {
|
||||
.space(space)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
@ConditionalOnMissingBean
|
||||
public DefaultConnectionContext connectionContext() {
|
||||
|
||||
String apiHost = this.cloudFoundryProperties.getUrl();
|
||||
Boolean skipSslValidation = this.cloudFoundryProperties.isSkipSslValidation();
|
||||
|
||||
return DefaultConnectionContext.builder()
|
||||
.apiHost(apiHost)
|
||||
.skipSslValidation(skipSslValidation)
|
||||
public ReactorCloudFoundryClient cloudFoundryClient(ConnectionContext connectionContext, TokenProvider tokenProvider) {
|
||||
return ReactorCloudFoundryClient.builder()
|
||||
.connectionContext(connectionContext)
|
||||
.tokenProvider(tokenProvider)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -128,6 +114,29 @@ public class CloudFoundryClientAutoConfiguration {
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
@ConditionalOnMissingBean
|
||||
public ReactorUaaClient uaaClient(ConnectionContext connectionContext, TokenProvider tokenProvider) {
|
||||
return ReactorUaaClient.builder()
|
||||
.connectionContext(connectionContext)
|
||||
.tokenProvider(tokenProvider)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
@ConditionalOnMissingBean
|
||||
public DefaultConnectionContext connectionContext() {
|
||||
String apiHost = this.cloudFoundryProperties.getUrl();
|
||||
Boolean skipSslValidation = this.cloudFoundryProperties.isSkipSslValidation();
|
||||
|
||||
return DefaultConnectionContext.builder()
|
||||
.apiHost(apiHost)
|
||||
.skipSslValidation(skipSslValidation)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
@ConditionalOnMissingBean
|
||||
@@ -139,16 +148,6 @@ public class CloudFoundryClientAutoConfiguration {
|
||||
.username(username)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
@ConditionalOnMissingBean
|
||||
public ReactorUaaClient uaaClient(ConnectionContext connectionContext, TokenProvider tokenProvider) {
|
||||
return ReactorUaaClient.builder()
|
||||
.connectionContext(connectionContext)
|
||||
.tokenProvider(tokenProvider)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -28,7 +28,10 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Configuration properties for a connection to a Cloud Foundry platform.
|
||||
*
|
||||
* @author Josh Long
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.cloud.cloudfoundry")
|
||||
public class CloudFoundryProperties implements InitializingBean {
|
||||
@@ -36,7 +39,8 @@ public class CloudFoundryProperties implements InitializingBean {
|
||||
/**
|
||||
* URL of Cloud Foundry API (Cloud Controller).
|
||||
*/
|
||||
private String url = "api.run.pivotal.io";
|
||||
@Value("${vcap.application.cf_api:api.run.pivotal.io}")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* Username to authenticate (usually an email address).
|
||||
@@ -49,12 +53,12 @@ public class CloudFoundryProperties implements InitializingBean {
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Organization name to authenticate with (default to user's default).
|
||||
* Organization name to initially target.
|
||||
*/
|
||||
private String org;
|
||||
|
||||
/**
|
||||
* Space name to authenticate with (default to user's default).
|
||||
* Space name to initially target.
|
||||
*/
|
||||
@Value("${vcap.application.space_name:}")
|
||||
private String space;
|
||||
@@ -127,19 +131,21 @@ public class CloudFoundryProperties implements InitializingBean {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
public void afterPropertiesSet() {
|
||||
this.url = safeUrl(this.url);
|
||||
this.password = this.password.trim();
|
||||
this.username = this.username.trim();
|
||||
this.org = this.org.trim();
|
||||
this.space = this.space.trim();
|
||||
if (this.org != null) {
|
||||
this.org = this.org.trim();
|
||||
}
|
||||
if (this.space != null) {
|
||||
this.space = this.space.trim();
|
||||
}
|
||||
|
||||
Map<String, String> vals = new HashMap<>();
|
||||
vals.put("org", getOrg());
|
||||
vals.put("url", getUrl());
|
||||
vals.put("username", getUsername());
|
||||
vals.put("password", getPassword());
|
||||
vals.put("space", getSpace());
|
||||
vals.forEach((key, value) -> Assert.hasText(value, String.format("'%s' must be provided", key)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user