Skip to content

Commit

Permalink
change to not fail update when the service is optional
Browse files Browse the repository at this point in the history
LMCROSSITXSADEPLOY-2784
  • Loading branch information
I562548 committed Jan 16, 2024
1 parent 0573010 commit 98e3c99
Show file tree
Hide file tree
Showing 14 changed files with 547 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ public class Messages {
public static final String COULD_NOT_BIND_OPTIONAL_SERVICE_TO_APP = "Could not bind optional service \"{0}\" to application \"{1}\"";
public static final String COULD_NOT_UNBIND_OPTIONAL_SERVICE_TO_APP = "Could not unbind optional service \"{0}\" to application \"{1}\"";
public static final String COULD_NOT_CREATE_OPTIONAL_SERVICE = "Could not create optional service \"{0}\"";
public static final String COULD_NOT_UPDATE_TAGS_OF_OPTIONAL_SERVICE = "Could not update tags of optional service \"{0}\" : {1}";
public static final String COULD_NOT_UPDATE_METADATA_OF_OPTIONAL_SERVICE = "Could not update metadata of optional service \"{0}\" : {1}";
public static final String COULD_NOT_UPDATE_PARAMETERS_OPTIONAL_SERVICE = "Could not update parameters of optional service \"{0}\" : {1}";
public static final String COULD_NOT_UPDATE_PLAN_OPTIONAL_SERVICE = "Could not update plan of optional service \"{0}\" : {1}";
public static final String COULD_NOT_UPDATE_SYSLOG_DRAIN_URL_OPTIONAL_SERVICE = "Could not update syslog drain url of optional service \"{0}\" : {1}";
public static final String COULD_NOT_GET_SERVICE_KEYS_FOR_OPTIONAL_SERVICE = "Could not get service keys for optional service \"{0}\"";
public static final String DEFAULT_FAILED_OPERATION_DESCRIPTION = "The service broker returned an error with no description!";
public static final String ERROR_DURING_CLEAN_UP_0 = "Error during clean-up: {0}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import org.springframework.util.Assert;

import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import com.sap.cloudfoundry.client.facade.CloudControllerException;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.CloudServiceBrokerException;
import com.sap.cloudfoundry.client.facade.domain.CloudServiceInstance;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation;

Expand All @@ -44,7 +42,12 @@ protected OperationExecutionState executeOperation(ProcessContext context, Cloud
if (operationExecutionState.isPresent()) {
return operationExecutionState.get();
}
processServiceCreationFailure(context, serviceInstance, e);
String exceptionDescription = MessageFormat.format(Messages.COULD_NOT_CREATE_OPTIONAL_SERVICE, serviceInstance.getName());
CloudOperationException cloudOperationException = new CloudOperationException(e.getStatusCode(),
e.getStatusText(),
exceptionDescription);

processServiceActionFailure(context, serviceInstance, cloudOperationException);
}
return OperationExecutionState.FINISHED;
}
Expand Down Expand Up @@ -74,21 +77,6 @@ private void setServiceGuid(ProcessContext context, CloudServiceInstanceExtended
new DynamicResolvableParametersContextUpdater(context).updateServiceGuid(serviceInstance);
}

private void processServiceCreationFailure(ProcessContext context, CloudServiceInstanceExtended serviceInstance,
CloudOperationException e) {
if (!serviceInstance.isOptional()) {
String detailedDescription = MessageFormat.format(Messages.ERROR_CREATING_SERVICE, serviceInstance.getName(),
serviceInstance.getLabel(), serviceInstance.getPlan(), e.getDescription());
if (e.getStatusCode() == HttpStatus.BAD_GATEWAY) {
context.setVariable(Variables.SERVICE_OFFERING, serviceInstance.getLabel());
throw new CloudServiceBrokerException(e.getStatusCode(), e.getStatusText(), detailedDescription);
}
throw new CloudControllerException(e.getStatusCode(), e.getStatusText(), detailedDescription);
}
getStepLogger().warn(MessageFormat.format(Messages.COULD_NOT_CREATE_OPTIONAL_SERVICE, serviceInstance.getName()), e,
ExceptionMessageTailMapper.map(configuration, CloudComponents.SERVICE_BROKERS, serviceInstance.getLabel()));
}

private Optional<OperationExecutionState> getServiceInstanceStateIfCreated(CloudControllerClient controllerClient,
CloudServiceInstanceExtended serviceInstance,
CloudOperationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import javax.inject.Inject;

import com.sap.cloudfoundry.client.facade.CloudControllerException;
import com.sap.cloudfoundry.client.facade.CloudServiceBrokerException;
import org.cloudfoundry.multiapps.common.util.JsonUtil;
import org.cloudfoundry.multiapps.controller.client.lib.domain.CloudServiceInstanceExtended;
import org.cloudfoundry.multiapps.controller.core.util.OperationExecutionState;
Expand All @@ -17,6 +19,7 @@
import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation;
import org.springframework.http.HttpStatus;

public abstract class ServiceStep extends AsyncFlowableStep {

Expand Down Expand Up @@ -61,6 +64,20 @@ private OperationExecutionState executeOperationAndHandleExceptions(ProcessConte
protected abstract OperationExecutionState executeOperation(ProcessContext context, CloudControllerClient controllerClient,
CloudServiceInstanceExtended service);

protected void processServiceActionFailure(ProcessContext context, CloudServiceInstanceExtended serviceInstance,
CloudOperationException e) {
if (!serviceInstance.isOptional()) {
String detailedDescription = MessageFormat.format(Messages.ERROR_CREATING_SERVICE, serviceInstance.getName(),
serviceInstance.getLabel(), serviceInstance.getPlan(), e.getDescription());
if (e.getStatusCode() == HttpStatus.BAD_GATEWAY) {
context.setVariable(Variables.SERVICE_OFFERING, serviceInstance.getLabel());
throw new CloudServiceBrokerException(e.getStatusCode(), e.getStatusText(), detailedDescription);
}
throw new CloudControllerException(e.getStatusCode(), e.getStatusText(), detailedDescription);
}
getStepLogger().warn(e.getDescription());
}

protected abstract ServiceOperation.Type getOperationType();

protected ServiceOperationGetter getServiceOperationGetter() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cloudfoundry.multiapps.controller.process.steps;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
Expand All @@ -13,6 +14,7 @@
import org.springframework.context.annotation.Scope;

import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation;

@Named("updateServiceMetadataStep")
Expand All @@ -24,10 +26,21 @@ protected OperationExecutionState executeOperation(ProcessContext context, Cloud
CloudServiceInstanceExtended service) {
getStepLogger().debug(Messages.UPDATING_METADATA_OF_SERVICE_INSTANCE_0, service.getName(), service.getResourceName());

UUID serviceGuid = client.getRequiredServiceInstanceGuid(service.getName());
client.updateServiceInstanceMetadata(serviceGuid, service.getV3Metadata());
try {
UUID serviceGuid = client.getRequiredServiceInstanceGuid(service.getName());
client.updateServiceInstanceMetadata(serviceGuid, service.getV3Metadata());
getStepLogger().debug(Messages.UPDATING_METADATA_OF_SERVICE_INSTANCE_0_DONE, service.getName());
} catch (CloudOperationException e) {
String exceptionDescription = MessageFormat.format(Messages.COULD_NOT_UPDATE_METADATA_OF_OPTIONAL_SERVICE, service.getName(),
e.getDescription());
CloudOperationException cloudOperationException = new CloudOperationException(e.getStatusCode(),
e.getStatusText(),
exceptionDescription);

processServiceActionFailure(context, service, cloudOperationException);
return OperationExecutionState.FINISHED;
}

getStepLogger().debug(Messages.UPDATING_METADATA_OF_SERVICE_INSTANCE_0_DONE, service.getName());
return OperationExecutionState.EXECUTING;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cloudfoundry.multiapps.controller.process.steps;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;

Expand All @@ -12,6 +13,7 @@
import org.springframework.context.annotation.Scope;

import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation;

@Named("updateServiceParametersStep")
Expand All @@ -27,9 +29,19 @@ protected OperationExecutionState executeOperation(ProcessContext context, Cloud
}
getStepLogger().info(Messages.UPDATING_SERVICE, service.getName());

client.updateServiceParameters(service.getName(), service.getCredentials());

getStepLogger().debug(Messages.SERVICE_UPDATED, service.getName());
try {
client.updateServiceParameters(service.getName(), service.getCredentials());
getStepLogger().debug(Messages.SERVICE_UPDATED, service.getName());
} catch (CloudOperationException e) {
String exceptionDescription = MessageFormat.format(Messages.COULD_NOT_UPDATE_PARAMETERS_OPTIONAL_SERVICE, service.getName(),
e.getDescription());
CloudOperationException cloudOperationException = new CloudOperationException(e.getStatusCode(),
e.getStatusText(),
exceptionDescription);

processServiceActionFailure(context, service, cloudOperationException);
return OperationExecutionState.FINISHED;
}

return OperationExecutionState.EXECUTING;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cloudfoundry.multiapps.controller.process.steps;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;

Expand All @@ -12,6 +13,7 @@
import org.springframework.context.annotation.Scope;

import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation;

@Named("updateServicePlanStep")
Expand All @@ -27,9 +29,20 @@ protected OperationExecutionState executeOperation(ProcessContext context, Cloud
}
getStepLogger().debug(Messages.UPDATING_SERVICE_0_WITH_PLAN_1, service.getName(), service.getPlan());

client.updateServicePlan(service.getName(), service.getPlan());
try {
client.updateServicePlan(service.getName(), service.getPlan());
getStepLogger().debug(Messages.SERVICE_PLAN_FOR_SERVICE_0_UPDATED, service.getName());
} catch (CloudOperationException e) {
String exceptionDescription = MessageFormat.format(Messages.COULD_NOT_UPDATE_PLAN_OPTIONAL_SERVICE, service.getName(),
e.getDescription());
CloudOperationException cloudOperationException = new CloudOperationException(e.getStatusCode(),
e.getStatusText(),
exceptionDescription);

processServiceActionFailure(context, service, cloudOperationException);
return OperationExecutionState.FINISHED;
}

getStepLogger().debug(Messages.SERVICE_PLAN_FOR_SERVICE_0_UPDATED, service.getName());
return OperationExecutionState.EXECUTING;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cloudfoundry.multiapps.controller.process.steps;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;

Expand All @@ -12,6 +13,7 @@
import org.springframework.context.annotation.Scope;

import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation.Type;

Expand All @@ -28,9 +30,20 @@ protected OperationExecutionState executeOperation(ProcessContext context, Cloud
}
getStepLogger().info(Messages.UPDATING_SERVICE_SYSLOG_URL, service.getName());

client.updateServiceSyslogDrainUrl(service.getName(), service.getSyslogDrainUrl());
try {
client.updateServiceSyslogDrainUrl(service.getName(), service.getSyslogDrainUrl());
getStepLogger().debug(Messages.SERVICE_SYSLOG_URL_UPDATED, service.getName());
} catch (CloudOperationException e) {
String exceptionDescription = MessageFormat.format(Messages.COULD_NOT_UPDATE_SYSLOG_DRAIN_URL_OPTIONAL_SERVICE,
service.getName(), e.getDescription());
CloudOperationException cloudOperationException = new CloudOperationException(e.getStatusCode(),
e.getStatusText(),
exceptionDescription);

processServiceActionFailure(context, service, cloudOperationException);
return OperationExecutionState.FINISHED;
}

getStepLogger().debug(Messages.SERVICE_SYSLOG_URL_UPDATED, service.getName());
return OperationExecutionState.EXECUTING;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cloudfoundry.multiapps.controller.process.steps;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;

Expand All @@ -12,6 +13,7 @@
import org.springframework.context.annotation.Scope;

import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation;

@Named("updateServiceTagsStep")
Expand All @@ -27,9 +29,20 @@ protected OperationExecutionState executeOperation(ProcessContext context, Cloud
}
getStepLogger().info(Messages.UPDATING_SERVICE_TAGS, service.getName());

client.updateServiceTags(service.getName(), service.getTags());
try {
client.updateServiceTags(service.getName(), service.getTags());
getStepLogger().debug(Messages.SERVICE_TAGS_UPDATED, service.getName());
} catch (CloudOperationException e) {
String exceptionDescription = MessageFormat.format(Messages.COULD_NOT_UPDATE_TAGS_OF_OPTIONAL_SERVICE, service.getName(),
e.getDescription());
CloudOperationException cloudOperationException = new CloudOperationException(e.getStatusCode(),
e.getStatusText(),
exceptionDescription);

processServiceActionFailure(context, service, cloudOperationException);
return OperationExecutionState.FINISHED;
}

getStepLogger().debug(Messages.SERVICE_TAGS_UPDATED, service.getName());
return OperationExecutionState.EXECUTING;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package org.cloudfoundry.multiapps.controller.process.steps;

import static java.lang.Boolean.TRUE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.domain.ImmutableCloudMetadata;
import org.cloudfoundry.client.v3.Metadata;
import org.cloudfoundry.multiapps.common.test.Tester;
import org.cloudfoundry.multiapps.controller.client.lib.domain.CloudServiceInstanceExtended;
import org.cloudfoundry.multiapps.controller.client.lib.domain.ImmutableCloudServiceInstanceExtended;
import org.cloudfoundry.multiapps.controller.core.cf.CloudControllerClientProvider;
import org.cloudfoundry.multiapps.controller.core.util.ApplicationConfiguration;
import org.cloudfoundry.multiapps.controller.persistence.services.FileService;
Expand Down Expand Up @@ -37,6 +45,7 @@
import org.slf4j.LoggerFactory;

import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import org.springframework.http.HttpStatus;

public abstract class SyncFlowableStepTest<T extends SyncFlowableStep> {

Expand All @@ -48,6 +57,12 @@ public abstract class SyncFlowableStepTest<T extends SyncFlowableStep> {
protected static final String SPACE_GUID = "spaceGuid";
protected final String TEST_CORRELATION_ID = "test";
protected final String TEST_TASK_ID = "testTask";
protected final String RETRY_STEP_EXECUTION_STATUS = "RETRY";
protected final String DONE_STEP_EXECUTION_STATUS = "DONE";
protected static final String SERVICE_NAME = "test-service";
private static final String METADATA_LABEL = "test-label";
private static final String METADATA_LABEL_VALUE = "test-label-value";
private static final String SYSLOG_DRAIN_URL = "test-syslog-url";

protected final Tester tester = Tester.forClass(getClass());

Expand Down Expand Up @@ -137,6 +152,32 @@ private ExecutionQuery createExecutionQueryMock() {
return mockExecutionQuery;
}

protected void assertExecutionStepStatus(String executionStepStatus) {
assertEquals(executionStepStatus, getExecutionStatus());
}

protected void prepareServiceToProcess(CloudServiceInstanceExtended serviceToProcess) {
context.setVariable(Variables.SERVICE_TO_PROCESS, serviceToProcess);
}

protected void prepareClient(CloudServiceInstanceExtended serviceToProcess) {
when(client.getRequiredServiceInstanceGuid(SERVICE_NAME)).thenReturn(serviceToProcess.getGuid());
}

protected CloudServiceInstanceExtended buildServiceToProcess(boolean isOptional) {
return ImmutableCloudServiceInstanceExtended.builder()
.name(SERVICE_NAME)
.metadata(ImmutableCloudMetadata.builder()
.guid(UUID.randomUUID())
.build())
.syslogDrainUrl(SYSLOG_DRAIN_URL)
.v3Metadata(Metadata.builder()
.label(METADATA_LABEL, METADATA_LABEL_VALUE)
.build())
.isOptional(isOptional)
.build();
}

protected void assertStepFinishedSuccessfully() {
assertEquals(StepPhase.DONE.toString(), getExecutionStatus());
}
Expand Down
Loading

0 comments on commit 98e3c99

Please sign in to comment.