Search in sources :

Example 6 with StackV4Request

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request in project cloudbreak by hortonworks.

the class StackService method getStackRequestByNameOrCrnInWorkspaceId.

public StackV4Request getStackRequestByNameOrCrnInWorkspaceId(NameOrCrn nameOrCrn, Long workspaceId) {
    try {
        return transactionService.required(() -> {
            ShowTerminatedClustersAfterConfig showTerminatedClustersAfterConfig = showTerminatedClusterConfigService.get();
            Optional<Stack> stack = findByNameOrCrnAndWorkspaceIdWithLists(nameOrCrn, workspaceId);
            if (stack.isEmpty()) {
                throw new NotFoundException(format(STACK_NOT_FOUND_BY_NAME_OR_CRN_EXCEPTION_MESSAGE, nameOrCrn));
            }
            StackV4Request request = stackToStackV4RequestConverter.convert(stack.get());
            request.getCluster().setName(null);
            request.setName(stack.get().getName());
            return request;
        });
    } catch (TransactionExecutionException e) {
        throw new TransactionRuntimeExecutionException(e);
    }
}
Also used : ShowTerminatedClustersAfterConfig(com.sequenceiq.cloudbreak.service.stack.ShowTerminatedClusterConfigService.ShowTerminatedClustersAfterConfig) StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) CloudbreakImageNotFoundException(com.sequenceiq.cloudbreak.core.CloudbreakImageNotFoundException) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) AutoscaleStack(com.sequenceiq.cloudbreak.domain.projection.AutoscaleStack) TransactionRuntimeExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException)

Example 7 with StackV4Request

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request in project cloudbreak by hortonworks.

the class StackCreatorService method determineImageCatalog.

private Future<StatedImage> determineImageCatalog(String stackName, String platformString, StackV4Request stackRequest, Blueprint blueprint, User user, Workspace workspace) {
    ClusterV4Request clusterRequest = stackRequest.getCluster();
    if (clusterRequest == null) {
        return null;
    }
    boolean shouldUseBaseCMImage = shouldUseBaseCMImage(clusterRequest, platformString);
    boolean baseImageEnabled = imageCatalogService.baseImageEnabled();
    Map<String, String> mdcContext = MDCBuilder.getMdcContextMap();
    CloudbreakUser cbUser = restRequestThreadLocalService.getCloudbreakUser();
    return executorService.submit(() -> {
        MDCBuilder.buildMdcContextFromMap(mdcContext);
        LOGGER.info("The stack with name {} has base images enabled: {} and should use base images: {}", stackName, baseImageEnabled, shouldUseBaseCMImage);
        StatedImage statedImage = ThreadBasedUserCrnProvider.doAs(user.getUserCrn(), () -> {
            try {
                restRequestThreadLocalService.setCloudbreakUser(cbUser);
                return imageService.determineImageFromCatalog(workspace.getId(), stackRequest.getImage(), platformString, stackRequest.getVariant(), blueprint, shouldUseBaseCMImage, baseImageEnabled, user, image -> true);
            } catch (CloudbreakImageNotFoundException | CloudbreakImageCatalogException e) {
                throw new RuntimeException(e);
            }
        });
        MDCBuilder.cleanupMdc();
        return statedImage;
    });
}
Also used : ClusterV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.cluster.ClusterV4Request) CloudbreakImageNotFoundException(com.sequenceiq.cloudbreak.core.CloudbreakImageNotFoundException) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) StatedImage(com.sequenceiq.cloudbreak.service.image.StatedImage) CloudbreakImageCatalogException(com.sequenceiq.cloudbreak.core.CloudbreakImageCatalogException)

Example 8 with StackV4Request

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request in project cloudbreak by hortonworks.

the class StackCreatorService method createStack.

public StackV4Response createStack(User user, Workspace workspace, StackV4Request stackRequest, boolean distroxRequest) {
    long start = System.currentTimeMillis();
    String stackName = stackRequest.getName();
    nodeCountLimitValidator.validateProvision(stackRequest);
    measure(() -> validateRecipeExistenceOnInstanceGroups(stackRequest.getInstanceGroups(), workspace.getId()), LOGGER, "Check that recipes do exist took {} ms");
    measure(() -> ensureStackDoesNotExists(stackName, workspace), LOGGER, "Stack does not exist check took {} ms");
    Stack stackStub = measure(() -> stackV4RequestToStackConverter.convert(stackRequest), LOGGER, "Stack request converted to stack took {} ms for stack {}", stackName);
    stackStub.setWorkspace(workspace);
    stackStub.setCreator(user);
    StackType stackType = determineStackTypeBasedOnTheUsedApi(stackStub, distroxRequest);
    stackStub.setType(stackType);
    String platformString = stackStub.getCloudPlatform().toLowerCase();
    MDCBuilder.buildMdcContext(stackStub);
    Stack savedStack;
    try {
        Blueprint blueprint = measure(() -> determineBlueprint(stackRequest, workspace), LOGGER, "Blueprint determined in {} ms for stack {}", stackName);
        Future<StatedImage> imgFromCatalogFuture = determineImageCatalog(stackName, platformString, stackRequest, blueprint, user, workspace);
        hueWorkaroundValidatorService.validateForStackRequest(getHueHostGroups(blueprint.getBlueprintText()), stackStub.getName());
        savedStack = transactionService.required(() -> {
            Stack stack = measure(() -> stackDecorator.decorate(stackStub, stackRequest, user, workspace), LOGGER, "Decorate Stack with data took {} ms");
            DetailedEnvironmentResponse environment = measure(() -> ThreadBasedUserCrnProvider.doAsInternalActor(regionAwareInternalCrnGeneratorFactory.iam().getInternalCrnForServiceAsString(), () -> environmentClientService.getByCrn(stack.getEnvironmentCrn())), LOGGER, "Get Environment from Environment service took {} ms");
            if (stack.getOrchestrator() != null && stack.getOrchestrator().getApiEndpoint() != null) {
                measure(() -> stackService.validateOrchestrator(stack.getOrchestrator()), LOGGER, "Validate orchestrator took {} ms");
            }
            stack.setUseCcm(environment.getTunnel().useCcm());
            stack.setTunnel(environment.getTunnel());
            if (stackRequest.getCluster() != null) {
                measure(() -> setStackType(stack, blueprint), LOGGER, "Set stacktype for stack object took {} ms");
                measure(() -> clusterCreationService.validate(stackRequest.getCluster(), stack, user, workspace, environment), LOGGER, "Validate cluster rds and autotls took {} ms");
            }
            measure(() -> fillInstanceMetadata(environment, stack), LOGGER, "Fill up instance metadata took {} ms");
            StatedImage imgFromCatalog = measure(() -> getImageCatalog(imgFromCatalogFuture), LOGGER, "Select the correct image took {} ms");
            stackRuntimeVersionValidator.validate(stackRequest, imgFromCatalog.getImage(), stackType);
            Stack newStack = measure(() -> stackService.create(stack, platformString, imgFromCatalog, user, workspace, Optional.ofNullable(stackRequest.getResourceCrn())), LOGGER, "Save the remaining stack data took {} ms");
            try {
                LOGGER.info("Create cluster entity in the database with name {}.", stackName);
                long clusterSaveStart = System.currentTimeMillis();
                createClusterIfNeeded(user, stackRequest, newStack, stackName, blueprint);
                LOGGER.info("Cluster save took {} ms", System.currentTimeMillis() - clusterSaveStart);
            } catch (CloudbreakImageCatalogException | IOException | TransactionExecutionException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
            measure(() -> assignOwnerRoleOnDataHub(user, stackRequest, newStack), LOGGER, "assignOwnerRoleOnDataHub to stack took {} ms with name {}.", stackName);
            return newStack;
        });
    } catch (TransactionExecutionException e) {
        stackUnderOperationService.off();
        if (e.getCause() instanceof DataIntegrityViolationException) {
            String msg = String.format("Error with resource [%s], error: [%s]", APIResourceType.STACK, getProperSqlErrorMessage((DataIntegrityViolationException) e.getCause()));
            throw new BadRequestException(msg, e.getCause());
        }
        throw new TransactionRuntimeExecutionException(e);
    }
    StackV4Response response = measure(() -> stackToStackV4ResponseConverter.convert(savedStack), LOGGER, "Stack response has been created for stack took {} ms with name {}", stackName);
    LOGGER.info("Generated stack response after creation: {}", JsonUtil.writeValueAsStringSilentSafe(response));
    FlowIdentifier flowIdentifier = measure(() -> flowManager.triggerProvisioning(savedStack.getId()), LOGGER, "Stack triggerProvisioning took {} ms with name {}", stackName);
    response.setFlowIdentifier(flowIdentifier);
    metricService.submit(STACK_PREPARATION, System.currentTimeMillis() - start);
    return response;
}
Also used : Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) FlowIdentifier(com.sequenceiq.flow.api.model.FlowIdentifier) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TransactionRuntimeExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException) StackType(com.sequenceiq.cloudbreak.api.endpoint.v4.common.StackType) StackV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.StackV4Response) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) StatedImage(com.sequenceiq.cloudbreak.service.image.StatedImage)

Example 9 with StackV4Request

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request in project cloudbreak by hortonworks.

the class StackRuntimeVersionValidatorTest method testValidationWhenDataHubVersionIsNotPresent.

@Test
public void testValidationWhenDataHubVersionIsNotPresent() {
    StackV4Request request = createStackRequestWithoutCm();
    when(entitlementService.isDifferentDataHubAndDataLakeVersionAllowed(any())).thenReturn(false);
    ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.validate(request, mock(Image.class), StackType.WORKLOAD));
    verify(entitlementService).isDifferentDataHubAndDataLakeVersionAllowed(any());
    verifyNoInteractions(sdxClientService);
}
Also used : StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) Test(org.junit.Test)

Example 10 with StackV4Request

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request in project cloudbreak by hortonworks.

the class StackV4RequestToStackConverterTest method testWhenRegionIsEmptyButDefaultRegionsAreEmptyThenBadRequestExceptionComes.

@Test
public void testWhenRegionIsEmptyButDefaultRegionsAreEmptyThenBadRequestExceptionComes() {
    setDefaultRegions(null);
    StackV4Request request = getRequest("stack.json");
    request.setCloudPlatform(MOCK);
    request.getPlacement().setRegion(null);
    BadRequestException resultException = assertThrows(BadRequestException.class, () -> underTest.convert(request));
    assertEquals("No default region is specified. Region cannot be empty.", resultException.getMessage());
}
Also used : StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) AbstractJsonConverterTest(com.sequenceiq.cloudbreak.converter.AbstractJsonConverterTest) Test(org.junit.jupiter.api.Test)

Aggregations

StackV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request)105 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)66 Test (org.junit.jupiter.api.Test)58 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)52 InstanceGroupV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.instancegroup.InstanceGroupV4Request)36 Test (org.junit.Test)36 LoadBalancer (com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer)34 CloudSubnet (com.sequenceiq.cloudbreak.cloud.model.CloudSubnet)33 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)33 SubnetTest (com.sequenceiq.cloudbreak.core.network.SubnetTest)32 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)31 ClusterV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.cluster.ClusterV4Request)17 InstanceGroup (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)14 Map (java.util.Map)13 Set (java.util.Set)13 Optional (java.util.Optional)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 CloudPlatform (com.sequenceiq.cloudbreak.common.mappable.CloudPlatform)11 HashMap (java.util.HashMap)11 List (java.util.List)11