Director.pm

filter: Statementstmt 45.8% 50 / 109 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 22.5% 7 / 31 Podpod 100% 11 / 11 totaltotal 37.9% 68 / 179 CC 20 SCAR 27.6
CC
20
Cov
38%
_error_handler
56.0
_create
30.0
_get
20.0
CRAP 15.9
↑ Summary Director/Serializer.pm »
1 package Net::Versa::Director;
2 2 $Net::Versa::Director::VERSION = '0.005000';
3 # ABSTRACT: Versa Director REST API client library
4  
5 2 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use feature qw( class signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Types::Standard qw( Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Net::Versa::Director::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 2 has 'user' => (
14 isa => Str,
15 is => 'rw',
16 );
17  
18 2 has 'passwd' => (
19 isa => Str,
20 is => 'rw',
21 );
22  
23 2 has '_refresh_token' => (
24 isa => Str,
25 is => 'rw',
26 clearer => 1,
27 );
28  
29 2 with 'Role::REST::Client';
30  
31 has '+serializer_class' => (
32 0 ▶ default => sub { 'Net::Versa::Director::Serializer' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 2 );
34  
35 2 with 'Role::REST::Client::Auth::Basic';
36  
37 0 ▶sub _is_oauth ($self) {
Subroutine _is_oauth: not called CC=1 SCAR=6.9
38 0 return $self->server =~ /:9183/;
39 }
40  
41 # has to be after "with 'Role::REST::Client::Auth::Basic';" to be called before
42 # its method modifier
43 0 ▶before '_call' => sub ($self, $method, $endpoint, $data, $args) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
44 # disable http basic auth if talking to the OAuth port
45 0 ▶ $args->{authentication} = 'none'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_oauth 0 0 0
46 if $self->_is_oauth;
47 2 };
48  
49 has '+persistent_headers' => (
50 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 return { Accept => 'application/json' };
52 },
53 2 );
54  
55 # around 'do_request' => sub($orig, $self, $method, $uri, $opts) {
56 # use DDP;
57 # warn 'request: ' . np($method) . np($uri) . np($opts);
58 # my $response = $orig->($self, $method, $uri, $opts);
59 # warn 'response: ' . np $response;
60 # return $response;
61 # };
62  
63  
64 2 class Net::Versa::Director::Exception {
65 2 ▶ use overload q{""} => 'as_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 }
67 2 class Net::Versa::Director::Exception::OAuth :isa(Net::Versa::Director::Exception) {
68 2 field $error :param :reader;
69 2 field $error_description :param :reader;
70  
71 0 ▶ method as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
72 0 return $error_description;
73 }
74 }
75 2 class Net::Versa::Director::Exception::Basic :isa(Net::Versa::Director::Exception) {
76 2 field $code :param :reader;
77 2 field $description :param :reader;
78 2 field $http_status_code :param :reader;
79 2 field $message :param :reader;
80 2 field $more_info :param :reader;
81  
82 0 ▶ method as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
83 0 return $description;
84 }
85 }
86 2 class Net::Versa::Director::Exception::VNMS :isa(Net::Versa::Director::Exception) {
87 2 field $error :param :reader;
88 2 field $exception :param :reader;
89 2 field $timestamp :param :reader;
90 2 field $http_status_code :param :reader;
91 2 field $message :param :reader;
92 2 field $path :param :reader;
93  
94 0 ▶ method as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
95 0 return $message;
96 }
97 }
98 2 class Net::Versa::Director::Exception::Other :isa(Net::Versa::Director::Exception) {
99 2 field $http_status_code :param :reader;
100 2 field $message :param :reader;
101  
102 0 ▶ method as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
103 0 return $message;
104 }
105 }
106  
107 0 ▶sub _error_handler ($self, $res) {
Subroutine _error_handler: not called CC=7 SCAR=40.3
108 0 ▶ if (ref $res->data eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res->data eq 'HASH') { } 0 0 0
109 0 ▶ if (exists $res->data->{error} && ref $res->data->{error} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $res->data->{'error'} and ref $res->data->{'error'} eq 'HASH') { } 0 0 0
elsif ($self->_is_oauth and exists $res->data->{'error_description'}) { } 0 0 0
Condition0%0/3
exists $res->data->{"error"} and ref $res->data->{"error"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $res->data->{"error"} and ref $res->data->{"error"} eq "HASH"
exists $res->data->{"error"} ref $res->data->{"error"} eq "HASH"
Truth table0%0/3
A and B
A= exists $res->data->{"error"}B= ref $res->data->{"error"} eq "HASH"
ABresult
0X0
100
111
110 0 ▶ if (exists $res->data->{error}->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $res->data->{"error"}{"code"}) 0 0 0
111 die Net::Versa::Director::Exception::Basic->new(
112 $res->data->{error}->%*,
113 0 );
114 }
115 die Net::Versa::Director::Exception::VNMS->new(
116 $res->data->{error}->%*,
117 0 );
118 }
119 ▶ elsif ($self->_is_oauth && exists $res->data->{error_description}) {
Condition0%0/3
$self->_is_oauth and exists $res->data->{"error_description"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_oauth and exists $res->data->{"error_description"}
$self->_is_oauth exists $res->data->{"error_description"}
Truth table0%0/3
A and B
A= $self->_is_oauthB= exists $res->data->{"error_description"}
ABresult
0X0
100
111
120 0 die Net::Versa::Director::Exception::OAuth->new(
121 $res->data->%*,
122 );
123 }
124  
125 0 die Net::Versa::Director::Exception::VNMS->new(
126 $res->data->%*
127 );
128 }
129 # handle undocumented API error responses
130 else {
131 0 die Net::Versa::Director::Exception::Other->new(
132 http_status_code => $res->code,
133 message => $res->response->status_line,
134 );
135 }
136 }
137  
138 0 ▶sub _create ($self, $url, $object_data, $query_params = {}, $expected_code = 201, $args = {}) {
Subroutine _create: not called CC=5 SCAR=34.0
139 0 my $params = $self->user_agent->www_form_urlencode( $query_params );
140 0 my $res = $self->post("$url?$params", $object_data, $args);
141 0 ▶ $self->_error_handler($res)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == $expected_code 0 0 0
142 unless $res->code == $expected_code;
143  
144 0 return $res->data;
145 }
146  
147 0 ▶sub _get ($self, $url, $query_params = {}, $args = {}) {
Subroutine _get: not called CC=4 SCAR=30.0
148 0 my $res = $self->get($url, $query_params, $args);
149 0 ▶ $self->_error_handler($res)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 0 0
150 unless $res->code == 200;
151  
152 0 return $res->data;
153 }
154  
155 0 ▶sub _update ($self, $url, $object, $object_data, $query_params = {}, $args = {}) {
Subroutine _update: not called CC=4 SCAR=30.0
156 0 my $updated_data = clone($object);
157 0 $updated_data = { %$updated_data, %$object_data };
158 0 my $params = $self->user_agent->www_form_urlencode( $query_params );
159 0 my $res = $self->put("$url?$params", $updated_data, $args);
160 0 ▶ $self->_error_handler($res)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 0 0
161 unless $res->code == 200;
162  
163 0 return $res->data;
164 }
165  
166 0 ▶sub _delete ($self, $url, $args = {}) {
Subroutine _delete: not called CC=3 SCAR=24.8
167 0 my $res = $self->delete($url, undef, $args);
168 0 ▶ $self->_error_handler($res)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 0 0
169 unless $res->code == 200;
170  
171 0 return 1;
172 }
173  
174  
175 0 ▶sub login ($self, $client_id, $client_secret) {
Subroutine login: not called CC=1 SCAR=6.9
Pod documented
176 0 my $login_response = $self->_create('/auth/token', {
177 client_id => $client_id,
178 client_secret => $client_secret,
179 username => $self->user,
180 password => $self->passwd,
181 grant_type => "password",
182 }, {}, 200);
183 0 my $access_token = $login_response->{access_token};
184 0 $self->set_persistent_header('Authorization', "Bearer $access_token");
185 0 $self->_refresh_token($login_response->{refresh_token});
186 0 return $login_response;
187 }
188  
189  
190 0 ▶sub logout ($self) {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
191 0 my $res = $self->_create('/auth/revoke', undef, {}, 200);
192  
193 0 $self->_clear_refresh_token;
194 0 $self->clear_persistent_headers;
195  
196 0 return $res;
197 }
198  
199  
200 0 ▶sub get_director_info ($self) {
Subroutine get_director_info: not called CC=1 SCAR=6.9
Pod documented
201 return $self->_get('/api/operational/system/package-info')
202 0 ->{'package-info'}->[0];
203 }
204  
205  
206 0 ▶sub get_version ($self) {
Subroutine get_version: not called CC=1 SCAR=6.9
Pod documented
207 0 return $self->get_director_info->{branch};
208 }
209  
210  
211 0 ▶sub list_appliances ($self) {
Subroutine list_appliances: not called CC=1 SCAR=6.9
Pod documented
212 return $self->_get('/vnms/appliance/appliance', { offset => 0, limit => 2048 })
213 0 ->{'versanms.ApplianceStatusResult'}->{appliances};
214 }
215  
216  
217 0 ▶sub list_device_workflows ($self) {
Subroutine list_device_workflows: not called CC=1 SCAR=6.9
Pod documented
218 return $self->_get('/vnms/sdwan/workflow/devices', { offset => 0, limit => 2048 })
219 0 ->{'versanms.sdwan-device-list'};
220 }
221  
222  
223 0 ▶sub get_device_workflow ($self, $device_workflow_name ) {
Subroutine get_device_workflow: not called CC=1 SCAR=6.9
Pod documented
224 return $self->_get("/vnms/sdwan/workflow/devices/device/$device_workflow_name")
225 0 ->{'versanms.sdwan-device-workflow'};
226 }
227  
228  
229 0 ▶sub list_assets ($self) {
Subroutine list_assets: not called CC=1 SCAR=6.9
Pod documented
230 return $self->_get('/vnms/assets/asset', { offset => 0, limit => 2048 })
231 0 ->{'versanms.AssetsResult'}->{assets};
232 }
233  
234  
235 0 ▶sub list_device_interfaces ($self, $devicename) {
Subroutine list_device_interfaces: not called CC=1 SCAR=6.9
Pod documented
236 return $self->_get("/api/config/devices/device/$devicename/config/interfaces?deep")
237 0 ->{interfaces};
238 }
239  
240  
241 0 ▶sub list_device_networks ($self, $devicename) {
Subroutine list_device_networks: not called CC=1 SCAR=6.9
Pod documented
242 return $self->_get("/api/config/devices/device/$devicename/config/networks/network")
243 0 ->{network};
244 }
245  
246  
247 0 ▶sub get_device_configuration ($self, $devicename) {
Subroutine get_device_configuration: not called CC=1 SCAR=6.9
Pod documented
248 0 $self->set_header(Accept => 'text/plain');
249 0 return $self->_get("/vnms/appliance/export",
250 {
251 applianceName => $devicename,
252 }, {
253 deserializer => undef,
254 });
255 }
256  
257  
258 2 1;
259  
260 __END__
↑ Summary Director/Serializer.pm »