diff --git a/lib/Net/Etcd.pm b/lib/Net/Etcd.pm index 518d3cc..16fb59e 100644 --- a/lib/Net/Etcd.pm +++ b/lib/Net/Etcd.pm @@ -29,7 +29,7 @@ Net::Etcd - etcd v3 REST API. =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 SYNOPSIS @@ -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) = @_; @@ -336,6 +339,14 @@ See L =cut +=head2 deleterange + +See L + + $etcd->deleterange({ key=>'test0' }); + +=cut + =head2 range See L diff --git a/lib/Net/Etcd/Auth.pm b/lib/Net/Etcd/Auth.pm index 8bf1f85..45fdc32 100644 --- a/lib/Net/Etcd/Auth.pm +++ b/lib/Net/Etcd/Auth.pm @@ -26,7 +26,7 @@ Net::Etcd::Auth =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Auth/Role.pm b/lib/Net/Etcd/Auth/Role.pm index 342998d..433af48 100644 --- a/lib/Net/Etcd/Auth/Role.pm +++ b/lib/Net/Etcd/Auth/Role.pm @@ -20,7 +20,7 @@ Net::Etcd::Auth::Role =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Auth/RolePermission.pm b/lib/Net/Etcd/Auth/RolePermission.pm index 4a04988..6be5bb7 100644 --- a/lib/Net/Etcd/Auth/RolePermission.pm +++ b/lib/Net/Etcd/Auth/RolePermission.pm @@ -21,7 +21,7 @@ Net::Etcd::Auth::RolePermission =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Config.pm b/lib/Net/Etcd/Config.pm index 9d71a9b..c68356d 100644 --- a/lib/Net/Etcd/Config.pm +++ b/lib/Net/Etcd/Config.pm @@ -13,7 +13,7 @@ Net::Etcd::Config =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 ACCESSORS diff --git a/lib/Net/Etcd/KV.pm b/lib/Net/Etcd/KV.pm index b62d674..f7aa3c0 100644 --- a/lib/Net/Etcd/KV.pm +++ b/lib/Net/Etcd/KV.pm @@ -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; @@ -24,7 +25,7 @@ Net::Etcd::KV =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION @@ -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 { @@ -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 diff --git a/lib/Net/Etcd/KV/Compare.pm b/lib/Net/Etcd/KV/Compare.pm index 5099c68..8ed6e39 100644 --- a/lib/Net/Etcd/KV/Compare.pm +++ b/lib/Net/Etcd/KV/Compare.pm @@ -20,7 +20,7 @@ Net::Etcd::KV::Compare =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/DeleteRange.pm b/lib/Net/Etcd/KV/DeleteRange.pm new file mode 100644 index 0000000..9b8dc80 --- /dev/null +++ b/lib/Net/Etcd/KV/DeleteRange.pm @@ -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; diff --git a/lib/Net/Etcd/KV/Op.pm b/lib/Net/Etcd/KV/Op.pm index 8dd9a91..396ffa7 100644 --- a/lib/Net/Etcd/KV/Op.pm +++ b/lib/Net/Etcd/KV/Op.pm @@ -20,7 +20,7 @@ Net::Etcd::KV::Op =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/Put.pm b/lib/Net/Etcd/KV/Put.pm index 1579649..768b20d 100644 --- a/lib/Net/Etcd/KV/Put.pm +++ b/lib/Net/Etcd/KV/Put.pm @@ -19,7 +19,7 @@ Net::Etcd::Put =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/Range.pm b/lib/Net/Etcd/KV/Range.pm index 064748a..b837f95 100644 --- a/lib/Net/Etcd/KV/Range.pm +++ b/lib/Net/Etcd/KV/Range.pm @@ -20,7 +20,7 @@ Net::Etcd::Range =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION @@ -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; diff --git a/lib/Net/Etcd/KV/Txn.pm b/lib/Net/Etcd/KV/Txn.pm index 78706ce..79fbb0b 100644 --- a/lib/Net/Etcd/KV/Txn.pm +++ b/lib/Net/Etcd/KV/Txn.pm @@ -20,7 +20,7 @@ Net::Etcd::KV::Txn =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Lease.pm b/lib/Net/Etcd/Lease.pm index 720e761..946c937 100644 --- a/lib/Net/Etcd/Lease.pm +++ b/lib/Net/Etcd/Lease.pm @@ -20,7 +20,7 @@ Net::Etcd::Lease =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Maintenance.pm b/lib/Net/Etcd/Maintenance.pm index 7a5288c..24d4ccf 100644 --- a/lib/Net/Etcd/Maintenance.pm +++ b/lib/Net/Etcd/Maintenance.pm @@ -18,7 +18,7 @@ Net::Etcd::Maintenance =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Role/Actions.pm b/lib/Net/Etcd/Role/Actions.pm index 8406053..13c6951 100644 --- a/lib/Net/Etcd/Role/Actions.pm +++ b/lib/Net/Etcd/Role/Actions.pm @@ -22,7 +22,7 @@ Net::Etcd::Role::Actions =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; has etcd => ( is => 'ro', diff --git a/lib/Net/Etcd/User.pm b/lib/Net/Etcd/User.pm index 9401274..e4696e8 100644 --- a/lib/Net/Etcd/User.pm +++ b/lib/Net/Etcd/User.pm @@ -20,7 +20,7 @@ Net::Etcd::User =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/User/Role.pm b/lib/Net/Etcd/User/Role.pm index daeb484..30144c9 100644 --- a/lib/Net/Etcd/User/Role.pm +++ b/lib/Net/Etcd/User/Role.pm @@ -17,7 +17,7 @@ Net::Etcd::User::Role =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Watch.pm b/lib/Net/Etcd/Watch.pm index 93703d1..f873006 100644 --- a/lib/Net/Etcd/Watch.pm +++ b/lib/Net/Etcd/Watch.pm @@ -20,7 +20,7 @@ Net::Etcd::Range =cut -our $VERSION = '0.013'; +our $VERSION = '0.014'; =head1 DESCRIPTION diff --git a/t/01-key_value.t b/t/01-key_value.t index 36805ca..4aa401c 100644 --- a/t/01-key_value.t +++ b/t/01-key_value.t @@ -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" ); diff --git a/t/06-watch.t b/t/06-watch.t index 104e102..335f029 100644 --- a/t/06-watch.t +++ b/t/06-watch.t @@ -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" );