Skip to content

Commit

Permalink
Replace use of ServiceInstance with API-specific request and response…
Browse files Browse the repository at this point in the history
… classes in ServiceInstanceController.
  • Loading branch information
scottfrederick committed Dec 8, 2015
1 parent a20936d commit a716a2b
Show file tree
Hide file tree
Showing 31 changed files with 766 additions and 819 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ task testsJar(type: Jar) {
baseName = baseNameTests
}

task sourcesJar(type: Jar, dependsOn:classes) {
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn:javadoc) {
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.cloudfoundry.community.servicebroker.controller;

import javax.servlet.http.HttpServletResponse;

import org.cloudfoundry.community.servicebroker.exception.*;
import org.cloudfoundry.community.servicebroker.model.*;
import org.slf4j.*;
Expand All @@ -13,64 +11,50 @@

/**
* Base controller.
*
* @author sgreenberg@gopivotal.com
*
* @author sgreenberg@gopivotal.com
*/
public class BaseController {

private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

@ExceptionHandler(ServiceBrokerApiVersionException.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(
ServiceBrokerApiVersionException ex,
HttpServletResponse response) {
return getErrorResponse(ex.getMessage(), HttpStatus.PRECONDITION_FAILED);
public ResponseEntity<ErrorMessage> handleException(ServiceBrokerApiVersionException ex) {
return getErrorResponse(ex.getMessage(), HttpStatus.PRECONDITION_FAILED);
}

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(
HttpMessageNotReadableException ex,
HttpServletResponse response) {
return getErrorResponse(ex.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
public ResponseEntity<ErrorMessage> handleException(HttpMessageNotReadableException ex) {
return getErrorResponse(ex.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(
MethodArgumentNotValidException ex,
HttpServletResponse response) {
BindingResult result = ex.getBindingResult();
String message = "Missing required fields:";
for (FieldError error: result.getFieldErrors()) {
message += " " + error.getField();
}
public ResponseEntity<ErrorMessage> handleException(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
String message = "Missing required fields:";
for (FieldError error : result.getFieldErrors()) {
message += " " + error.getField();
}
return getErrorResponse(message, HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(
Exception ex,
HttpServletResponse response) {
public ResponseEntity<ErrorMessage> handleException(Exception ex) {
logger.warn("Exception", ex);
return getErrorResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
return getErrorResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(ServiceBrokerAsyncRequiredException.class)
public ResponseEntity<AsyncRequiredErrorMessage> handleException(
ServiceBrokerAsyncRequiredException ex,
HttpServletResponse response) {
return new ResponseEntity<AsyncRequiredErrorMessage>(
public ResponseEntity<AsyncRequiredErrorMessage> handleException(ServiceBrokerAsyncRequiredException ex) {
return new ResponseEntity<>(
new AsyncRequiredErrorMessage(ex.getDescription()), HttpStatus.UNPROCESSABLE_ENTITY);

}

public ResponseEntity<ErrorMessage> getErrorResponse(String message, HttpStatus status) {
return new ResponseEntity<ErrorMessage>(new ErrorMessage(message),
status);
return new ResponseEntity<>(new ErrorMessage(message), status);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
import org.springframework.web.bind.annotation.*;

/**
* See: http://docs.cloudfoundry.com/docs/running/architecture/services/writing-service.html
* See: http://docs.cloudfoundry.org/services/api.html
*
* @author sgreenberg@gopivotal.com
*/
@Controller
@RequestMapping("/v2/service_instances")
public class ServiceInstanceController extends BaseController {

public static final String BASE_PATH = "/v2/service_instances";

private static final Logger logger = LoggerFactory.getLogger(ServiceInstanceController.class);

private ServiceInstanceService service;
Expand All @@ -31,108 +30,133 @@ public ServiceInstanceController(ServiceInstanceService service, CatalogService
this.service = service;
this.catalogService = catalogService;
}

@RequestMapping(value = BASE_PATH + "/{instanceId}", method = RequestMethod.PUT)
public ResponseEntity<ServiceInstance> createServiceInstance(
@PathVariable("instanceId") String serviceInstanceId,
@RequestParam(value="accepts_incomplete", required=false) boolean acceptsIncomplete,

@RequestMapping(value = "/{instanceId}", method = RequestMethod.PUT)
public ResponseEntity<?> createServiceInstance(
@PathVariable("instanceId") String serviceInstanceId,
@Valid @RequestBody CreateServiceInstanceRequest request) throws
ServiceDefinitionDoesNotExistException,
ServiceInstanceExistsException,
ServiceBrokerException, ServiceBrokerAsyncRequiredException {
logger.debug("PUT: " + BASE_PATH + "/{instanceId}?accepts_incomplete=" + acceptsIncomplete
+ ", createServiceInstance(), serviceInstanceId = " + serviceInstanceId);
ServiceDefinition svc = catalogService.getServiceDefinition(request.getServiceDefinitionId());
if (svc == null) {
throw new ServiceDefinitionDoesNotExistException(request.getServiceDefinitionId());
}
ServiceInstance instance = service.createServiceInstance(
request.withServiceDefinition(svc).and().withServiceInstanceId(serviceInstanceId)
.and().withAcceptsIncomplete(acceptsIncomplete));
logger.debug("ServiceInstance Created: " + instance.getServiceInstanceId());

return new ResponseEntity<>(
instance, instance.isAsync() ? HttpStatus.ACCEPTED : HttpStatus.CREATED);

ServiceBrokerException,
ServiceBrokerAsyncRequiredException {
logger.debug("createServiceInstance(): serviceInstanceId=" + serviceInstanceId);

ServiceDefinition serviceDefinition = getServiceDefinition(request.getServiceDefinitionId());

request.withServiceDefinition(serviceDefinition)
.withServiceInstanceId(serviceInstanceId);

CreateServiceInstanceResponse response = service.createServiceInstance(request);

logger.debug("createServiceInstance(): succeeded: serviceInstanceId=" + serviceInstanceId);

return new ResponseEntity<>(response, response.isAsync() ? HttpStatus.ACCEPTED : HttpStatus.CREATED);
}

@RequestMapping(value = BASE_PATH + "/{instanceId}/last_operation", method = RequestMethod.GET)
public ResponseEntity<?> getServiceInstanceLastOperation(
@PathVariable("instanceId") String instanceId) {

logger.debug("GET: " + BASE_PATH + "/{instanceId}/last_operation"
+ ", getServiceInstance(), serviceInstanceId = " + instanceId);
@RequestMapping(value = "/{instanceId}/last_operation", method = RequestMethod.GET)
public ResponseEntity<?> getServiceInstanceLastOperation(@PathVariable("instanceId") String instanceId)
throws ServiceInstanceDoesNotExistException {

ServiceInstance instance = service.getServiceInstance(instanceId);
logger.debug("getServiceInstanceLastOperation(): serviceInstanceId=" + instanceId);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
if (null == instance) {
return new ResponseEntity<>("{}", headers, HttpStatus.GONE);
}
ServiceInstanceLastOperation lastOperation = instance.getServiceInstanceLastOperation();
logger.debug("ServiceInstance: " + instance.getServiceInstanceId() + "is in " + lastOperation.getState() + " state. Details : " +lastOperation.getDescription());
return new ResponseEntity<>(lastOperation, headers, HttpStatus.OK);
GetLastServiceOperationRequest request = new GetLastServiceOperationRequest(instanceId);

GetLastServiceOperationResponse response = service.getLastOperation(request);

logger.debug("getServiceInstanceLastOperation(): succeeded: serviceInstanceId=" + instanceId
+ ", state=" + response.getState()
+ ", description=" + response.getDescription());

return new ResponseEntity<>(response, response.isDeletionComplete() ? HttpStatus.GONE : HttpStatus.OK);
}
@RequestMapping(value = BASE_PATH + "/{instanceId}", method = RequestMethod.DELETE)

@RequestMapping(value = "/{instanceId}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteServiceInstance(
@PathVariable("instanceId") String instanceId,
@PathVariable("instanceId") String instanceId,
@RequestParam("service_id") String serviceId,
@RequestParam("plan_id") String planId,
@RequestParam(value="accepts_incomplete", required=false) boolean acceptsIncomplete)
@RequestParam(value="accepts_incomplete", required=false) boolean acceptsIncomplete)
throws ServiceBrokerException, ServiceBrokerAsyncRequiredException {
logger.debug( "DELETE: " + BASE_PATH + "/{instanceId}?accepts_incomplete=" + acceptsIncomplete
+ ", deleteServiceInstanceBinding(), serviceInstanceId = " + instanceId
+ ", serviceId = " + serviceId
+ ", planId = " + planId);
ServiceInstance instance = service.deleteServiceInstance(
new DeleteServiceInstanceRequest(instanceId, serviceId, planId, acceptsIncomplete));

if (instance == null) {
logger.debug("deleteServiceInstance(): "
+ "serviceInstanceId=" + instanceId
+ ", serviceId=" + serviceId
+ ", planId=" + planId
+ ", acceptsIncomplete=" + acceptsIncomplete);

try {
DeleteServiceInstanceRequest request =
new DeleteServiceInstanceRequest(instanceId, serviceId, planId, acceptsIncomplete);

DeleteServiceInstanceResponse response = service.deleteServiceInstance(request);

logger.debug("deleteServiceInstance(): succeeded: "
+ "serviceInstanceId=" + instanceId);

return new ResponseEntity<>("{}", response.isAsync() ? HttpStatus.ACCEPTED : HttpStatus.OK);
} catch (ServiceInstanceDoesNotExistException e) {
logger.debug("deleteServiceInstance(): error: "
+ "serviceInstanceId=" + instanceId
+ ", exception=" + e.getMessage());

return new ResponseEntity<>("{}", HttpStatus.GONE);
}

logger.debug("ServiceInstance Deleted: " + instance.getServiceInstanceId());
return new ResponseEntity<>(instance,
instance.isAsync() ? HttpStatus.ACCEPTED : HttpStatus.OK);
}
@RequestMapping(value = BASE_PATH + "/{instanceId}", method = RequestMethod.PATCH)

@RequestMapping(value = "/{instanceId}", method = RequestMethod.PATCH)
public ResponseEntity<String> updateServiceInstance(
@PathVariable("instanceId") String instanceId,
@RequestParam(value="accepts_incomplete", required=false) boolean acceptsIncomplete,
@Valid @RequestBody UpdateServiceInstanceRequest request) throws
@Valid @RequestBody UpdateServiceInstanceRequest request) throws
ServiceInstanceUpdateNotSupportedException,
ServiceInstanceDoesNotExistException,
ServiceBrokerException, ServiceBrokerAsyncRequiredException {
logger.debug("UPDATE: " + BASE_PATH + "/{instanceId}?accepts_incomplete=" + acceptsIncomplete
+ ", updateServiceInstanceBinding(), serviceInstanceId = "
+ instanceId + ", instanceId = " + instanceId + ", planId = "
+ request.getPlanId());
ServiceInstance instance = service.updateServiceInstance(
request.withInstanceId(instanceId).withAcceptsIncomplete(acceptsIncomplete));
logger.debug("ServiceInstance updated: " + instance.getServiceInstanceId());
HttpStatus status = instance.isAsync() ? HttpStatus.ACCEPTED : HttpStatus.OK;
return new ResponseEntity<>("{}", status);
ServiceInstanceDoesNotExistException,
ServiceDefinitionDoesNotExistException,
ServiceBrokerException,
ServiceBrokerAsyncRequiredException {
logger.debug("updateServiceInstance(): "
+ "instanceId = " + instanceId
+ ", planId = " + request.getPlanId());

ServiceDefinition serviceDefinition = getServiceDefinition(request.getServiceDefinitionId());

request.withServiceDefinition(serviceDefinition).withServiceInstanceId(instanceId);

UpdateServiceInstanceResponse response = service.updateServiceInstance(request);

logger.debug("updateServiceInstance(): succeeded: "
+ "serviceInstanceId=" + instanceId);

return new ResponseEntity<>("{}", response.isAsync() ? HttpStatus.ACCEPTED : HttpStatus.OK);
}

private ServiceDefinition getServiceDefinition(String serviceDefinitionId)
throws ServiceDefinitionDoesNotExistException {
ServiceDefinition serviceDefinition = catalogService.getServiceDefinition(serviceDefinitionId);
if (serviceDefinition == null) {
throw new ServiceDefinitionDoesNotExistException(serviceDefinitionId);
}
return serviceDefinition;
}

@ExceptionHandler(ServiceInstanceDoesNotExistException.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(ServiceInstanceDoesNotExistException ex) {
return getErrorResponse(ex.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(ServiceDefinitionDoesNotExistException.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(ServiceDefinitionDoesNotExistException ex) {
return getErrorResponse(ex.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
return getErrorResponse(ex.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(ServiceInstanceExistsException.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(ServiceInstanceExistsException ex) {
return getErrorResponse(ex.getMessage(), HttpStatus.CONFLICT);
return getErrorResponse(ex.getMessage(), HttpStatus.CONFLICT);
}

@ExceptionHandler(ServiceInstanceUpdateNotSupportedException.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(ServiceInstanceUpdateNotSupportedException ex) {
return getErrorResponse(ex.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class ServiceBrokerApiVersionException extends Exception {

public ServiceBrokerApiVersionException(String expectedVersion, String providedVersion) {
super("The provided service broker API version is not supported: "
+ "Expected Version = " + expectedVersion + ", "
+ "Provided Version = " + providedVersion);
+ "expected version=" + expectedVersion
+ ", provided version = " + providedVersion);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ServiceDefinitionDoesNotExistException extends Exception {
private static final long serialVersionUID = -62090827040416788L;

public ServiceDefinitionDoesNotExistException(String serviceDefinitionId) {
super("ServiceDefinition does not exist: id = " + serviceDefinitionId);
super("Service definition does not exist: id=" + serviceDefinitionId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class ServiceInstanceBindingExistsException extends Exception {
private static final long serialVersionUID = -914571358227517785L;

public ServiceInstanceBindingExistsException(ServiceInstanceBinding binding) {
super("ServiceInstanceBinding already exists: serviceInstanceBinding.id = "
+ binding.getId()
+ ", serviceInstance.id = " + binding.getServiceInstanceId());
super("Service instance binding already exists: " +
"serviceInstanceBinding.id=" + binding.getId()
+ ", serviceInstance.id=" + binding.getServiceInstanceId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ServiceInstanceDoesNotExistException extends Exception {
private static final long serialVersionUID = -1879753092397657116L;

public ServiceInstanceDoesNotExistException(String serviceInstanceId) {
super("ServiceInstance does not exist: id = " + serviceInstanceId);
super("Service instance does not exist: id=" + serviceInstanceId);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.cloudfoundry.community.servicebroker.exception;

import org.cloudfoundry.community.servicebroker.model.ServiceInstance;

/**
* Thrown when a duplicate service instance creation request is
* received.
Expand All @@ -12,10 +10,10 @@ public class ServiceInstanceExistsException extends Exception {

private static final long serialVersionUID = -914571358227517785L;

public ServiceInstanceExistsException(ServiceInstance instance) {
super("ServiceInstance with the given ID already exists: " +
"ServiceInstance.id = " + instance.getServiceInstanceId() +
", Service.id = " + instance.getServiceDefinitionId());
public ServiceInstanceExistsException(String serviceInstanceId, String serviceDefinitionId) {
super("Service instance with the given ID already exists: " +
"serviceInstanceId=" + serviceInstanceId +
", serviceDefinitionId=" + serviceDefinitionId);
}

}
Loading

0 comments on commit a716a2b

Please sign in to comment.