Search in sources :

Example 1 with DevfileFormatException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project che-server by eclipse-che.

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;
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ValidationException(org.eclipse.che.api.core.ValidationException) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Traced(org.eclipse.che.commons.annotation.Traced)

Example 2 with DevfileFormatException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project che-server by eclipse-che.

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;
}
Also used : KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Env(org.eclipse.che.api.core.model.workspace.devfile.Env) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) PLUGIN_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.PLUGIN_COMPONENT_TYPE) Function(java.util.function.Function) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Map(java.util.Map) Components.getIdentifiableComponentName(org.eclipse.che.api.workspace.server.devfile.Components.getIdentifiableComponentName) EDITOR_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE) DOCKERIMAGE_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) OPENSHIFT_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.OPENSHIFT_COMPONENT_TYPE) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) Optional(java.util.Optional) Command(org.eclipse.che.api.core.model.workspace.devfile.Command) Project(org.eclipse.che.api.core.model.workspace.devfile.Project) Pattern(java.util.regex.Pattern) Action(org.eclipse.che.api.core.model.workspace.devfile.Action) Endpoint(org.eclipse.che.api.core.model.workspace.devfile.Endpoint) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Env(org.eclipse.che.api.core.model.workspace.devfile.Env) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) HashSet(java.util.HashSet)

Example 3 with DevfileFormatException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project che-server by eclipse-che.

the class DevfileSchemaValidator method validate.

public void validate(JsonNode contentNode) throws DevfileFormatException {
    try {
        List<Problem> validationErrors = new ArrayList<>();
        ProblemHandler handler = ProblemHandler.collectingTo(validationErrors);
        String devfileVersion = devfileVersionDetector.devfileVersion(contentNode);
        if (!schemasByVersion.containsKey(devfileVersion)) {
            throw new DevfileFormatException(String.format("Version '%s' of the devfile is not supported. Supported versions are '%s'.", devfileVersion, SUPPORTED_VERSIONS));
        }
        JsonSchema schema = schemasByVersion.get(devfileVersion);
        try (JsonReader reader = service.createReader(new StringReader(jsonMapper.writeValueAsString(contentNode)), schema, handler)) {
            reader.read();
        }
        if (!validationErrors.isEmpty()) {
            String error = errorMessageComposer.extractMessages(validationErrors, new StringBuilder());
            throw new DevfileFormatException(error);
        }
    } catch (DevfileException dfe) {
        throw new DevfileFormatException(format("Devfile schema validation failed. Error: %s", dfe.getMessage()));
    } catch (IOException e) {
        throw new DevfileFormatException("Unable to validate Devfile. Error: " + e.getMessage());
    }
}
Also used : ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonSchema(org.leadpony.justify.api.JsonSchema) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) Problem(org.leadpony.justify.api.Problem) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) IOException(java.io.IOException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 4 with DevfileFormatException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project devspaces-images by redhat-developer.

the class DevfileParserTest method shouldThrowExceptionWhenExceptionOccurredDuringSchemaValidation.

@Test(expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "non valid")
public void shouldThrowExceptionWhenExceptionOccurredDuringSchemaValidation() throws Exception {
    // given
    doThrow(new DevfileFormatException("non valid")).when(schemaValidator).validate(any());
    // when
    devfileParser.parseYaml(DEVFILE_YAML_CONTENT);
}
Also used : DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Test(org.testng.annotations.Test)

Example 5 with DevfileFormatException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException in project devspaces-images by redhat-developer.

the class DevfileIntegrityValidator method validateContentReferences.

/**
 * Validates that various selectors in the devfile reference something in the referenced content.
 *
 * @param devfile the validated devfile
 * @param provider the file content provider to fetch the referenced content with.
 * @throws DevfileFormatException when some selectors don't match any objects in the referenced
 *     content or any other exception thrown during the validation
 */
public void validateContentReferences(Devfile devfile, FileContentProvider provider) throws DevfileFormatException {
    for (Component component : devfile.getComponents()) {
        ComponentIntegrityValidator validator = validators.get(component.getType());
        if (validator == null) {
            throw new DevfileFormatException(format("Unknown component type: %s", component.getType()));
        }
        validator.validateComponent(component, provider);
    }
}
Also used : DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Component(org.eclipse.che.api.core.model.workspace.devfile.Component)

Aggregations

DevfileFormatException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException)22 IOException (java.io.IOException)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 BadRequestException (jakarta.ws.rs.BadRequestException)4 WebApplicationException (jakarta.ws.rs.WebApplicationException)4 ValidationException (org.eclipse.che.api.core.ValidationException)4 Component (org.eclipse.che.api.core.model.workspace.devfile.Component)4 Devfile (org.eclipse.che.api.core.model.workspace.devfile.Devfile)4 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)4 Test (org.testng.annotations.Test)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Container (io.fabric8.kubernetes.api.model.Container)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 JsonReader (jakarta.json.JsonReader)2 StringReader (java.io.StringReader)2 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2