org.springframework.cloud.servicebroker.exception.ServiceInstanceExistsException Java Examples

The following examples show how to use org.springframework.cloud.servicebroker.exception.ServiceInstanceExistsException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceInstanceControllerIntegrationTest.java    From spring-cloud-open-service-broker with Apache License 2.0 6 votes vote down vote up
@Test
void createDuplicateServiceInstanceIdFails() throws Exception {
	setupCatalogService();

	setupServiceInstanceService(new ServiceInstanceExistsException(SERVICE_INSTANCE_ID, serviceDefinition.getId()));

	MvcResult mvcResult = mockMvc.perform(put(buildCreateUpdateUrl())
			.content(createRequestBody)
			.contentType(MediaType.APPLICATION_JSON)
			.accept(MediaType.APPLICATION_JSON))
			.andExpect(request().asyncStarted())
			.andReturn();

	mockMvc.perform(asyncDispatch(mvcResult))
			.andExpect(status().isConflict())
			.andExpect(jsonPath("$.description", containsString(SERVICE_INSTANCE_ID)));
}
 
Example #2
Source File: ServiceInstanceControllerIntegrationTest.java    From spring-cloud-open-service-broker with Apache License 2.0 6 votes vote down vote up
@Test
void createDuplicateServiceInstanceIdFails() {
	setupCatalogService();

	setupServiceInstanceService(new ServiceInstanceExistsException(SERVICE_INSTANCE_ID, serviceDefinition.getId()));

	client.put().uri(buildCreateUpdateUrl())
			.contentType(MediaType.APPLICATION_JSON)
			.bodyValue(createRequestBody)
			.accept(MediaType.APPLICATION_JSON)
			.exchange()
			.expectStatus().is4xxClientError()
			.expectStatus().isEqualTo(HttpStatus.CONFLICT)
			.expectBody()
			.jsonPath("$.description").isNotEmpty()
			.consumeWith(result -> assertDescriptionContains(result,
					String.format("serviceInstanceId=%s, serviceDefinitionId=%s", SERVICE_INSTANCE_ID,
							serviceDefinition.getId())));
}
 
Example #3
Source File: MongoServiceInstanceService.java    From cloudfoundry-service-broker with Apache License 2.0 6 votes vote down vote up
@Override
public CreateServiceInstanceResponse createServiceInstance(CreateServiceInstanceRequest request) {
	// TODO MongoDB dashboard
	ServiceInstance instance = repository.findOne(request.getServiceInstanceId());
	if (instance != null) {
		throw new ServiceInstanceExistsException(request.getServiceInstanceId(), request.getServiceDefinitionId());
	}

	instance = new ServiceInstance(request);

	if (mongo.databaseExists(instance.getServiceInstanceId())) {
		// ensure the instance is empty
		mongo.deleteDatabase(instance.getServiceInstanceId());
	}

	MongoDatabase db = mongo.createDatabase(instance.getServiceInstanceId());
	if (db == null) {
		throw new ServiceBrokerException("Failed to create new DB instance: " + instance.getServiceInstanceId());
	}
	repository.save(instance);

	return new CreateServiceInstanceResponse();
}
 
Example #4
Source File: EcsService.java    From ecs-cf-service-broker with Apache License 2.0 5 votes vote down vote up
Map<String, Object> createBucket(String id, ServiceDefinitionProxy service,
                                 PlanProxy plan, Map<String, Object> parameters) {
    if (parameters == null) parameters = new HashMap<>();

    logger.info(String.format("Creating bucket %s", id));
    try {
        if (bucketExists(id)) {
            throw new ServiceInstanceExistsException(id, service.getId());
        }
        // merge serviceSettings into parameters, overwriting parameter values
        // with service/plan serviceSettings, since serviceSettings are forced
        // by administrator through the catalog.
        parameters.putAll(plan.getServiceSettings());
        parameters.putAll(service.getServiceSettings());

        // Validate the reclaim-policy
        if (!ReclaimPolicy.isPolicyAllowed(parameters)) {
            throw new ServiceBrokerException("Reclaim Policy "+ReclaimPolicy.getReclaimPolicy(parameters)+" is not one of the allowed polices "+ReclaimPolicy.getAllowedReclaimPolicies(parameters));
        }

        BucketAction.create(connection, new ObjectBucketCreate(prefix(id),
                broker.getNamespace(), replicationGroupID, parameters));

        if (parameters.containsKey(QUOTA) && parameters.get(QUOTA) != null) {
            logger.info("Applying quota");
            Map<String, Integer> quota = (Map<String, Integer>) parameters.get(QUOTA);
            BucketQuotaAction.create(connection, prefix(id), broker.getNamespace(), quota.get(LIMIT), quota.get(WARN));
        }

        if (parameters.containsKey(DEFAULT_RETENTION) && parameters.get(DEFAULT_RETENTION) != null) {
            logger.info("Applying retention policy");
            BucketRetentionAction.update(connection, broker.getNamespace(),
                    prefix(id), (int) parameters.get(DEFAULT_RETENTION));
        }
    } catch (Exception e) {
        logger.error(String.format("Failed to create bucket %s", id), e);
        throw new ServiceBrokerException(e);
    }
    return parameters;
}
 
Example #5
Source File: EcsService.java    From ecs-cf-service-broker with Apache License 2.0 5 votes vote down vote up
Map<String, Object> createNamespace(String id, ServiceDefinitionProxy service, PlanProxy plan, Map<String, Object> parameters)
        throws EcsManagementClientException {
    if (namespaceExists(id))
        throw new ServiceInstanceExistsException(id, service.getId());
    if (parameters == null) parameters = new HashMap<>();
    // merge serviceSettings into parameters, overwriting parameter values
    // with service/plan serviceSettings, since serviceSettings are forced
    // by administrator through the catalog.
    parameters.putAll(plan.getServiceSettings());
    parameters.putAll(service.getServiceSettings());
    NamespaceAction.create(connection, new NamespaceCreate(prefix(id),
            replicationGroupID, parameters));

    if (parameters.containsKey(QUOTA)) {
        @SuppressWarnings(UNCHECKED)
        Map<String, Integer> quota = (Map<String, Integer>) parameters
                .get(QUOTA);
        NamespaceQuotaParam quotaParam = new NamespaceQuotaParam(id,
                quota.get(LIMIT), quota.get(WARN));
        NamespaceQuotaAction.create(connection, prefix(id), quotaParam);
    }

    if (parameters.containsKey(RETENTION)) {
        @SuppressWarnings(UNCHECKED)
        Map<String, Integer> retention = (Map<String, Integer>) parameters
                .get(RETENTION);
        for (Map.Entry<String, Integer> entry : retention.entrySet()) {
            NamespaceRetentionAction.create(connection, prefix(id),
                    new RetentionClassCreate(entry.getKey(),
                            entry.getValue()));
        }
    }
    return parameters;
}
 
Example #6
Source File: ServiceBrokerExceptionHandlerTest.java    From spring-cloud-open-service-broker with Apache License 2.0 5 votes vote down vote up
@Test
void serviceInstanceExistsException() {
	ServiceInstanceExistsException exception =
			new ServiceInstanceExistsException("service-instance-id", "service-definition-id");

	ErrorMessage errorMessage = exceptionHandler.handleException(exception);

	assertThat(errorMessage.getMessage()).contains("serviceInstanceId=service-instance-id");
	assertThat(errorMessage.getMessage()).contains("serviceDefinitionId=service-definition-id");
}
 
Example #7
Source File: MongoServiceInstanceServiceTest.java    From cloudfoundry-service-broker with Apache License 2.0 4 votes vote down vote up
@Test(expected=ServiceInstanceExistsException.class)
public void serviceInstanceCreationFailsWithExistingInstance() throws Exception {
	when(repository.findOne(any(String.class))).thenReturn(ServiceInstanceFixture.getServiceInstance());

	service.createServiceInstance(buildCreateRequest());
}
 
Example #8
Source File: ServiceBrokerExceptionHandler.java    From spring-cloud-open-service-broker with Apache License 2.0 2 votes vote down vote up
/**
 * Handle a {@link ServiceInstanceExistsException}
 *
 * @param ex the exception
 * @return an error message
 */
@ExceptionHandler(ServiceInstanceExistsException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public ErrorMessage handleException(ServiceInstanceExistsException ex) {
	return getErrorResponse(ex);
}