Code Monkey home page Code Monkey logo

cv4pve-api-java's Introduction

cv4pve-api-java

Proxmox VE Client API Java

GitHub release

Proxmox VE Api

   ______                _                      __
  / ____/___  __________(_)___ _   _____  _____/ /_
 / /   / __ \/ ___/ ___/ / __ \ | / / _ \/ ___/ __/
/ /___/ /_/ / /  (__  ) / / / / |/ /  __(__  ) /_
\____/\____/_/  /____/_/_/ /_/|___/\___/____/\__/

Corsinvest for Proxmox VE Api Client  (Made in Italy)

Copyright and License

Copyright: Corsinvest Srl For licensing details please visit LICENSE

Commercial Support

This software is part of a suite of tools called cv4pve-tools. If you want commercial support, visit the site

General

The client is generated from a JSON Api on Proxmox VE.

Result

The result is class Result and contain methods:

  • getResponse() returned from Proxmox VE (data,errors,...) JSONObject .
  • responseInError (bool) : Contains errors from Proxmox VE.
  • getStatusCode() (int) : Status code of the HTTP response.
  • getReasonPhrase() (string): The reason phrase which typically is sent by servers together with the status code.
  • isSuccessStatusCode() (bool) : Gets a value that indicates if the HTTP response was successful.
  • getError() (string) : Get error.

Main features

  • Easy to learn
  • Method named
  • Implementation respect the Api structure of Proxmox VE
  • Full method generated from documentation
  • Comment any method and parameters
  • Parameters indexed eg [n] is structured in array index and value
  • Tree structure
    • client.getNodes().get("pve1").getQemu().vmlist().getResponse().getJSONArray("data")
  • Return data Proxmox VE
  • Debug Level show to console information
  • Return result
    • Request
    • Response
    • Status
  • Last result action
  • Wait task finish task
    • waitForTaskToFinish
    • taskIsRunning
    • getExitStatusTask
  • Method directly access
    • get
    • set
    • create
    • delete
  • Login return bool if access
  • Return Result class more information
  • Minimal dependency library
  • ClientBase lite function
  • Form Proxmox VE 6.2 support Api Token for user
  • Login with One-time password for Two-factor authentication
  • Support for Proxy
  • Set Timeout for the Connection.

Api token

From version 6.2 of Proxmox VE is possible to use Api token. This feature permit execute Api without using user and password. If using Privilege Separation when create api token remember specify in permission. Format USER@REALM!TOKENID=UUID

Usage

//if you want use lite version only get/set/create/delete use PveClientBase

PveClient client = new PveClient("10.92.90.91", 8006);
if (client.login("root", "password", "pam")) {
        //version
        System.out.println(client.getVersion().version().getResponse().get("data"));

        // same for put/post/delete
        //loop nodes for
        JSONArray nodes = client.getNodes().index().getResponse().getJSONArray("data");
        for (int i = 0; i < nodes.length(); i++) {
                System.out.println(nodes.get(i));
        }

        //loop nodes for each
        PveClient.<JSONObject>JSONArrayToList(client.getNodes().index().getResponse().getJSONArray("data")).forEach((node) -> {
                System.out.println(node);
        });

        //loops vms qemu
        JSONArray vms = client.getNodes().get("pve1").getQemu().vmlist().getResponse().getJSONArray("data");
        for (int i = 0; i < vms.length(); i++) {
                System.out.println(vms.get(i));
        }

        //loop snapshots
        JSONArray snapshots = client.getNodes().get("pve1")
                .getQemu().get(100).getSnapshot().snapshotList().getResponse().getJSONArray("data");
        for (int i = 0; i < snapshots.length(); i++) {
                System.out.println(snapshots.get(i));
        }

        //create snapshot
        JSONObject retCreateSnap = client.getNodes().get("pve1").getQemu().get(100).getSnapshot().snapshot("pippo").getResponse();

        //print UPID
        System.out.println(retCreateSnap.get("data"));

        //wait creation
        client.waitForTaskToFinish("pve1", retCreateSnap.getString("data"), 500, 10000);

        //delete snapshot
        Result retDeleSnap = client.getNodes().get("pve1").getQemu().get(100).getSnapshot().get("pippo").delsnapshot();
        System.out.println(retDeleSnap.getResponse().get("data"));
}

cv4pve-api-java's People

Contributors

dependabot[bot] avatar franklupo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cv4pve-api-java's Issues

Connection not working with pam

I'm experiencing an issue with the api at login:

org.json.JSONException: A JSONObject text must begin with '{' at character 0

The connection to the pve server works just fine with curl and the php api.

What is the issue here?

Mistake in waitForTaskToFinish

In line 476, class PveClientBase, there is the following condition in the method waitForTaskToFinish:
while (isRunning && (timeStart - System.currentTimeMillis()) < timeOut)

After a timeout, the (timeStart - System.currentTimeMillis()) < timeOut condition should be false.
But this condition is never false, because timeStart - System.currentTimeMillis() is always negative and less than a positive timeout.

Right condition would be: (System.currentTimeMillis() - timeStart) < timeOut
Condition must also get corrected in the last line of this method.

Please correct me if i am wrong

// Edit: Maybe it would be better to return a boolean instead of an int. 'false' for timeout exceeded, 'true' for task finished in a time less than timeout

Connection timeout

What happened?

Hi,

I am opening a connection using vp4pve-api-java to my proxmox server. This connection is maintained for multi requests purposes.

After a day, each request returns an empty "data" JSON field as if there is no communication anymore.

I feel like the connection to the proxmox server goes timeout and there is no reconnection process included in the API nor a disconnect / logout method.

I would like to keep the connection open to increase performance.

Do you have any idea to help me solve this problem?

Thanks for any help you can provide.

Expected behavior

The connection is renewed or at least give us the possibility to disconnect and reconnect manually.

Relevant log output

org.json.JSONException: JSONObject["data"] not found.

Java Version

SE 8

Proxmox VE Version

7.2

Version (bug)

v7.1.0

Version (working)

No response

On what operating system are you experiencing the issue?

Linux

Pull Request

  • I would like to do a Pull Request

Get UPID of shutdown task

Hello,

is it possible to get the UPID of a shutdown task? I am calling the shutdown like this:
Result result = node.getLxc().get(serverId).getStatus().getShutdown().vmShutdown();

But the result doesn't contain a UPID. I want to wait for the task to finish.

When i call the shutdown POST method via pvesh i get the UPID:
pvesh create /nodes/host01/lxc/100/status/shutdown --output-format json-pretty
Results in the following output:
"UPID:host01:0000377E:19E15146:5F3C0469:vzshutdown:100:root@pam:"

Login dont Work with another realm

We have an AD Realm named e.x. ad. And the User test. The User has Administrator rights at /. But the login isnt working when i say: client.login("test", "test", "ad"). Other realms are working fine.

Parameter verification failed when configuring network devices for lxc / qemu

Hello,

i have a problem with configuring network devices via your api on lxc creation (and also qemu creation using cloudinit). Maybe i am doing something wrong, but maybe there is some internal problem in your api?

Basically, i have the following code:

Map<Integer, String> netN = new HashMap<>(); netN.put(0, "name=eth0,rate=15,bridge=vmbr0,firewall=1,gw=xx.xx.xx.xx,ip=xx.xx.xx.xx/24,hwaddr=02:00:be:0f:f2:63");

The netN map i am passing to the getLxc().createVm function.
I also tried it with only passing the name field ("name=eth0"). Am i doing something wrong?

I am also using your PHP api and there are no problems.

Here is the output of the debug messages, the net0 string is normally right, using the same in your php api:
Parameters: net0 : name=eth0,rate=15,bridge=vmbr0,firewall=1,gw=xx.xx.xx.xx,ip=xx.xx.xx.xx/24,hwaddr=02:00:be:0f:f2:63 memory : 1024 cpulimit : 1 ostemplate : local:vztmpl/debian-10.0-x86_64.tar.gz hostname : test password : test cores : 1 vmid : 289 swap : 512 start : 1 unprivileged : 1 onboot : 1 rootfs : disk1:50 arch : amd64

I always get the following result:
StatusCode: 400
ReasonPhrase: Parameter verification failed.
IsSuccessStatusCode: false

SSH Keys not working

Hey there,

i am trying to change my ssh keys with the code below inside PVEConfig.
Bildschirmfoto 2020-04-13 um 23 51 36

Now an error occures.

java.io.IOException: Server returned HTTP response code: 400 for URL: https://192.168.178.131:8006/api2/json/nodes/pve/qemu/1203/config
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_192]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_192]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_192]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_192]
	at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944) ~[na:1.8.0_192]
	at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939) ~[na:1.8.0_192]
	at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_192]
	at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1938) ~[na:1.8.0_192]
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1508) ~[na:1.8.0_192]
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) ~[na:1.8.0_192]
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263) ~[na:1.8.0_192]
	at it.corsinvest.proxmoxve.api.PveClientBase.executeAction(PveClientBase.java:355) [main/:na]
	at it.corsinvest.proxmoxve.api.PveClientBase.create(PveClientBase.java:178) [main/:na]
	at it.corsinvest.proxmoxve.api.PveClient$PVENodes$PVEItemNode$PVEQemu$PVEItemVmid$PVEConfig.setRest(PveClient.java:9391) [main/:na]
	at de.vpsnow.restendpoint.service.impl.ProxmoxDeploymentService.lambda$createServer$0(ProxmoxDeploymentService.java:68) [main/:na]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_192]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_192]
	at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_192]
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://192.168.178.131:8006/api2/json/nodes/pve/qemu/1203/config
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894) ~[na:1.8.0_192]
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) ~[na:1.8.0_192]
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) ~[na:1.8.0_192]
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347) ~[na:1.8.0_192]
	at it.corsinvest.proxmoxve.api.PveClientBase.executeAction(PveClientBase.java:352) [main/:na]
	... 6 common frames omitted

Don't manage to create QEMU VM

Hi,

First of all thanks for your java portage. I have a problem when i try to create a VM.
If i call the methode node.getQemu().createVm(vmId) it work with default params. But when i try to customise a little bit i go trought params exceptions.

For example when i try to put a the net[n] parameters i got exception :

Map<Integer, String> netN = new HashMap<>();
netN.put(0,URLEncoder.encode("name=eth0,rate=15,bridge=vmbr0,firewall=1,gw=255.255.255.0,ip=192.168.0.1/24,hwaddr=02:00:be:0f:f2:63","utf8"));

the error is "Parameter verification failed."

Any help would be appreciated !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.