Skip to content

Commit

Permalink
che-starter redhat-developer#254: Cleanup & Refactoring - removing al…
Browse files Browse the repository at this point in the history
…l toggle / workspace migration / OpenShift specific code

Signed-off-by: Ilya Buziuk <ibuziuk@redhat.com>
  • Loading branch information
ibuziuk committed Jan 16, 2018
1 parent 2d6db29 commit 4c9ceac
Show file tree
Hide file tree
Showing 39 changed files with 182 additions and 1,801 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public enum CheRestEndpoints {
STOP_WORKSPACE ("/api/workspace/{id}/runtime"),
LIST_STACKS ("/api/stack?maxItems=1000"),
DELETE_PROJECT ("/project/{id}"),
SET_OAUTH_TOKEN_V1 ("/wsmaster/api/oauth/token?oauth_provider={provider}"),
SET_OAUTH_TOKEN_V2 ("/wsmaster/api/token/github"),
SET_OAUTH_TOKEN ("/wsmaster/api/token/github"),
GET_PREFERENCES ("/wsmaster/api/preferences"),
UPDATE_PREFERENCES ("/wsmaster/api/preferences");

Expand Down
114 changes: 0 additions & 114 deletions src/main/java/io/fabric8/che/starter/client/CheServerClient.java

This file was deleted.

60 changes: 31 additions & 29 deletions src/main/java/io/fabric8/che/starter/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
Expand All @@ -30,53 +31,54 @@
public class ProjectClient {
private static final Logger LOG = LoggerFactory.getLogger(ProjectClient.class);

@Value("${MULTI_TENANT_CHE_SERVER_URL:https://che.prod-preview.openshift.io}")
private String multiTenantCheServerURL;

@Autowired
private WorkspaceClient workspaceClient;

/**
* Delete a project from workspace. Workspace must be running to delete a
* project.
*
* @param cheServerURL
* @param workspaceName
* @param projectName
*/
public void deleteProject(String cheServerURL, Workspace workspace, String projectName, String keycloakToken) {
String wsAgentUrl = getWsAgentUrl(workspace);

String deleteProjectURL = CheRestEndpoints.DELETE_PROJECT.generateUrl(wsAgentUrl, projectName);
LOG.info("Deleting project {}", projectName);
RestTemplate template = new KeycloakRestTemplate(keycloakToken);
template.delete(deleteProjectURL);
}

@Async
public void deleteAllProjectsAndWorkspace(String cheServerURL, String workspaceName, String masterUrl, String namespace, String openShiftToken,
String keycloakToken) throws WorkspaceNotFound {
Workspace runningWorkspace = workspaceClient.getStartedWorkspace(cheServerURL, keycloakToken);
public void deleteAllProjectsAndWorkspace(String workspaceName, String keycloakToken) throws WorkspaceNotFound {
Workspace runningWorkspace = workspaceClient.getStartedWorkspace(keycloakToken);

Workspace workspaceToDelete = workspaceClient.startWorkspace(cheServerURL, workspaceName, masterUrl, namespace, openShiftToken, keycloakToken);
workspaceClient.waitUntilWorkspaceIsRunning(cheServerURL, workspaceToDelete, keycloakToken);
workspaceToDelete = workspaceClient.getWorkspaceById(cheServerURL, workspaceToDelete.getId(), keycloakToken);
Workspace workspaceToDelete = workspaceClient.startWorkspace(workspaceName, keycloakToken);
workspaceClient.waitUntilWorkspaceIsRunning(workspaceToDelete, keycloakToken);
workspaceToDelete = workspaceClient.getWorkspaceById(workspaceToDelete.getId(), keycloakToken);

List<Project> projectsToDelete = workspaceToDelete.getConfig().getProjects();
if (projectsToDelete != null && !projectsToDelete.isEmpty()) {
for (Project project : projectsToDelete) {
deleteProject(cheServerURL, workspaceToDelete, project.getName(), keycloakToken);
deleteProject(workspaceToDelete, project.getName(), keycloakToken);
}
}

workspaceClient.stopWorkspace(cheServerURL, workspaceToDelete, keycloakToken);
workspaceClient.waitUntilWorkspaceIsStopped(masterUrl, namespace, openShiftToken, cheServerURL, workspaceToDelete, keycloakToken);

workspaceClient.deleteWorkspace(cheServerURL, workspaceToDelete.getId(), keycloakToken);
workspaceClient.stopWorkspace(workspaceToDelete, keycloakToken);
workspaceClient.waitUntilWorkspaceIsStopped(workspaceToDelete, keycloakToken);
workspaceClient.deleteWorkspace(workspaceToDelete.getId(), keycloakToken);

if (runningWorkspace != null && !runningWorkspace.getConfig().getName().equals(workspaceName)) {
workspaceClient.startWorkspace(cheServerURL, runningWorkspace.getConfig().getName(), masterUrl, namespace, openShiftToken, keycloakToken);
workspaceClient.startWorkspace(runningWorkspace.getConfig().getName(), keycloakToken);

}
}

/**
* Delete a project from workspace. Workspace must be running to delete a
* project.
*
* @param workspaceName
* @param projectName
* @param keycloakToken
*/
public void deleteProject(Workspace workspace, String projectName, String keycloakToken) {
String wsAgentUrl = getWsAgentUrl(workspace);

String deleteProjectURL = CheRestEndpoints.DELETE_PROJECT.generateUrl(wsAgentUrl, projectName);
LOG.info("Deleting project {}", projectName);
RestTemplate template = new KeycloakRestTemplate(keycloakToken);
template.delete(deleteProjectURL);
}

private String getWsAgentUrl(final Workspace workspace) {
return workspace.getRuntime().getDevMachine().getRuntime().getServers().get("4401/tcp").getUrl();
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/io/fabric8/che/starter/client/StackClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.NoSuchElementException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
Expand All @@ -29,8 +30,11 @@
@Component
public class StackClient {

public List<Stack> listStacks(String cheServerUrl, String keycloakToken) {
String url = CheRestEndpoints.LIST_STACKS.generateUrl(cheServerUrl);
@Value("${MULTI_TENANT_CHE_SERVER_URL:https://che.prod-preview.openshift.io}")
private String multiTenantCheServerURL;

public List<Stack> listStacks(String keycloakToken) {
String url = CheRestEndpoints.LIST_STACKS.generateUrl(multiTenantCheServerURL);

RestTemplate template = new KeycloakRestTemplate(keycloakToken);

Expand All @@ -44,14 +48,13 @@ public List<Stack> listStacks(String cheServerUrl, String keycloakToken) {
* Gets image for specified stack ID. Throws StackNotFoundException if there is no such stack
* on the Che server.
*
* @param cheServerUrl URL of Che server
* @param stackId stack ID
* @param keycloakToken Keycloak token
* @return image name for stack
* @throws StackNotFoundException if no image name exists for such stack ID or call to Che server was not successful
*/
public Stack getStack(String cheServerUrl, String stackId, String keycloakToken) throws StackNotFoundException {
List<Stack> stacks = listStacks(cheServerUrl, keycloakToken);
public Stack getStack(String stackId, String keycloakToken) throws StackNotFoundException {
List<Stack> stacks = listStacks(keycloakToken);
if (stacks != null && !stacks.isEmpty()) {
try {
Stack stack = stacks.stream().filter(s -> stackId.equals(s.getId())).findFirst().get();
Expand Down
Loading

0 comments on commit 4c9ceac

Please sign in to comment.