use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project devspaces-images by redhat-developer.
the class WorkspaceManager method createWorkspace.
/**
* Creates a workspace out of a devfile.
*
* <p>The devfile should have been validated using the {@link
* DevfileIntegrityValidator#validateDevfile(Devfile)}. This method does rest of the validation
* and actually creates the workspace.
*
* @param devfile the devfile describing the workspace
* @param namespace workspace name is unique in this namespace
* @param attributes workspace instance attributes
* @param contentProvider the content provider to use for resolving content references in the
* devfile
* @return new workspace instance
* @throws NullPointerException when either {@code config} or {@code namespace} is null
* @throws NotFoundException when account with given id was not found
* @throws ConflictException when any conflict occurs (e.g Workspace with such name already exists
* for {@code owner})
* @throws ServerException when any other error occurs
* @throws ValidationException when incoming configuration or attributes are not valid
*/
@Traced
public WorkspaceImpl createWorkspace(Devfile devfile, String namespace, Map<String, String> attributes, FileContentProvider contentProvider) throws ServerException, NotFoundException, ConflictException, ValidationException {
requireNonNull(devfile, "Required non-null devfile");
requireNonNull(namespace, "Required non-null namespace");
validator.validateAttributes(attributes);
devfile = generateNameIfNeeded(devfile);
try {
devfileIntegrityValidator.validateContentReferences(devfile, contentProvider);
} catch (DevfileFormatException e) {
throw new ValidationException(e.getMessage(), e);
}
WorkspaceImpl workspace = doCreateWorkspace(devfile, accountManager.getByName(namespace), attributes, false);
TracingTags.WORKSPACE_ID.set(workspace.getId());
return workspace;
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project devspaces-images by redhat-developer.
the class DevfileParser method parse.
private DevfileImpl parse(JsonNode parsed, ObjectMapper mapper, Map<String, String> overrideProperties) throws DevfileFormatException, OverrideParameterException {
if (parsed == null) {
throw new DevfileFormatException("Unable to parse Devfile - provided source is empty");
}
DevfileImpl devfile;
try {
parsed = overridePropertiesApplier.applyPropertiesOverride(parsed, overrideProperties);
schemaValidator.validate(parsed);
devfile = mapper.treeToValue(parsed, DevfileImpl.class);
} catch (JsonProcessingException e) {
throw new DevfileFormatException(e.getMessage());
}
integrityValidator.validateDevfile(devfile);
return devfile;
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project devspaces-images by redhat-developer.
the class DevfileIntegrityValidator method validateComponents.
private Set<String> validateComponents(Devfile devfile) throws DevfileFormatException {
Set<String> definedAliases = new HashSet<>();
Component editorComponent = null;
Map<String, Set<String>> idsPerComponentType = new HashMap<>();
for (Component component : devfile.getComponents()) {
if (component.getAlias() != null && !definedAliases.add(component.getAlias())) {
throw new DevfileFormatException(format("Duplicate component alias found:'%s'", component.getAlias()));
}
Optional<Map.Entry<String, Long>> duplicatedEndpoint = component.getEndpoints().stream().map(Endpoint::getName).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream().filter(e -> e.getValue() > 1L).findFirst();
if (duplicatedEndpoint.isPresent()) {
throw new DevfileFormatException(format("Duplicated endpoint name '%s' found in '%s' component", duplicatedEndpoint.get().getKey(), getIdentifiableComponentName(component)));
}
Set<String> tempSet = new HashSet<>();
for (Env env : component.getEnv()) {
if (!tempSet.add(env.getName())) {
throw new DevfileFormatException(format("Duplicate environment variable '%s' found in component '%s'", env.getName(), getIdentifiableComponentName(component)));
}
}
if (!idsPerComponentType.computeIfAbsent(component.getType(), __ -> new HashSet<>()).add(getIdentifiableComponentName(component))) {
throw new DevfileFormatException(format("There are multiple components '%s' of type '%s' that cannot be uniquely" + " identified. Please add aliases that would distinguish the components.", getIdentifiableComponentName(component), component.getType()));
}
if (component.getAutomountWorkspaceSecrets() != null && component.getAlias() == null) {
throw new DevfileFormatException(format("The 'automountWorkspaceSecrets' property cannot be used in component which doesn't have alias. " + "Please add alias to component '%s' that would allow to distinguish its containers.", getIdentifiableComponentName(component)));
}
switch(component.getType()) {
case EDITOR_COMPONENT_TYPE:
if (editorComponent != null) {
throw new DevfileFormatException(format("Multiple editor components found: '%s', '%s'", getIdentifiableComponentName(editorComponent), getIdentifiableComponentName(component)));
}
editorComponent = component;
break;
case PLUGIN_COMPONENT_TYPE:
case KUBERNETES_COMPONENT_TYPE:
case OPENSHIFT_COMPONENT_TYPE:
case DOCKERIMAGE_COMPONENT_TYPE:
// do nothing
break;
default:
throw new DevfileFormatException(format("One of the components has unsupported component type: '%s'", component.getType()));
}
}
return definedAliases;
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project devspaces-images by redhat-developer.
the class WorkspaceEntityProvider method readFrom.
@Override
public WorkspaceDto readFrom(Class<WorkspaceDto> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
try {
JsonNode wsNode = mapper.readTree(entityStream);
JsonNode devfileNode = wsNode.path("devfile");
if (!devfileNode.isNull() && !devfileNode.isMissingNode()) {
devfileParser.parseJson(devfileNode.toString());
}
return DtoFactory.getInstance().createDtoFromJson(wsNode.toString(), WorkspaceDto.class);
} catch (DevfileFormatException e) {
throw new BadRequestException(e.getMessage());
} catch (IOException e) {
throw new WebApplicationException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project devspaces-images by redhat-developer.
the class KubernetesComponentValidator method validateEntrypointSelector.
private void validateEntrypointSelector(Component component, List<HasMetadata> filteredObjects) throws DevfileException {
if (component.getEntrypoints() == null || component.getEntrypoints().isEmpty()) {
return;
}
for (Entrypoint ep : component.getEntrypoints()) {
ContainerSearch search = new ContainerSearch(ep.getParentName(), ep.getParentSelector(), ep.getContainerName());
List<Container> cs = search.search(filteredObjects);
if (cs.isEmpty()) {
throw new DevfileFormatException(format("Component '%s' of type '%s' contains an entry point that doesn't match any" + " container.", getIdentifiableComponentName(component), component.getType()));
}
}
}
Aggregations