line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Cloudstack; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
313151
|
use Mojo::Base 'Mojo::UserAgent'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
4
|
1
|
|
|
1
|
|
184
|
use Mojo::Parameters; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
5
|
1
|
|
|
1
|
|
25
|
use Mojo::URL; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
6
|
1
|
|
|
1
|
|
23
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
7
|
1
|
|
|
1
|
|
39
|
use Mojo::JSON 'j'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
63
|
|
8
|
1
|
|
|
1
|
|
6
|
use Mojo::Collection 'c'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
59
|
|
9
|
1
|
|
|
1
|
|
419
|
use Mojo::Cloudstack::Base; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
9
|
|
10
|
1
|
|
|
1
|
|
644
|
use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex); |
|
1
|
|
|
|
|
1369
|
|
|
1
|
|
|
|
|
58
|
|
11
|
1
|
|
|
1
|
|
7
|
use MIME::Base64; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
57
|
|
12
|
1
|
|
|
1
|
|
519
|
use URI::Encode 'uri_encode'; |
|
1
|
|
|
|
|
1440
|
|
|
1
|
|
|
|
|
69
|
|
13
|
1
|
|
|
1
|
|
6
|
use Data::Dumper 'Dumper'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
930
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has 'host' => "localhost"; |
16
|
|
|
|
|
|
|
has 'path' => "/client/api"; |
17
|
|
|
|
|
|
|
has 'port' => "8080"; |
18
|
|
|
|
|
|
|
has 'scheme' => "https"; |
19
|
|
|
|
|
|
|
has 'api_key' => ""; |
20
|
|
|
|
|
|
|
has 'secret_key' => ""; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
23
|
|
|
|
|
|
|
our $AUTOLOAD; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _build_request { |
26
|
1
|
|
|
1
|
|
206144
|
my ($self, $params) = @_; |
27
|
1
|
|
|
|
|
4
|
my $baseurl = sprintf ("%s://%s:%s%s?", $self->scheme, $self->host, $self->port, $self->path); |
28
|
1
|
|
|
|
|
13
|
$params->{ apiKey } = $self->api_key; |
29
|
1
|
|
|
|
|
5
|
$params->{ response } = 'json'; |
30
|
1
|
|
|
|
|
2
|
my $secret_key = $self->secret_key; |
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
6
|
my $req_params = Mojo::Parameters->new(); |
33
|
1
|
|
|
|
|
9
|
foreach my $p (sort keys %$params) { |
34
|
3
|
|
|
|
|
1397
|
$req_params->param($p => uri_encode($params->{ $p })); |
35
|
|
|
|
|
|
|
} |
36
|
1
|
|
|
|
|
635
|
my $params_str = lc ($req_params->to_string); |
37
|
1
|
|
|
|
|
108
|
my $digest = hmac_sha1($params_str, $secret_key); |
38
|
1
|
|
|
|
|
35
|
my $base64_encoded = encode_base64($digest); |
39
|
1
|
|
|
|
|
2
|
chomp ($base64_encoded); |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
2
|
my $uri_encoded = uri_encode($base64_encoded,1); |
42
|
1
|
|
|
|
|
638
|
my $url = Mojo::URL->new($baseurl); |
43
|
1
|
|
|
|
|
383
|
$url->query($req_params->to_string); |
44
|
|
|
|
|
|
|
|
45
|
1
|
|
|
|
|
82
|
return $url->to_string . '&signature='.$uri_encoded; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub AUTOLOAD { |
50
|
0
|
|
|
0
|
|
|
my $self = shift; |
51
|
0
|
|
|
|
|
|
(my $command = $AUTOLOAD) =~ s/.*:://; |
52
|
0
|
|
|
|
|
|
my %params = @_; |
53
|
0
|
|
|
|
|
|
$params{command} = $command; |
54
|
0
|
|
|
|
|
|
my $req = $self->_build_request(\%params); |
55
|
0
|
|
|
|
|
|
my $res = $self->get($req)->res; |
56
|
0
|
|
|
|
|
|
my $items = $res->json; |
57
|
|
|
|
|
|
|
#warn Dumper 'ITEMS', $items; |
58
|
0
|
0
|
|
|
|
|
die sprintf("Could not get response for %s %s %s", $req, $res->code, $res->message) unless $items; |
59
|
0
|
|
|
|
|
|
my $responsetype = (keys %$items)[0]; |
60
|
0
|
0
|
|
|
|
|
if($responsetype =~ /^(list|expunge|error|create|update|delete|stop|start|restart|deploy|assign|attach|detach|query)(.*)(response)$/){ |
61
|
0
|
|
|
|
|
|
my ($otype, $oname, $oresponse) = ($1, $2, $3); |
62
|
|
|
|
|
|
|
#warn Dumper $otype, $oname, $oresponse; |
63
|
0
|
0
|
|
|
|
|
if($oname =~ /(s)$/){ |
64
|
0
|
|
|
|
|
|
$oname =~ s/$1$//; |
65
|
|
|
|
|
|
|
} |
66
|
0
|
0
|
|
|
|
|
if($otype eq 'list'){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
return c( |
68
|
0
|
|
|
|
|
|
map { Mojo::Cloudstack::Base->new('Mojo::Cloudstack::' . ucfirst($oname),$_) } @{$items->{$responsetype}{$oname}} |
|
0
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
} elsif($otype eq 'query'){ |
71
|
|
|
|
|
|
|
#warn Dumper 'QUERY', $items->{$responsetype}; |
72
|
0
|
|
|
|
|
|
return Mojo::Cloudstack::Base->new('Mojo::Cloudstack::AsyncJobResult', $items->{$responsetype}); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
} elsif(exists $items->{$responsetype}{errorcode}){ |
75
|
0
|
|
|
|
|
|
return Mojo::Cloudstack::Base->new('Mojo::Cloudstack::Error', $items->{$responsetype}); |
76
|
|
|
|
|
|
|
} else { |
77
|
0
|
0
|
|
|
|
|
return Mojo::Cloudstack::Base->new('Mojo::Cloudstack::' . (exists $items->{$responsetype}{jobid} |
78
|
|
|
|
|
|
|
? 'AsyncJobRequest' |
79
|
|
|
|
|
|
|
: ucfirst($oname)), $items->{$responsetype}); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} else { |
82
|
0
|
|
|
|
|
|
die "unknown response type $responsetype for reqest \n$req"; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
__END__ |