File Coverage

blib/lib/Mojo/Redfish/Client.pm
Criterion Covered Total %
statement 29 30 96.6
branch 2 4 50.0
condition 2 6 33.3
subroutine 9 9 100.0
pod 2 2 100.0
total 44 51 86.2


line stmt bran cond sub pod time code
1             package Mojo::Redfish::Client;
2              
3 2     2   635062 use Mojo::Base -base;
  2         12  
  2         11  
4              
5 2     2   313 use Carp ();
  2         4  
  2         30  
6 2     2   425 use Mojo::Collection;
  2         3001  
  2         223  
7 2     2   810 use Mojo::Redfish::Client::Result;
  2         7  
  2         17  
8 2     2   69 use Scalar::Util ();
  2         2  
  2         1000  
9              
10             our $VERSION = '0.02';
11             $VERSION = eval $VERSION;
12              
13             has ssl => 1;
14             has [qw/host password token username/];
15              
16             has ua => sub {
17             my $self = shift;
18             my $ua = Mojo::UserAgent->new(insecure => 1);
19              
20             Scalar::Util::weaken $self;
21             $ua->on(prepare => sub {
22             my ($ua, $tx) = @_;
23             my $url = $tx->req->url;
24              
25             if (defined(my $host = $self->host)) {
26             $url->host($host);
27             }
28              
29             if (defined(my $ssl = $self->ssl)) {
30             $url->scheme($ssl ? 'https' : 'http');
31             }
32              
33             if (my $token = $self->token) {
34             $tx->req->headers->header('X-Auth-Token', $token);
35             } elsif (my $userinfo = $self->_userinfo) {
36             $url->userinfo($userinfo);
37             }
38             });
39              
40             return $ua;
41             };
42              
43             sub get {
44 4     4 1 22 my ($self, $url) = @_;
45 4         13 my $tx = $self->ua->get($url);
46 4 50       33251 if (my $err = $tx->error) { Carp::croak $err->{message} }
  0         0  
47 4         64 my $data = $tx->res->json;
48 4         985 return $self->_result($tx->res->json);
49             }
50              
51             sub root {
52 1     1 1 46 my $self = shift;
53 1   33     6 return $self->{root} ||= $self->get('/redfish/v1');
54             }
55              
56             sub _result {
57 4     4   66 my ($self, $data) = @_;
58 4         22 return Mojo::Redfish::Client::Result->new(
59             data => $data,
60             client => $self,
61             );
62             };
63              
64             sub _userinfo {
65 1     1   7 my $self = shift;
66 1         2 my ($user, $pass) = @{$self}{qw/username password/};
  1         4  
67 1 50 33     4 return undef unless $user || $pass;
68 1         4 return "$user:$pass";
69             };
70              
71             1;
72              
73             =head1 NAME
74              
75             Mojo::Redfish::Client - A Redfish client with a Mojo flair
76              
77             =head1 SYNOPSIS
78              
79             my $client = Mojo::Redfish::Client->new(host => '192.168.0.1');
80             my $system = $client->root->get('/Systems')->get('/Members')->first;
81             my $name = $system->value('/Name');
82             say "Name: $name";
83              
84             =head1 DESCRIPTION
85              
86             L is a modern standards-based system for querying computer systems for information.
87             It replaces the existing IPMI "standard", such as it was, both in standardization and in using JSON over HTTP rather than binary protocols.
88              
89             L is, as the name suggests, a client for Redfish.
90             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.
91              
92             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.
93              
94             =head1 ATTRIBUTES
95              
96             L inherits all attributes from L and implements the following new ones.
97              
98             =head2 host
99              
100             The Redfish host.
101              
102             =head2 password
103              
104             Password used for authentication by the default L (with L).
105              
106             =head2 ssl
107              
108             If true, the default L will establish the connection using SSL/TLS by setting the request scheme to C.
109             If false, the request scheme will be C.
110             If not defined, the url scheme will not be set.
111             Default is true.
112              
113             =head2 token
114              
115             Session token to be used by the default L, overrides L and L.
116              
117             =head2 ua
118              
119             The instance of L used to make requests.
120             The default is an instance which subscribes to L to set authentication and ssl.
121              
122             =head2 username
123              
124             Username used for authentication by the default L (with L).
125              
126             =head1 METHODS
127              
128             L inherits all methods from L and implements the following new ones.
129              
130             =head2 get
131              
132             my $result = $client->get('/redfish/v1/Systems');
133              
134             Requests the requested url via the L.
135             Returns an instance of L.
136             Dies on errors (the exact exception and behavior is subject to change).
137              
138             =head2 root
139              
140             my $result = $client->root;
141              
142             Requests the Redfish root url (C) from the L via L or fetches a cached copy.
143             Caches and returns the result.
144              
145             # same as (except for the caching)
146             my $result = $client->get('/redfish/v1');
147              
148             =head1 FUTURE WORK
149              
150             This module is still in early development.
151             Future work will include
152              
153             =over
154              
155             =item *
156              
157             Non-blocking (promise-based) api
158              
159             =item *
160              
161             Session management
162              
163             =item *
164              
165             Even more testing
166              
167             =back
168              
169              
170             =head1 SEE ALSO
171              
172             =over
173              
174             =item L.
175              
176             =back
177              
178             =head1 THANKS
179              
180             This module's development was sponsored by L.
181              
182             =head1 SOURCE REPOSITORY
183              
184             L
185              
186             =head1 AUTHOR
187              
188             Joel Berger, Ejoel.a.berger@gmail.comE
189              
190             =head1 CONTRIBUTORS
191              
192             None yet.
193              
194             =head1 COPYRIGHT AND LICENSE
195              
196             Copyright (C) 2019 by L and L
197              
198             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
199              
200