Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full Txn support and tests #15

Merged
merged 2 commits into from
Jun 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Revision history for Net::Etcd
0.011
[ ENHANCEMENTS ]
* Add full support for Txn with tests. Recommended for use with etcd 3.2.0+

0.010
[ ENHANCEMENTS ]
* Add intial support for snapshot
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ lib/Net/Etcd/Lease.pm
lib/Net/Etcd/Maintenance.pm
lib/Net/Etcd/Role/Actions.pm
lib/Net/Etcd/KV.pm
lib/Net/Etcd/KV/Compare.pm
lib/Net/Etcd/KV/MultiOp.pm
lib/Net/Etcd/KV/Put.pm
lib/Net/Etcd/KV/Range.pm
lib/Net/Etcd/KV/Txn.pm
Expand Down
27 changes: 27 additions & 0 deletions examples/watch.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Net::Etcd;
use Data::Dumper;
my ($host, $port);

if ( $ENV{ETCD_TEST_HOST} and $ENV{ETCD_TEST_PORT}) {
$host = $ENV{ETCD_TEST_HOST};
$port = $ENV{ETCD_TEST_PORT};
}

my ($watch,$key);
my $etcd = Net::Etcd->new( { host => $host, port => $port } );

# create watch with callback and store events
$watch = $etcd->watch( { key => 'foo'}, sub {
my ($result) = @_;
print STDERR Dumper($result);
})->create;

$etcd->put({ key => 'foo', value => 'bar' });

$etcd->range({ key => 'foo' });

1;
12 changes: 12 additions & 0 deletions lib/Net/Etcd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ Returns a L<Net::Etcd::KV::Txn> object.

=cut

=head2 op

Returns a L<Net::Etcd::KV::Op> object.

=cut

=head2 compare

Returns a L<Net::Etcd::KV::Compare> object.

=cut

=head2 configuration

Initialize configuration checks to see it etcd is installed locally.
Expand Down
46 changes: 40 additions & 6 deletions lib/Net/Etcd/KV.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use Moo::Role;
use Types::Standard qw(Str Int Bool HashRef ArrayRef);
use Net::Etcd::KV::Put;
use Net::Etcd::KV::Range;
use Net::Etcd::KV::Txn;
use Net::Etcd::KV::Op;
use Net::Etcd::KV::Compare;

with 'Net::Etcd::Role::Actions';
use namespace::clean;
Expand All @@ -35,8 +38,12 @@ Key Value role providing easy access to Put and Range classes

Range gets the keys in the range from the key-value store.

# get range
$etcd->range({key =>'test0', range_end => 'test100'})

# delete range
$etcd->range({key =>'test0', range_end => 'test100'})->delete

=cut

sub range {
Expand All @@ -48,8 +55,8 @@ sub range {
cb => $cb,
( $options ? %$options : () ),
);
$range->request;
return $range;
$range->request unless $range->hold;
return $range
}

=head2 put
Expand All @@ -58,7 +65,7 @@ Put puts the given key into the key-value store. A put request increments
the revision of the key-value store and generates one event in the event
history.

$etcd->range({key =>'test0', range_end => 'test100'})
$etcd->put({key =>'test0', value=> 'bar'})

=cut

Expand All @@ -71,7 +78,7 @@ sub put {
cb => $cb,
( $options ? %$options : () ),
);
$put->request;
$put->request unless $put->hold;
return $put;
}

Expand All @@ -94,8 +101,35 @@ sub txn {
cb => $cb,
( $options ? %$options : () ),
);
$txn->request;
return $txn;
return $txn->create;
}

=head2 op

=cut

sub op {
my ( $self, $options ) = @_;
my $cb = pop if ref $_[-1] eq 'CODE';
my $op = Net::Etcd::KV::Op->new(
%$self,
( $options ? %$options : () ),
);
return $op->create;
}

=head2 compare

=cut

sub compare {
my ( $self, $options ) = @_;
my $cb = pop if ref $_[-1] eq 'CODE';
my $cmp = Net::Etcd::KV::Compare->new(
%$self,
( $options ? %$options : () ),
);
return $cmp->json_args;
}

1;
105 changes: 105 additions & 0 deletions lib/Net/Etcd/KV/Compare.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use utf8;
package Net::Etcd::KV::Compare;

use strict;
use warnings;

use Moo;
use Types::Standard qw(Str Int Bool HashRef ArrayRef);
use MIME::Base64;
use Data::Dumper;
use JSON;

with 'Net::Etcd::Role::Actions';

use namespace::clean;

=head1 NAME

Net::Etcd::KV::Compare

=cut

our $VERSION = '0.010';

=head1 DESCRIPTION

Op


=head1 ACCESSORS

=head2 result

result is logical comparison operation for this comparison.

=cut

has result => (
is => 'ro',
);

=head2 target

target is the key-value field to inspect for the comparison.

=cut

has target => (
is => 'ro',
);

=head2 key

key is the subject key for the comparison operation.

=cut

has key => (
is => 'ro',
coerce => sub { return encode_base64( $_[0], '' ) if $_[0] },
);


=head2 version

version is the version of the given key

=cut

has version => (
is => 'ro',
);

=head2 create_revision

create_revision is the creation revision of the given key

=cut

has create_revision => (
is => 'ro',
);

=head2 mod_revision

mod_revision is the last modified revision of the given key.

=cut

has mod_revision => (
is => 'ro',
);

=head2 value

value is the value of the given key, in bytes.

=cut

has value => (
is => 'ro',
coerce => sub { return encode_base64( $_[0], '' ) if $_[0] },
);

1;
75 changes: 75 additions & 0 deletions lib/Net/Etcd/KV/Op.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use utf8;
package Net::Etcd::KV::Op;

use strict;
use warnings;

use Moo;
use Types::Standard qw(InstanceOf Str Int Bool HashRef ArrayRef);
use MIME::Base64;
use Data::Dumper;
use JSON;

with 'Net::Etcd::Role::Actions';

use namespace::clean;

=head1 NAME

Net::Etcd::KV::Op

=cut

our $VERSION = '0.010';

=head1 DESCRIPTION

Op

=head1 ACCESSORS

=head2 request_range

=cut

has request_range => (
is => 'ro',
);

=head2 request_put

=cut

has request_put => (
is => 'ro',
);

=head2 request_delete_range

=cut

has request_delete_range => (
is => 'ro',
);

=head2 create

create op

=cut

#TODO this dirty hack should be a perl data object and then make json.

sub create {
my $self = shift;
my @op;
my $put = $self->request_put;
my $range = $self->request_range;
my $delete_range = $self->request_delete_range;
push @op, '{"requestPut":' . $put->json_args . '}' if defined $put;
push @op, '{"requestRange":' . $range->json_args . '}' if defined $range;
push @op, '{"requestDeleteRange":' . $delete_range->json_args . '}' if defined $delete_range;
return @op;
}

1;
28 changes: 27 additions & 1 deletion lib/Net/Etcd/KV/Txn.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use strict;
use warnings;

use Moo;
use Types::Standard qw(Str Int Bool HashRef ArrayRef);
use Types::Standard qw(InstanceOf Str Int Bool HashRef ArrayRef);
use MIME::Base64;
use Data::Dumper;
use JSON;
Expand Down Expand Up @@ -97,4 +97,30 @@ has failure => (
isa => ArrayRef,
);

=head1 PUBLIC METHODS

=head2 create

create txn

=cut

#TODO hack alert

sub create {
my $self = shift;
my $compare = $self->compare;
my $success = $self->success;
my $failure = $self->failure;

my $txn ='"compare":[' . join(',',@$compare) . '],';
$txn .= '"success":[' . join(',', @$success) . ']' if defined $success;
$txn .= ',' if defined $success and defined $failure;
$txn .= '"failure":[ ' . join(',', @$failure) . ']' if defined $failure;
$self->{json_args} = '{' . $txn . '}';
# print STDERR Dumper($self);
$self->request;
return $self;
}

1;
Loading