Skip to content

Commit

Permalink
feat(cloudfoundry): improved error messages for failed service bindin…
Browse files Browse the repository at this point in the history
…gs (#5391)

* feat(cloudfoundry): improved error messages for failed service bindings

* feat(cloudfoundry): spotless apply

* feat(cloudfoundry): fix imports

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mattgogerly and mergify[bot] committed Jun 29, 2021
1 parent cc9c0bc commit 9427faa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,8 @@ public Void operate(List<Void> priorOutputs) {
.getClient()
.getServiceInstances()
.findAllServicesBySpaceAndNames(description.getSpace(), serviceInstanceNames)
.stream()
.forEach(s -> serviceInstanceGuids.put(s.getEntity().getName(), s.getMetadata().getGuid()));

if (serviceInstanceNames.size() != description.getServiceBindingRequests().size()) {
throw new CloudFoundryApiException(
"Number of service instances found does not match the number of service binding requests.");
}

List<CreateServiceBinding> bindings =
description.getServiceBindingRequests().stream()
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.io.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -144,8 +145,10 @@ public DeploymentResult operate(List priorOutputs) {

private void createServiceBindings(
CloudFoundryServerGroup serverGroup, DeployCloudFoundryServerGroupDescription description) {

List<String> serviceNames = description.getApplicationAttributes().getServices();
if (serviceNames == null || serviceNames.isEmpty()) return;

getTask()
.updateStatus(
PHASE,
Expand All @@ -154,32 +157,44 @@ private void createServiceBindings(
+ "' and services: "
+ description.getApplicationAttributes().getServices());

Map<String, String> serviceInstanceGuids = new HashMap<>();

// find guids for services
description
.getClient()
.getServiceInstances()
.findAllServicesBySpaceAndNames(serverGroup.getSpace(), serviceNames)
.forEach(s -> serviceInstanceGuids.put(s.getEntity().getName(), s.getMetadata().getGuid()));

// try and create service binding request for each service
List<CreateServiceBinding> bindings =
description
.getClient()
.getServiceInstances()
.findAllServicesBySpaceAndNames(
serverGroup.getSpace(), description.getApplicationAttributes().getServices())
.stream()
serviceNames.stream()
.map(
s ->
new CreateServiceBinding(
s.getMetadata().getGuid(), serverGroup.getId(), Collections.emptyMap()))
.collect(toList());

if (bindings.size() != description.getApplicationAttributes().getServices().size()) {
getTask()
.updateStatus(
PHASE,
"Failed to create Cloud Foundry service bindings between application '"
+ description.getServerGroupName()
+ "' and services: "
+ description.getApplicationAttributes().getServices());
throw new CloudFoundryApiException(
"Number of service instances does not match the number of service names");
}
s -> {
String serviceGuid = serviceInstanceGuids.get(s);
if (serviceGuid == null || serviceGuid.isEmpty()) {
getTask()
.updateStatus(
PHASE,
"Failed to create Cloud Foundry service bindings between application '"
+ description.getServerGroupName()
+ "' and services: "
+ serviceNames);

throw new CloudFoundryApiException(
"Unable to find service with the name: '"
+ s
+ "' in "
+ serverGroup.getSpace());
}

return new CreateServiceBinding(
serviceGuid, serverGroup.getId(), Collections.emptyMap());
})
.collect(Collectors.toList());

bindings.forEach(b -> description.getClient().getServiceInstances().createServiceBinding(b));

getTask()
.updateStatus(
PHASE,
Expand Down

0 comments on commit 9427faa

Please sign in to comment.