Giter Site home page Giter Site logo

corsinvest / cv4pve-api-dotnet Goto Github PK

View Code? Open in Web Editor NEW
51.0 5.0 11.0 1.62 MB

Proxmox VE Client API .Net C#

Home Page: https://www.corsinvest.it/cv4pve

License: GNU General Public License v3.0

C# 99.93% PowerShell 0.07%
proxmox-ve proxmox-apis api api-client api-rest dotnet dotnet-core proxmox enterprise proxmox-cluster

cv4pve-api-dotnet's People

Contributors

franklupo avatar heinleit avatar oren-c avatar simonegli8 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

Watchers

 avatar  avatar  avatar  avatar  avatar

cv4pve-api-dotnet's Issues

Creating new HttpClient for each request

While analyzing the source code, I came across the following line:

var client = new HttpClient(handler);

However, according to Microsoft documentation, it's strongly recommended to reuse the same copy of HttpClient:

HttpClient is intended to be instantiated once per application, rather than per-use

https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-6.0

If I understand correctly, the using statement ensures that the HttpClient instance is disposed, preventing resource leaks. However, creating a new instance of HttpClient every time can lead to performance issues and even resource exhaustion.

Could you please explain why it's done that way, using "using"?

NuGet package is not up-to-date

What happened?

Hello,
I just updated the Nuget package, but unfortunately it seems that the version that is installed with Nuget is not the one that is provided on Github.

Can you please check this and possibly update it?

Thank you

Philipp

Expected behavior

Here is an example:

Github code:

public async Task<Result> Vzdump(bool? all = null, int? bwlimit = null, string compress = null, string dumpdir = null, string exclude = null, string exclude_path = null, int? ionice = null, int? lockwait = null, string mailnotification = null, string mailto = null, int? maxfiles = null, string mode = null, string notes_template = null, string notification_policy = null, string notification_target = null, string performance = null, int? pigz = null, string pool = null, bool? protected_ = null, string prune_backups = null, bool? quiet = null, bool? remove = null, string script = null, bool? stdexcludes = null, bool? stdout = null, bool? stop = null, int? stopwait = null, string storage = null, string tmpdir = null, string vmid = null, int? zstd = null)
{
    var parameters = new Dictionary<string, object>();
    parameters.Add("all", all);
    parameters.Add("bwlimit", bwlimit);
    parameters.Add("compress", compress);
    parameters.Add("dumpdir", dumpdir);
    parameters.Add("exclude", exclude);
    parameters.Add("exclude-path", exclude_path);
    parameters.Add("ionice", ionice);
    parameters.Add("lockwait", lockwait);
    parameters.Add("mailnotification", mailnotification);
    parameters.Add("mailto", mailto);
    parameters.Add("maxfiles", maxfiles);
    parameters.Add("mode", mode);
    parameters.Add("notes-template", notes_template);
    parameters.Add("notification-policy", notification_policy);
    parameters.Add("notification-target", notification_target);
    parameters.Add("performance", performance);
    parameters.Add("pigz", pigz);
    parameters.Add("pool", pool);
    parameters.Add("protected", protected_);
    parameters.Add("prune-backups", prune_backups);
    parameters.Add("quiet", quiet);
    parameters.Add("remove", remove);
    parameters.Add("script", script);
    parameters.Add("stdexcludes", stdexcludes);
    parameters.Add("stdout", stdout);
    parameters.Add("stop", stop);
    parameters.Add("stopwait", stopwait);
    parameters.Add("storage", storage);
    parameters.Add("tmpdir", tmpdir);
    parameters.Add("vmid", vmid);
    parameters.Add("zstd", zstd);
    return await _client.Create($"/nodes/{_node}/vzdump", parameters);
}

Code of Nuget Package:

public async Task<Result> Vzdump(bool? all = null, int? bwlimit = null, string compress = null, string dumpdir = null, string exclude = null, string exclude_path = null, int? ionice = null, int? lockwait = null, string mailnotification = null, string mailto = null, int? maxfiles = null, string mode = null, string notes_template = null, string performance = null, int? pigz = null, string pool = null, bool? protected_ = null, string prune_backups = null, bool? quiet = null, bool? remove = null, string script = null, bool? stdexcludes = null, bool? stdout = null, bool? stop = null, int? stopwait = null, string storage = null, string tmpdir = null, string vmid = null, int? zstd = null)
{
    var parameters = new Dictionary<string, object>();
    parameters.Add("all", all);
    parameters.Add("bwlimit", bwlimit);
    parameters.Add("compress", compress);
    parameters.Add("dumpdir", dumpdir);
    parameters.Add("exclude", exclude);
    parameters.Add("exclude-path", exclude_path);
    parameters.Add("ionice", ionice);
    parameters.Add("lockwait", lockwait);
    parameters.Add("mailnotification", mailnotification);
    parameters.Add("mailto", mailto);
    parameters.Add("maxfiles", maxfiles);
    parameters.Add("mode", mode);
    parameters.Add("notes-template", notes_template);
    parameters.Add("performance", performance);
    parameters.Add("pigz", pigz);
    parameters.Add("pool", pool);
    parameters.Add("protected", protected_);
    parameters.Add("prune-backups", prune_backups);
    parameters.Add("quiet", quiet);
    parameters.Add("remove", remove);
    parameters.Add("script", script);
    parameters.Add("stdexcludes", stdexcludes);
    parameters.Add("stdout", stdout);
    parameters.Add("stop", stop);
    parameters.Add("stopwait", stopwait);
    parameters.Add("storage", storage);
    parameters.Add("tmpdir", tmpdir);
    parameters.Add("vmid", vmid);
    parameters.Add("zstd", zstd);
    return await _client.Create($"/nodes/{_node}/vzdump", parameters);
}

Relevant log output

No response

Net Version

.Net 8.0

Proxmox VE Version

8.1.0

Version (bug)

8.1.0

Version (working)

No response

On what operating system are you experiencing the issue?

Windows

Pull Request

  • I would like to do a Pull Request

client.UseApiToken is Error

Corsinvest.ProxmoxVE.Api (2.4.0.0)

client.UseApiToken("");

Error CS1061 'PveClient' does not contain a definition for 'UseApiToken' and no accessible extension method 'UseApiToken' accepting a first argument of type 'PveClient' could be found (are you missing a using directive or an assembly reference?)

Upload ISO Task fails

What happened?

We got very far with our wrapping of this awesome package, but failed at the uploading of ISOs.

Our Code:

internal bool UploadISO(string hostname, string isoFile, string sha256, string? overrideFileName)
{
    var task = _client.Nodes[hostname].Storage["local"].Upload.Upload("iso", overrideFileName ?? Path.GetFileName(isoFile), checksum: sha256, checksum_algorithm: "sha256")
    task.Wait();
    var result = task.Result;
    return result.IsSuccessStatusCode;
}

Where isoFile is for example "C:/someISO.iso", overrideFileName would be "windowsXYZ.iso" or null and the sha matching.

We fail on the task.Wait() with the exception of:
One or more errors occurred. (An error occurred while sending the request.)

Expected behavior

We would expect our file to be uploaded just as with the ui, but it's also unclear to us, how the file content is being uploaded. Maybe hence we get an error?

Relevant log output

at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at osisa.SystemManagement.Proxmox.Proxmox.UploadISO(MachineName hostname, String isoFile, String sha256, String overrideFileName) in C:\git\main\netbase\src\osisa.SystemManagement.Proxmox\Proxmox.cs:line 291
   at osisa.SystemManagement.Proxmox.Host.UploadISO(String isoFile, String sha256, String overrideFileName) in C:\git\main\netbase\src\osisa.SystemManagement.Proxmox\Host.cs:line 195
   at osisa.SystemManagement.Proxmox.Tests.SpikeTests.UploadISO(String isoFile, String sha256) in C:\git\main\netbase\src\osisa.SystemManagement.Proxmox.Tests\SpikeTests.cs:line 188
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

   at System.Net.Http.HttpConnection.<SendAsyncCore>d__61.MoveNext()
   at System.Net.Http.HttpConnectionPool.<SendWithVersionDetectionAndRetryAsync>d__86.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.RedirectHandler.<SendAsync>d__4.MoveNext()
   at System.Net.Http.HttpClient.<<SendAsync>g__Core|83_0>d.MoveNext()
   at Corsinvest.ProxmoxVE.Api.PveClientBase.<ExecuteAction>d__36.MoveNext()
   at Corsinvest.ProxmoxVE.Api.PveClientBase.<Create>d__32.MoveNext()
   at Corsinvest.ProxmoxVE.Api.PveClient.PveNodes.PveNodeItem.PveStorage.PveStorageItem.PveUpload.<Upload>d__4.MoveNext()

The response ended prematurely.

   at System.Net.Http.HttpConnection.<SendAsyncCore>d__61.MoveNext()

Net Version

netstandard2.0

Proxmox VE Version

7.3-1

Version (bug)

8.0.0

Version (working)

No response

On what operating system are you experiencing the issue?

Windows

Pull Request

  • I would like to do a Pull Request

cpulimit inside of /nodes/{node}/lxc/{vmid} has the wrong type

.Nodes[node].Lxc.CreateVm() takes in a parameter cpulimit of type int?

In the API, this value is specified as a "number" and not "integer", meaning it can be floating point, and is allowed to be non-integer trough the Proxmox API and GUI.
This doesn't seem possible with this library as it requires integers, making fractional limits that aren't a taking a full core impossible (1.5, 0.5, etc.)

How do you use Get, Set, Create etc?

Hello,

thank you for this package, however I lack of understanding how it works.
For example, I'm trying to get all users and log the response in a richTextBox:

var client = new PveClient(File.ReadLines(ipList).First());
if(client.Login("root", File.ReadLines(passwordList).First()))
{
    Log("Connected to " + client.Hostname, 1);
    var users = client.Get("/access/users");
    Log(users.Response, 2);
}

however it does not work as supposed to. What am I doing wrong?

VMInfo SetStatus resume functionality

Is there a reason why in Corsinvest.ProxmoxVE.Api.Extension.VM.VMInfo.SetStatus resume is not an available case considering Suspend is implemented?

Just curious if there's a reason for that or if just hasn't been implemented yet, either way I figure I could always handle that in my own code.

#

Hi guys,

Im searching for a way to create a virtual machine using the proxmox api,

This is working well but i cant find anywhere how i can create a vm.

            string ip = Settings.Default["ip"].ToString();
            string token = Settings.Default["token"].ToString();


            var client = new PveClient(ip);
            client.ApiToken = token;

Maybe someone can help me with a example orsomething

Thnx,

WaitForTaskToFinish is inneficient and causes 100% CPU usage

What happened?

Trying to use PveClientBase.WaitForTaskToFinish() causes 100% CPU usage on one core for prolonged periods of time until the task is finished.

This is due to how its coded, a while loop that constantly checks a condition.

Expected behavior

PveClientBase.WaitForTaskToFinish(), since it already blocks a thread waiting for something, it shouldn't constantly try to check for time when its supplied a "wait" parameter - Instead, a Thread.Sleep() would be much better - Both block the thread, but Thread.Sleep() doesn't also utilize 100% of a CPU core.

Suggested Fix
Change these lines (

while (isRunning && (DateTime.Now - timeStart).Milliseconds < timeOut)
{
if ((DateTime.Now - waitTime).TotalMilliseconds >= wait)
{
waitTime = DateTime.Now;
isRunning = TaskIsRunning(node, task);
}
}
) to this:

while (isRunning && (DateTime.Now - timeStart).Milliseconds < timeOut)
{
   // Maybe even make the "wait" a int, instead of long by default
   Thread.Sleep((int)wait);
   isRunning = TaskIsRunning(node, task);
}

Relevant log output

No response

Net Version

.Net 5.0

Proxmox VE Version

7.0-13

Version (bug)

7.0.1

Version (working)

No response

On what operating system are you experiencing the issue?

Linux

Pull Request

  • I would like to do a Pull Request

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.