Search in sources :

Example 1 with UpdateHostGroupRecipes

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes in project cloudbreak by hortonworks.

the class UpdateRecipeService method refreshRecipesForCluster.

/**
 * Updating recipes for an existing cluster. The input should contain host group - recipes mapping
 * If a host group key from the mappings is missing from the input, that is not going to be updated.
 * (or both - that is the default). Output is the newly attached/detached recipes in db.
 */
public UpdateRecipesV4Response refreshRecipesForCluster(Long workspaceId, Stack stack, List<UpdateHostGroupRecipes> recipesPerHostGroup) {
    Set<String> recipesToFind = recipesPerHostGroup.stream().flatMap(rphg -> rphg.getRecipeNames().stream()).collect(Collectors.toSet());
    Map<String, Set<String>> recipesToUpdate = recipesPerHostGroup.stream().collect(Collectors.toMap(UpdateHostGroupRecipes::getHostGroupName, UpdateHostGroupRecipes::getRecipeNames, (n1, n2) -> n1));
    LOGGER.debug("Update recipes {}", recipesToUpdate);
    Set<Recipe> recipes = recipeService.getByNamesForWorkspaceId(recipesToFind, workspaceId);
    validate(recipesToFind, recipes);
    Set<HostGroup> hostGroups = hostGroupService.getByClusterWithRecipes(stack.getCluster().getId());
    UpdateRecipesV4Response result = updateRecipesForHostGroups(recipesToUpdate, recipes, hostGroups);
    LOGGER.debug("Update recipes result: {}", result);
    return result;
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Logger(org.slf4j.Logger) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) AttachRecipeV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.AttachRecipeV4Response) UpdateRecipesV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.UpdateRecipesV4Response) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HostGroupService(com.sequenceiq.cloudbreak.service.hostgroup.HostGroupService) UpdateHostGroupRecipes(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes) Collectors(java.util.stream.Collectors) DetachRecipeV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.DetachRecipeV4Response) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) HashSet(java.util.HashSet) List(java.util.List) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) Service(org.springframework.stereotype.Service) Map(java.util.Map) Optional(java.util.Optional) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) Joiner(com.google.common.base.Joiner) Set(java.util.Set) HashSet(java.util.HashSet) UpdateRecipesV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.UpdateRecipesV4Response) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup)

Example 2 with UpdateHostGroupRecipes

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes in project cloudbreak by hortonworks.

the class UpdateRecipeService method collectAttachHostGroupRecipes.

private Optional<UpdateHostGroupRecipes> collectAttachHostGroupRecipes(String hostGroupName, Set<String> existingRecipeNames, Set<Recipe> updateRecipes) {
    Set<String> recipesToAddToHostGroup = updateRecipes.stream().map(Recipe::getName).filter(r -> !existingRecipeNames.contains(r)).collect(Collectors.toSet());
    Optional<UpdateHostGroupRecipes> result = Optional.empty();
    if (!recipesToAddToHostGroup.isEmpty()) {
        UpdateHostGroupRecipes attachHostGroupRecipes = new UpdateHostGroupRecipes();
        attachHostGroupRecipes.setHostGroupName(hostGroupName);
        attachHostGroupRecipes.setRecipeNames(recipesToAddToHostGroup);
        result = Optional.of(attachHostGroupRecipes);
    }
    return result;
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Logger(org.slf4j.Logger) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) AttachRecipeV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.AttachRecipeV4Response) UpdateRecipesV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.UpdateRecipesV4Response) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HostGroupService(com.sequenceiq.cloudbreak.service.hostgroup.HostGroupService) UpdateHostGroupRecipes(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes) Collectors(java.util.stream.Collectors) DetachRecipeV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.DetachRecipeV4Response) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) HashSet(java.util.HashSet) List(java.util.List) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) Service(org.springframework.stereotype.Service) Map(java.util.Map) Optional(java.util.Optional) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) Joiner(com.google.common.base.Joiner) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) UpdateHostGroupRecipes(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes)

Example 3 with UpdateHostGroupRecipes

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes in project cloudbreak by hortonworks.

the class UpdateRecipeService method collectDetachHostGroupRecipes.

private Optional<UpdateHostGroupRecipes> collectDetachHostGroupRecipes(String hostGroupName, Set<String> recipesForHostGroup, Set<Recipe> existingRecipes) {
    Set<String> recipesToDeleteFromHostGroup = existingRecipes.stream().map(Recipe::getName).filter(name -> !recipesForHostGroup.contains(name)).collect(Collectors.toSet());
    Optional<UpdateHostGroupRecipes> result = Optional.empty();
    if (!recipesToDeleteFromHostGroup.isEmpty()) {
        UpdateHostGroupRecipes detachHostGroupRecipes = new UpdateHostGroupRecipes();
        detachHostGroupRecipes.setHostGroupName(hostGroupName);
        detachHostGroupRecipes.setRecipeNames(recipesToDeleteFromHostGroup);
        result = Optional.of(detachHostGroupRecipes);
    }
    return result;
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Logger(org.slf4j.Logger) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) AttachRecipeV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.AttachRecipeV4Response) UpdateRecipesV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.UpdateRecipesV4Response) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HostGroupService(com.sequenceiq.cloudbreak.service.hostgroup.HostGroupService) UpdateHostGroupRecipes(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes) Collectors(java.util.stream.Collectors) DetachRecipeV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.DetachRecipeV4Response) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) HashSet(java.util.HashSet) List(java.util.List) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) Service(org.springframework.stereotype.Service) Map(java.util.Map) Optional(java.util.Optional) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) Joiner(com.google.common.base.Joiner) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) UpdateHostGroupRecipes(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes)

Example 4 with UpdateHostGroupRecipes

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes in project cloudbreak by hortonworks.

the class UpdateRecipeServiceTest method createUpdateHostGroupRecipes.

private List<UpdateHostGroupRecipes> createUpdateHostGroupRecipes(Map<String, Set<String>> hostGroupRecipesMap) {
    List<UpdateHostGroupRecipes> result = new ArrayList<>();
    for (Map.Entry<String, Set<String>> entry : hostGroupRecipesMap.entrySet()) {
        UpdateHostGroupRecipes item = new UpdateHostGroupRecipes();
        item.setHostGroupName(entry.getKey());
        item.setRecipeNames(entry.getValue());
        result.add(item);
    }
    return result;
}
Also used : ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) UpdateHostGroupRecipes(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with UpdateHostGroupRecipes

use of com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes in project cloudbreak by hortonworks.

the class UpdateRecipeService method doHostGroupRecipeUpdate.

private UpdateHostGroupRecipesPair doHostGroupRecipeUpdate(Map<String, Set<String>> recipesToUpdate, Set<Recipe> recipes, HostGroup hostGroup) {
    String hostGroupName = hostGroup.getName();
    Optional<UpdateHostGroupRecipes> attachHostGroupRecipesOpt = Optional.empty();
    Optional<UpdateHostGroupRecipes> detachHostGroupRecipesOpt = Optional.empty();
    if (recipesToUpdate.containsKey(hostGroupName)) {
        LOGGER.debug("Checking host group '{}' needs any refresh.", hostGroupName);
        Set<String> recipesForHostGroup = recipesToUpdate.get(hostGroupName);
        Set<Recipe> existingRecipes = hostGroup.getRecipes();
        Set<String> existingRecipeNames = existingRecipes.stream().map(Recipe::getName).collect(Collectors.toSet());
        LOGGER.debug("Current recipes: {}", existingRecipes);
        boolean allExists = existingRecipes.stream().allMatch(r -> recipesForHostGroup.contains(r.getName()));
        if (allExists && recipesForHostGroup.size() == existingRecipes.size()) {
            LOGGER.debug("No need for any recipe update for '{}' host group", hostGroupName);
        } else {
            Set<Recipe> updateRecipes = recipes.stream().filter(r -> recipesForHostGroup.contains(r.getName())).collect(Collectors.toSet());
            attachHostGroupRecipesOpt = collectAttachHostGroupRecipes(hostGroupName, existingRecipeNames, updateRecipes);
            detachHostGroupRecipesOpt = collectDetachHostGroupRecipes(hostGroupName, recipesForHostGroup, existingRecipes);
            Set<GeneratedRecipe> generatedRecipeToDeleteSet = new HashSet<>();
            if (detachHostGroupRecipesOpt.isPresent()) {
                UpdateHostGroupRecipes detachHostGroupRecipes = detachHostGroupRecipesOpt.get();
                generatedRecipeToDeleteSet = hostGroup.getGeneratedRecipes().stream().filter(gr -> recipeNamesMatchForGeneratedRecipe(gr, detachHostGroupRecipes.getRecipeNames())).collect(Collectors.toSet());
                hostGroup.getGeneratedRecipes().removeAll(generatedRecipeToDeleteSet);
            }
            hostGroup.setRecipes(updateRecipes);
            hostGroupService.save(hostGroup);
            if (detachHostGroupRecipesOpt.isPresent() && !generatedRecipeToDeleteSet.isEmpty()) {
                generatedRecipeService.deleteAll(generatedRecipeToDeleteSet);
            }
        }
    }
    return new UpdateHostGroupRecipesPair(attachHostGroupRecipesOpt.orElse(null), detachHostGroupRecipesOpt.orElse(null));
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Logger(org.slf4j.Logger) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) AttachRecipeV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.AttachRecipeV4Response) UpdateRecipesV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.UpdateRecipesV4Response) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HostGroupService(com.sequenceiq.cloudbreak.service.hostgroup.HostGroupService) UpdateHostGroupRecipes(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes) Collectors(java.util.stream.Collectors) DetachRecipeV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.DetachRecipeV4Response) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) HashSet(java.util.HashSet) List(java.util.List) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) Service(org.springframework.stereotype.Service) Map(java.util.Map) Optional(java.util.Optional) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) Joiner(com.google.common.base.Joiner) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) UpdateHostGroupRecipes(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes) HashSet(java.util.HashSet)

Aggregations

UpdateHostGroupRecipes (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 Set (java.util.Set)5 Joiner (com.google.common.base.Joiner)4 AttachRecipeV4Response (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.AttachRecipeV4Response)4 DetachRecipeV4Response (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.DetachRecipeV4Response)4 UpdateRecipesV4Response (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.UpdateRecipesV4Response)4 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)4 NotFoundException (com.sequenceiq.cloudbreak.common.exception.NotFoundException)4 Recipe (com.sequenceiq.cloudbreak.domain.Recipe)4 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)4 GeneratedRecipe (com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe)4 HostGroup (com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup)4 HostGroupService (com.sequenceiq.cloudbreak.service.hostgroup.HostGroupService)4 List (java.util.List)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4