Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pve-manager
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
pve-manager
Commits
96338cef
Commit
96338cef
authored
Jan 17, 2012
by
Dietmar Maurer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix APIClient and examples
parent
ce2d54c9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
18 deletions
+34
-18
API2Client.pm
PVE/API2Client.pm
+21
-9
example1.pl
bin/test/example1.pl
+6
-4
example2.pl
bin/test/example2.pl
+5
-3
perftest1.pl
bin/test/perftest1.pl
+2
-2
No files found.
PVE/API2Client.pm
View file @
96338cef
...
...
@@ -34,6 +34,13 @@ sub delete {
return
$self
->
call
('
DELETE
',
$path
,
$param
);
}
sub
update_ticket
{
my
(
$self
,
$ticket
)
=
@_
;
my
$domain
=
"
$self
->{host}.local
"
unless
$self
->
{
host
}
=~
/\./
;
$self
->
{
cookie_jar
}
->
set_cookie
(
0
,
'
PVEAuthCookie
',
$ticket
,
'
/
',
$domain
);
}
sub
call
{
my
(
$self
,
$method
,
$path
,
$param
)
=
@_
;
...
...
@@ -54,7 +61,7 @@ sub call {
$uri
->
scheme
(
$self
->
{
protocol
});
$uri
->
host
(
$self
->
{
host
});
$uri
->
port
(
$self
->
{
port
});
$uri
->
path
('
/api2/json/ticket
');
$uri
->
path
('
/api2/json/
access/
ticket
');
my
$response
=
$ua
->
post
(
$uri
,
{
username
=>
$self
->
{
username
},
...
...
@@ -63,7 +70,11 @@ sub call {
if
(
!
$response
->
is_success
)
{
die
$response
->
status_line
.
"
\n
";
}
# the auth cookie should be set now
my
$data
=
from_json
(
$response
->
decoded_content
,
{
utf8
=>
1
,
allow_nonref
=>
1
});
$self
->
update_ticket
(
$data
->
{
data
}
->
{
ticket
});
$self
->
{
csrftoken
}
=
$data
->
{
data
}
->
{
CSRFPreventionToken
};
}
my
$uri
=
URI
->
new
();
...
...
@@ -75,7 +86,11 @@ sub call {
# print $ua->{cookie_jar}->as_string;
#print "CALL $method : " . $uri->as_string() . "\n";
if
(
$self
->
{
csrftoken
})
{
$self
->
{
useragent
}
->
default_header
('
CSRFPreventionToken
'
=>
$self
->
{
csrftoken
});
}
my
$response
;
if
(
$method
eq
'
GET
')
{
$uri
->
query_form
(
$param
);
...
...
@@ -111,6 +126,7 @@ sub new {
my
$self
=
{
ticket
=>
$param
{
ticket
},
csrftoken
=>
$param
{
csrftoken
},
username
=>
$param
{
username
},
password
=>
$param
{
password
},
host
=>
$param
{
host
}
||
'
localhost
',
...
...
@@ -128,11 +144,7 @@ sub new {
$self
->
{
cookie_jar
}
=
HTTP::
Cookies
->
new
(
ignore_discard
=>
1
);
if
(
$self
->
{
ticket
})
{
my
$domain
=
"
$self
->{host}.local
"
unless
$self
->
{
host
}
=~
/\./
;
$self
->
{
cookie_jar
}
->
set_cookie
(
0
,
'
PVEAuthCookie
',
$self
->
{
ticket
},
'
/
',
$domain
);
}
$self
->
update_ticket
(
$self
->
{
ticket
})
if
$self
->
{
ticket
};
$self
->
{
useragent
}
=
LWP::
UserAgent
->
new
(
cookie_jar
=>
$self
->
{
cookie_jar
},
...
...
@@ -140,7 +152,7 @@ sub new {
timeout
=>
$self
->
{
timeout
},
);
$self
->
{
useragent
}
->
default_header
('
Accept-Encoding
'
=>
'
gzip
');
# allow gzip
$self
->
{
useragent
}
->
default_header
('
Accept-Encoding
'
=>
'
gzip
');
# allow gzip
return
$self
;
}
...
...
bin/test/example1.pl
View file @
96338cef
...
...
@@ -11,13 +11,15 @@ use Data::Dumper;
my
$hostname
=
PVE::INotify::
read_file
("
hostname
");
# normally you use username/password,
# but we can simply create a ticket if we are root
my
$ticket
=
PVE::AccessControl::
assemble_ticket
('
root
');
# but we can simply create a ticket and CRSF token if we are root
my
$ticket
=
PVE::AccessControl::
assemble_ticket
('
root@pam
');
my
$csrftoken
=
PVE::AccessControl::
assemble_csrf_prevention_token
('
root@pam
');
my
$conn
=
PVE::
API2Client
->
new
(
# username => 'root
',
#
password => 'yourpassword',
#username => 'root@pam
',
#
password => 'yourpassword',
ticket
=>
$ticket
,
csrftoken
=>
$csrftoken
,
host
=>
$hostname
,
);
...
...
bin/test/example2.pl
View file @
96338cef
...
...
@@ -11,13 +11,15 @@ use Data::Dumper;
my
$hostname
=
PVE::INotify::
read_file
("
hostname
");
# normally you use username/password,
# but we can simply create a ticket if we are root
my
$ticket
=
PVE::AccessControl::
assemble_ticket
('
root
');
# but we can simply create a ticket and CRSF token if we are root
my
$ticket
=
PVE::AccessControl::
assemble_ticket
('
root@pam
');
my
$csrftoken
=
PVE::AccessControl::
assemble_csrf_prevention_token
('
root@pam
');
my
$conn
=
PVE::
API2Client
->
new
(
#username => 'root',
#username => 'root
@pam
',
#password => 'yourpassword',
ticket
=>
$ticket
,
csrftoken
=>
$csrftoken
,
host
=>
$hostname
,
);
...
...
bin/test/perftest1.pl
View file @
96338cef
...
...
@@ -12,7 +12,7 @@ my $hostname = PVE::INotify::read_file("hostname");
# normally you use username/password,
# but we can simply create a ticket if we are root
my
$ticket
=
PVE::AccessControl::
assemble_ticket
('
root
');
my
$ticket
=
PVE::AccessControl::
assemble_ticket
('
root
@pam
');
my
$wcount
=
4
;
my
$qcount
=
500
;
...
...
@@ -21,7 +21,7 @@ sub test_rpc {
my
(
$host
)
=
@_
;
my
$conn
=
PVE::
API2Client
->
new
(
#username => 'root',
#username => 'root
@pam
',
#password => 'yourpassword',
ticket
=>
$ticket
,
host
=>
$host
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment