blib/lib/Lemonldap/NG/Common/Conf/Backends/REST.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 135 CC 11 SCAR 24.2
CC
11
Cov
0%
ua
30.0
prereq
12.0
getJson
12.0
CRAP 11.2
1 package Lemonldap::NG::Common::Conf::Backends::REST;
2  
3 use strict;
4 use Lemonldap::NG::Common::UserAgent;
5 use JSON qw(from_json to_json);
6  
7 our $VERSION = '2.18.0';
8  
9 #parameter baseUrl, user, password, realm, lwpOpts
10  
11 BEGIN {
12 *Lemonldap::NG::Common::Conf::getJson = \&getJson;
13 *Lemonldap::NG::Common::Conf::ua = \&ua;
14 *Lemonldap::NG::Common::Conf::base = \&base;
15 }
16  
17 sub prereq {
18 my $self = shift;
19 unless ( $self->{baseUrl} ) {
20 $Lemonldap::NG::Common::Conf::msg .=
21 "url parameter is required in REST configuration type \n";
22 return 0;
23 }
24 if ( $self->{user} and not $self->{realm} ) {
25 $Lemonldap::NG::Common::Conf::msg .=
26 "realm is required when user/password are set\n";
27 return 0;
28 }
29 1;
30 }
31  
32 sub ua {
33 my ($self) = @_;
34 return $self->{ua} if ( $self->{ua} );
35 my $ua = Lemonldap::NG::Common::UserAgent->new(
36 { lwpOpts => $self->{lwpOpts}, lwpSslOpts => $self->{lwpSslOpts} } );
37 if ( $self->{user} ) {
38 my $url = $self->{baseUrl};
39 my $port = ( $url =~ /^https/ ? 443 : 80 );
40 $url =~ s#https?://([^/]*).*$#$1#;
41 if ( $url =~ s/:(\d+)$// ) {
42 $port = $1;
43 }
44 $ua->credentials( "$url:$port", $self->{realm},
45 $self->{user}, $self->{password} );
46 }
47 return $self->{ua} = $ua;
48 }
49  
50 sub getJson {
51 my $self = shift;
52 my $url = shift;
53 my $resp = $self->ua->get( $self->base . $url, @_ );
54 if ( $resp->is_success ) {
55 my $res;
56 eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
57 if ($@) {
58 $Lemonldap::NG::Common::Conf::msg .= "Request failed: $@\n";
59 return undef;
60 }
61 return $res;
62 }
63 else {
64 $Lemonldap::NG::Common::Conf::msg .=
65 "Request failed: status code " . $resp->status_line;
66 return undef;
67 }
68 }
69  
70 sub base {
71 my ($self) = @_;
72 $self->{baseUrl} =~ s#/*$#/#;
73 return $self->{baseUrl};
74 }
75  
76 sub available {
77  
78 # TODO
79 print STDERR 'Not implemented for now';
80 return undef;
81 }
82  
83 sub lastCfg {
84 my $self = shift;
85 my $res = $self->getJson('latest') or return;
86 return $res->{cfgNum};
87 }
88  
89 # lock and unlock must not be requested by the SOAP client, since
90 # they will be done by the SOAP server when storing the config
91 sub lock {
92 return 1;
93 }
94  
95 sub unlock {
96 return 1;
97 }
98  
99 sub isLocked {
100 return 1;
101 }
102  
103 sub store {
104  
105 # TODO
106 print STDERR 'Not implemented for now';
107 return undef;
108 my ( $self, $conf ) = @_;
109 my $req = HTTP::Request->new( POST => $self->base );
110 $req->content( to_json($conf) );
111 $req->header( 'Content-Type' => 'application/json' );
112 my $resp = $self->ua->request($req);
113  
114 if ( $resp->is_success ) {
115 my $res;
116 eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
117 if ($@) {
118 $Lemonldap::NG::Common::Conf::msg .= "Unknown error: $@";
119 return undef;
120 }
121 return $res->{cfgNum};
122 }
123 $Lemonldap::NG::Common::Conf::msg .= 'Unknown error: ' . $resp->status_line;
124 return undef;
125 return $self->_soapCall( 'store', @_ );
126 }
127  
128 sub load {
129 my ( $self, $cfgNum ) = @_;
130 my $res = $self->getJson("$cfgNum?full=1") or return;
131 return $res;
132 }
133  
134 1;