Skip to content

Commit

Permalink
deleterange exposed as independent call (#25)
Browse files Browse the repository at this point in the history
* - Added support for deleterange to delete a given key - bumped up version to 0.014

Signed-off-by: Ananth Kavuri <kavuria@pocofy.com>

* Fixed tests to work with deleterange
  • Loading branch information
foobargeez authored and hexfusion committed Aug 30, 2017
1 parent 609a67f commit f4fd0cb
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 38 deletions.
13 changes: 12 additions & 1 deletion lib/Net/Etcd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Net::Etcd - etcd v3 REST API.
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 SYNOPSIS
Expand All @@ -54,6 +54,9 @@ our $VERSION = '0.013';
# return array { key => value } pairs from range request.
my @users = $range->all
# delete single key
$etcd->deleterange({ key => 'test0' });
# watch key range, streaming.
$watch = $etcd->watch( { key => 'foo', range_end => 'fop'}, sub {
my ($result) = @_;
Expand Down Expand Up @@ -336,6 +339,14 @@ See L<Net::Etcd::KV::Put>
=cut

=head2 deleterange
See L<Net::Etcd::KV::DeleteRange>
$etcd->deleterange({ key=>'test0' });
=cut

=head2 range
See L<Net::Etcd::KV::Range>
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Auth.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Net::Etcd::Auth
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Auth/Role.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::Auth::Role
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Auth/RolePermission.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Net::Etcd::Auth::RolePermission
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Net::Etcd::Config
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 ACCESSORS
Expand Down
29 changes: 25 additions & 4 deletions lib/Net/Etcd/KV.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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::DeleteRange;
use Net::Etcd::KV::Txn;
use Net::Etcd::KV::Op;
use Net::Etcd::KV::Compare;
Expand All @@ -24,7 +25,7 @@ Net::Etcd::KV
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand All @@ -41,9 +42,6 @@ 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 @@ -59,6 +57,29 @@ sub range {
return $range
}

=head2 deleterange
DeleteRange deletes the given range from the key-value store. A delete
request increments the revision of the key-value store and generates a
delete event in the event history for every deleted key.
$etcd->deleterange({key => 'test0'})
=cut

sub deleterange {
my ( $self, $options ) = @_;
my $cb = pop if ref $_[-1] eq 'CODE';
my $delete_range = Net::Etcd::KV::DeleteRange->new(
endpoint => '/kv/deleterange',
etcd => $self,
cb => $cb,
( $options ? %$options : () ),
);
$delete_range->request unless $delete_range->hold;
return $delete_range;
}

=head2 put
Put puts the given key into the key-value store. A put request increments
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/KV/Compare.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::KV::Compare
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
84 changes: 84 additions & 0 deletions lib/Net/Etcd/KV/DeleteRange.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use utf8;
package Net::Etcd::KV::DeleteRange;

use strict;
use warnings;

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

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

use namespace::clean;

=head1 NAME
Net::Etcd::DeleteRange
=cut

our $VERSION = '0.014';

=head1 DESCRIPTION
DeleteRange deletes the given range from the key-value store. A
delete request increments the revision of the key-value store and
generates a delete event in the event history for every deleted key.
=head1 ACCESSORS
=head2 endpoint
=cut

has endpoint => (
is => 'ro',
isa => Str,
default => '/kv/deleterange'
);

=head2 key
key is the key, in bytes, to put into the key-value store.
=cut

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

=head2 prev_kv
If prev_kv is set, etcd gets the previous key-value pair before changing it.
The previous key-value pair will be returned in the put response.
=cut

has prev_kv => (
is => 'ro',
isa => Bool,
coerce => sub { no strict 'refs'; return $_[0] ? JSON::true : JSON::false }
);

=head2 range_end
range_end is the upper bound on the requested range [key, range_end). If range_end is '\0',
the range is all keys >= key. If the range_end is one bit larger than the given key, then
the range requests get the all keys with the prefix (the given key). If both key and
range_end are '\0', then range requests returns all keys. the key is encoded with base64.
type bytes. NOTE: If range_end is not given, the request only looks up key.
=cut

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

1;
2 changes: 1 addition & 1 deletion lib/Net/Etcd/KV/Op.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::KV::Op
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/KV/Put.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Net::Etcd::Put
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
18 changes: 1 addition & 17 deletions lib/Net/Etcd/KV/Range.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::Range
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down Expand Up @@ -217,20 +217,4 @@ has max_create_revision => (
isa => Int,
);

=head2 delete
DeleteRange deletes the given range from the key-value store. A delete request increments the
revision of the key-value store and generates a delete event in the event history for every
deleted key.
$etcd->range({key =>'test0'})->delete
=cut

sub delete {
my ( $self ) = @_;
$self->{endpoint} => '/kv/deleterange',
return $self->request;
}

1;
2 changes: 1 addition & 1 deletion lib/Net/Etcd/KV/Txn.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::KV::Txn
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Lease.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::Lease
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Maintenance.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Net::Etcd::Maintenance
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Role/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Net::Etcd::Role::Actions
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

has etcd => (
is => 'ro',
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::User
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/User/Role.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Net::Etcd::User::Role
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Watch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::Range
=cut

our $VERSION = '0.013';
our $VERSION = '0.014';

=head1 DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion t/01-key_value.t
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cmp_ok( $key->{response}{success}, '==', 1, "kv range success" );
# delete range
lives_ok(
sub {
$key = $etcd->range( { key => 'foo1' } )->delete
$key = $etcd->deleterange( { key => 'foo1' } )
},
"kv range_delete"
);
Expand Down
2 changes: 1 addition & 1 deletion t/06-watch.t
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ cmp_ok( scalar @events, '==', 2, "number of async events stored. (create_watch,
# delete range
lives_ok(
sub {
$key = $etcd->range( { key => 'foo' } )->delete
$key = $etcd->deleterange( { key => 'foo' } )
},
"kv range_delete"
);
Expand Down

0 comments on commit f4fd0cb

Please sign in to comment.