File Coverage

blib/lib/BigIP/REST.pm
Criterion Covered Total %
statement 15 97 15.4
branch 0 44 0.0
condition 0 3 0.0
subroutine 5 13 38.4
pod 6 6 100.0
total 26 163 15.9


line stmt bran cond sub pod time code
1             package BigIP::REST;
2              
3 1     1   5157 use warnings;
  1         8  
  1         33  
4 1     1   5 use strict;
  1         1  
  1         19  
5              
6 1     1   5 use Carp;
  1         1  
  1         100  
7 1     1   736 use LWP::UserAgent;
  1         49958  
  1         42  
8 1     1   858 use JSON;
  1         11055  
  1         6  
9              
10             our $VERSION = '0.1';
11              
12             sub new {
13 0     0 1   my ($class, %params) = @_;
14              
15 0 0         croak "missing url parameter" unless $params{url};
16              
17 0           my $url = $params{url};
18 0           my $agent = LWP::UserAgent->new();
19              
20             $agent->timeout($params{timeout})
21 0 0         if $params{timeout};
22 0           $agent->ssl_opts(%{$params{ssl_opts}})
23 0 0 0       if $params{ssl_opts} && ref $params{ssl_opts} eq 'HASH';
24              
25 0           my $self = {
26             url => $url,
27             agent => $agent
28             };
29 0           bless $self, $class;
30              
31 0           return $self;
32             }
33              
34             sub create_session {
35 0     0 1   my ($self, %params) = @_;
36              
37 0 0         croak "missing username parameter" unless $params{username};
38 0 0         croak "missing password parameter" unless $params{password};
39              
40             my $result = $self->_post(
41             "/mgmt/shared/authn/login",
42             username => $params{username},
43             password => $params{password},
44 0           loginProviderName => 'tmos'
45             );
46              
47 0           $self->{agent}->default_header('X-F5-Auth-Token' => "$result->{token}->{token}");
48             }
49              
50             sub get_certificates {
51 0     0 1   my ($self, %params) = @_;
52              
53 0           my @parameters;
54 0 0         if ($params{partition}) {
55 0           push @parameters, '$filter=partition%20eq%20' . $params{partition};
56             }
57 0 0         if ($params{properties}) {
58 0           push @parameters, '$select=' . $params{properties};
59             }
60              
61 0           my $url = "/mgmt/tm/sys/file/ssl-cert";
62 0 0         if (@parameters) {
63 0           $url .= '/?' . join('&', @parameters);
64             }
65              
66 0           my $result = $self->_get($url);
67              
68 0           return $result;
69             }
70              
71             sub get_virtual_addresses {
72 0     0 1   my ($self, %params) = @_;
73              
74 0           my @parameters;
75 0 0         if ($params{partition}) {
76 0           push @parameters, '$filter=partition%20eq%20' . $params{partition};
77             }
78 0 0         if ($params{properties}) {
79 0           push @parameters, '$select=' . $params{properties};
80             }
81              
82 0           my $url = "/mgmt/tm/ltm/virtual-address";
83 0 0         if (@parameters) {
84 0           $url .= '/?' . join('&', @parameters);
85             }
86              
87 0           my $result = $self->_get($url);
88              
89 0           return $result;
90             }
91              
92             sub get_virtual_servers {
93 0     0 1   my ($self, %params) = @_;
94              
95 0           my @parameters;
96 0 0         if ($params{partition}) {
97 0           push @parameters, '$filter=partition%20eq%20' . $params{partition};
98             }
99 0 0         if ($params{properties}) {
100 0           push @parameters, '$select=' . $params{properties};
101             }
102 0 0         if ($params{expandSubcollections}) {
103 0           push @parameters, 'expandSubcollections=' . $params{expandSubcollections};
104             }
105              
106 0           my $url = "/mgmt/tm/ltm/virtual";
107 0 0         if (@parameters) {
108 0           $url .= '/?' . join('&', @parameters);
109             }
110              
111 0           my $result = $self->_get($url);
112              
113 0           return $result;
114             }
115              
116             sub get_pools {
117 0     0 1   my ($self, %params) = @_;
118              
119 0           my @parameters;
120 0 0         if ($params{partition}) {
121 0           push @parameters, '$filter=partition%20eq%20' . $params{partition};
122             }
123 0 0         if ($params{properties}) {
124 0           push @parameters, '$select=' . $params{properties};
125             }
126              
127 0           my $url = "/mgmt/tm/ltm/pool";
128 0 0         if (@parameters) {
129 0           $url .= '/?' . join('&', @parameters);
130             }
131              
132 0           my $result = $self->_get($url);
133              
134 0           return $result;
135             }
136              
137             sub _post {
138 0     0     my ($self, $path, %params) = @_;
139              
140 0           my $content = to_json(\%params);
141              
142             my $response = $self->{agent}->post(
143 0           $self->{url} . $path,
144             'Content-Type' => 'application/json',
145             'Content' => $content
146             );
147              
148 0           my $result = eval { from_json($response->content()) };
  0            
149              
150 0 0         if ($response->is_success()) {
151 0           return $result;
152             } else {
153 0 0         if ($result) {
154 0           croak "server error: " . $result->{message};
155             } else {
156 0           croak "communication error: " . $response->message()
157             }
158             }
159             }
160              
161             sub _get {
162 0     0     my ($self, $path, %params) = @_;
163              
164 0           my $url = URI->new($self->{url} . $path);
165 0           $url->query_form(%params);
166              
167 0           my $response = $self->{agent}->get($url);
168              
169 0           my $result = eval { from_json($response->content()) };
  0            
170              
171 0 0         if ($response->is_success()) {
172 0           return $result;
173             } else {
174 0 0         if ($result) {
175 0           croak "server error: " . $result->{message};
176             } else {
177 0           croak "communication error: " . $response->message()
178             }
179             }
180             }
181              
182             1;
183             __END__