File Coverage

blib/lib/Net/Etcd/Lease.pm
Criterion Covered Total %
statement 30 55 54.5
branch 0 8 0.0
condition 0 3 0.0
subroutine 10 15 66.6
pod 5 5 100.0
total 45 86 52.3


line stmt bran cond sub pod time code
1 9     9   48 use utf8;
  9         12  
  9         44  
2             package Net::Etcd::Lease;
3              
4 9     9   324 use strict;
  9         13  
  9         140  
5 9     9   26 use warnings;
  9         18  
  9         180  
6              
7 9     9   36 use Moo;
  9         12  
  9         35  
8 9     9   2191 use Types::Standard qw(Str Int Bool HashRef ArrayRef);
  9         21  
  9         43  
9 9     9   6282 use Data::Dumper;
  9         16  
  9         319  
10 9     9   37 use Carp;
  9         12  
  9         335  
11 9     9   40 use JSON;
  9         13  
  9         122  
12              
13             with 'Net::Etcd::Role::Actions';
14              
15 9     9   701 use namespace::clean;
  9         21  
  9         49  
16              
17             =head1 NAME
18              
19             Net::Etcd::Lease
20              
21             =cut
22              
23             our $VERSION = '0.022';
24              
25             =head1 DESCRIPTION
26              
27             LeaseGrant creates a lease which expires if the server does not receive a keepAlive within
28             a given time to live period. All keys attached to the lease will be expired and deleted if
29             the lease expires. Each expired key generates a delete event in the event history.
30              
31             =head1 ACCESSORS
32              
33             =head2 endpoint
34              
35             =cut
36              
37             has endpoint => (
38             is => 'rwp',
39             isa => Str,
40             );
41              
42             =head2 TTL
43              
44             TTL is the advisory time-to-live in seconds.
45              
46             =cut
47              
48             has TTL => (
49             is => 'ro',
50             isa => Str,
51             );
52              
53             =head2 ID
54              
55             ID is the requested ID for the lease. If ID is set to 0, the lessor chooses an ID.
56              
57             =cut
58              
59             has ID => (
60             is => 'ro',
61             coerce => sub { return $_[0]; },
62             );
63              
64             =head2 keys
65              
66             keys is true to query all the keys attached to this lease.
67              
68             =cut
69              
70             has keys => (
71             is => 'ro',
72             isa => Bool,
73 9     9   3600 coerce => sub { no strict 'refs'; return $_[0] ? JSON::true : JSON::false }
  9         22  
  9         2691  
74             );
75              
76             =head1 PUBLIC METHODS
77              
78             =head2 grant
79              
80             LeaseGrant creates a lease which expires if the server does not receive a keepAlive within
81             a given time to live period. All keys attached to the lease will be expired and deleted if
82             the lease expires. Each expired key generates a delete event in the event history.
83              
84              
85             $etcd->lease({ ID => 7587821338341002662, TTL => 20 })->grant
86              
87             =cut
88              
89             sub grant {
90 0     0 1   my $self = shift;
91 0           $self->{endpoint} = '/lease/grant';
92             confess 'TTL and ID are required for ' . __PACKAGE__ . '->grant'
93 0 0 0       unless ($self->{ID} && $self->{TTL});
94 0           $self->request;
95 0           return $self;
96             }
97              
98             =head2 revoke
99              
100             LeaseRevoke revokes a lease. All keys attached to the lease will expire and be deleted.
101              
102             $etcd->lease({ ID => 7587821338341002662 })->revoke
103              
104             =cut
105              
106             sub revoke {
107 0     0 1   my $self = shift;
108 0           $self->{endpoint} = '/kv/lease/revoke';
109             confess 'ID is required for ' . __PACKAGE__ . '->revoke'
110 0 0         unless $self->{ID};
111 0           $self->request;
112 0           return $self;
113             }
114              
115             =head2 ttl
116              
117             LeaseTimeToLive retrieves lease information.
118              
119             $etcd->lease({ ID => 7587821338341002662, keys => 1 })->ttl
120              
121             =cut
122              
123             sub ttl {
124 0     0 1   my $self = shift;
125 0           $self->{endpoint} = '/kv/lease/timetolive';
126             confess 'ID is required for ' . __PACKAGE__ . '->ttl'
127 0 0         unless $self->{ID};
128 0           $self->request;
129 0           return $self;
130             }
131              
132              
133             =head2 keepalive
134              
135             LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
136             to the server and streaming keep alive responses from the server to the client."
137              
138             $etcd->lease({{ ID => 7587821338341002662 })->keepalive
139              
140             =cut
141              
142             sub keepalive {
143 0     0 1   my $self = shift;
144 0           $self->{endpoint} = '/lease/keepalive';
145             confess 'ID is required for ' . __PACKAGE__ . '->keepalive'
146 0 0         unless $self->{ID};
147 0           $self->request;
148 0           return $self;
149             }
150              
151             =head2 leases
152             lists all existing leases.
153              
154             $etcd->lease()->leases
155              
156             =cut
157              
158             sub leases {
159 0     0 1   my $self = shift;
160 0           $self->{endpoint} = '/kv/lease/leases';
161 0           $self->{json_args} = '{}';
162 0           $self->request;
163 0           return $self;
164             }
165              
166             1;