Git Product home page Git Product logo

proxmox-ve-api-perl's Introduction

SYNOPSIS

use Net::Proxmox::VE;

%args = (
    host     => 'proxmox.local.domain',
    password => 'barpassword',
    username => 'root', # optional
    port     => 8006,   # optional
    realm    => 'pam',  # optional
);

$host = Net::Proxmox::VE->new(%args);

$host->login() or die ('Couldn\'t log in to proxmox host');

DESCRIPTION

This Class provides the framework for talking to Proxmox VE 2.0 API instances. This just provides a get/delete/put/post abstraction layer as methods on Proxmox VE REST API This also handles the ticket headers required for authentication

This class provides the building blocks for someone wanting to use Perl to talk to Proxmox PVE. It provides a get/put/post/delete abstraction layer as methods on top of Proxmox's REST API, while also handling the Login Ticket headers required for authentication.

Object representations of the Proxmox VE REST API are included in seperate modules.

WARNING

We are still moving things around and trying to come up with something that makes sense. We havent yet implemented all the API functions, so far we only have a basic internal abstraction of the REST interface and a few modules for each function tree within the API.

Any enchancements are greatly appreciated ! (use github, link below)

Please dont be offended if we refactor and rework submissions. Perltidy with default settings is prefered style.

Oh, our tests are all against a running server. Care to help make them better?

METHODS

action

This calls raw actions against your proxmox server. Ideally you don't use this directly.

api_version

Returns the API version of the proxmox server we are talking to

api_version_check

Checks that the api we are talking to is at least version 2.0

Returns true if the api version is at least 2.0 (perl style true or false)

debug

Has a single optional argument of 1 or 0 representing enable or disable debugging.

Undef (ie no argument) leaves the debug status untouched, making this method call simply a query.

Returns the resultant debug status (perl style true or false)

delete

An action helper method that just takes a path as an argument and returns the value of action() with the DELETE method

get

An action helper method that just takes a path as an argument and returns the value of action with the GET method

new

Creates the Net::Proxmox::VE object and returns it.

Examples...

my $obj = Net::Proxmox::VE->new(%args);
my $obj = Net::Proxmox::VE->new(\%args);

Valid arguments are...

host

Proxmox host instance to interact with. Required so no default.

username

User name used for authentication. Defaults to 'root', optional.

password

Pass word user for authentication. Required so no default.

port

TCP port number used to by the Proxmox host instance. Defaults to 8006, optional.

realm

Authentication realm to request against. Defaults to 'pam' (local auth), optional.

ssl_opts

If you're using a self-signed certificate, SSL verification is going to fail, and we need to tell IO::Socket::SSL not to attempt certificate verification.

This option is passed on as ssl_opts options to LWP::UserAgent->new(), ultimately for IO::Socket::SSL.

Using it like this, causes LWP::UserAgent and IO::Socket::SSL not to attempt SSL verification:

use IO::Socket::SSL qw(SSL_VERIFY_NONE);
..
%args = (
    ...
    ssl_opts => {
        SSL_verify_mode => SSL_VERIFY_NONE,
        verify_hostname => 0
    },
    ...
);
my $proxmox = Net::Proxmox::VE->new(%args);

Your connection will work now, but beware: you are now susceptible to a man-in-the-middle attack.

debug

Enabling debugging of this API (not related to proxmox debugging in any way). Defaults to false, optional.

post

An action helper method that takes two parameters: $path, \%post_data $path to post to, hash ref to %post_data

You are returned what action() with the POST method returns

put

An action helper method that takes two parameters: $path, hash ref to \%put_data

You are returned what action() with the PUT method returns

url_prefix

Returns the url prefix used in the rest api calls

PVE VERSIONS SUPPORT

Firstly, there isn't currently any handling of different versions of the API.

Secondly, Proxmox API reference documentation is also, frustratingly, published only alongside the current release. This makes it difficult to support older versions of the API or different versions of the API concurrently.

Fortunately the API is relatively stable.

Based on the above the bug reporting policy is as follows:

A function in this module doesn't work against the current published API? This a bug and hope to fix it. Pull requests welcome.
A function in this module doesn't exist in the current published API? Pull requests welcomes and promptly merged.
A function in this module doesn't work against a previous version of the API? A note will be made in the pod only.
A function in this module doesn't exist against a previous version of the API? Pull requests will be merged on a case per case basis.

As such breaking changes may be made to this module to support the current API when necessary.

DESIGN NOTE

This API would be far nicer if it returned nice objects representing different aspects of the system. Such an arrangement would be far better than how this module is currently layed out. It might also be less repetitive code.

SEE ALSO

Proxmox Website

http://www.proxmox.com

API Reference

More details on the API can be found at http://pve.proxmox.com/wiki/Proxmox_VE_API and https://pve.proxmox.com/pve-docs/api-viewer/

proxmox-ve-api-perl's People

Contributors

djzort avatar dpiquet avatar ebl avatar japp-0xlabs avatar manwar avatar martijnlievaart avatar mrproper avatar p5i avatar pmorch avatar poptix avatar sauriol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

proxmox-ve-api-perl's Issues

login() API provides no way for caller to investigate error code/msg

The canonical way to login is to:

$host->login() or die ('Couldnt log in to proxmox host');

But what exactly failed? The caller has no idea. login() should return an error that could be investigated or provide an error() API call for that purpose so proper diagnosis is possible without having to revert to

vi lib/Net/Proxmox/VE/Access.pm

and printf...

If login() changed and returned false on OK, but returned the HTTP::Response / $reponse on error, one could:

if (my $error = $host->login()) {
    die sprintf "Couldn't login. HTTP code: %d status line: %s",
        $error->code(),
        $error->status_line
}

(In our case,

printf("Code: %d\nStatus Line: %s\n",
    $response->code,
    $response->status_line
);

printed:

Code: 500
Status Line: 500 Can't connect to proxmox01:8006 (certificate verify failed)

but that is a separate issue - more on that later)

Json parsing error with threads

I am blocked by using this interesting module because it is unable is parse the JSON response. The parent script who is calling the API's are using threads.

use threads;
use threads::shared;
use Thread::Queue;

Sample error:
2023/08/01 02:31:37 PID5817[undef] WARN TA::Comm::Service::Proxmox::_connect(): 42 : Couldn't log in to proxmox host. ERROR: JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this) at /usr/local/share/perl/5.26.1/Net/Proxmox/VE/Access.pm line 435.

Suggested fix:

` if ( $response->is_success ) {
# my $content = $response->decoded_content;
+ $self->{ticket} = JSON->new->allow_nonref->decode($response->decoded_content)->{data};
- my $login_ticket_data = decode_json( $response->decoded_content );
- $self->{ticket} = $login_ticket_data->{data};

    # We use request time as the time to get the json ticket is undetermined,
    # id rather have a ticket a few seconds shorter than have a ticket that incorrectly
    # says its valid for a couple more
    $self->{ticket_timestamp} = $request_time;
    print "DEBUG: login successful\n"
      if $self->{params}->{debug};
    return 1;
}

`

Net::Proxmox::VE's sub action should not print warnings to STDOUT

sub action has this:

sub action {
    ...
    if ( $response->is_success ) {
        ...
    }
    else {
        print "WARNING: request failed: " . $request->as_string . "\n";
        print "WARNING: response status: " . $response->status_line . "\n";
    }
    return;
}

No. A library should never print to STDOUT or STDERR.

warn($warning) is perfectly good for that. Or provide a possibility in the API to handle the warning. Or something other than silently and uncatchably print to STDOUT.

Also, isn't a get that fails an error, not a warning?

(Actually - for the next person to stumble onto this - it can be caught like this:

    my $interfaces;
    my $stdout;
    {
        open local(*STDOUT),'>',\$stdout;
        $interfaces = $proxmox->get(
            $getVMURL(123) . '/agent/network-get-interfaces'
        );
    }
    if ($stdout) {
        # Handle anticipated errors
        if ($stdout =~ /response status: 500 QEMU guest agent is not running/ ||
            $stdout =~ /response status: 500 No QEMU guest agent configured/) {
            return;
        } else {
            # die on other errors
            chomp $stdout;
            die sprintf "Got error from %s: %s",
                $self->_getVMURL . '/agent/network-get-interfaces',
                $stdout;
        }
    }

)

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.