File Coverage

blib/lib/Mojo/Redfish/Client.pm
Criterion Covered Total %
statement 42 44 95.4
branch 4 8 50.0
condition 2 6 33.3
subroutine 14 14 100.0
pod 4 4 100.0
total 66 76 86.8


line stmt bran cond sub pod time code
1             package Mojo::Redfish::Client;
2              
3 3     3   1213616 use Mojo::Base -base;
  3         24  
  3         19  
4              
5 3     3   462 use Carp ();
  3         7  
  3         64  
6 3     3   518 use Mojo::Collection;
  3         3291  
  3         117  
7 3     3   517 use Mojo::Promise;
  3         138926  
  3         23  
8 3     3   1472 use Mojo::Redfish::Client::Result;
  3         10  
  3         26  
9 3     3   99 use Scalar::Util ();
  3         7  
  3         2544  
10              
11             our $VERSION = '0.03';
12             $VERSION = eval $VERSION;
13              
14             has concurrency => 5;
15             has ssl => 1;
16             has [qw/host password token username/];
17              
18             has ua => sub {
19             my $self = shift;
20             my $ua = Mojo::UserAgent->new(insecure => 1);
21              
22             Scalar::Util::weaken $self;
23             $ua->on(prepare => sub {
24             my ($ua, $tx) = @_;
25             my $url = $tx->req->url;
26              
27             if (defined(my $host = $self->host)) {
28             $url->host($host);
29             }
30              
31             if (defined(my $ssl = $self->ssl)) {
32             $url->scheme($ssl ? 'https' : 'http');
33             }
34              
35             if (my $token = $self->token) {
36             $tx->req->headers->header('X-Auth-Token', $token);
37             } elsif (my $userinfo = $self->_userinfo) {
38             $url->userinfo($userinfo);
39             }
40             });
41              
42             return $ua;
43             };
44              
45             sub get {
46 4     4 1 22 my ($self, $url) = @_;
47 4         9 my $tx = $self->ua->get($url);
48 4 50       37049 if (my $err = $tx->error) { Carp::croak $err->{message} }
  0         0  
49 4         74 my $data = $tx->res->json;
50 4         993 return $self->_result($tx->res->json);
51             }
52              
53             sub get_p {
54 4     4 1 21 my ($self, $url) = @_;
55             $self->ua->get_p($url)->then(sub{
56 4     4   41080 my $tx = shift;
57 4 50       14 if (my $err = $tx->error) { Carp::croak $err->{message} }
  0         0  
58 4         74 my $data = $tx->res->json;
59 4         995 return $self->_result($tx->res->json);
60 4         9 });
61             }
62              
63             sub root {
64 1     1 1 56 my $self = shift;
65 1   33     8 return $self->{root} ||= $self->get('/redfish/v1');
66             }
67              
68             sub root_p {
69 1     1 1 61 my $self = shift;
70             return Mojo::Promise->resolve($self->{root})
71 1 50       6 if $self->{root};
72             return $self->get_p('/redfish/v1')->then(sub{
73 1     1   176 return $self->{root} = shift;
74 1         7 });
75             }
76              
77             sub _result {
78 8     8   141 my ($self, $data) = @_;
79 8         57 return Mojo::Redfish::Client::Result->new(
80             data => $data,
81             client => $self,
82             );
83             };
84              
85             sub _userinfo {
86 2     2   18 my $self = shift;
87 2         4 my ($user, $pass) = @{$self}{qw/username password/};
  2         7  
88 2 50 33     21 return undef unless $user || $pass;
89 2         14 return "$user:$pass";
90             };
91              
92             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 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 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 inherits all attributes from L and implements the following new ones.
118              
119             =head2 concurrency
120              
121             The B 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 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 (with L).
134              
135             =head2 ssl
136              
137             If true, the default L will establish the connection using SSL/TLS by setting the request scheme to C.
138             If false, the request scheme will be C.
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, overrides L and L.
145              
146             =head2 ua
147              
148             The instance of L used to make requests.
149             The default is an instance which subscribes to L to set authentication and ssl.
150              
151             =head2 username
152              
153             Username used for authentication by the default L (with L).
154              
155             =head1 METHODS
156              
157             L inherits all methods from L 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.
164             Returns an instance of L.
165             Dies on errors (the exact exception and behavior is subject to change).
166              
167             =head2 get_p
168              
169             Same as L but returns a L that resolves to the result.
170              
171             =head2 root
172              
173             my $result = $client->root;
174              
175             Requests the Redfish root url (C) from the L via L 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 but returns a L 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.
204              
205             =back
206              
207             =head1 THANKS
208              
209             This module's development was sponsored by L.
210              
211             =head1 SOURCE REPOSITORY
212              
213             L
214              
215             =head1 AUTHOR
216              
217             Joel Berger, Ejoel.a.berger@gmail.comE
218              
219             =head1 CONTRIBUTORS
220              
221             None yet.
222              
223             =head1 COPYRIGHT AND LICENSE
224              
225             Copyright (C) 2019 by L and L
226              
227             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
228              
229