From 6d341181901a72dd31c2b37639137256030a7e85 Mon Sep 17 00:00:00 2001 From: Sam Batschelet Date: Thu, 23 Nov 2017 11:53:09 -0500 Subject: [PATCH] Maintenance (#32) * Add support and tests for Maintenance status and defragment. * Add support and tests for Maintenance class methods version, status and defragment * Update POD * Add is_success and content helper methods. * Update version and change log for pending release. --- Changes | 7 ++++ README | 17 ++++++++- README.md | 18 ++++++++- lib/Net/Etcd.pm | 34 +++++++++++++++-- lib/Net/Etcd/Auth.pm | 2 +- lib/Net/Etcd/Auth/Role.pm | 2 +- lib/Net/Etcd/Auth/RolePermission.pm | 2 +- lib/Net/Etcd/Config.pm | 2 +- lib/Net/Etcd/KV.pm | 2 +- lib/Net/Etcd/KV/Compare.pm | 2 +- lib/Net/Etcd/KV/DeleteRange.pm | 2 +- lib/Net/Etcd/KV/Op.pm | 2 +- lib/Net/Etcd/KV/Put.pm | 2 +- lib/Net/Etcd/KV/Range.pm | 3 +- lib/Net/Etcd/KV/Txn.pm | 2 +- lib/Net/Etcd/Lease.pm | 2 +- lib/Net/Etcd/Maintenance.pm | 58 ++++++++++++++++++++++++++++- lib/Net/Etcd/Role/Actions.pm | 31 ++++++++++++++- lib/Net/Etcd/User.pm | 2 +- lib/Net/Etcd/User/Role.pm | 2 +- lib/Net/Etcd/Watch.pm | 2 +- t/03-maint.t | 36 +++++++++++++++++- t/pod-spelling.t | 3 ++ 23 files changed, 211 insertions(+), 24 deletions(-) diff --git a/Changes b/Changes index 21178f3..b8f41d0 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,11 @@ Revision history for Net::Etcd +0.016 +[ ENHANCEMENTS ] +* Added status, defragment, version and status methods to Maintenance class. +* Added ability to check $etcd->version +* Add is_success and content helper methods to Action role. +* Updated POD. + 0.015 [ ENHANCEMENTS ] * Add ability to retry_auth. This feature is very useful for refreshing stale tokens. Closes GH #26 diff --git a/README b/README index 4eb12d3..f3195eb 100644 --- a/README +++ b/README @@ -9,7 +9,10 @@ SYNOPSIS $etcd = Net::Etcd->new({ host => $host, port => $port, ssl => 1 }); # put key - $result = $etcd->put({ key =>'foo1', value => 'bar' }); + $put_key = $etcd->put({ key =>'foo1', value => 'bar' }); + + # check for success of a transaction + $put_key->is_success; # get single key $key = $etcd->range({ key =>'test0' }); @@ -50,6 +53,13 @@ SYNOPSIS # grant role $etcd->user_role( { user => 'samba', role => 'myrole' } )->grant; + # defrag member's backend database + $defrag = $etcd->maintenance()->defragment; + print "Defrag request complete!" if $defrag->is_success; + + # member version + $v = $etcd->version; + DESCRIPTION Net::Etcd is object oriented interface to the v3 REST API provided by the etcd grpc-gateway . @@ -82,6 +92,11 @@ ACCESSORS is changed. PUBLIC METHODS + version + Returns the etcd member version + + $etcd->version() + watch See Net::Etcd::Watch diff --git a/README.md b/README.md index 33c8af3..138a6b2 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,10 @@ Net::Etcd - etcd v3 REST API. $etcd = Net::Etcd->new({ host => $host, port => $port, ssl => 1 }); # put key - $result = $etcd->put({ key =>'foo1', value => 'bar' }); + $put_key = $etcd->put({ key =>'foo1', value => 'bar' }); + + # check for success of a transaction + $put_key->is_success; # get single key $key = $etcd->range({ key =>'test0' }); @@ -54,6 +57,13 @@ Net::Etcd - etcd v3 REST API. # grant role $etcd->user_role( { user => 'samba', role => 'myrole' } )->grant; + # defrag member's backend database + $defrag = $etcd->maintenance()->defragment; + print "Defrag request complete!" if $defrag->is_success; + + # member version + $v = $etcd->version; + # DESCRIPTION [Net::Etcd](https://metacpan.org/pod/Net::Etcd) is object oriented interface to the v3 REST API provided by the etcd [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway). @@ -95,6 +105,12 @@ authentication process and stored until no longer valid or username is changed. # PUBLIC METHODS +## version + +Returns the etcd member version + + $etcd->version() + ## watch See [Net::Etcd::Watch](https://metacpan.org/pod/Net::Etcd::Watch) diff --git a/lib/Net/Etcd.pm b/lib/Net/Etcd.pm index 27f6618..3c86f6e 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.015'; +our $VERSION = '0.016'; =head1 SYNOPSIS @@ -40,7 +40,10 @@ our $VERSION = '0.015'; $etcd = Net::Etcd->new({ host => $host, port => $port, ssl => 1 }); # put key - $result = $etcd->put({ key =>'foo1', value => 'bar' }); + $put_key = $etcd->put({ key =>'foo1', value => 'bar' }); + + # check for success of a transaction + $put_key->is_success; # get single key $key = $etcd->range({ key =>'test0' }); @@ -81,11 +84,17 @@ our $VERSION = '0.015'; # grant role $etcd->user_role( { user => 'samba', role => 'myrole' } )->grant; + # defrag member's backend database + $defrag = $etcd->maintenance()->defragment; + print "Defrag request complete!" if $defrag->is_success; + + # member version + $v = $etcd->version; + =head1 DESCRIPTION L is object oriented interface to the v3 REST API provided by the etcd L. - =head1 ACCESSORS =head2 host @@ -185,6 +194,25 @@ has auth_token => ( =head1 PUBLIC METHODS +=head2 version + +Returns the etcd member version + + $etcd->version() + +=cut + +sub version { + my ( $self, $options ) = @_; + my $cb = pop if ref $_[-1] eq 'CODE'; + my $member = Net::Etcd::Maintenance->new( + etcd => $self, + cb => $cb, + ( $options ? %$options : () ), + ); + return $member->version; +} + =head2 watch See L diff --git a/lib/Net/Etcd/Auth.pm b/lib/Net/Etcd/Auth.pm index cda4f70..8f1701a 100644 --- a/lib/Net/Etcd/Auth.pm +++ b/lib/Net/Etcd/Auth.pm @@ -26,7 +26,7 @@ Net::Etcd::Auth =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Auth/Role.pm b/lib/Net/Etcd/Auth/Role.pm index 426f5d2..6870784 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.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Auth/RolePermission.pm b/lib/Net/Etcd/Auth/RolePermission.pm index df6c274..ddff19b 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.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Config.pm b/lib/Net/Etcd/Config.pm index bfa30bf..b0038d5 100644 --- a/lib/Net/Etcd/Config.pm +++ b/lib/Net/Etcd/Config.pm @@ -13,7 +13,7 @@ Net::Etcd::Config =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; =head1 ACCESSORS diff --git a/lib/Net/Etcd/KV.pm b/lib/Net/Etcd/KV.pm index a715b85..7b4a1e1 100644 --- a/lib/Net/Etcd/KV.pm +++ b/lib/Net/Etcd/KV.pm @@ -25,7 +25,7 @@ Net::Etcd::KV =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/Compare.pm b/lib/Net/Etcd/KV/Compare.pm index ebe4607..fb41d7c 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.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/DeleteRange.pm b/lib/Net/Etcd/KV/DeleteRange.pm index 098cb98..39f11b7 100644 --- a/lib/Net/Etcd/KV/DeleteRange.pm +++ b/lib/Net/Etcd/KV/DeleteRange.pm @@ -19,7 +19,7 @@ Net::Etcd::DeleteRange =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/Op.pm b/lib/Net/Etcd/KV/Op.pm index b5988a1..38d79d1 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.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/Put.pm b/lib/Net/Etcd/KV/Put.pm index f283940..6b461ff 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.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/Range.pm b/lib/Net/Etcd/KV/Range.pm index 5b76e06..e503b10 100644 --- a/lib/Net/Etcd/KV/Range.pm +++ b/lib/Net/Etcd/KV/Range.pm @@ -10,7 +10,6 @@ use MIME::Base64; use JSON; with 'Net::Etcd::Role::Actions'; -#extends 'Net::Etcd::KV'; use namespace::clean; @@ -20,7 +19,7 @@ Net::Etcd::Range =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/KV/Txn.pm b/lib/Net/Etcd/KV/Txn.pm index 01cc4a3..f734ccd 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.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Lease.pm b/lib/Net/Etcd/Lease.pm index 5364c53..76e043d 100644 --- a/lib/Net/Etcd/Lease.pm +++ b/lib/Net/Etcd/Lease.pm @@ -20,7 +20,7 @@ Net::Etcd::Lease =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Maintenance.pm b/lib/Net/Etcd/Maintenance.pm index b4fae5e..237724d 100644 --- a/lib/Net/Etcd/Maintenance.pm +++ b/lib/Net/Etcd/Maintenance.pm @@ -20,7 +20,17 @@ Net::Etcd::Maintenance =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; +=head1 SYNOPSIS + + # defrag member's backend database + $defrag = $etcd->maintenance()->defragment; + + # check status + $status = $etcd->maintenance()->status; + + # member version + $status = $etcd->version; =head1 DESCRIPTION @@ -56,4 +66,50 @@ sub snapshot { return $self; } +=head2 status + +Status gets the status of the member. + +=cut + +sub status { + my ( $self, $options ) = @_; + my $cb = pop if ref $_[-1] eq 'CODE'; + $self->{endpoint} = '/maintenance/status'; + $self->{json_args} = '{}'; + $self->request; + return $self; +} + +=head2 defragment + +Defragment defragments a member's backend database to recover storage space. + +=cut + +sub defragment { + my ( $self, $options ) = @_; + my $cb = pop if ref $_[-1] eq 'CODE'; + $self->{endpoint} = '/maintenance/defragment'; + $self->{json_args} = '{}'; + $self->request; + return $self; +} + +=head2 version + +Returns the member version. + +=cut + +sub version { + my ( $self, $options ) = @_; + my $cb = pop if ref $_[-1] eq 'CODE'; + my $status = $self->status; + if ( $status->is_success ) { + return $status->content->{version}; + } + return; +} + 1; diff --git a/lib/Net/Etcd/Role/Actions.pm b/lib/Net/Etcd/Role/Actions.pm index c113883..48f854e 100644 --- a/lib/Net/Etcd/Role/Actions.pm +++ b/lib/Net/Etcd/Role/Actions.pm @@ -23,7 +23,7 @@ Net::Etcd::Role::Actions =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; has etcd => ( is => 'ro', @@ -226,4 +226,33 @@ sub all { return $kvs; } +=head2 is_success + +Success is returned if the response is a 200 + +=cut + +sub is_success { + my ($self) = @_; + my $response = $self->response; + if ( defined $response->{success} ) { + return $response->{success}; + } + return; +} + +=head2 content + +returns JSON decoded content hash + +=cut + +sub content { + my ($self) = @_; + my $response = $self->response; + my $content = from_json( $response->{content} ); + return $content if $content; + return; +} + 1; diff --git a/lib/Net/Etcd/User.pm b/lib/Net/Etcd/User.pm index fb2e8ff..88f4730 100644 --- a/lib/Net/Etcd/User.pm +++ b/lib/Net/Etcd/User.pm @@ -20,7 +20,7 @@ Net::Etcd::User =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/User/Role.pm b/lib/Net/Etcd/User/Role.pm index bd2ef47..741a206 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.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/lib/Net/Etcd/Watch.pm b/lib/Net/Etcd/Watch.pm index 0261c32..0bcea4f 100644 --- a/lib/Net/Etcd/Watch.pm +++ b/lib/Net/Etcd/Watch.pm @@ -20,7 +20,7 @@ Net::Etcd::Range =cut -our $VERSION = '0.015'; +our $VERSION = '0.016'; =head1 DESCRIPTION diff --git a/t/03-maint.t b/t/03-maint.t index c849841..740dda4 100644 --- a/t/03-maint.t +++ b/t/03-maint.t @@ -11,7 +11,7 @@ my ($host, $port); if ( $ENV{ETCD_TEST_HOST} and $ENV{ETCD_TEST_PORT}) { $host = $ENV{ETCD_TEST_HOST}; $port = $ENV{ETCD_TEST_PORT}; - plan tests => 2; + plan tests => 8; } else { plan skip_all => "Please set environment variable ETCD_TEST_HOST and ETCD_TEST_PORT."; @@ -20,6 +20,7 @@ else { my $maint; my $etcd = Net::Etcd->new( { host => $host, port => $port } ); +# snapshot lives_ok( sub { $maint = $etcd->maintenance()->snapshot; @@ -30,4 +31,37 @@ lives_ok( #print STDERR Dumper($maint); cmp_ok( $maint->{response}{content}, 'ne', "", "snapshot create" ); +# status +lives_ok( + sub { + $maint = $etcd->maintenance()->status; + }, + "check status" +); + +#print STDERR Dumper($maint); +cmp_ok( $maint->is_success, '==', 1, "check status success" ); + +# defragment +lives_ok( + sub { + $maint = $etcd->maintenance()->defragment; + }, + "defragment request" +); + +#print STDERR Dumper($maint); +cmp_ok( $maint->is_success, '==', 1, "defragment request success" ); +my $version; + +# version helper +lives_ok( + sub { + $version = $etcd->version; + }, + "version" +); + +cmp_ok( $version, 'ne', "", "version success" ); + 1; diff --git a/t/pod-spelling.t b/t/pod-spelling.t index 5f1e4aa..6c4c7d7 100644 --- a/t/pod-spelling.t +++ b/t/pod-spelling.t @@ -64,3 +64,6 @@ Ananth Kavuri READWRITE permType +Defragment +defragment +defragments