File Coverage

blib/lib/Consul/API/Agent.pm
Criterion Covered Total %
statement 57 133 42.8
branch 1 32 3.1
condition n/a
subroutine 20 38 52.6
pod 0 16 0.0
total 78 219 35.6


line stmt bran cond sub pod time code
1             package Consul::API::Agent;
2             $Consul::API::Agent::VERSION = '0.027';
3 9     9   4973 use namespace::autoclean;
  9         19  
  9         49  
4              
5 9     9   629 use Moo::Role;
  9         18  
  9         45  
6 9     9   2910 use Types::Standard qw(Str);
  9         16  
  9         73  
7              
8             requires qw(_version_prefix _api_exec);
9              
10             has _agent_endpoint => ( is => 'lazy', isa => Str );
11             sub _build__agent_endpoint {
12 2     2   54 shift->_version_prefix . '/agent';
13             }
14              
15             sub agent {
16 2     2 0 4037 my $self = shift;
17 2 50       38 $self = Consul->new(@_) unless ref $self;
18 2         62 return bless \$self, "Consul::API::Agent::Impl";
19             }
20              
21             package
22             Consul::API::Agent::Impl; # hide from PAUSE
23              
24 9     9   4949 use Moo;
  9         16  
  9         57  
25              
26 9     9   2743 use Consul::Check;
  9         20  
  9         208  
27 9     9   42 use Consul::Service;
  9         17  
  9         243  
28              
29 9     9   51 use Carp qw(croak);
  9         14  
  9         455  
30 9     9   50 use Scalar::Util qw( blessed );
  9         18  
  9         10746  
31              
32             sub checks {
33 0     0 0 0 my ($self, %args) = @_;
34             $$self->_api_exec($$self->_agent_endpoint."/checks", 'GET', %args, sub {
35 0     0   0 [ map { Consul::API::Agent::Check->new(%$_) } values %{$_[0]} ]
  0         0  
  0         0  
36 0         0 });
37             }
38              
39             sub services {
40 0     0 0 0 my ($self, %args) = @_;
41             $$self->_api_exec($$self->_agent_endpoint."/services", 'GET', %args, sub {
42 0     0   0 [ map { Consul::API::Agent::Service->new(%$_) } values %{$_[0]} ]
  0         0  
  0         0  
43 0         0 });
44             }
45              
46             sub members {
47 4     4 0 2772 my ($self, %args) = @_;
48             $$self->_api_exec($$self->_agent_endpoint."/members", 'GET', %args, sub {
49 0     0     [ map { Consul::API::Agent::Member->new(%$_) } @{$_[0]} ]
  0            
  0            
50 4         101 });
51             }
52              
53             sub self {
54 0     0 0   my ($self, %args) = @_;
55             $$self->_api_exec($$self->_agent_endpoint."/self", 'GET', %args, sub {
56 0     0     Consul::API::Agent::Self->new(%{$_[0]})
  0            
57 0           });
58             }
59              
60             sub maintenance {
61 0     0 0   my ($self, $enable, %args) = @_;
62 0 0         croak 'usage: $agent->maintenance($enable, [%args])' if grep { !defined } ($enable);
  0            
63 0 0         $$self->_api_exec($$self->_agent_endpoint."/maintenance", 'PUT', enable => ($enable ? "true" : "false"), %args);
64 0           return;
65             }
66              
67             sub join {
68 0     0 0   my ($self, $address, %args) = @_;
69 0 0         croak 'usage: $agent->join($address, [%args])' if grep { !defined } ($address);
  0            
70 0           $$self->_api_exec($$self->_agent_endpoint."/join/".$address, 'PUT', %args);
71 0           return;
72             }
73              
74             sub force_leave {
75 0     0 0   my ($self, $node, %args) = @_;
76 0 0         croak 'usage: $agent->force_leave($node, [%args])' if grep { !defined } ($node);
  0            
77 0           $$self->_api_exec($$self->_agent_endpoint."/force-leave/".$node, 'PUT', %args);
78 0           return;
79             }
80              
81             sub check_register {
82 0     0 0   my ($self, $check, %args) = @_;
83 0 0         croak 'usage: $agent->check_register($check, [%args])' if grep { !defined } ($check);
  0            
84             {
85 0           local $Carp::CarpInternal{ (__PACKAGE__) } = 1;
  0            
86 0 0         $check = Consul::Check->smart_new($check) if !blessed $check;
87             }
88 0           $$self->_api_exec($$self->_agent_endpoint."/check/register", 'PUT', %args, _content => $check->to_json);
89 0           return;
90             }
91              
92             sub check_deregister {
93 0     0 0   my ($self, $check_id, %args) = @_;
94 0 0         croak 'usage: $agent->check_deregister($check_id, [%args])' if grep { !defined } ($check_id);
  0            
95 0           $$self->_api_exec($$self->_agent_endpoint."/check/deregister/".$check_id, 'PUT', %args);
96 0           return;
97             }
98              
99             sub check_pass {
100 0     0 0   my ($self, $check_id, %args) = @_;
101 0 0         croak 'usage: $agent->check_pass($check_id, [%args])' if grep { !defined } ($check_id);
  0            
102 0           $$self->_api_exec($$self->_agent_endpoint."/check/pass/".$check_id, 'PUT', %args);
103 0           return;
104             }
105              
106             sub check_warn {
107 0     0 0   my ($self, $check_id, %args) = @_;
108 0 0         croak 'usage: $agent->check_warn($check_id, [%args])' if grep { !defined } ($check_id);
  0            
109 0           $$self->_api_exec($$self->_agent_endpoint."/check/warn/".$check_id, 'PUT', %args);
110 0           return;
111             }
112              
113             sub check_fail {
114 0     0 0   my ($self, $check_id, %args) = @_;
115 0 0         croak 'usage: $agent->check_fail($check_id, [%args])' if grep { !defined } ($check_id);
  0            
116 0           $$self->_api_exec($$self->_agent_endpoint."/check/fail/".$check_id, 'PUT', %args);
117 0           return;
118             }
119              
120             sub service_register {
121 0     0 0   my ($self, $service, %args) = @_;
122 0 0         croak 'usage: $agent->service_register($service, [%args])' if grep { !defined } ($service);
  0            
123 0 0         $service = Consul::Service->new($service) if !blessed $service;
124 0           $$self->_api_exec($$self->_agent_endpoint."/service/register", 'PUT', %args, _content => $service->to_json);
125 0           return;
126             }
127              
128             sub service_deregister {
129 0     0 0   my ($self, $service_id, %args) = @_;
130 0 0         croak 'usage: $agent->service_deregister($check_id, [%args])' if grep { !defined } ($service_id);
  0            
131 0           $$self->_api_exec($$self->_agent_endpoint."/service/deregister/".$service_id, 'PUT', %args);
132 0           return;
133             }
134              
135             sub service_maintenance {
136 0     0 0   my ($self, $service_id, $enable, %args) = @_;
137 0 0         croak 'usage: $agent->service_maintenance($service_id, $enable, [%args])' if grep { !defined } ($service_id, $enable);
  0            
138 0 0         $$self->_api_exec($$self->_agent_endpoint."/service/maintenance/".$service_id, 'PUT', enable => ($enable ? "true" : "false"), %args);
139 0           return;
140             }
141              
142             package Consul::API::Agent::Check;
143             $Consul::API::Agent::Check::VERSION = '0.027';
144 9     9   85 use Moo;
  9         31  
  9         62  
145 9     9   2815 use Types::Standard qw(Str);
  9         17  
  9         41  
146              
147             has node => ( is => 'ro', isa => Str, init_arg => 'Node', required => 1 );
148             has id => ( is => 'ro', isa => Str, init_arg => 'CheckID', required => 1 );
149             has name => ( is => 'ro', isa => Str, init_arg => 'Name', required => 1 );
150             has status => ( is => 'ro', isa => Str, init_arg => 'Status', required => 1 );
151             has notes => ( is => 'ro', isa => Str, init_arg => 'Notes', required => 1 );
152             has output => ( is => 'ro', isa => Str, init_arg => 'Output', required => 1 );
153             has service_id => ( is => 'ro', isa => Str, init_arg => 'ServiceID', required => 1 );
154             has service_name => ( is => 'ro', isa => Str, init_arg => 'ServiceName', required => 1 );
155              
156             package Consul::API::Agent::Service;
157             $Consul::API::Agent::Service::VERSION = '0.027';
158 9     9   5062 use Moo;
  9         18  
  9         42  
159 9     9   2943 use Types::Standard qw(Str Int ArrayRef);
  9         40  
  9         77  
160              
161             has id => ( is => 'ro', isa => Str, init_arg => 'ID', required => 1 );
162             has service => ( is => 'ro', isa => Str, init_arg => 'Service', required => 1 );
163             has address => ( is => 'ro', isa => Str, init_arg => 'Address', required => 1 );
164             has port => ( is => 'ro', isa => Int, init_arg => 'Port', required => 1 );
165             has tags => ( is => 'ro', isa => ArrayRef[Str], init_arg => 'Tags', required => 1, coerce => sub { $_[0] || [] } );
166              
167             package Consul::API::Agent::Member;
168             $Consul::API::Agent::Member::VERSION = '0.027';
169 9     9   6986 use Moo;
  9         17  
  9         47  
170 9     9   2673 use Types::Standard qw(Str Int HashRef);
  9         24  
  9         41  
171              
172             has name => ( is => 'ro', isa => Str, init_arg => 'Name', required => 1 );
173             has addr => ( is => 'ro', isa => Str, init_arg => 'Addr', required => 1 );
174             has port => ( is => 'ro', isa => Int, init_arg => 'Port', required => 1 );
175             has tags => ( is => 'ro', isa => HashRef[Str], init_arg => 'Tags', required => 1, coerce => sub { $_[0] || {} } );
176             has status => ( is => 'ro', isa => Int, init_arg => 'Status', required => 1 );
177             has protocol_min => ( is => 'ro', isa => Int, init_arg => 'ProtocolMin', required => 1 );
178             has protocol_max => ( is => 'ro', isa => Int, init_arg => 'ProtocolMax', required => 1 );
179             has protocol_cur => ( is => 'ro', isa => Int, init_arg => 'ProtocolCur', required => 1 );
180             has delegate_min => ( is => 'ro', isa => Int, init_arg => 'DelegateMin', required => 1 );
181             has delegate_max => ( is => 'ro', isa => Int, init_arg => 'DelegateMax', required => 1 );
182             has delegate_cur => ( is => 'ro', isa => Int, init_arg => 'DelegateCur', required => 1 );
183              
184             package Consul::API::Agent::Self;
185             $Consul::API::Agent::Self::VERSION = '0.027';
186 9     9   7185 use Moo;
  9         16  
  9         35  
187 9     9   2723 use Types::Standard qw(HashRef);
  9         18  
  9         50  
188 9     9   3897 use Type::Utils qw(class_type);
  9         18  
  9         57  
189              
190             # XXX raw hash. not happy about this, but the list of config keys don't seem to be consistent across environments
191             has config => ( is => 'ro', isa => HashRef, init_arg => 'Config', required => 1, coerce => sub { $_[0] || {} } );
192             has member => ( is => 'ro', isa => class_type('Consul::API::Agent::Member'), init_arg => 'Member', required => 1, coerce => sub { Consul::API::Agent::Member->new($_[0]) } );
193              
194             1;
195              
196             =pod
197              
198             =encoding UTF-8
199              
200             =head1 NAME
201              
202             Consul::API::Agent - Agent API
203              
204             =head1 SYNOPSIS
205              
206             use Consul;
207             my $agent = Consul->agent;
208             $agent->self;
209              
210             =head1 DESCRIPTION
211              
212             The Agent API is used to interact with the local Consul agent.
213              
214             This API is fully documented at L.
215              
216             =head1 METHODS
217              
218             =head2 checks
219              
220             =head2 services
221              
222             =head2 members
223              
224             =head2 self
225              
226             =head2 maintenance
227              
228             =head2 join
229              
230             =head2 force_leave
231              
232             =head2 check_register
233              
234             =head2 check_deregister
235              
236             =head2 check_pass
237              
238             =head2 check_warn
239              
240             =head2 check_fail
241              
242             =head2 service_register
243              
244             =head2 service_deregister
245              
246             =head2 service_maintenance
247              
248             =head1 SEE ALSO
249              
250             L
251              
252             =cut