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;
}
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;
}
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;
}
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;
}
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));
}
Aggregations