Search in sources :

Example 11 with DevfileException

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

the class DevfileVersionDetector method devfileMajorVersion.

/**
 * Gives major version of the devfile.
 *
 * <pre>
 *   1 -> 1
 *   1.0.0 -> 1
 *   1.99 -> 1
 *   2.0.0 -> 2
 *   2.1 -> 2
 *   a.a -> DevfileException
 *   a -> DevfileException
 * </pre>
 *
 * @param devfile to inspect
 * @return major version of the devfile
 * @throws DevfileException when can't find the field with version
 */
public int devfileMajorVersion(JsonNode devfile) throws DevfileException {
    String version = devfileVersion(devfile);
    int dot = version.indexOf(".");
    final String majorVersion = dot > 0 ? version.substring(0, dot) : version;
    try {
        return Integer.parseInt(majorVersion);
    } catch (NumberFormatException nfe) {
        throw new DevfileException("Unable to parse devfile version. This is not a valid devfile.", nfe);
    }
}
Also used : DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 12 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException 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 13 with DevfileException

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

the class PluginFQNParser method evaluateFqn.

/**
 * Evaluates plugin FQN from provided reference by trying to fetch and parse its meta information.
 *
 * @param reference plugin reference to evaluate FQN from
 * @param fileContentProvider content provider instance to perform plugin meta requests
 * @return plugin FQN evaluated from given reference
 * @throws InfrastructureException if plugin reference is invalid or inaccessible
 */
public ExtendedPluginFQN evaluateFqn(String reference, FileContentProvider fileContentProvider) throws InfrastructureException {
    JsonNode contentNode;
    try {
        String pluginMetaContent = fileContentProvider.fetchContent(reference);
        contentNode = yamlReader.readTree(pluginMetaContent);
    } catch (DevfileException | IOException e) {
        throw new InfrastructureException(format("Plugin reference URL '%s' is invalid.", reference), e);
    }
    JsonNode publisher = contentNode.path("publisher");
    if (publisher.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "publisher"));
    }
    JsonNode name = contentNode.get("name");
    if (name.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "name"));
    }
    JsonNode version = contentNode.get("version");
    if (version.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "version"));
    }
    if (!version.isValueNode()) {
        throw new InfrastructureException(format("Plugin specified by reference URL '%s' has version field that cannot be parsed to string", reference));
    }
    return new ExtendedPluginFQN(reference, publisher.textValue(), name.textValue(), version.asText());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 14 with DevfileException

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

the class DevfileConverterTest method shouldThrowServerExceptionIfAnyDevfileExceptionOccursOnConvertingDevfileToWorkspaceConfig.

@Test(expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "error")
public void shouldThrowServerExceptionIfAnyDevfileExceptionOccursOnConvertingDevfileToWorkspaceConfig() throws Exception {
    devfileConverter = spy(devfileConverter);
    doThrow(new DevfileException("error")).when(devfileConverter).devFileToWorkspaceConfig(any(), any());
    devfileConverter.convert(new DevfileImpl());
}
Also used : DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Test(org.testng.annotations.Test)

Example 15 with DevfileException

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

the class DockerimageComponentToWorkspaceApplier method apply.

/**
 * Applies changes on workspace config according to the specified dockerimage component.
 *
 * <p>Dockerimage component is provisioned as Deployment in Kubernetes recipe.<br>
 * Generated deployment contains container with environment variables, memory limit, docker image,
 * arguments and commands specified in component.<br>
 * Also, environment is provisioned with machine config with volumes and servers specified, then
 * Kubernetes infra will created needed PVC, Services, Ingresses, Routes according to specified
 * configuration.
 *
 * @param workspaceConfig workspace config on which changes should be applied
 * @param dockerimageComponent dockerimage component that should be applied
 * @param contentProvider optional content provider that may be used for external component
 *     resource fetching
 * @throws DevfileException if specified workspace config already has default environment where
 *     dockerimage component should be stored
 * @throws IllegalArgumentException if specified workspace config or plugin component is null
 * @throws IllegalArgumentException if specified component has type different from dockerimage
 */
@Override
public void apply(WorkspaceConfigImpl workspaceConfig, ComponentImpl dockerimageComponent, FileContentProvider contentProvider) throws DevfileException {
    checkArgument(workspaceConfig != null, "Workspace config must not be null");
    checkArgument(dockerimageComponent != null, "Component must not be null");
    checkArgument(DOCKERIMAGE_COMPONENT_TYPE.equals(dockerimageComponent.getType()), format("Plugin must have `%s` type", DOCKERIMAGE_COMPONENT_TYPE));
    String componentAlias = dockerimageComponent.getAlias();
    String machineName = componentAlias == null ? toMachineName(dockerimageComponent.getImage()) : componentAlias;
    MachineConfigImpl machineConfig = createMachineConfig(dockerimageComponent, componentAlias);
    List<HasMetadata> componentObjects = createComponentObjects(dockerimageComponent, machineName);
    k8sEnvProvisioner.provision(workspaceConfig, KubernetesEnvironment.TYPE, componentObjects, ImmutableMap.of(machineName, machineConfig));
    workspaceConfig.getCommands().stream().filter(c -> componentAlias != null && componentAlias.equals(c.getAttributes().get(Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE))).forEach(c -> c.getAttributes().put(MACHINE_NAME_ATTRIBUTE, machineName));
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) DEVFILE_COMPONENT_ALIAS_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.MachineConfig.DEVFILE_COMPONENT_ALIAS_ATTRIBUTE) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ComponentToWorkspaceApplier.convertEndpointsIntoServers(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier.convertEndpointsIntoServers) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) Containers(org.eclipse.che.workspace.infrastructure.kubernetes.util.Containers) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Named(javax.inject.Named) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) DOCKERIMAGE_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE) ImmutableMap(com.google.common.collect.ImmutableMap) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.VolumeImpl) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) List(java.util.List) SINGLE_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.SingleHostExternalServiceExposureStrategy.SINGLE_HOST_STRATEGY) PROJECTS_VOLUME_NAME(org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME) Constants(org.eclipse.che.api.workspace.server.devfile.Constants) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) KubernetesSize(org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSize) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)

Aggregations

DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)52 Test (org.testng.annotations.Test)22 IOException (java.io.IOException)20 ApiException (org.eclipse.che.api.core.ApiException)18 FileContentProvider (org.eclipse.che.api.workspace.server.devfile.FileContentProvider)18 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)16 HashMap (java.util.HashMap)14 List (java.util.List)14 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)14 Map (java.util.Map)12 Optional (java.util.Optional)12 ServerException (org.eclipse.che.api.core.ServerException)12 ExtendedError (org.eclipse.che.api.core.rest.shared.dto.ExtendedError)12 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)12 ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)12 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 Collections (java.util.Collections)10 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)10 UnknownScmProviderException (org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException)10