use of org.eclipse.che.api.core.model.workspace.config.Environment in project devspaces-images by redhat-developer.
the class WorkspaceRuntimes method validate.
/**
* Validates the specified workspace configuration.
*
* <p>The typical usage of this method is performing validating before asynchronous start of
* workspace.
*
* @param workspace workspace config that contains environment or devfile to start
* @param envName environment to start. Default will be used if null.
* @throws NotFoundException if workspace does not have environment with the specified name
* @throws NotFoundException if workspace has environment with type that is not supported
* @throws ValidationException if environment is not a valid and can not be run
* @throws ServerException if any other issue occurred during validation
*/
public void validate(WorkspaceImpl workspace, @Nullable String envName) throws ValidationException, NotFoundException, ServerException {
WorkspaceConfigImpl config = workspace.getConfig();
if (workspace.getDevfile() != null) {
config = devfileConverter.convert(workspace.getDevfile());
}
if (envName != null && !config.getEnvironments().containsKey(envName)) {
throw new NotFoundException(format("Workspace '%s:%s' doesn't contain environment '%s'", workspace.getNamespace(), config.getName(), envName));
}
if (envName == null) {
// use default environment if it is not defined
envName = config.getDefaultEnv();
}
if (envName == null && SidecarToolingWorkspaceUtil.isSidecarBasedWorkspace(config.getAttributes())) {
// Sidecar-based workspaces are allowed not to have any environments
return;
}
// validate environment in advance
if (envName == null) {
throw new NotFoundException(format("Workspace %s:%s can't use null environment", workspace.getNamespace(), config.getName()));
}
Environment environment = config.getEnvironments().get(envName);
if (environment == null) {
throw new NotFoundException(format("Workspace does not have environment with name %s that specified to be run", envName));
}
String type = environment.getRecipe().getType();
if (!infrastructure.getRecipeTypes().contains(type)) {
throw new NotFoundException("Infrastructure not found for type: " + type);
}
// try to create internal environment to check if the specified environment is valid
try {
createInternalEnvironment(environment, emptyMap(), emptyList(), config.getDevfile());
} catch (InfrastructureException e) {
throw new ServerException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.core.model.workspace.config.Environment in project devspaces-images by redhat-developer.
the class WorkspaceValidator method validateEnvironments.
private void validateEnvironments(WorkspaceConfig config) throws ValidationException {
boolean environmentIsNotSet = (config.getEnvironments() == null || config.getEnvironments().isEmpty()) && isNullOrEmpty(config.getDefaultEnv());
boolean isSidecarWorkspace = isSidecarBasedWorkspace(config.getAttributes());
if (environmentIsNotSet && isSidecarWorkspace) {
// sidecar based workspaces allowed not to have environment
return;
}
check(!isNullOrEmpty(config.getDefaultEnv()), "Workspace default environment name required");
checkNotNull(config.getEnvironments(), "Workspace must contain at least one environment");
check(config.getEnvironments().containsKey(config.getDefaultEnv()), "Workspace default environment configuration required");
for (Environment environment : config.getEnvironments().values()) {
checkNotNull(environment, "Environment must not be null");
Recipe recipe = environment.getRecipe();
checkNotNull(recipe, "Environment recipe must not be null");
checkNotNull(recipe.getType(), "Environment recipe type must not be null");
for (Entry<String, ? extends MachineConfig> machineEntry : environment.getMachines().entrySet()) {
validateMachine(machineEntry.getKey(), machineEntry.getValue());
}
}
}
use of org.eclipse.che.api.core.model.workspace.config.Environment in project che-server by eclipse-che.
the class LimitsCheckingWorkspaceManager method checkRamResourcesAvailability.
@VisibleForTesting
void checkRamResourcesAvailability(String accountId, String namespace, WorkspaceConfig config, @Nullable String envName) throws NotFoundException, ServerException, ConflictException {
if (config.getEnvironments().isEmpty()) {
return;
}
final Environment environment = config.getEnvironments().get(firstNonNull(envName, config.getDefaultEnv()));
final ResourceImpl ramToUse = new ResourceImpl(RamResourceType.ID, environmentRamCalculator.calculate(environment), RamResourceType.UNIT);
try {
resourceManager.checkResourcesAvailability(accountId, singletonList(ramToUse));
} catch (NoEnoughResourcesException e) {
final Resource requiredRam = // starting of workspace requires only RAM resource
e.getRequiredResources().get(0);
final Resource availableRam = getResourceOrDefault(e.getAvailableResources(), RamResourceType.ID, 0, RamResourceType.UNIT);
final Resource usedRam = getResourceOrDefault(resourceManager.getUsedResources(accountId), RamResourceType.ID, 0, RamResourceType.UNIT);
throw new LimitExceededException(format("Workspace %s/%s needs %s to start. Your account has %s available and %s in use. " + "The workspace can't be start. Stop other workspaces or grant more resources.", namespace, config.getName(), printResourceInfo(requiredRam), printResourceInfo(availableRam), printResourceInfo(usedRam)));
}
}
use of org.eclipse.che.api.core.model.workspace.config.Environment in project che-server by eclipse-che.
the class InternalEnvironmentFactoryTest method testApplyContainerSourceAttributeToTheMachineSpecifiedInEnv.
@Test
public void testApplyContainerSourceAttributeToTheMachineSpecifiedInEnv() throws Exception {
// given
final Environment sourceEnv = mock(Environment.class);
MachineConfigImpl machineConfig = mock(MachineConfigImpl.class);
final Map<String, MachineConfigImpl> machineConfigMap = ImmutableMap.of("envMachine", machineConfig);
doReturn(machineConfigMap).when(sourceEnv).getMachines();
when(environmentFactory.doCreate(any(), any(), any())).thenAnswer(invocation -> {
Map<String, InternalMachineConfig> envMachines = invocation.getArgument(1);
final InternalEnvironment internalEnv = mock(InternalEnvironment.class);
when(internalEnv.getMachines()).thenReturn(envMachines);
return internalEnv;
});
// when
InternalEnvironment resultEnv = environmentFactory.create(sourceEnv);
// then
assertEquals(resultEnv.getMachines().get("envMachine").getAttributes().get(CONTAINER_SOURCE_ATTRIBUTE), RECIPE_CONTAINER_SOURCE);
}
use of org.eclipse.che.api.core.model.workspace.config.Environment in project che-server by eclipse-che.
the class InternalEnvironmentFactoryTest method testApplyContainerSourceAttributeToTheMachineThatComesFromRecipe.
@Test
public void testApplyContainerSourceAttributeToTheMachineThatComesFromRecipe() throws Exception {
// given
final Environment sourceEnv = mock(Environment.class);
final InternalEnvironment internalEnv = mock(InternalEnvironment.class);
final InternalMachineConfig internalMachine = new InternalMachineConfig();
when(internalEnv.getMachines()).thenReturn(ImmutableMap.of("internalMachine", internalMachine));
when(environmentFactory.doCreate(any(), any(), any())).thenReturn(internalEnv);
// when
InternalEnvironment resultEnv = environmentFactory.create(sourceEnv);
// then
assertEquals(resultEnv.getMachines().get("internalMachine").getAttributes().get(CONTAINER_SOURCE_ATTRIBUTE), RECIPE_CONTAINER_SOURCE);
}
Aggregations