Skip to content

Commit 267a5e0

Browse files
authored
Merge pull request #88 from eharris369/87-supportProjectCreate
Issue #87: Add support for project create
2 parents 76c2999 + 143da35 commit 267a5e0

File tree

15 files changed

+897
-77
lines changed

15 files changed

+897
-77
lines changed

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/MCUtil.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018 IBM Corporation and others.
2+
* Copyright (c) 2018, 2019 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -46,6 +46,17 @@ public void run() {
4646
}
4747
});
4848
}
49+
50+
public static boolean openConfirmDialog(String title, String msg) {
51+
final boolean[] result = new boolean[1];
52+
Display.getDefault().syncExec(new Runnable() {
53+
@Override
54+
public void run() {
55+
result[0] = MessageDialog.open(MessageDialog.CONFIRM, Display.getDefault().getActiveShell(), title, msg, 0);
56+
}
57+
});
58+
return result[0];
59+
}
4960

5061

5162
public static String readAllFromStream(InputStream stream) {

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/connection/MicroclimateConnection.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.ibm.microclimate.core.internal.MicroclimateApplication;
4040
import com.ibm.microclimate.core.internal.MicroclimateApplicationFactory;
4141
import com.ibm.microclimate.core.internal.console.ProjectLogInfo;
42+
import com.ibm.microclimate.core.internal.console.ProjectTemplateInfo;
4243
import com.ibm.microclimate.core.internal.constants.MCConstants;
4344
import com.ibm.microclimate.core.internal.constants.ProjectType;
4445
import com.ibm.microclimate.core.internal.messages.Messages;
@@ -601,6 +602,41 @@ public JSONObject requestProjectCapabilities(MicroclimateApplication app) throws
601602
return capabilities;
602603
}
603604

605+
public List<ProjectTemplateInfo> requestProjectTemplates() throws IOException, JSONException {
606+
List<ProjectTemplateInfo> templates = new ArrayList<ProjectTemplateInfo>();
607+
final URI uri = baseUrl.resolve(MCConstants.APIPATH_BASEV2 + "/" + MCConstants.APIPATH_PROJECT_TYPES);
608+
HttpResult result = HttpUtil.get(uri);
609+
checkResult(result, uri, true);
610+
611+
JSONArray templateArray = new JSONArray(result.response);
612+
for (int i = 0; i < templateArray.length(); i++) {
613+
templates.add(new ProjectTemplateInfo(templateArray.getJSONObject(i)));
614+
}
615+
616+
return templates;
617+
}
618+
619+
public void requestProjectCreate(ProjectTemplateInfo templateInfo, String name)
620+
throws JSONException, IOException {
621+
622+
// Special case node.js projects which don't have a template
623+
if (ProjectType.LANGUAGE_NODEJS.equals(templateInfo.getLanguage())) {
624+
requestNodeProjectCreate(name);
625+
return;
626+
}
627+
628+
String endpoint = MCConstants.APIPATH_BASEV2 + "/" + MCConstants.APIPATH_PROJECTS;
629+
630+
URI uri = baseUrl.resolve(endpoint);
631+
632+
JSONObject createProjectPayload = new JSONObject();
633+
createProjectPayload.put(MCConstants.KEY_NAME, name);
634+
createProjectPayload.put(MCConstants.KEY_EXTENSION, templateInfo.getExtension());
635+
636+
HttpResult result = HttpUtil.post(uri, createProjectPayload);
637+
checkResult(result, uri, false);
638+
}
639+
604640
private void checkResult(HttpResult result, URI uri, boolean checkContent) throws IOException {
605641
if (!result.isGoodResponse) {
606642
final String msg = String.format("Received bad response code %d for uri %s with error message %s", //$NON-NLS-1$
@@ -762,11 +798,12 @@ public void requestNodeProjectCreate(String name)
762798
public void requestProjectDelete(String projectId)
763799
throws JSONException, IOException {
764800

765-
String createEndpoint = MCConstants.APIPATH_PROJECT_LIST + "/" + projectId;
801+
String endpoint = MCConstants.APIPATH_PROJECT_LIST + "/" + projectId;
766802

767-
URI url = baseUrl.resolve(createEndpoint);
803+
URI uri = baseUrl.resolve(endpoint);
768804

769-
HttpUtil.delete(url);
805+
HttpResult result = HttpUtil.delete(uri);
806+
checkResult(result, uri, false);
770807
}
771808

772809
public IPath getWorkspacePath() {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* IBM Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.ibm.microclimate.core.internal.console;
13+
14+
import org.json.JSONException;
15+
import org.json.JSONObject;
16+
17+
import com.ibm.microclimate.core.internal.MCLogger;
18+
19+
public class ProjectTemplateInfo {
20+
21+
public static final String LABEL_KEY = "label";
22+
public static final String DESCRIPTION_KEY = "description";
23+
public static final String EXTENSION_KEY = "extension";
24+
public static final String LANGUAGE_KEY = "language";
25+
26+
private JSONObject projectInfo;
27+
28+
public ProjectTemplateInfo(JSONObject projectInfo) {
29+
this.projectInfo = projectInfo;
30+
}
31+
32+
public String getLabel() {
33+
return getString(LABEL_KEY);
34+
}
35+
36+
public String getDescription() {
37+
return getString(DESCRIPTION_KEY);
38+
}
39+
40+
public String getLanguage() {
41+
return getString(LANGUAGE_KEY);
42+
}
43+
44+
public String getExtension() {
45+
return getString(EXTENSION_KEY);
46+
}
47+
48+
private String getString(String key) {
49+
String value = null;
50+
if (projectInfo.has(key)) {
51+
try {
52+
value = projectInfo.getString(key);
53+
} catch (JSONException e) {
54+
MCLogger.logError("An error occurred retrieving the value from the project template object for key: " + key, e);
55+
}
56+
} else {
57+
MCLogger.logError("The project template object did not have the expected key: " + key);
58+
}
59+
return value;
60+
}
61+
62+
}

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/constants/MCConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ private MCConstants() {}
3737
APIPATH_CAPABILITIES = "capabilities",
3838
APIPATH_LOGS = "logs",
3939
APIPATH_METRICS_STATUS = "metrics/status",
40+
41+
// V2 API endpoints
42+
APIPATH_BASEV2 = "api/v2",
43+
APIPATH_PROJECT_TYPES = "project-types",
44+
APIPATH_PROJECTS = "projects",
4045

4146
// JSON keys
4247
KEY_PROJECT_ID = "projectID",
@@ -79,6 +84,7 @@ private MCConstants() {}
7984

8085
KEY_LANGUAGE = "language",
8186
KEY_FRAMEWORK = "framework",
87+
KEY_EXTENSION = "extension",
8288

8389
KEY_START_MODE = "startMode",
8490
KEY_ACTION = "action",

dev/com.ibm.microclimate.ui/plugin.properties

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ Bundle-Name = Microclimate UI Plugin
1717

1818
ACTION_REFRESH=Re&fresh
1919

20-
ACTION_CONNECTION_OPEN_MICROCLIMATE_UI=Open Microclimate &UI
21-
ACTION_CONNECTION_CREATE_NEW_PROJECT=Open New &Project Page
22-
ACTION_CONNECTION_IMPORT_PROJECT=Open &Import Project Page
2320
ACTION_CONNECTION_REMOVE_CONNECTION=&Remove Connection
2421

2522
ACTION_OPEN_APP=&Open Application

dev/com.ibm.microclimate.ui/plugin.xml

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@
9393
</or>
9494
</enablement>
9595
</actionProvider>
96+
<actionProvider
97+
id="com.ibm.microclimate.ui.microclimateConnectionActionProvider"
98+
class="com.ibm.microclimate.ui.internal.actions.MicroclimateConnectionActionProvider">
99+
<enablement>
100+
<or>
101+
<instanceof value="com.ibm.microclimate.core.internal.connection.MicroclimateConnection" />
102+
</or>
103+
</enablement>
104+
</actionProvider>
96105
<actionProvider
97106
id="com.ibm.microclimate.ui.microclimateApplicationActionProvider"
98107
class="com.ibm.microclimate.ui.internal.actions.MicroclimateApplicationActionProvider">
@@ -196,27 +205,6 @@
196205
menubarPath="group.additions"
197206
label="%ACTION_CONNECTION_REMOVE_CONNECTION"
198207
class="com.ibm.microclimate.ui.internal.actions.RemoveConnectionAction"/>
199-
<action
200-
id="com.ibm.microclimate.ui.importProject"
201-
enablesFor="1"
202-
menubarPath="group.new"
203-
icon="%DEFAULT_ICON_PATH"
204-
label="%ACTION_CONNECTION_IMPORT_PROJECT"
205-
class="com.ibm.microclimate.ui.internal.actions.OpenMicroclimateUIAction"/>
206-
<action
207-
id="com.ibm.microclimate.ui.createNewProject"
208-
enablesFor="1"
209-
menubarPath="group.new"
210-
icon="%DEFAULT_ICON_PATH"
211-
label="%ACTION_CONNECTION_CREATE_NEW_PROJECT"
212-
class="com.ibm.microclimate.ui.internal.actions.OpenMicroclimateUIAction"/>
213-
<action
214-
id="com.ibm.microclimate.ui.openMicroclimateUI"
215-
enablesFor="1"
216-
menubarPath="group.new"
217-
icon="%DEFAULT_ICON_PATH"
218-
label="%ACTION_CONNECTION_OPEN_MICROCLIMATE_UI"
219-
class="com.ibm.microclimate.ui.internal.actions.OpenMicroclimateUIAction"/>
220208
</objectContribution>
221209
</extension>
222210

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* IBM Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.ibm.microclimate.ui.internal.actions;
13+
14+
import org.eclipse.core.resources.IProject;
15+
import org.eclipse.core.resources.ResourcesPlugin;
16+
import org.eclipse.core.runtime.NullProgressMonitor;
17+
import org.eclipse.jface.viewers.ISelectionProvider;
18+
import org.eclipse.jface.viewers.IStructuredSelection;
19+
import org.eclipse.osgi.util.NLS;
20+
import org.eclipse.ui.ISharedImages;
21+
import org.eclipse.ui.PlatformUI;
22+
import org.eclipse.ui.actions.SelectionProviderAction;
23+
24+
import com.ibm.microclimate.core.internal.MCEclipseApplication;
25+
import com.ibm.microclimate.core.internal.MCLogger;
26+
import com.ibm.microclimate.core.internal.MCUtil;
27+
import com.ibm.microclimate.ui.internal.messages.Messages;
28+
29+
/**
30+
* Action for deleting a Microclimate project. If a project exists in the
31+
* workspace with the same name and location as the Microclimate project,
32+
* it will be deleted as well.
33+
*/
34+
public class DeleteProjectAction extends SelectionProviderAction {
35+
36+
MCEclipseApplication app;
37+
38+
public DeleteProjectAction(ISelectionProvider selectionProvider) {
39+
super(selectionProvider, Messages.DeleteProjectLabel);
40+
selectionChanged(getStructuredSelection());
41+
setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ETOOL_DELETE));
42+
}
43+
44+
@Override
45+
public void selectionChanged(IStructuredSelection sel) {
46+
if (sel.size() == 1) {
47+
Object obj = sel.getFirstElement();
48+
if (obj instanceof MCEclipseApplication) {
49+
app = (MCEclipseApplication) obj;
50+
setEnabled(app.isAvailable());
51+
return;
52+
}
53+
}
54+
setEnabled(false);
55+
}
56+
57+
@Override
58+
public void run() {
59+
if (app == null) {
60+
// should not be possible
61+
MCLogger.logError("DeleteProjectAction ran but no application was selected");
62+
return;
63+
}
64+
65+
if (MCUtil.openConfirmDialog(Messages.DeleteProjectTitle, NLS.bind(Messages.DeleteProjectMessage, app.name))) {
66+
try {
67+
app.mcConnection.requestProjectDelete(app.projectID);
68+
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(app.name);
69+
if (project != null && project.exists() && project.getLocation().equals(app.fullLocalPath)) {
70+
project.delete(false, true, new NullProgressMonitor());
71+
}
72+
} catch (Exception e) {
73+
MCLogger.logError("An error occurred deleting the project: " + app.name + ", with id: " + app.projectID, e);
74+
MCUtil.openDialog(true, Messages.DeleteProjectErrorTitle,
75+
NLS.bind(Messages.DeleteProjectErrorMsg, new String[] {app.name, e.getMessage()}));
76+
}
77+
}
78+
}
79+
}

dev/com.ibm.microclimate.ui/src/com/ibm/microclimate/ui/internal/actions/MicroclimateApplicationActionProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class MicroclimateApplicationActionProvider extends CommonActionProvider
2525
private ValidateAction validateAction;
2626
private AttachDebuggerAction attachDebuggerAction;
2727
private OpenAppMonitorAction openAppMonitorAction;
28+
private DeleteProjectAction deleteProjectAction;
2829

2930
@Override
3031
public void init(ICommonActionExtensionSite aSite) {
@@ -33,6 +34,7 @@ public void init(ICommonActionExtensionSite aSite) {
3334
validateAction = new ValidateAction(selProvider);
3435
attachDebuggerAction = new AttachDebuggerAction(selProvider);
3536
openAppMonitorAction = new OpenAppMonitorAction(selProvider);
37+
deleteProjectAction = new DeleteProjectAction(selProvider);
3638
}
3739

3840
@Override
@@ -46,6 +48,8 @@ public void fillContextMenu(IMenuManager menu) {
4648
if (openAppMonitorAction.showAction()) {
4749
menu.appendToGroup(ICommonMenuConstants.GROUP_OPEN, openAppMonitorAction);
4850
}
51+
menu.appendToGroup(ICommonMenuConstants.GROUP_ADDITIONS, deleteProjectAction);
52+
4953
}
5054

5155
}

0 commit comments

Comments
 (0)