use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project devspaces-images by redhat-developer.
the class URLFactoryBuilder method createFactory.
/**
* Converts given devfile json into factory based on the devfile version.
*
* @param overrideProperties map of overridden properties to apply in devfile
* @param fileContentProvider service-specific devfile related file content provider
* @param location devfile's location
* @return new factory created from the given devfile
* @throws OverrideParameterException when any issue when overriding parameters occur
* @throws DevfileException when devfile is not valid or we can't work with it
*/
private FactoryMetaDto createFactory(JsonNode devfileJson, Map<String, String> overrideProperties, FileContentProvider fileContentProvider, DevfileLocation location) throws OverrideParameterException, DevfileException {
if (devfileVersionDetector.devfileMajorVersion(devfileJson) == 1) {
DevfileImpl devfile = devfileParser.parseJsonNode(devfileJson, overrideProperties);
devfileParser.resolveReference(devfile, fileContentProvider);
devfile = ensureToUseGenerateName(devfile);
return newDto(FactoryDto.class).withV(CURRENT_VERSION).withDevfile(DtoConverter.asDto(devfile)).withSource(location.filename().isPresent() ? location.filename().get() : null);
} else {
return newDto(FactoryDevfileV2Dto.class).withV(CURRENT_VERSION).withDevfile(devfileParser.convertYamlToMap(devfileJson)).withSource(location.filename().isPresent() ? location.filename().get() : null);
}
}
use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project devspaces-images by redhat-developer.
the class WorkspaceManager method updateWorkspace.
/**
* Updates an existing workspace with a new configuration.
*
* <p>
*
* <p>Replace strategy is used for workspace update, it means that existing workspace data will be
* replaced with given {@code update}.
*
* @param update workspace update
* @return updated instance of the workspace
* @throws NullPointerException when either {@code workspaceId} or {@code update} is null
* @throws NotFoundException when workspace with given id doesn't exist
* @throws ConflictException when any conflict occurs (e.g Workspace with such name already exists
* in {@code namespace})
* @throws ServerException when any other error occurs
*/
public WorkspaceImpl updateWorkspace(String id, Workspace update) throws ConflictException, ServerException, NotFoundException, ValidationException {
requireNonNull(id, "Required non-null workspace id");
requireNonNull(update, "Required non-null workspace update");
checkArgument(update.getConfig() != null ^ update.getDevfile() != null, "Required non-null workspace configuration or devfile update but not both");
if (update.getConfig() != null) {
validator.validateConfig(update.getConfig());
}
WorkspaceImpl workspace = workspaceDao.get(id);
validator.validateUpdateAttributes(workspace.getAttributes(), update.getAttributes());
if (workspace.getConfig() != null) {
workspace.setConfig(new WorkspaceConfigImpl(update.getConfig()));
}
if (workspace.getDevfile() != null) {
workspace.setDevfile(new DevfileImpl(update.getDevfile()));
}
workspace.setAttributes(update.getAttributes());
workspace.getAttributes().put(UPDATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis()));
workspace.setTemporary(update.isTemporary());
return normalizeState(workspaceDao.update(workspace), true);
}
use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project devspaces-images by redhat-developer.
the class WorkspaceManager method generateNameIfNeeded.
/**
* If 'generateName' is defined and 'name' is not, we generate name using 'generateName' as a
* prefix following {@link Constants#WORKSPACE_GENERATE_NAME_CHARS_APPEND} random characters and
* set it to 'name'.
*/
private Devfile generateNameIfNeeded(Devfile origDevfile) {
if (origDevfile.getMetadata() != null) {
MetadataImpl metadata = new MetadataImpl(origDevfile.getMetadata());
if (metadata.getName() == null && metadata.getGenerateName() != null) {
metadata.setName(NameGenerator.generate(metadata.getGenerateName(), WORKSPACE_GENERATE_NAME_CHARS_APPEND));
DevfileImpl devfileWithGeneratedName = new DevfileImpl(origDevfile);
devfileWithGeneratedName.setMetadata(metadata);
return devfileWithGeneratedName;
}
}
return origDevfile;
}
use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project devspaces-images by redhat-developer.
the class DefaultEditorProvisioner method apply.
/**
* Provision default editor if there is no editor. Also provisions default plugins for default
* editor regardless whether it is provisioned or set by user.
*
* @param devfile devfile where editor and plugins should be provisioned
* @param contentProvider content provider for plugin references retrieval
*/
public void apply(DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException {
if (defaultEditorRef == null) {
// there is no default editor configured
return;
}
if ("true".equals(devfile.getAttributes().get(EDITOR_FREE_DEVFILE_ATTRIBUTE))) {
return;
}
List<ComponentImpl> components = devfile.getComponents();
Optional<ComponentImpl> editorOpt = components.stream().filter(t -> EDITOR_COMPONENT_TYPE.equals(t.getType())).findFirst();
boolean isDefaultEditorUsed;
if (!editorOpt.isPresent()) {
components.add(new ComponentImpl(EDITOR_COMPONENT_TYPE, defaultEditorRef));
isDefaultEditorUsed = true;
} else {
Component editor = editorOpt.get();
String editorPublisherAndName = getPluginPublisherAndName(editor, contentProvider);
isDefaultEditorUsed = defaultEditor.equals(editorPublisherAndName);
}
if (isDefaultEditorUsed) {
provisionDefaultPlugins(components, contentProvider);
}
if ("false".equals(devfile.getAttributes().get(PERSIST_VOLUMES_ATTRIBUTE)) && "true".equals(devfile.getAttributes().get(ASYNC_PERSIST_ATTRIBUTE))) {
provisionAsyncStoragePlugin(components, contentProvider);
}
}
use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project devspaces-images by redhat-developer.
the class DevfileConverter method devFileToWorkspaceConfig.
/**
* Converts given {@link Devfile} into {@link WorkspaceConfigImpl workspace config}.
*
* @param devfile initial devfile
* @param contentProvider content provider for recipe-type component or plugin references
* @return constructed workspace config
* @throws DevfileException when general devfile error occurs
* @throws DevfileException when devfile requires additional files content but the specified
* content provider does not support it
* @throws DevfileFormatException when devfile format is invalid
* @throws DevfileRecipeFormatException when content of the file specified in recipe type
* component is empty or its format is invalid
*/
public WorkspaceConfigImpl devFileToWorkspaceConfig(DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException {
checkArgument(devfile != null, "Devfile must not be null");
checkArgument(contentProvider != null, "Content provider must not be null");
// make copy to avoid modification of original devfile
devfile = new DevfileImpl(devfile);
validateCurrentVersion(devfile);
defaultEditorProvisioner.apply(devfile, contentProvider);
WorkspaceConfigImpl config = new WorkspaceConfigImpl();
config.setName(devfile.getName());
for (Command command : devfile.getCommands()) {
CommandImpl com = commandConverter.toWorkspaceCommand(command, contentProvider);
if (com != null) {
config.getCommands().add(com);
}
}
// so, commands should be already converted
for (ComponentImpl component : devfile.getComponents()) {
ComponentToWorkspaceApplier applier = componentTypeToApplier.get(component.getType());
if (applier == null) {
throw new DevfileException(String.format("Devfile contains component `%s` with type `%s` that can not be converted to workspace", getIdentifiableComponentName(component), component.getType()));
}
applier.apply(config, component, contentProvider);
}
for (ProjectImpl project : devfile.getProjects()) {
ProjectConfigImpl projectConfig = projectConverter.toWorkspaceProject(project);
config.getProjects().add(projectConfig);
}
config.getAttributes().putAll(devfile.getAttributes());
config.setDevfile(devfile);
return config;
}
Aggregations