-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProxmoxAPI.pm
56 lines (46 loc) · 1.25 KB
/
ProxmoxAPI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl
package ProxmoxAPI;
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use HTTP::Request;
use URI::Escape;
use Config;
sub new {
my $class = shift;
my $self = {
token => undef,
ua => LWP::UserAgent->new(
ssl_opts => { verify_hostname => 0, SSL_verify_mode => 0 }
)
};
bless $self, $class;
return $self;
}
sub login {
my $self = shift;
my $url = "https://$Config::PROXMOX_HOST:$Config::PROXMOX_PORT/api2/json/access/ticket";
my $response = $self->{ua}->post($url, [
username => $Config::PROXMOX_USER,
password => $Config::PROXMOX_PASSWORD
]);
if ($response->is_success) {
my $data = decode_json($response->content);
$self->{token} = $data->{data}{ticket};
return 1;
}
return 0;
}
sub get_resources {
my $self = shift;
my $url = "https://$Config::PROXMOX_HOST:$Config::PROXMOX_PORT/api2/json/cluster/resources";
my $request = HTTP::Request->new(GET => $url);
$request->header('Cookie' => "PVEAuthCookie=" . $self->{token});
my $response = $self->{ua}->request($request);
if ($response->is_success) {
return decode_json($response->content)->{data};
}
return [];
}
1;