| 1 | package Zenoss::Router; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use JSON qw{}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use HTTP::Request::Common qw{POST}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Moose::Util qw {apply_all_roles}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Zenoss::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | with 'Zenoss::Error'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | #************************************************************************** | ||||||||||||||||||||||||||||
| 15 | # Constants | ||||||||||||||||||||||||||||
| 16 | #************************************************************************** | ||||||||||||||||||||||||||||
| 17 | 1 | our $JSON_CONTENT_TYPE; *JSON_CONTENT_TYPE = \q{application/json; charset=utf-8}; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #************************************************************************** | ||||||||||||||||||||||||||||
| 20 | # Public Attributes | ||||||||||||||||||||||||||||
| 21 | #************************************************************************** | ||||||||||||||||||||||||||||
| 22 | 1 | has 'connector' => ( | |||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||
| 24 | isa => 'Zenoss::Connector', | ||||||||||||||||||||||||||||
| 25 | required => 1, | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | #************************************************************************** | ||||||||||||||||||||||||||||
| 29 | # Private Attributes | ||||||||||||||||||||||||||||
| 30 | #************************************************************************** | ||||||||||||||||||||||||||||
| 31 | 1 | has '_agent' => ( | |||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||
| 33 | isa => 'LWP::UserAgent', | ||||||||||||||||||||||||||||
| 34 | builder => '_build_agent', | ||||||||||||||||||||||||||||
| 35 | lazy => 1, | ||||||||||||||||||||||||||||
| 36 | init_arg => undef, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | has '_is_authenticated' => ( | |||||||||||||||||||||||||||
| 40 | is => 'rw', | ||||||||||||||||||||||||||||
| 41 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 42 | default => 0, | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | has '_transaction_count' => ( | |||||||||||||||||||||||||||
| 46 | traits => ['Counter'], | ||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | isa => 'Num', | ||||||||||||||||||||||||||||
| 49 | default => 0, | ||||||||||||||||||||||||||||
| 50 | handles => { | ||||||||||||||||||||||||||||
| 51 | _increase_transaction_count => 'inc', | ||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #************************************************************************** | ||||||||||||||||||||||||||||
| 56 | # Private methods | ||||||||||||||||||||||||||||
| 57 | #************************************************************************** | ||||||||||||||||||||||||||||
| 58 | #====================================================================== | ||||||||||||||||||||||||||||
| 59 | # BUILD | ||||||||||||||||||||||||||||
| 60 | #====================================================================== | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # API Router location | ||||||||||||||||||||||||||||
| 65 | 0 | (my $search_path = __PACKAGE__) =~ s!::!/!g; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Search for available API routers | ||||||||||||||||||||||||||||
| 68 | 0 | my %router; | |||||||||||||||||||||||||||
| 69 | 0 | my @INC_LOCAL = @INC; | |||||||||||||||||||||||||||
| 70 | 0 | foreach my $search_dir (@INC_LOCAL) { | |||||||||||||||||||||||||||
| 71 | 0 | $search_dir .= q{/} . $search_path; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | next if (!-d $search_dir); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | File::Find::find( | ||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | no_chdir => 1, | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | wanted => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return unless $File::Find::name =~ /\.pm$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | (my $path = $File::Find::name) =~ s!^\\./!!; | |||||||||||||||||||||||||||
| 80 | 0 | my ($filename,undef, $suffix) = fileparse($path, '.pm'); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Storing in a hash will allow for de-duplication | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $router{$filename} = 1 unless $filename eq 'Tree'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||
| 85 | }, | ||||||||||||||||||||||||||||
| 86 | 0 | $search_dir, | |||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Build the FQ Router API list | ||||||||||||||||||||||||||||
| 91 | 0 | my @router_install; | |||||||||||||||||||||||||||
| 92 | 0 | foreach (keys %router) { | |||||||||||||||||||||||||||
| 93 | 0 | push(@router_install, "Zenoss::Router::$_"); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Install role via META | ||||||||||||||||||||||||||||
| 97 | 0 | apply_all_roles($self, @router_install); | |||||||||||||||||||||||||||
| 98 | } # END BUILD | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | #====================================================================== | ||||||||||||||||||||||||||||
| 101 | # _router_request | ||||||||||||||||||||||||||||
| 102 | #====================================================================== | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub _router_request { | ||||||||||||||||||||||||||
|
Subroutine
_router_request: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 104 | 0 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 105 | 0 | my $zenoss_url = $self->connector->endpoint; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Login if not already logged in | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (!$self->_is_authenticated) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $self->_process_login; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Increase the transaction counter | ||||||||||||||||||||||||||||
| 113 | 0 | $self->_increase_transaction_count; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Build JSON request in HASHREF | ||||||||||||||||||||||||||||
| 116 | my $JSON_DATA = { | ||||||||||||||||||||||||||||
| 117 | action => $args->{'action'}, | ||||||||||||||||||||||||||||
| 118 | method => $args->{'method'}, | ||||||||||||||||||||||||||||
| 119 | 0 | data => $args->{'data'}, | |||||||||||||||||||||||||||
| 120 | type => 'rpc', | ||||||||||||||||||||||||||||
| 121 | tid => $self->_transaction_count, | ||||||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Setup query | ||||||||||||||||||||||||||||
| 125 | 0 | my $query = HTTP::Request->new(POST => "$zenoss_url/$args->{'location'}"); | |||||||||||||||||||||||||||
| 126 | 0 | my $json_encoder = JSON->new->allow_nonref; | |||||||||||||||||||||||||||
| 127 | 0 | $query->content_type($JSON_CONTENT_TYPE); | |||||||||||||||||||||||||||
| 128 | 0 | $query->content($json_encoder->encode($JSON_DATA)); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Return Zenoss::Response object | ||||||||||||||||||||||||||||
| 131 | 0 | my (undef, undef, undef, $hints, undef, undef) = caller(1); | |||||||||||||||||||||||||||
| 132 | 0 | return Zenoss::Response->new( | |||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | handler => $self->_agent->request($query), | ||||||||||||||||||||||||||||
| 135 | sent_tid => $self->_transaction_count, | ||||||||||||||||||||||||||||
| 136 | _caller => $hints, | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | )->_validate_api_method_exists(); | ||||||||||||||||||||||||||||
| 139 | } # END _router_request | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | #====================================================================== | ||||||||||||||||||||||||||||
| 142 | # _check_args | ||||||||||||||||||||||||||||
| 143 | #====================================================================== | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub _check_args { | ||||||||||||||||||||||||||
|
Subroutine
_check_args: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 145 | 0 | my ($self, $args, $definition) = @_; | |||||||||||||||||||||||||||
| 146 | 0 | my (undef, undef, undef, $hints, undef, undef) = caller(1); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Methods require HASH as argument | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | unless (!defined($args) or ref($args) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $args or ref $args eq "HASH"
MC/DC0%0/2
not defined $args or ref $args eq "HASH"
not defined $args
ref $args eq "HASH"
Truth table0%0/3
not(A) or B
A= not defined $argsB= ref $args eq "HASH"
| |||||||||||||||||||||||||||||
| 150 | 0 | $self->_croak("[$hints] requires [HASH] as an argument"); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Check to see if $definition provides any defaults | ||||||||||||||||||||||||||||
| 154 | # If so and we dont have the key set, merge the default in | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (exists($definition->{'defaults'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | foreach my $param (keys %{$definition->{'defaults'}}) { | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | if (!exists($args->{$param})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | $args->{$param} = $definition->{'defaults'}{$param}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # Check for required parameters | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if (exists($definition->{'required'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | foreach my $requirement (@{$definition->{'required'}}) { | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | if (!grep { $_ eq $requirement } keys %{$args}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | $self->_croak("[$hints] requires [$requirement] as a provided parameter"); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # NOTE, this method doesnt need to return specific as modifications | ||||||||||||||||||||||||||||
| 173 | # to $args are done via reference. $args is a pointer to the original | ||||||||||||||||||||||||||||
| 174 | # memory space, thus not its own unique entity. | ||||||||||||||||||||||||||||
| 175 | 0 | return $self; | |||||||||||||||||||||||||||
| 176 | } # END _check_args | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | #====================================================================== | ||||||||||||||||||||||||||||
| 179 | # _build_agent | ||||||||||||||||||||||||||||
| 180 | #====================================================================== | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub _build_agent { | ||||||||||||||||||||||||||
|
Subroutine
_build_agent: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 182 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # Setup the User Agent | ||||||||||||||||||||||||||||
| 185 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Enable cookies | ||||||||||||||||||||||||||||
| 188 | 0 | $ua->cookie_jar({}); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Timeout | ||||||||||||||||||||||||||||
| 191 | 0 | $ua->timeout($self->connector->timeout); | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | return $ua; | |||||||||||||||||||||||||||
| 194 | } # END _build_agent | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | #====================================================================== | ||||||||||||||||||||||||||||
| 197 | # _process_login | ||||||||||||||||||||||||||||
| 198 | #====================================================================== | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub _process_login { | ||||||||||||||||||||||||||
|
Subroutine
_process_login: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 200 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 201 | 0 | my $zenoss_url = $self->connector->endpoint; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Format the LOGIN request | ||||||||||||||||||||||||||||
| 204 | # application/x-www-form-urlencoded | ||||||||||||||||||||||||||||
| 205 | 0 | my $login_request = POST "$zenoss_url/zport/acl_users/cookieAuthHelper/login", | |||||||||||||||||||||||||||
| 206 | [ | ||||||||||||||||||||||||||||
| 207 | __ac_name => $self->connector->username, | ||||||||||||||||||||||||||||
| 208 | __ac_password => $self->connector->password, | ||||||||||||||||||||||||||||
| 209 | submitted => 'true', | ||||||||||||||||||||||||||||
| 210 | came_from => "$zenoss_url/zport/dmd", | ||||||||||||||||||||||||||||
| 211 | ]; | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # Login | ||||||||||||||||||||||||||||
| 214 | 0 | $self->_agent->request($login_request); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # Setup the test query | ||||||||||||||||||||||||||||
| 217 | 0 | my $query = HTTP::Request->new(POST => "$zenoss_url/zport/dmd/"); | |||||||||||||||||||||||||||
| 218 | 0 | $query->content_type('application/json; charset=utf-8'); | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # Process the test query | ||||||||||||||||||||||||||||
| 221 | 0 | my $response = $self->_agent->request($query); | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | if ($response->is_success && $response->code == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->is_success and $response->code == 200
MC/DC0%0/2
$response->is_success and $response->code == 200
$response->is_success
$response->code == 200
Truth table0%0/3
A and B
A= $response->is_successB= $response->code == 200
| |||||||||||||||||||||||||||||
| 223 | # Set internal attribute confirming authentication | ||||||||||||||||||||||||||||
| 224 | 0 | $self->_is_authenticated(1); | |||||||||||||||||||||||||||
| 225 | } else { | ||||||||||||||||||||||||||||
| 226 | # Bomb out | ||||||||||||||||||||||||||||
| 227 | 0 | $self->_croak("Unable to login to Zenoss with provided credentials"); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | } # END _process_login | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | #************************************************************************** | ||||||||||||||||||||||||||||
| 232 | # Package end | ||||||||||||||||||||||||||||
| 233 | #************************************************************************** | ||||||||||||||||||||||||||||
| 234 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 235 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 1 | 1; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | __END__ | ||||||||||||||||||||||||||||