Make discovery client more robust if there is no connection
The default behaviour is to know nothing if the client can't connect to the cloud controller.
This commit is contained in:
@@ -114,9 +114,16 @@ public class CloudFoundryDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String s) {
|
||||
CloudApplication applications = this.cloudFoundryClient.getApplication(s);
|
||||
return this.createServiceInstancesFromCloudApplications(
|
||||
Collections.singletonList(applications));
|
||||
try {
|
||||
CloudApplication applications = this.cloudFoundryClient.getApplication(s);
|
||||
return this.createServiceInstancesFromCloudApplications(
|
||||
Collections.singletonList(applications));
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("Could not get service instances: " + e.getClass() + " ("
|
||||
+ e.getMessage() + ")");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isRunning(CloudApplication ca) {
|
||||
@@ -136,7 +143,15 @@ public class CloudFoundryDiscoveryClient implements DiscoveryClient {
|
||||
@Override
|
||||
public List<String> getServices() {
|
||||
List<String> services = new ArrayList<>();
|
||||
List<CloudApplication> applications = this.cloudFoundryClient.getApplications();
|
||||
List<CloudApplication> applications;
|
||||
try {
|
||||
applications = this.cloudFoundryClient.getApplications();
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("Could not get applications: " + e.getClass() + " ("
|
||||
+ e.getMessage() + ")");
|
||||
applications = Collections.emptyList();
|
||||
}
|
||||
Set<String> serviceIds = new HashSet<>();
|
||||
for (CloudApplication ca : applications) {
|
||||
if (isRunning(ca)) {
|
||||
|
||||
@@ -131,6 +131,15 @@ public class CloudFoundryDiscoveryClientTest {
|
||||
assertEquals(instances.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstancesNotAvailable() {
|
||||
given(this.cloudFoundryClient.getApplications()).willThrow(new RuntimeException("Planned"));
|
||||
|
||||
List<ServiceInstance> instances = this.cloudFoundryDiscoveryClient
|
||||
.getInstances(this.hiServiceServiceId);
|
||||
assertEquals(instances.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalServiceInstanceRunning() {
|
||||
|
||||
@@ -170,7 +179,7 @@ public class CloudFoundryDiscoveryClientTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalServiceInstanceNotFoundg() {
|
||||
public void testLocalServiceInstanceNotFound() {
|
||||
|
||||
given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication))
|
||||
.willThrow(new CloudFoundryException(HttpStatus.NOT_FOUND));
|
||||
|
||||
Reference in New Issue
Block a user