Client.pm

filter: Statementstmt 93.8% 61 / 65 Branchbran 50.0% 9 / 18 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 81.4% 92 / 113 CC 12 SCAR 11.3
CC
12
Cov
81%
__ANON__
7.8
_userinfo
3.2
root
2.3
CRAP 3.1
« Client/Result.pm ↑ Summary
1 package Mojo::Redfish::Client;
2  
3 3 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Mojo::Redfish::Client::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.03';
12 3 $VERSION = eval $VERSION;
13  
14 3 has concurrency => 5;
15 3 has ssl => 1;
16 3 has [qw/host password token username/];
17  
18 2 ▶has ua => sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
19 2 my $self = shift;
20 2 my $ua = Mojo::UserAgent->new(insecure => 1);
21  
22 2 Scalar::Util::weaken $self;
23 8 ▶ $ua->on(prepare => sub {
Subroutine __ANON__: called CC=6 SCAR=20.5
24 8 my ($ua, $tx) = @_;
25 8 my $url = $tx->req->url;
26  
27 8 ▶ if (defined(my $host = $self->host)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $host = $self->host)) 0 8 8
28 0 $url->host($host);
29 }
30  
31 8 ▶ if (defined(my $ssl = $self->ssl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ssl = $self->ssl)) 0 8 8
32 0 ▶ $url->scheme($ssl ? 'https' : 'http');
Branch: 1 branch
BranchTrueFalseTotal
$ssl ? : 0 0 0
33 }
34  
35 8 ▶ if (my $token = $self->token) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $token = $self->token) { } 6 2 8
elsif (my $userinfo = $self->_userinfo) { } 2 0 2
36 6 $tx->req->headers->header('X-Auth-Token', $token);
37 } elsif (my $userinfo = $self->_userinfo) {
38 2 $url->userinfo($userinfo);
39 }
40 2 });
41  
42 2 return $ua;
43 3 };
44  
45 4 ▶sub get {
Subroutine get: called CC=2 SCAR=7.2
Pod documented
46 4 my ($self, $url) = @_;
47 4 my $tx = $self->ua->get($url);
48 0 ▶ if (my $err = $tx->error) { Carp::croak $err->{message} }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx->error) 0 4 4
49 4 my $data = $tx->res->json;
50 4 return $self->_result($tx->res->json);
51 }
52  
53 4 ▶sub get_p {
Subroutine get_p: called CC=1 SCAR=0.1
Pod documented
54 4 my ($self, $url) = @_;
55 4 ▶ $self->ua->get_p($url)->then(sub{
Subroutine __ANON__: called CC=2 SCAR=7.4
56 4 my $tx = shift;
57 0 ▶ if (my $err = $tx->error) { Carp::croak $err->{message} }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx->error) 0 4 4
58 4 my $data = $tx->res->json;
59 4 return $self->_result($tx->res->json);
60 4 });
61 }
62  
63 1 ▶sub root {
Subroutine root: called CC=2 SCAR=8.1
Pod documented
64 1 my $self = shift;
65 1 ▶ return $self->{root} ||= $self->get('/redfish/v1');
Condition33%1/3
$self->{"root"} ||= $self->get("/redfish/v1")
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"root"} ||= $self->get("/redfish/v1")
$self->{"root"} $self->get("/redfish/v1")
Truth table33%1/3
A ||= B
A= $self->{"root"}B= $self->get("/redfish/v1")
ABresult
1X1
011
000
66 }
67  
68 1 ▶sub root_p {
Subroutine root_p: called CC=2 SCAR=7.0
Pod documented
69 1 my $self = shift;
70 return Mojo::Promise->resolve($self->{root})
71 1 ▶ if $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"root"} 0 1 1
72 1 ▶ return $self->get_p('/redfish/v1')->then(sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 return $self->{root} = shift;
74 1 });
75 }
76  
77 8 ▶sub _result {
Subroutine _result: called CC=1 SCAR=0.0
78 8 my ($self, $data) = @_;
79 8 return Mojo::Redfish::Client::Result->new(
80 data => $data,
81 client => $self,
82 );
83 };
84  
85 2 ▶sub _userinfo {
Subroutine _userinfo: called CC=3 SCAR=11.8
86 2 my $self = shift;
87 2 my ($user, $pass) = @{$self}{qw/username password/};
88 2 ▶ return undef unless $user || $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless $user or $pass 0 2 2
Condition33%1/3
$user or $pass
l!l&&r!l&&!r
200
MC/DC0%0/2
$user or $pass
$user $pass
Truth table33%1/3
A or B
A= $userB= $pass
ABresult
1X1
011
000
89 2 return "$user:$pass";
90 };
91  
92 3 1;
93  
94 =head1 NAME
95  
96 Mojo::Redfish::Client - A Redfish client with a Mojo flair
97  
98 =head1 SYNOPSIS
99  
100 my $client = Mojo::Redfish::Client->new(host => '192.168.0.1');
101 my $system = $client->root->get('/Systems')->get('/Members')->first;
102 my $name = $system->value('/Name');
103 say "Name: $name";
104  
105 =head1 DESCRIPTION
106  
107 L<Redfish|https://redfish.dmtf.org/> is a modern standards-based system for querying computer systems for information.
108 It replaces the existing IPMI "standard", such as it was, both in standardization and in using JSON over HTTP rather than binary protocols.
109  
110 L<Mojo::Redfish::Client> is, as the name suggests, a client for Redfish.
111 It works to smooth out some of the common pain points of working with Redfish, especially the task of walking the data structure to find relevant information.
112  
113 This is still a work-in-progress, however the author uses it in work application so every effort will be made to keep the api reasonably stable while improving where possible.
114  
115 =head1 ATTRIBUTES
116  
117 L<Mojo::Redfish::Client> inherits all attributes from L<Mojo::Base> and implements the following new ones.
118  
119 =head2 concurrency
120  
121 The B<per-operation> concurrency limit.
122 This is not a global concurrency, it only limits concurrency when a single operation would make several concurrent requests within it.
123 For example (and the only current case), L<Mojo::Redfish::Client/get_p> on an array.
124 If zero (or otherwise falsey), no concurrency limit will be applied.
125 Default is C<5>.
126  
127 =head2 host
128  
129 The Redfish host.
130  
131 =head2 password
132  
133 Password used for authentication by the default L</ua> (with L</username>).
134  
135 =head2 ssl
136  
137 If true, the default L</ua> will establish the connection using SSL/TLS by setting the request scheme to C<https>.
138 If false, the request scheme will be C<http>.
139 If not defined, the url scheme will not be set.
140 Default is true.
141  
142 =head2 token
143  
144 Session token to be used by the default L</ua>, overrides L</username> and L</password>.
145  
146 =head2 ua
147  
148 The instance of L<Mojo::UserAgent> used to make requests.
149 The default is an instance which subscribes to L<Mojo::UserAgent/prepare> to set authentication and ssl.
150  
151 =head2 username
152  
153 Username used for authentication by the default L</ua> (with L</password>).
154  
155 =head1 METHODS
156  
157 L<Mojo::Redfish::Client> inherits all methods from L<Mojo::Base> and implements the following new ones.
158  
159 =head2 get
160  
161 my $result = $client->get('/redfish/v1/Systems');
162  
163 Requests the requested url via the L</ua>.
164 Returns an instance of L<Mojo::Redfish::Client::Result>.
165 Dies on errors (the exact exception and behavior is subject to change).
166  
167 =head2 get_p
168  
169 Same as L</get> but returns a L<Mojo::Promise> that resolves to the result.
170  
171 =head2 root
172  
173 my $result = $client->root;
174  
175 Requests the Redfish root url (C</redfish/v1>) from the L</host> via L</get> or fetches a cached copy.
176 Caches and returns the result.
177  
178 # same as (except for the caching)
179 my $result = $client->get('/redfish/v1');
180  
181 =head2 root_p
182  
183 Same as L</root> but returns a L<Mojo::Promise> that resolves to the (possibly cached) root result.
184  
185 =head1 FUTURE WORK
186  
187 This module is still in early development.
188 Future work will include
189  
190 =over
191  
192 =item *
193  
194 Session management
195  
196 =back
197  
198  
199 =head1 SEE ALSO
200  
201 =over
202  
203 =item L<https://redfish.dmtf.org>.
204  
205 =back
206  
207 =head1 THANKS
208  
209 This module's development was sponsored by L<ServerCentral Turing Group|https://www.servercentral.com/>.
210  
211 =head1 SOURCE REPOSITORY
212  
213 L<http://github.com/jberger/Mojo-Redfish-Client>
214  
215 =head1 AUTHOR
216  
217 Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
218  
219 =head1 CONTRIBUTORS
220  
221 None yet.
222  
223 =head1 COPYRIGHT AND LICENSE
224  
225 Copyright (C) 2019 by L</AUTHOR> and L</CONTRIBUTORS>
226  
227 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
228  
229  
« Client/Result.pm ↑ Summary