Search in sources :

Example 11 with RecipeImpl

use of org.eclipse.che.api.workspace.server.model.impl.RecipeImpl in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method internalEnvironmentShouldThrowExceptionWhenNoEnvironmentFactoryFoundForRecipeType.

@Test(expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "InternalEnvironmentFactory is not configured for recipe type: 'not-supported-type'")
public void internalEnvironmentShouldThrowExceptionWhenNoEnvironmentFactoryFoundForRecipeType() throws Exception {
    EnvironmentImpl environment = new EnvironmentImpl();
    environment.setRecipe(new RecipeImpl("not-supported-type", "", "", null));
    runtimes.createInternalEnvironment(environment, emptyMap(), emptyList(), null);
}
Also used : RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) Test(org.testng.annotations.Test)

Example 12 with RecipeImpl

use of org.eclipse.che.api.workspace.server.model.impl.RecipeImpl in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method shouldSuspendRecoverIfEnvironmentFactoryCannotBeFound.

@Test
public void shouldSuspendRecoverIfEnvironmentFactoryCannotBeFound() throws Exception {
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", "default", "myId", "infraNamespace");
    WorkspaceConfigImpl config = mock(WorkspaceConfigImpl.class);
    EnvironmentImpl environment = mock(EnvironmentImpl.class);
    when(environment.getRecipe()).thenReturn(new RecipeImpl("UNKNOWN", "contentType1", "content1", null));
    doReturn(ImmutableMap.of(identity.getEnvName(), environment)).when(config).getEnvironments();
    WorkspaceImpl workspace = mock(WorkspaceImpl.class);
    when(workspace.getConfig()).thenReturn(config);
    when(workspaceDao.get(identity.getWorkspaceId())).thenReturn(workspace);
    // try recover
    try {
        runtimes.recoverOne(infrastructure, identity);
        fail("Not expected");
    } catch (ServerException e) {
    }
    ;
    verify(statuses).remove("workspace123");
    assertWorkspaceEventFired("workspace123", WorkspaceStatus.STOPPED, WorkspaceStatus.STOPPING, "Workspace is stopped. Reason: InternalEnvironmentFactory is not configured for recipe type: 'UNKNOWN'", false, Collections.emptyMap());
    assertFalse(runtimesMap.containsKey("workspace123"));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ServerException(org.eclipse.che.api.core.ServerException) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 13 with RecipeImpl

use of org.eclipse.che.api.workspace.server.model.impl.RecipeImpl in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method mockEnvironment.

private EnvironmentImpl mockEnvironment() {
    EnvironmentImpl environment = mock(EnvironmentImpl.class);
    lenient().when(environment.getRecipe()).thenReturn(new RecipeImpl(TEST_ENVIRONMENT_TYPE, "contentType1", "content1", null));
    return environment;
}
Also used : RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)

Example 14 with RecipeImpl

use of org.eclipse.che.api.workspace.server.model.impl.RecipeImpl in project devspaces-images by redhat-developer.

the class WorkspaceDaoTest method shouldUpdateWorkspaceWithConfig.

@Test(dependsOnMethods = "shouldGetWorkspaceById")
public void shouldUpdateWorkspaceWithConfig() throws Exception {
    final WorkspaceImpl workspace = new WorkspaceImpl(workspaces[0], workspaces[0].getAccount());
    // Remove an existing project configuration from workspace
    workspace.getConfig().getProjects().remove(1);
    // Add new project to the workspace configuration
    final SourceStorageImpl source3 = new SourceStorageImpl();
    source3.setType("type3");
    source3.setLocation("location3");
    source3.setParameters(new HashMap<>(ImmutableMap.of("param1", "value1", "param2", "value2", "param3", "value3")));
    final ProjectConfigImpl newProjectCfg = new ProjectConfigImpl();
    newProjectCfg.setPath("/path3");
    newProjectCfg.setType("type3");
    newProjectCfg.setName("project3");
    newProjectCfg.setDescription("description3");
    newProjectCfg.getMixins().addAll(asList("mixin3", "mixin4"));
    newProjectCfg.setSource(source3);
    newProjectCfg.getAttributes().put("new-key", asList("1", "2"));
    workspace.getConfig().getProjects().add(newProjectCfg);
    // Update an existing project configuration
    final ProjectConfigImpl projectCfg = workspace.getConfig().getProjects().get(0);
    projectCfg.getAttributes().clear();
    projectCfg.getSource().setLocation("new-location");
    projectCfg.getSource().setType("new-type");
    projectCfg.getSource().getParameters().put("new-param", "new-param-value");
    projectCfg.getMixins().add("new-mixin");
    projectCfg.setPath("/new-path");
    projectCfg.setDescription("new project description");
    // Remove an existing command
    workspace.getConfig().getCommands().remove(1);
    // Add a new command
    final CommandImpl newCmd = new CommandImpl();
    newCmd.setName("name3");
    newCmd.setType("type3");
    newCmd.setCommandLine("cmd3");
    newCmd.getAttributes().putAll(ImmutableMap.of("attr1", "value1", "attr2", "value2", "attr3", "value3"));
    workspace.getConfig().getCommands().add(newCmd);
    // Update an existing command
    final CommandImpl command = workspace.getConfig().getCommands().get(0);
    command.setName("new-name");
    command.setType("new-type");
    command.setCommandLine("new-command-line");
    command.getAttributes().clear();
    // Add a new environment
    final RecipeImpl newRecipe = new RecipeImpl();
    newRecipe.setLocation("new-location");
    newRecipe.setType("new-type");
    newRecipe.setContentType("new-content-type");
    newRecipe.setContent("new-content");
    final MachineConfigImpl newMachine = new MachineConfigImpl();
    final ServerConfigImpl serverConf1 = new ServerConfigImpl("2265", "http", "path1", singletonMap("key", "value"));
    final ServerConfigImpl serverConf2 = new ServerConfigImpl("2266", "ftp", "path2", singletonMap("key", "value"));
    newMachine.setServers(ImmutableMap.of("ref1", serverConf1, "ref2", serverConf2));
    newMachine.setAttributes(singletonMap("att1", "val"));
    newMachine.setAttributes(singletonMap("CHE_ENV", "value"));
    final EnvironmentImpl newEnv = new EnvironmentImpl();
    newEnv.setMachines(ImmutableMap.of("new-machine", newMachine));
    newEnv.setRecipe(newRecipe);
    workspace.getConfig().getEnvironments().put("new-env", newEnv);
    // Update an existing environment
    final EnvironmentImpl defaultEnv = workspace.getConfig().getEnvironments().get(workspace.getConfig().getDefaultEnv());
    // Remove an existing machine config
    final List<String> machineNames = new ArrayList<>(defaultEnv.getMachines().keySet());
    // Update an existing machine
    final MachineConfigImpl existingMachine = defaultEnv.getMachines().get(machineNames.get(1));
    existingMachine.setAttributes(ImmutableMap.of("attr1", "value1", "attr2", "value2", "attr3", "value3"));
    existingMachine.getServers().clear();
    existingMachine.getServers().put("new-ref", new ServerConfigImpl("new-port", "new-protocol", "new-path", singletonMap("key", "value")));
    defaultEnv.getMachines().remove(machineNames.get(0));
    defaultEnv.getRecipe().setContent("updated-content");
    defaultEnv.getRecipe().setContentType("updated-content-type");
    defaultEnv.getRecipe().setLocation("updated-location");
    defaultEnv.getRecipe().setType("updated-type");
    // Remove an existing environment
    final Optional<String> nonDefaultEnv = workspace.getConfig().getEnvironments().keySet().stream().filter(key -> !key.equals(workspace.getConfig().getDefaultEnv())).findAny();
    assertTrue(nonDefaultEnv.isPresent());
    workspace.getConfig().getEnvironments().remove(nonDefaultEnv.get());
    // Update workspace configuration
    final WorkspaceConfigImpl wCfg = workspace.getConfig();
    wCfg.setDefaultEnv("new-env");
    wCfg.setName("new-name");
    wCfg.setDescription("This is a new description");
    // Update workspace object
    workspace.setAccount(new AccountImpl("accId", "new-namespace", "test"));
    workspace.getAttributes().clear();
    workspaceDao.update(workspace);
    assertEquals(workspaceDao.get(workspace.getId()), new WorkspaceImpl(workspace, workspace.getAccount()));
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) BeforeWorkspaceRemovedEvent(org.eclipse.che.api.workspace.server.event.BeforeWorkspaceRemovedEvent) Arrays(java.util.Arrays) SourceStorageImpl(org.eclipse.che.api.workspace.server.model.impl.SourceStorageImpl) Listeners(org.testng.annotations.Listeners) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) TckRepository(org.eclipse.che.commons.test.tck.repository.TckRepository) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) Collections.singletonList(java.util.Collections.singletonList) CascadeEvent(org.eclipse.che.core.db.cascade.event.CascadeEvent) Mockito.doThrow(org.mockito.Mockito.doThrow) CascadeEventSubscriber(org.eclipse.che.core.db.cascade.CascadeEventSubscriber) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) SourceImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.SourceImpl) WorkspaceDao(org.eclipse.che.api.workspace.server.spi.WorkspaceDao) EventService(org.eclipse.che.api.core.notification.EventService) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) ActionImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ActionImpl) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeMethod(org.testng.annotations.BeforeMethod) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.VolumeImpl) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) MetadataImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.MetadataImpl) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Mockito.doCallRealMethod(org.mockito.Mockito.doCallRealMethod) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) Assert.assertNull(org.testng.Assert.assertNull) Page(org.eclipse.che.api.core.Page) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) AccountImpl(org.eclipse.che.account.spi.AccountImpl) TckRepositoryException(org.eclipse.che.commons.test.tck.repository.TckRepositoryException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) ProjectImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ProjectImpl) ConflictException(org.eclipse.che.api.core.ConflictException) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) Collections.singletonMap(java.util.Collections.singletonMap) EnvImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) WorkspaceRemovedEvent(org.eclipse.che.api.workspace.shared.event.WorkspaceRemovedEvent) Assert.fail(org.testng.Assert.fail) NotFoundException(org.eclipse.che.api.core.NotFoundException) EntrypointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl) Collectors.toList(java.util.stream.Collectors.toList) TckListener(org.eclipse.che.commons.test.tck.TckListener) ServerException(org.eclipse.che.api.core.ServerException) Assert.assertTrue(org.testng.Assert.assertTrue) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ArrayList(java.util.ArrayList) AccountImpl(org.eclipse.che.account.spi.AccountImpl) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) SourceStorageImpl(org.eclipse.che.api.workspace.server.model.impl.SourceStorageImpl) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl) Test(org.testng.annotations.Test)

Example 15 with RecipeImpl

use of org.eclipse.che.api.workspace.server.model.impl.RecipeImpl 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));
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Aggregations

RecipeImpl (org.eclipse.che.api.workspace.server.model.impl.RecipeImpl)42 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)38 Test (org.testng.annotations.Test)20 MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)18 ArrayList (java.util.ArrayList)16 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)12 ServerConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl)12 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)12 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)8 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)7 ProjectConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)7 SourceStorageImpl (org.eclipse.che.api.workspace.server.model.impl.SourceStorageImpl)7 HashMap (java.util.HashMap)6 AccountImpl (org.eclipse.che.account.spi.AccountImpl)6 WorkspaceDao (org.eclipse.che.api.workspace.server.spi.WorkspaceDao)6 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)5 DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)5 VolumeImpl (org.eclipse.che.api.workspace.server.model.impl.VolumeImpl)5 ServerException (org.eclipse.che.api.core.ServerException)4 TypeLiteral (com.google.inject.TypeLiteral)3