diff --git a/pom.xml b/pom.xml index 891c7ad..7b9933d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,11 +1,11 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 it.corsinvest.proxmoxve cv4pve-api-java - 8.1.0 + 8.1.1 jar cv4pve-api-java Corsinvest for Proxmox VE Client API Java diff --git a/src/main/java/it/corsinvest/proxmoxve/api/PveClient.java b/src/main/java/it/corsinvest/proxmoxve/api/PveClient.java index edab161..3cbbae7 100644 --- a/src/main/java/it/corsinvest/proxmoxve/api/PveClient.java +++ b/src/main/java/it/corsinvest/proxmoxve/api/PveClient.java @@ -5,6 +5,7 @@ package it.corsinvest.proxmoxve.api; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.json.JSONException; @@ -679,7 +680,7 @@ public Result getSendmailEndpoint() throws JSONException { * @throws JSONException */ - public Result updateSendmailEndpoint(String author, String comment, String delete, String digest, Boolean disable, String from_address, String mailto, String mailto_user) throws JSONException { + public Result updateSendmailEndpoint(String author, String comment, List delete, String digest, Boolean disable, String from_address, List mailto, List mailto_user) throws JSONException { Map parameters = new HashMap<>(); parameters.put("author", author); parameters.put("comment", comment); @@ -729,7 +730,7 @@ public Result getSendmailEndpoints() throws JSONException { * @throws JSONException */ - public Result createSendmailEndpoint(String name, String author, String comment, Boolean disable, String from_address, String mailto, String mailto_user) throws JSONException { + public Result createSendmailEndpoint(String name, String author, String comment, Boolean disable, String from_address, List mailto, List mailto_user) throws JSONException { Map parameters = new HashMap<>(); parameters.put("name", name); parameters.put("author", author); @@ -816,7 +817,7 @@ public Result getGotifyEndpoint() throws JSONException { * @throws JSONException */ - public Result updateGotifyEndpoint(String comment, String delete, String digest, Boolean disable, String server, String token) throws JSONException { + public Result updateGotifyEndpoint(String comment, List delete, String digest, Boolean disable, String server, String token) throws JSONException { Map parameters = new HashMap<>(); parameters.put("comment", comment); parameters.put("delete", delete); @@ -964,7 +965,7 @@ public Result getSmtpEndpoint() throws JSONException { * @throws JSONException */ - public Result updateSmtpEndpoint(String author, String comment, String delete, String digest, Boolean disable, String from_address, String mailto, String mailto_user, String mode, String password, Integer port, String server, String username) throws JSONException { + public Result updateSmtpEndpoint(String author, String comment, List delete, String digest, Boolean disable, String from_address, List mailto, List mailto_user, String mode, String password, Integer port, String server, String username) throws JSONException { Map parameters = new HashMap<>(); parameters.put("author", author); parameters.put("comment", comment); @@ -1028,7 +1029,7 @@ public Result getSmtpEndpoints() throws JSONException { * @throws JSONException */ - public Result createSmtpEndpoint(String from_address, String name, String server, String author, String comment, Boolean disable, String mailto, String mailto_user, String mode, String password, Integer port, String username) throws JSONException { + public Result createSmtpEndpoint(String from_address, String name, String server, String author, String comment, Boolean disable, List mailto, List mailto_user, String mode, String password, Integer port, String username) throws JSONException { Map parameters = new HashMap<>(); parameters.put("from-address", from_address); parameters.put("name", name); @@ -1209,7 +1210,7 @@ public Result getMatcher() throws JSONException { * @throws JSONException */ - public Result updateMatcher(String comment, String delete, String digest, Boolean disable, Boolean invert_match, String match_calendar, String match_field, String match_severity, String mode, String target) throws JSONException { + public Result updateMatcher(String comment, List delete, String digest, Boolean disable, Boolean invert_match, List match_calendar, List match_field, List match_severity, String mode, List target) throws JSONException { Map parameters = new HashMap<>(); parameters.put("comment", comment); parameters.put("delete", delete); @@ -1266,7 +1267,7 @@ public Result getMatchers() throws JSONException { * @throws JSONException */ - public Result createMatcher(String name, String comment, Boolean disable, Boolean invert_match, String match_calendar, String match_field, String match_severity, String mode, String target) throws JSONException { + public Result createMatcher(String name, String comment, Boolean disable, Boolean invert_match, List match_calendar, List match_field, List match_severity, String mode, List target) throws JSONException { Map parameters = new HashMap<>(); parameters.put("name", name); parameters.put("comment", comment); @@ -2902,7 +2903,7 @@ public Result readJob() throws JSONException { * @throws JSONException */ - public Result updateJob(Boolean all, Integer bwlimit, String comment, String compress, String delete, String dow, String dumpdir, Boolean enabled, String exclude, String exclude_path, Integer ionice, Integer lockwait, String mailnotification, String mailto, Integer maxfiles, String mode, String node, String notes_template, String notification_policy, String notification_target, String performance, Integer pigz, String pool, Boolean protected_, String prune_backups, Boolean quiet, Boolean remove, Boolean repeat_missed, String schedule, String script, String starttime, Boolean stdexcludes, Boolean stop, Integer stopwait, String storage, String tmpdir, String vmid, Integer zstd) throws JSONException { + public Result updateJob(Boolean all, Integer bwlimit, String comment, String compress, String delete, String dow, String dumpdir, Boolean enabled, String exclude, List exclude_path, Integer ionice, Integer lockwait, String mailnotification, String mailto, Integer maxfiles, String mode, String node, String notes_template, String notification_policy, String notification_target, String performance, Integer pigz, String pool, Boolean protected_, String prune_backups, Boolean quiet, Boolean remove, Boolean repeat_missed, String schedule, String script, String starttime, Boolean stdexcludes, Boolean stop, Integer stopwait, String storage, String tmpdir, String vmid, Integer zstd) throws JSONException { Map parameters = new HashMap<>(); parameters.put("all", all); parameters.put("bwlimit", bwlimit); @@ -3041,7 +3042,7 @@ public Result index() throws JSONException { * @throws JSONException */ - public Result createJob(Boolean all, Integer bwlimit, String comment, String compress, String dow, String dumpdir, Boolean enabled, String exclude, String exclude_path, String id, Integer ionice, Integer lockwait, String mailnotification, String mailto, Integer maxfiles, String mode, String node, String notes_template, String notification_policy, String notification_target, String performance, Integer pigz, String pool, Boolean protected_, String prune_backups, Boolean quiet, Boolean remove, Boolean repeat_missed, String schedule, String script, String starttime, Boolean stdexcludes, Boolean stop, Integer stopwait, String storage, String tmpdir, String vmid, Integer zstd) throws JSONException { + public Result createJob(Boolean all, Integer bwlimit, String comment, String compress, String dow, String dumpdir, Boolean enabled, String exclude, List exclude_path, String id, Integer ionice, Integer lockwait, String mailnotification, String mailto, Integer maxfiles, String mode, String node, String notes_template, String notification_policy, String notification_target, String performance, Integer pigz, String pool, Boolean protected_, String prune_backups, Boolean quiet, Boolean remove, Boolean repeat_missed, String schedule, String script, String starttime, Boolean stdexcludes, Boolean stop, Integer stopwait, String storage, String tmpdir, String vmid, Integer zstd) throws JSONException { Map parameters = new HashMap<>(); parameters.put("all", all); parameters.put("bwlimit", bwlimit); @@ -4598,7 +4599,7 @@ public Result get() throws JSONException { * @throws JSONException */ - public Result update(String delete, String description, String digest, String map, Boolean mdev) throws JSONException { + public Result update(String delete, String description, String digest, List map, Boolean mdev) throws JSONException { Map parameters = new HashMap<>(); parameters.put("delete", delete); parameters.put("description", description); @@ -4658,7 +4659,7 @@ public Result index() throws JSONException { * @throws JSONException */ - public Result create(String id, String map, String description, Boolean mdev) throws JSONException { + public Result create(String id, List map, String description, Boolean mdev) throws JSONException { Map parameters = new HashMap<>(); parameters.put("id", id); parameters.put("map", map); @@ -4676,7 +4677,7 @@ public Result create(String id, String map, String description, Boolean mdev) th * @throws JSONException */ - public Result create(String id, String map) throws JSONException { + public Result create(String id, List map) throws JSONException { Map parameters = new HashMap<>(); parameters.put("id", id); parameters.put("map", map); @@ -4742,7 +4743,7 @@ public Result get() throws JSONException { * @throws JSONException */ - public Result update(String map, String delete, String description, String digest) throws JSONException { + public Result update(List map, String delete, String description, String digest) throws JSONException { Map parameters = new HashMap<>(); parameters.put("map", map); parameters.put("delete", delete); @@ -4759,7 +4760,7 @@ public Result update(String map, String delete, String description, String diges * @throws JSONException */ - public Result update(String map) throws JSONException { + public Result update(List map) throws JSONException { Map parameters = new HashMap<>(); parameters.put("map", map); return client.set("/cluster/mapping/usb/" + this.id + "", parameters); @@ -4803,7 +4804,7 @@ public Result index() throws JSONException { * @throws JSONException */ - public Result create(String id, String map, String description) throws JSONException { + public Result create(String id, List map, String description) throws JSONException { Map parameters = new HashMap<>(); parameters.put("id", id); parameters.put("map", map); @@ -4820,7 +4821,7 @@ public Result create(String id, String map, String description) throws JSONExcep * @throws JSONException */ - public Result create(String id, String map) throws JSONException { + public Result create(String id, List map) throws JSONException { Map parameters = new HashMap<>(); parameters.put("id", id); parameters.put("map", map); @@ -4996,7 +4997,7 @@ public Result read() throws JSONException { * @throws JSONException */ - public Result update(String delete, String dhcp_dns_server, String dhcp_range, String digest, String dnszoneprefix, String gateway, Boolean snat) throws JSONException { + public Result update(String delete, String dhcp_dns_server, List dhcp_range, String digest, String dnszoneprefix, String gateway, Boolean snat) throws JSONException { Map parameters = new HashMap<>(); parameters.put("delete", delete); parameters.put("dhcp-dns-server", dhcp_dns_server); @@ -5065,7 +5066,7 @@ public Result index() throws JSONException { * @throws JSONException */ - public Result create(String subnet, String type, String dhcp_dns_server, String dhcp_range, String dnszoneprefix, String gateway, Boolean snat) throws JSONException { + public Result create(String subnet, String type, String dhcp_dns_server, List dhcp_range, String dnszoneprefix, String gateway, Boolean snat) throws JSONException { Map parameters = new HashMap<>(); parameters.put("subnet", subnet); parameters.put("type", type); @@ -8423,7 +8424,7 @@ protected PVEExec(PveClient client, Object node, Object vmid) { * @return Result * @throws JSONException */ - public Result exec(String command, String input_data) throws JSONException { + public Result exec(List command, String input_data) throws JSONException { Map parameters = new HashMap<>(); parameters.put("command", command); parameters.put("input-data", input_data); @@ -8440,7 +8441,7 @@ public Result exec(String command, String input_data) throws JSONException { * @throws JSONException */ - public Result exec(String command) throws JSONException { + public Result exec(List command) throws JSONException { Map parameters = new HashMap<>(); parameters.put("command", command); return client.create("/nodes/" + this.node + "/qemu/" + this.vmid + "/agent/exec", parameters); @@ -15470,7 +15471,7 @@ public Result extractconfig(String volume) throws JSONException { * @return Result * @throws JSONException */ - public Result vzdump(Boolean all, Integer bwlimit, String compress, String dumpdir, String exclude, String exclude_path, Integer ionice, Integer lockwait, String mailnotification, String mailto, Integer maxfiles, String mode, String notes_template, String notification_policy, String notification_target, String performance, Integer pigz, String pool, Boolean protected_, String prune_backups, Boolean quiet, Boolean remove, String script, Boolean stdexcludes, Boolean stdout, Boolean stop, Integer stopwait, String storage, String tmpdir, String vmid, Integer zstd) throws JSONException { + public Result vzdump(Boolean all, Integer bwlimit, String compress, String dumpdir, String exclude, List exclude_path, Integer ionice, Integer lockwait, String mailnotification, String mailto, Integer maxfiles, String mode, String notes_template, String notification_policy, String notification_target, String performance, Integer pigz, String pool, Boolean protected_, String prune_backups, Boolean quiet, Boolean remove, String script, Boolean stdexcludes, Boolean stdout, Boolean stop, Integer stopwait, String storage, String tmpdir, String vmid, Integer zstd) throws JSONException { Map parameters = new HashMap<>(); parameters.put("all", all); parameters.put("bwlimit", bwlimit); diff --git a/src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java b/src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java index bce255d..38ab20c 100644 --- a/src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java +++ b/src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java @@ -7,11 +7,9 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.Proxy; import java.net.URL; -import java.net.URLEncoder; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; @@ -294,31 +292,28 @@ private Result executeAction(String resource, MethodType methodType, Map(); + Map params = new LinkedHashMap<>(); if (parameters != null) { parameters.entrySet().stream().filter((entry) -> (entry.getValue() != null)).forEachOrdered((entry) -> { - String value = entry.getValue().toString(); - if (entry.getValue() instanceof Boolean) { - value = ((Boolean) entry.getValue()) ? "1" : "0"; + Object value = entry.getValue(); + if (value instanceof Boolean) { + params.put(entry.getKey(), ((Boolean) value) ? 1 : 0); + } else { + params.put(entry.getKey(), value); } - params.put(entry.getKey(), value); }); } @@ -364,12 +359,10 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) { if (!params.isEmpty()) { StringBuilder urlParams = new StringBuilder(); params.forEach((key, value) -> { - try { - urlParams.append(urlParams.length() > 0 ? "&" : "").append(key).append("=") - .append(URLEncoder.encode((String) value, "UTF-8")); - } catch (UnsupportedEncodingException ex) { - Logger.getLogger(PveClientBase.class.getName()).log(Level.SEVERE, null, ex); - } + urlParams.append(urlParams.length() > 0 ? "&" : "") + .append(key) + .append("=") + .append(value.toString()); }); url += "?" + urlParams.toString(); } @@ -382,20 +375,24 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) { case SET: case CREATE: { - StringBuilder postData = new StringBuilder(); + /* + var postData = new StringBuilder(); params.forEach((key, value) -> { postData.append(postData.length() > 0 ? "&" : "").append(key).append("=").append(value); }); - byte[] postDataBytes = postData.toString().getBytes("UTF-8"); + var postDataBytes = postData.toString().getBytes("UTF-8"); + */ + + String data = new JSONObject(params).toString(); httpCon = (HttpURLConnection) new URL(url).openConnection(_proxy); httpCon.setRequestMethod(httpMethod); - httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - httpCon.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); + httpCon.setRequestProperty("Content-Type", "application/json"); + httpCon.setRequestProperty("Content-Length", String.valueOf(data.length())); setToken(httpCon); httpCon.setDoOutput(true); - httpCon.getOutputStream().write(postDataBytes); + httpCon.getOutputStream().write(data.getBytes("UTF-8")); break; } @@ -507,6 +504,7 @@ public boolean waitForTaskToFinish(String task, long wait, long timeOut) throws if (timeOut < wait) { timeOut = wait + 5000; } + long timeStart = System.currentTimeMillis(); long waitTime = System.currentTimeMillis(); while (isRunning && (System.currentTimeMillis() - timeStart) < timeOut) { @@ -550,7 +548,7 @@ public String getExitStatusTask(String task) throws JSONException { * @throws JSONException */ public static List JSONArrayToList(JSONArray array) throws JSONException { - List ret = new ArrayList<>(); + ArrayList ret = new ArrayList(); if (array != null) { for (int i = 0; i < array.length(); i++) { ret.add((T) array.get(i)); diff --git a/src/test/java/Test.java b/src/test/java/Test.java index 52f76a1..10dad1d 100644 --- a/src/test/java/Test.java +++ b/src/test/java/Test.java @@ -2,19 +2,19 @@ * SPDX-FileCopyrightText: Copyright Corsinvest Srl * SPDX-License-Identifier: GPL-3.0-only */ - -import it.corsinvest.proxmoxve.api.PveClient; -import it.corsinvest.proxmoxve.api.PveExceptionAuthentication; -import it.corsinvest.proxmoxve.api.Result; +import it.corsinvest.proxmoxve.api.*; +import java.util.ArrayList; +import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class Test { - public static void main(String[] args) throws JSONException, PveExceptionAuthentication { - PveClient client = new PveClient("10.92.90.101", 8006); - if (client.login("test@pve", "test12345")) { + public static void main(String[] args) throws JSONException ,PveExceptionAuthentication{ + PveClient client = new PveClient("192.168.0.2", 8006); + client.setDebugLevel(999); + if (client.login("root", "password")) { //version System.out.println(client.getVersion().version().getResponse().get("data")); @@ -26,10 +26,19 @@ public static void main(String[] args) throws JSONException, PveExceptionAuthent } //loop nodes for each - PveClient.JSONArrayToList(client.getNodes().index().getResponse().getJSONArray("data")).forEach((node) -> { + PveClientBase.JSONArrayToList(client.getNodes().index().getResponse().getJSONArray("data")).forEach((node) -> { System.out.println(node); }); + List commands = new ArrayList<>(); + commands.add("powershell"); + commands.add("-command"); + commands.add("echo"); + commands.add("test"); + + client.getNodes().get("cc01").getQemu().get(1006).getAgent().getExec().exec(commands); + + /* String nodeId = "cv-pve02"; //loops vms qemu @@ -58,6 +67,7 @@ public static void main(String[] args) throws JSONException, PveExceptionAuthent //delete snapshot Result retDeleSnap = client.getNodes().get(nodeId).getQemu().get(100).getSnapshot().get("pippo").delsnapshot(); System.out.println(retDeleSnap.getResponse().get("data")); + */ } } }