use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.
the class KubernetesComponentToWorkspaceApplier method provisionVolumes.
private void provisionVolumes(ComponentImpl component, Container container, MachineConfigImpl config) throws DevfileException {
for (org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl componentVolume : component.getVolumes()) {
Optional<VolumeMount> sameNameMount = container.getVolumeMounts().stream().filter(vm -> vm.getName().equals(componentVolume.getName())).findFirst();
if (sameNameMount.isPresent() && sameNameMount.get().getMountPath().equals(componentVolume.getContainerPath())) {
continue;
} else if (sameNameMount.isPresent()) {
throw new DevfileException(format("Conflicting volume with same name ('%s') but different path ('%s') found for component '%s' and its container '%s'.", componentVolume.getName(), componentVolume.getContainerPath(), getIdentifiableComponentName(component), container.getName()));
}
if (container.getVolumeMounts().stream().anyMatch(vm -> vm.getMountPath().equals(componentVolume.getContainerPath()))) {
throw new DevfileException(format("Conflicting volume with same path ('%s') but different name ('%s') found for component '%s' and its container '%s'.", componentVolume.getContainerPath(), componentVolume.getName(), getIdentifiableComponentName(component), container.getName()));
}
config.getVolumes().put(componentVolume.getName(), new VolumeImpl().withPath(componentVolume.getContainerPath()));
}
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.
the class KubernetesEnvironmentProvisioner method provision.
/**
* Provisions default K8s/OS environment with specified objects (K8s/OS objects, machines) into
* {@link WorkspaceConfigImpl}.
*
* <p>If there is already a default environment with kubernetes/openshift recipe then content will
* be updated with result or merging existing objects and specified ones.
*
* @param workspaceConfig workspace where recipe should be provisioned
* @param environmentType type of environment that should be provisioned. Should be one of the
* Kubernetes-based environments.
* @param componentObjects objects that should be provisioned into the workspace config
* @param machines machines that should be provisioned into the workspace config
* @throws DevfileRecipeFormatException if exception occurred during existing environment parsing
* @throws DevfileRecipeFormatException if exception occurred during kubernetes object
* serialization
* @throws DevfileException if any other exception occurred
*/
public void provision(WorkspaceConfigImpl workspaceConfig, String environmentType, List<HasMetadata> componentObjects, Map<String, MachineConfigImpl> machines) throws DevfileException, DevfileRecipeFormatException {
String defaultEnv = workspaceConfig.getDefaultEnv();
EnvironmentImpl environment = workspaceConfig.getEnvironments().get(defaultEnv);
if (environment == null) {
checkItemsHasUniqueKindToName(componentObjects);
RecipeImpl recipe = new RecipeImpl(environmentType, YAML_CONTENT_TYPE, asYaml(componentObjects), null);
String envName = "default";
EnvironmentImpl env = new EnvironmentImpl(recipe, emptyMap());
env.getMachines().putAll(machines);
workspaceConfig.getEnvironments().put(envName, env);
workspaceConfig.setDefaultEnv(envName);
} else {
RecipeImpl envRecipe = environment.getRecipe();
for (Entry<String, MachineConfigImpl> machineEntry : machines.entrySet()) {
if (environment.getMachines().put(machineEntry.getKey(), machineEntry.getValue()) != null) {
throw new DevfileException(format("Environment already contains machine '%s'", machineEntry.getKey()));
}
}
environment.getMachines().putAll(machines);
// check if it is needed to update recipe type since
// kubernetes component is compatible with openshift but not vice versa
Set<String> allowedEnvTypeBases = allowedEnvironmentTypeUpgrades.get(environmentType);
if (allowedEnvTypeBases != null) {
envRecipe.setType(environmentType);
}
// workspace already has k8s/OS recipe
// it is needed to merge existing recipe objects with component's ones
List<HasMetadata> envObjects = unmarshalObjects(envRecipe);
mergeProjectsPVC(envObjects, componentObjects);
envObjects.addAll(componentObjects);
checkItemsHasUniqueKindToName(envObjects);
envRecipe.setContent(asYaml(envObjects));
}
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.
the class DevfileToApiExceptionMapperTest method shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized.
@Test
public void shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized() {
ScmUnauthorizedException scmUnauthorizedException = new ScmUnauthorizedException("msg", "gitlab", "2.0", "http://gitlab.com/oauth/authenticate");
ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", scmUnauthorizedException));
assertTrue(exception instanceof UnauthorizedException);
assertEquals(((ExtendedError) exception.getServiceError()).getErrorCode(), 401);
assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().size(), 3);
assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_version"), "2.0");
assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_authentication_url"), "http://gitlab.com/oauth/authenticate");
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.
the class DevfileToApiExceptionMapperTest method shouldReturnServerExceptionWhenCauseIsUnknownProvider.
@Test
public void shouldReturnServerExceptionWhenCauseIsUnknownProvider() {
UnknownScmProviderException scmProviderException = new UnknownScmProviderException("unknown", "http://gitlab.com/oauth/authenticate");
ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", scmProviderException));
assertTrue(exception instanceof ServerException);
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.
the class URLFactoryBuilderTest method testDevfileSpecifyingFilename.
@Test
public void testDevfileSpecifyingFilename() throws ApiException, DevfileException {
String myLocation = "http://foo-location/";
Map<String, Object> devfileAsMap = Map.of("hello", "there", "how", "are", "you", "?");
JsonNode devfile = new ObjectNode(JsonNodeFactory.instance);
when(devfileParser.parseYamlRaw(anyString())).thenReturn(devfile);
when(devfileParser.convertYamlToMap(devfile)).thenReturn(devfileAsMap);
when(devfileVersionDetector.devfileMajorVersion(devfile)).thenReturn(2);
RemoteFactoryUrl githubLikeRemoteUrl = new RemoteFactoryUrl() {
private String devfileName = "default-devfile.yaml";
@Override
public String getProviderName() {
return null;
}
@Override
public List<DevfileLocation> devfileFileLocations() {
return Collections.singletonList(new DevfileLocation() {
@Override
public Optional<String> filename() {
return Optional.of(devfileName);
}
@Override
public String location() {
return myLocation;
}
});
}
@Override
public String rawFileLocation(String filename) {
return null;
}
@Override
public String getHostName() {
return null;
}
@Override
public String getBranch() {
return null;
}
@Override
public void setDevfileFilename(String devfileName) {
this.devfileName = devfileName;
}
};
String pathToDevfile = "my-custom-devfile-path.yaml";
Map<String, String> propertiesMap = singletonMap(URLFactoryBuilder.DEVFILE_FILENAME, pathToDevfile);
FactoryMetaDto factory = urlFactoryBuilder.createFactoryFromDevfile(githubLikeRemoteUrl, s -> myLocation + ".list", propertiesMap).get();
assertNotNull(factory);
// Check that we didn't fetch from default files but from the parameter
assertEquals(factory.getSource(), pathToDevfile);
assertTrue(factory instanceof FactoryDevfileV2Dto);
assertEquals(((FactoryDevfileV2Dto) factory).getDevfile(), devfileAsMap);
}
Aggregations