Extended and refactored vici perl implementation
This commit is contained in:
parent
a17b6d469c
commit
7d24aa0624
|
@ -5,7 +5,7 @@ use AutoLoader qw(AUTOLOAD);
|
|||
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw(
|
||||
new, from_data, hash, encode, raw
|
||||
new, from_data, hash, encode, raw, result
|
||||
);
|
||||
our $VERSION = '0.9';
|
||||
|
||||
|
@ -62,6 +62,12 @@ sub raw {
|
|||
return '{' . raw_hash($self->{'Hash'}) . '}';
|
||||
}
|
||||
|
||||
sub result {
|
||||
my $self = shift;
|
||||
my $result = $self->{'Hash'};
|
||||
return ($result->{'success'} eq 'yes', $result->{'errmsg'});
|
||||
}
|
||||
|
||||
# private functions
|
||||
|
||||
sub parse {
|
||||
|
|
|
@ -12,6 +12,7 @@ our $VERSION = '0.9';
|
|||
use strict;
|
||||
use warnings;
|
||||
use Switch;
|
||||
use Vici::Message;
|
||||
use Vici::Transport;
|
||||
|
||||
use constant {
|
||||
|
@ -36,18 +37,19 @@ sub new {
|
|||
}
|
||||
|
||||
sub request {
|
||||
my ($self, $command, $data) = @_;
|
||||
my $request = pack('CC/a*a*', CMD_REQUEST, $command, $data);
|
||||
my ($self, $command, $vars) = @_;
|
||||
my $out = defined $vars ? $vars->encode() : '';
|
||||
my $request = pack('CC/a*a*', CMD_REQUEST, $command, $out);
|
||||
$self->{'Transport'}->send($request);
|
||||
|
||||
my $response = $self->{'Transport'}->receive();
|
||||
my ($type, $msg) = unpack('Ca*', $response);
|
||||
my ($type, $data) = unpack('Ca*', $response);
|
||||
|
||||
switch ($type)
|
||||
{
|
||||
case CMD_RESPONSE
|
||||
{
|
||||
return $msg
|
||||
return Vici::Message->from_data($data);
|
||||
}
|
||||
case CMD_UNKNOWN
|
||||
{
|
||||
|
@ -111,13 +113,15 @@ sub unregister {
|
|||
}
|
||||
|
||||
sub streamed_request {
|
||||
my ($self, $command, $event, $data) = @_;
|
||||
$self->register($event);
|
||||
my ($self, $command, $event, $vars) = @_;
|
||||
my $out = defined $vars ? $vars->encode() : '';
|
||||
|
||||
my $request = pack('CC/a*a*', CMD_REQUEST, $command, $data);
|
||||
$self->register($event);
|
||||
|
||||
my $request = pack('CC/a*a*', CMD_REQUEST, $command, $out);
|
||||
$self->{'Transport'}->send($request);
|
||||
my $more = 1;
|
||||
my $msg = "";
|
||||
my @list = ();
|
||||
|
||||
while ($more)
|
||||
{
|
||||
|
@ -129,9 +133,11 @@ sub streamed_request {
|
|||
case EVENT
|
||||
{
|
||||
(my $event_name, $data) = unpack('C/a*a*', $data);
|
||||
if ($event_name == $event)
|
||||
|
||||
if ($event_name eq $event)
|
||||
{
|
||||
$msg .= $data;
|
||||
my $msg = Vici::Message->from_data($data);
|
||||
push(@list, $msg);
|
||||
}
|
||||
}
|
||||
case CMD_RESPONSE
|
||||
|
@ -146,7 +152,7 @@ sub streamed_request {
|
|||
}
|
||||
}
|
||||
}
|
||||
return $msg;
|
||||
return \@list;
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -5,9 +5,11 @@ use AutoLoader qw(AUTOLOAD);
|
|||
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw(
|
||||
new, version, stats, reload_settings, initiate, list_sas, list_policies,
|
||||
list_conns, get_conns, list_certs, list_authorities, get_authorities,
|
||||
get_pools
|
||||
new, version, stats, reload_settings, initiate, terminate, install,
|
||||
uninstall, list_sas, list_policies, list_conns, get_conns, list_certs,
|
||||
list_authorities, get_authorities, load_conn, unload_conn, load_cert,
|
||||
load_key, load_shared, clear_creds, load_authority, unload_authority,
|
||||
load_pool, unload_pool, get_pools, get_algorithms
|
||||
);
|
||||
our $VERSION = '0.9';
|
||||
|
||||
|
@ -27,104 +29,131 @@ sub new {
|
|||
}
|
||||
|
||||
sub version {
|
||||
my $self = shift;
|
||||
my $data = $self->{'Packet'}->request('version');
|
||||
return Vici::Message->from_data($data);
|
||||
return request('version', @_);
|
||||
}
|
||||
|
||||
sub stats {
|
||||
my $self = shift;
|
||||
my $data = $self->{'Packet'}->request('stats');
|
||||
return Vici::Message->from_data($data);
|
||||
return request('stats', @_);
|
||||
}
|
||||
|
||||
sub reload_settings {
|
||||
my $self = shift;
|
||||
my $data = $self->{'Packet'}->request('reload-settings');
|
||||
my $msg = Vici::Message->from_data($data);
|
||||
my $res = $msg->hash();
|
||||
return $res->{'success'} == 'yes';
|
||||
return request_res('reload-settings', @_);
|
||||
}
|
||||
|
||||
sub initiate {
|
||||
my ($self, $msg) = @_;
|
||||
my $vars = '';
|
||||
if (defined $msg)
|
||||
{
|
||||
$vars = $msg->encode();
|
||||
}
|
||||
my $data = $self->{'Packet'}->request('initiate', $vars);
|
||||
my $msg = Vici::Message->from_data($data);
|
||||
my $res = $msg->hash();
|
||||
return $res->{'success'} == 'yes';
|
||||
return request_vars_res('initiate', @_);
|
||||
}
|
||||
|
||||
sub terminate {
|
||||
return request_vars_res('terminate', @_);
|
||||
}
|
||||
|
||||
sub install {
|
||||
return request_vars_res('install', @_);
|
||||
}
|
||||
|
||||
sub uninstall {
|
||||
return request_vars_res('uninstall', @_);
|
||||
}
|
||||
|
||||
sub list_sas {
|
||||
my ($self, $msg) = @_;
|
||||
my $vars = '';
|
||||
if (defined $msg)
|
||||
{
|
||||
$vars = $msg->encode();
|
||||
}
|
||||
my $data = $self->{'Packet'}->streamed_request('list-sas',
|
||||
'list-sa', $vars);
|
||||
return Vici::Message->from_data($data);
|
||||
return request_list('list-sas', 'list-sa', @_);
|
||||
}
|
||||
|
||||
sub list_policies {
|
||||
my $self = shift;
|
||||
my $data = $self->{'Packet'}->streamed_request('list-policies',
|
||||
'list-policy');
|
||||
return Vici::Message->from_data($data);
|
||||
return request_list('list-policies', 'list-policy', @_);
|
||||
}
|
||||
|
||||
sub list_conns {
|
||||
my ($self, $msg) = @_;
|
||||
my $vars = '';
|
||||
if (defined $msg)
|
||||
{
|
||||
$vars = $msg->encode();
|
||||
}
|
||||
my $data = $self->{'Packet'}->streamed_request('list-conns',
|
||||
'list-conn', $vars);
|
||||
return Vici::Message->from_data($data);
|
||||
return request_list('list-conns', 'list-conn', @_);
|
||||
}
|
||||
|
||||
sub get_conns {
|
||||
my $self = shift;
|
||||
my $data = $self->{'Packet'}->request('get-conns');
|
||||
return Vici::Message->from_data($data);
|
||||
return request('get-conns', @_);
|
||||
}
|
||||
|
||||
sub list_certs {
|
||||
my ($self, $msg) = @_;
|
||||
my $vars = '';
|
||||
if (defined $msg)
|
||||
{
|
||||
$vars = $msg->encode();
|
||||
}
|
||||
my $data = $self->{'Packet'}->streamed_request('list-authorities',
|
||||
'list-authority', $vars);
|
||||
return Vici::Message->from_data($data);
|
||||
return request_list('list-certs', 'list-cert', @_);
|
||||
}
|
||||
|
||||
sub list_authorities {
|
||||
my $self = shift;
|
||||
my $data = $self->{'Packet'}->streamed_request('list-authorities',
|
||||
'list-authority');
|
||||
return Vici::Message->from_data($data);
|
||||
return request_list('list-authorities', 'list-authority', @_);
|
||||
}
|
||||
|
||||
sub get_authorities {
|
||||
my $self = shift;
|
||||
my $data = $self->{'Packet'}->request('get-authorities');
|
||||
return Vici::Message->from_data($data);
|
||||
return request('get-authorities', @_);
|
||||
}
|
||||
|
||||
sub load_conn {
|
||||
return request_vars_res('load-conn', @_);
|
||||
}
|
||||
|
||||
sub unload_conn {
|
||||
return request_vars_res('unload-conn', @_);
|
||||
}
|
||||
|
||||
sub load_cert {
|
||||
return request_vars_res('load-cert', @_);
|
||||
}
|
||||
|
||||
sub load_key {
|
||||
return request_vars_res('load-key', @_);
|
||||
}
|
||||
|
||||
sub load_shared {
|
||||
return request_vars_res('load-shared', @_);
|
||||
}
|
||||
|
||||
sub clear_creds {
|
||||
return request_res('clear-creds', @_);
|
||||
}
|
||||
|
||||
sub load_authority {
|
||||
return request_vars_res('load-authority', @_);
|
||||
}
|
||||
|
||||
sub unload_authority {
|
||||
return request_vars_res('unload-authority', @_);
|
||||
}
|
||||
|
||||
sub load_pool {
|
||||
return request_vars_res('load-pool', @_);
|
||||
}
|
||||
|
||||
sub unload_pool {
|
||||
return request_vars_res('unload-pool', @_);
|
||||
}
|
||||
|
||||
sub get_pools {
|
||||
my $self = shift;
|
||||
my $data = $self->{'Packet'}->request('get-pools');
|
||||
return Vici::Message->from_data($data);
|
||||
return request('get-pools', @_);
|
||||
}
|
||||
|
||||
sub get_algorithms {
|
||||
return request('get-algorithms', @_);
|
||||
}
|
||||
|
||||
# Private functions
|
||||
|
||||
sub request {
|
||||
my ($command, $self) = @_;
|
||||
return $self->{'Packet'}->request($command);
|
||||
}
|
||||
|
||||
sub request_res {
|
||||
my ($command, $self) = @_;
|
||||
my $msg = $self->{'Packet'}->request($command);
|
||||
return $msg->result();
|
||||
}
|
||||
|
||||
sub request_vars_res {
|
||||
my ($command, $self, $vars) = @_;
|
||||
my $msg = $self->{'Packet'}->request($command, $vars);
|
||||
return $msg->result();
|
||||
}
|
||||
|
||||
sub request_list {
|
||||
my ($command, $event, $self, $vars) = @_;
|
||||
return $self->{'Packet'}->streamed_request($command, $event, $vars);
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
Loading…
Reference in New Issue