Skip to content

Commit

Permalink
Merge pull request #55 from swordqiu/hotfix/qj-webconsole-rdp
Browse files Browse the repository at this point in the history
fix: support webconolse api of ssh and rdp
  • Loading branch information
swordqiu authored Dec 14, 2023
2 parents 084eba3 + cc67a0a commit 6166333
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.yunionyun.mcp</groupId>
<artifactId>mcclient</artifactId>
<version>3.2.10</version>
<version>3.2.11</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import com.yunionyun.mcp.mcclient.EndpointType;
import com.yunionyun.mcp.mcclient.managers.BaseWebConsoleManager;

import com.alibaba.fastjson.JSONObject;
import com.yunionyun.mcp.mcclient.*;
import com.yunionyun.mcp.mcclient.common.McClientJavaBizException;

import java.io.IOException;
import java.io.*;

/**
* @author zxc
* @date 2020/01/19
Expand All @@ -15,4 +22,77 @@ public WebConsoleManager() {
public WebConsoleManager(EndpointType endpointType) {
super("webconsole", "webconsole", endpointType, new String[]{}, new String[]{});
}

public JSONObject DoConnect(Session s, String connType, String id, String action, JSONObject params)
throws McClientJavaBizException, IOException, JSONClientException {
StringBuilder url = new StringBuilder();
url.append("/webconsole/");
url.append(connType);
if (id != null && id.length() > 0) {
url.append("/");
url.append(id);
}
if (action != null && action.length() > 0) {
url.append("/");
url.append(action);
}
JSONObject body = new JSONObject();
if (params != null) {
body.put("webconsole", params);
}
System.out.println(url.toString() + body.toString());
return this._post(s, url.toString(), body, "webconsole");
}

/**
* @param s Session, client session, required
* @param id Id of server to connect, required
* @param ip IP to connect, optional for server with single IP
* @param port Port number to connect, optional if the port is 22
* @param username username to login, optional
* @param password password for the user, optional
*/
public JSONObject DoSshConnect(Session s, String id, String ip, int port, String username, String password)
throws McClientJavaBizException, IOException, JSONClientException {
JSONObject params = new JSONObject();
if (ip != null && ip.length() > 0) {
params.put("ip", ip);
}
if (port > 0) {
params.put("port", port);
}
if (username != null && username.length() > 0) {
params.put("username", username);
}
if (password != null && password.length() > 0) {
params.put("password", password);
}
return this.DoConnect(s, "ssh", id, "", params);
}

/**
* @param s Session, client session, required
* @param id Id of server to connect, required
* @param host Host to connect, optional for server with single IP
* @param port Port number to connect, optional if the port is 3389
* @param username username to login, optional
* @param password password for the user, optional
*/
public JSONObject DoRdpConnect(Session s, String id, String ip, int port, String username, String password)
throws McClientJavaBizException, IOException, JSONClientException {
JSONObject params = new JSONObject();
if (ip != null && ip.length() > 0) {
params.put("host", ip);
}
if (port > 0) {
params.put("port", port);
}
if (username != null && username.length() > 0) {
params.put("username", username);
}
if (password != null && password.length() > 0) {
params.put("password", password);
}
return this.DoConnect(s, "server-rdp", id, "", params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.yunionyun.mcp.mcclient;

import com.alibaba.fastjson.JSONObject;
import com.yunionyun.mcp.mcclient.EndpointType;
import com.yunionyun.mcp.mcclient.common.McClientJavaBizException;
import com.yunionyun.mcp.mcclient.keystone.TokenCredential;
import com.yunionyun.mcp.mcclient.managers.ListResult;
import com.yunionyun.mcp.mcclient.managers.impl.webconsole.WebConsoleManager;
import junit.framework.TestCase;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
* @author zxc
* @date 2020/03/27
*/
public class WebconsoleManagerTest extends TestCase {
public void testApp() {
Client cli = new Client("https://192.18.222.171:30357/v3", 500, true, true);
try {
TokenCredential token = cli.Authenticate("test", "password", "Domain", "system", "Default");
Session s = cli.newSession("region0", "", EndpointType.PublicURL, token);

WebConsoleManager webconsole = new WebConsoleManager();

JSONObject result = webconsole.DoSshConnect(s, "servername", "", 0, "", "");
System.out.println(result);

result = webconsole.DoRdpConnect(s, "ed73df6af7b", "", 0, "Administrator", "passwd0");
System.out.println(result);

} catch (JSONClientException e) {
e.printStackTrace();
} catch (McClientJavaBizException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

0 comments on commit 6166333

Please sign in to comment.