Search in sources :

Example 36 with TransactionExecutionException

use of com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException in project cloudbreak by hortonworks.

the class CertRenewalService method renewInternalCertificate.

public void renewInternalCertificate(SdxCluster sdxCluster) {
    try {
        FlowIdentifier flowIdentifier = ThreadBasedUserCrnProvider.doAsInternalActor(regionAwareInternalCrnGeneratorFactory.iam().getInternalCrnForServiceAsString(), () -> stackV4Endpoint.renewInternalCertificate(WORKSPACE_ID_DEFAULT, sdxCluster.getStackCrn()));
        transactionService.required(() -> saveChainIdAndStatus(sdxCluster, flowIdentifier));
    } catch (TransactionExecutionException e) {
        throw new TransactionRuntimeExecutionException(e);
    } catch (WebApplicationException e) {
        String errorMessage = webApplicationExceptionMessageExtractor.getErrorMessage(e);
        throw new RuntimeException(errorMessage);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) FlowIdentifier(com.sequenceiq.flow.api.model.FlowIdentifier) TransactionRuntimeExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException)

Example 37 with TransactionExecutionException

use of com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException in project cloudbreak by hortonworks.

the class CertRenewalService method renewCertificate.

public void renewCertificate(SdxCluster sdxCluster, String userCrn) {
    try {
        transactionService.required(() -> {
            FlowIdentifier flowIdentifier = ThreadBasedUserCrnProvider.doAsInternalActor(regionAwareInternalCrnGeneratorFactory.iam().getInternalCrnForServiceAsString(), () -> stackV4Endpoint.renewCertificate(WORKSPACE_ID_DEFAULT, sdxCluster.getClusterName(), userCrn));
            saveChainIdAndStatus(sdxCluster, flowIdentifier);
        });
    } catch (TransactionExecutionException e) {
        throw new TransactionRuntimeExecutionException(e);
    }
}
Also used : TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) FlowIdentifier(com.sequenceiq.flow.api.model.FlowIdentifier) TransactionRuntimeExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException)

Example 38 with TransactionExecutionException

use of com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException in project cloudbreak by hortonworks.

the class ClusterTemplateV4Controller method getByName.

@Override
@CheckPermissionByResourceName(action = AuthorizationResourceAction.DESCRIBE_CLUSTER_DEFINITION)
public ClusterTemplateV4Response getByName(Long workspaceId, @ResourceName String name) {
    try {
        ClusterTemplate clusterTemplate = transactionService.required(() -> clusterTemplateService.getByNameForWorkspaceId(name, threadLocalService.getRequestedWorkspaceId()));
        ClusterTemplateV4Response response = transactionService.required(() -> clusterTemplateToClusterTemplateV4ResponseConverter.convert(clusterTemplate));
        Optional.ofNullable(response.getEnvironmentCrn()).ifPresent(crn -> environmentServiceDecorator.prepareEnvironment(response));
        return response;
    } catch (TransactionExecutionException cse) {
        LOGGER.warn("Unable to find cluster definition due to " + cse.getMessage(), cse.getCause());
        throw new CloudbreakServiceException("Unable to obtain cluster definition!", cse.getCause());
    }
}
Also used : ClusterTemplateV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.clustertemplate.responses.ClusterTemplateV4Response) ClusterTemplate(com.sequenceiq.cloudbreak.domain.stack.cluster.ClusterTemplate) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) CheckPermissionByResourceName(com.sequenceiq.authorization.annotation.CheckPermissionByResourceName)

Example 39 with TransactionExecutionException

use of com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException in project cloudbreak by hortonworks.

the class ClusterTemplateV4Controller method getByCrn.

@Override
@CheckPermissionByResourceCrn(action = AuthorizationResourceAction.DESCRIBE_CLUSTER_DEFINITION)
public ClusterTemplateV4Response getByCrn(Long workspaceId, @TenantAwareParam @ResourceCrn String crn) {
    boolean internalTenant = isInternalTenant();
    try {
        ClusterTemplate clusterTemplate = transactionService.required(() -> clusterTemplateService.getByCrn(crn, threadLocalService.getRequestedWorkspaceId(), internalTenant));
        ClusterTemplateV4Response response = transactionService.required(() -> clusterTemplateToClusterTemplateV4ResponseConverter.convert(clusterTemplate));
        if (!StringUtils.isEmpty(response.getEnvironmentCrn())) {
            environmentServiceDecorator.prepareEnvironment(response);
        } else {
            LOGGER.warn("Skipping response decoration with environment name. Environment CRN was empty.");
        }
        return response;
    } catch (TransactionExecutionException cse) {
        LOGGER.warn("Unable to find cluster definition due to {}", cse.getMessage());
        throw new CloudbreakServiceException("Unable to obtain cluster definition!");
    }
}
Also used : ClusterTemplateV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.clustertemplate.responses.ClusterTemplateV4Response) ClusterTemplate(com.sequenceiq.cloudbreak.domain.stack.cluster.ClusterTemplate) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) CheckPermissionByResourceCrn(com.sequenceiq.authorization.annotation.CheckPermissionByResourceCrn)

Example 40 with TransactionExecutionException

use of com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException in project cloudbreak by hortonworks.

the class UserServiceTest method testCreateUserWithDuplicateException.

@Test
public void testCreateUserWithDuplicateException() throws TransactionExecutionException {
    doAnswer(invocation -> ((Supplier<?>) invocation.getArgument(0)).get()).when(transactionService).requiresNew(any());
    when(tenantService.findByName(anyString())).thenReturn(Optional.empty());
    when(tenantService.save(any())).thenReturn(createTenant());
    when(workspaceService.create(any())).thenReturn(createWorkspace());
    when(userRepository.findByTenantNameAndUserId(anyString(), anyString())).thenAnswer(new Answer() {

        private int count;

        public Object answer(InvocationOnMock invocation) {
            if (count++ == 0) {
                return Optional.empty();
            }
            return Optional.of(createUser());
        }
    });
    when(userRepository.save(any())).thenAnswer(new Answer() {

        private int count;

        public Object answer(InvocationOnMock invocation) {
            if (count++ == 0) {
                TransactionExecutionException exception = new TransactionExecutionException("", new RuntimeException("User already exists!"));
                throw new TransactionRuntimeExecutionException(exception);
            }
            TransactionExecutionException exception = new TransactionExecutionException("", new RuntimeException("This is not expected to run"));
            throw new TransactionRuntimeExecutionException(exception);
        }
    });
    User user = underTest.getOrCreate(createCbUser());
    assertNotNull(user);
    verify(cachedUserService, times(1)).getUser(any(), any(), any());
    verify(workspaceService, times(1)).create(any());
    verify(tenantService, times(1)).save(any());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) CrnUser(com.sequenceiq.cloudbreak.auth.CrnUser) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) User(com.sequenceiq.cloudbreak.workspace.model.User) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransactionRuntimeExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException) Test(org.junit.Test)

Aggregations

TransactionExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException)56 TransactionRuntimeExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException)34 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)26 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)17 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)12 TransactionService (com.sequenceiq.cloudbreak.common.service.TransactionService)10 List (java.util.List)10 Set (java.util.Set)10 Inject (javax.inject.Inject)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 FlowIdentifier (com.sequenceiq.flow.api.model.FlowIdentifier)9 Optional (java.util.Optional)9 StackService (com.sequenceiq.cloudbreak.service.stack.StackService)8 Collection (java.util.Collection)8 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 Service (org.springframework.stereotype.Service)8 InstanceStatus (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.InstanceStatus)7 CloudbreakServiceException (com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)7