| 1 | 6 | ▶ | use 5.12.1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | package Opsview::RestAPI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | $Opsview::RestAPI::VERSION = '1.210260'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Interact with the Opsview Rest API interface | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Data::Dump qw(pp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use REST::Client; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use URI::Encode::XS qw(uri_encode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Opsview::RestAPI::Exception; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 6 | ▶ | my ( $class, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | my $self = bless {%args}, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | $self->{url} ||= 'http://localhost'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"url"} ||= "http://localhost"
MC/DC0%0/1
$self->{"url"} ||= "http://localhost"
$self->{"url"}
Truth table50%1/2
A
A= $self->{"url"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | ▶ | $self->{ssl_verify_hostname} = defined $args{ssl_verify_hostname} ? $args{ssl_verify_hostname} : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | $self->{username} ||= 'admin'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"username"} ||= "admin"
MC/DC0%0/1
$self->{"username"} ||= "admin"
$self->{"username"}
Truth table50%1/2
A
A= $self->{"username"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | ▶ | $self->{password} ||= 'initial'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"password"} ||= "initial"
MC/DC0%0/1
$self->{"password"} ||= "initial"
$self->{"password"}
Truth table50%1/2
A
A= $self->{"password"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | $self->{debug} //= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"debug"} //= 0
MC/DC0%0/1
$self->{"debug"} //= 0
$self->{"debug"}
Truth table50%1/2
A
A= $self->{"debug"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # Create the conenction here to info can be called before logging in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | $self->{json} = JSON->new->allow_nonref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 6 | $self->{client} = REST::Client->new(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | $self->_client->setHost( $self->{url} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | $self->_client->addHeader( 'Content-Type', 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # Set the SSL options for use with https connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | $self->_client->getUseragent->ssl_opts( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 6 | verify_hostname => $self->{ssl_verify_hostname} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Make sure we follow any redirects if originally given | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # http but get redirected to https | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | $self->_client->setFollow(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # and make sure POST will also redirect correctly (doesn't by default) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | push @{ $self->_client->getUseragent->requests_redirectable }, 'POST'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # internal convenience functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 56 | ▶ | sub _client { return $_[0]->{client} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_client: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | sub _json { return $_[0]->{json} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | sub _log { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 22 | ▶ | my ( $self, $level, @message ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 22 | ▶ | say scalar(localtime), ': ', @message if ( $level <= $self->{debug} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 22 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | sub _dump { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ( $self, $level, $object ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dump: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | say scalar(localtime), ': ', pp($object) if ( $level <= $self->{debug} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 10 | ▶ | sub url { return $_[0]->{url} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | sub username { return $_[0]->{username} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
username: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | ▶ | sub password { return $_[0]->{password} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
password: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | sub _parse_response_to_json { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | ▶ | my($self, $code, $response) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_response_to_json: called CC=3 SCAR=13.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | $self->_log( 3, "Raw response: ", $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 5 | my $json_result = eval { $self->_json->decode($response); }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | my %call_info = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | type => $self->{type}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | url => $self->url, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | http_code => $code, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | response => $response, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | ▶ | if (my $error = $@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 5 | my %exception = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | eval_error => $error, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | message => "Failed to read JSON in response from server ($response)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 5 | croak( Opsview::RestAPI::Exception->new(%call_info, %exception) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $self->_log( 2, "result: ", pp($json_result) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ( $json_result->{message}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | croak( Opsview::RestAPI::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | %call_info, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | response => $response, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | message => $json_result->{message}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | )); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | return $json_result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | sub _generate_url { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 8 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_url: called CC=8 SCAR=23.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 8 | $args{api} =~ s!^/rest/!!; # tidy any 'ref' URL we may have been given | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | my $url = "/rest" . ( $args{api} ? '/' . $args{api} : '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 8 | my @param_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 8 | for my $param ( sort keys( %{ $args{params} } ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | ▶ | if ( ! defined $args{params}{$param} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | croak( Opsview::RestAPI::Exception->new( message => "Parameter '$param' is not valid" ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } elsif ( ! ref($args{params}{$param}) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | push(@param_list, $param . '=' . uri_encode( $args{params}{$param} ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } elsif (ref($args{params}{$param}) eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | for my $arg ( @{ $args{params}{$param} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | push(@param_list, $param . '=' . uri_encode( $arg ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | croak( Opsview::RestAPI::Exception->new( message => "Parameter '$param' is not an accepted type: " . ref( $args{params}{$param} ) ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 8 | my $params = join( '&', @param_list); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | ▶ | $url .= '?' . $params if $params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | return $url; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | sub _query { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 5 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_query: called CC=10 SCAR=28.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | croak "Unknown type '$args{type}'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 5 | ▶ | if ( $args{type} !~ m/^(GET|POST|PUT|DELETE)$/ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | croak( Opsview::RestAPI::Exception->new( message => "Not logged in" ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | unless ( $self->{token} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | || !defined( $args{api} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | || !$args{api} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 5 | ▶ | || $args{api} =~ m/login/ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$self->{"token"} or not defined $args{"api"} or not $args{"api"} or $args{"api"} =~ /login/u
$self->{"token"} or not defined $args{"api"} or not $args{"api"}
$self->{"token"} or not defined $args{"api"}
MC/DC0%0/4
$self->{"token"} or not defined $args{"api"} or not $args{"api"} or $args{"api"} =~ /login/u
$self->{"token"}
not defined $args{"api"}
not $args{"api"}
$args{"api"} =~ /login/u
Truth table20%1/5
A or not(B) or not(C) or D
A= $self->{"token"}B= not defined $args{"api"}C= not $args{"api"}D= $args{"api"} =~ /login/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 5 | $self->{type} = $args{type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 5 | my $url = $self->_generate_url( %args ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 5 | ▶ | my $data = $args{data} ? $self->_json->encode( $args{data} ) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 5 | $self->_log( 2, "TYPE: $self->{type} URL: $url DATA: ", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | pp($data) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 5 | my $type = $self->{type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 5 | my $deadlock_attempts = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | DEADLOCK: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 5 | $self->_client->$type( $url, $data ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 5 | ▶ | if ( $self->_client->responseCode ne 200 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 5 | $self->_log( 2, "Non-200 response - checking for deadlock" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 5 | ▶ | if ( $self->_client->responseContent =~ m/deadlock/i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->_client->responseContent =~ /deadlock/iu and $deadlock_attempts < 5
MC/DC0%0/2
$self->_client->responseContent =~ /deadlock/iu and $deadlock_attempts < 5
$self->_client->responseContent =~ /deadlock/iu
$deadlock_attempts < 5
Truth table33%1/3
A and B
A= $self->_client->responseContent =~ /deadlock/iuB= $deadlock_attempts < 5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | && $deadlock_attempts < 5 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | $deadlock_attempts++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | $self->_log( 1, "Encountered deadlock: ", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | $self->_client->responseContent()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | $self->_log( 1, "Retrying (count: $deadlock_attempts)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | sleep 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | redo DEADLOCK; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | return $self->_parse_response_to_json( $self->_client->responseCode, $self->_client->responseContent() ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | sub login { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
login: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # make sure we are communicating with at least Opsview v4.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my $api_version = $self->api_version; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if ( $api_version->{api_version} < 4.0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | croak( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | Opsview::RestPI::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | message => $self->{url} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | . " is running Opsview version " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | . $api_version->{api_version} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | . ". Need at least version 4.0", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | http_code => 505, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $self->_log( 2, "About to login" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | if ( $self->{token} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $self->_log( 1, "Already have token $self->{token}" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | my $result = eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | $self->post( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | api => "login", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | params => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | username => $self->{username}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | password => $self->{password}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | my $e = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $self->_log( 2, "Exception object:" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | $self->_dump( 2, $e ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | die $e->message, $/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $self->{token} = $result->{token}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | $self->_client->addHeader( 'X-Opsview-Username', $self->{username} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | $self->_client->addHeader( 'X-Opsview-Token', $result->{token} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | $self->opsview_info(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | $self->_log( 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | "Successfully logged in to '$self->{url}' as '$self->{username}'" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | sub api_version { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_version: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 5 | ▶ | if ( !$self->{api_version} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 5 | $self->_log( 2, "Fetching api_version information" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 5 | $self->{api_version} = $self->get( api => '' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | return $self->{api_version}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | sub opsview_info { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opsview_info: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if ( !$self->{opsview_info} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | $self->_log( 2, "Fetching opsview_info information" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | $self->{opsview_info} = $self->get( api => 'info' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | return $self->{opsview_info}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | sub opsview_version { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opsview_version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | return qv( $self->opsview_info->{opsview_version} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | sub opsview_build { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opsview_build: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | $self->opsview_info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | return $self->{opsview_info}->{opsview_build}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | sub interval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | my ( $self, $interval ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
interval: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # if this is a 4.6 system, adjust the interval to be minutes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | if ( $self->{api_version}->{api_version} < 5.0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $interval = int( $interval / 60 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | $interval += 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | return $interval; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | sub post { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | return $self->_query( %args, type => 'POST' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | sub get { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 5 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=5 SCAR=29.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 5 | ▶ | if ( $args{batch_size} && $args{params}{rows} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args{"batch_size"} and $args{"params"}{"rows"}
MC/DC0%0/2
$args{"batch_size"} and $args{"params"}{"rows"}
$args{"batch_size"}
$args{"params"}{"rows"}
Truth table33%1/3
A and B
A= $args{"batch_size"}B= $args{"params"}{"rows"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | croak( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | Opsview::RestAPI::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | message => "Cannot specify both 'batch_size' and 'rows'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 5 | ▶ | if ( $args{batch_size} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my @data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | my %hash = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | list => \@data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | summary => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | allrows => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | rows => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # fetch just summary information for what we are after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | my %get_args = %args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | $get_args{params}{rows} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | delete( $get_args{batch_size} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | $self->_log( 1, "batch_size request: fetching summary data only" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | my $summary = $self->_query( %get_args, type => 'GET' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # This is reassembled to make it look like everything was fetched in one go | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | $hash{summary}{allrows} = $summary->{summary}->{allrows}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $hash{summary}{rows} = $summary->{summary}->{allrows}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | my $totalpages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | = int( $summary->{summary}->{allrows} / $args{batch_size} ) + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | $self->_log( 2, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | "Fetching $hash{summary}{allrows} rows in batches of $args{batch_size}, $totalpages pages to fetch" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | my $start_time = time(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # now start fetching the data in batch_size increments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | my $page = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | $get_args{params}{rows} = $args{batch_size}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | while ( $page++ < $totalpages ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | $get_args{params}{page} = $page; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | $self->_log( 3, "About to fetch page $page" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | my $result = $self->_query( %get_args, type => 'GET' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | push( @data, @{ $result->{list} } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my $elapsed_time = time() - $start_time; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | $self->_log( 2, "Fetch completed in ${elapsed_time}s" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | return \%hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 5 | return $self->_query( %args, type => 'GET' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | sub put { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | return $self->_query( %args, type => 'PUT' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | sub delete { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | return $self->_query( %args, type => 'DELETE' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub reload { return $_[0]->post( api => 'reload' ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reload: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | sub reload_pending { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | my $result = $_[0]->get( api => 'reload' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reload_pending: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | ▶ | if(! defined $result->{configuration_status}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | croak( Opsview::RestAPI::Exception->new(message => "'configuration_status' not found", result => $result ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | ▶ | return $result->{configuration_status} eq 'pending' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # NOTE: use LWP::UserAgent directly to make use of its file upload functionality | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # as REST::Client doesn't allow it to work as expected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | sub file_upload { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file_upload: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | my $ua = $self->_client->getUseragent(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | $ua->default_header( 'Content-Type', 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | $ua->default_header( 'X-Opsview-Username', $self->{username} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | $ua->default_header( 'X-Opsview-Token', $self->{token} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | my $url = $self->{url}."/rest/$args{api}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | ▶ | $url .= '/upload' unless $url =~ m!/upload$!; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | #warn "url=$url"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | my $response = $ua->post( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | $url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | Accept => "text/html", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | Content_Type => 'form-data', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | Content => [ filename => [ $args{local_file} => $args{remote_file} ] ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | return $self->_parse_response_to_json( $response->code, $response->content ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | sub logout { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
logout: called CC=2 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 1 | $self->_log( 2, "In logout" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 1 | ▶ | return unless ( $self->{token} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | $self->_log( 2, "found token, on to logout" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | $self->post( api => 'logout' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | $self->_log( 1, "Successfully logged out from $self->{url}" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | # invalidate all the info held internally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | $self->{token} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | $self->{api_version} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | $self->{opsview_info} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | $self->_log( 2, "Token removed" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | # Copied from Opsview::Utils so that module does not need to be installed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | sub remove_keys_from_hash { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | my ( $class, $hash, $allowed_keys, $do_not_die_on_non_hash ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_keys_from_hash: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | if ( ref $hash ne "HASH" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # Double negative as default is to die | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | unless ($do_not_die_on_non_hash) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | die "Not a HASH: $hash"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | return $hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # We cache the keys_list into | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | if ( !defined $allowed_keys ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | die "Must specify $allowed_keys"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # OK | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | elsif ( ref $allowed_keys eq "HASH" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | elsif ( ref $allowed_keys eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | my @temp = @$allowed_keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | $allowed_keys = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | map { $allowed_keys->{$_} = 1 } @temp; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | elsif ( ref $allowed_keys ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | $allowed_keys = { $allowed_keys => 1 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | die "allowed_keys incorrect"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | foreach my $k ( keys %$hash ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | ▶ | if ( exists $allowed_keys->{$k} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | delete $hash->{$k}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | elsif ( ref $hash->{$k} eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | my @new_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | foreach my $item ( @{ $hash->{$k} } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | push @new_list, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | $class->remove_keys_from_hash( $item, $allowed_keys, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | $do_not_die_on_non_hash ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | $hash->{$k} = \@new_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | elsif ( ref $hash->{$k} eq "HASH" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | $hash->{$k} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | = $class->remove_keys_from_hash( $hash->{$k}, $allowed_keys, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | $do_not_die_on_non_hash ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | return $hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 1 | $self->_log( 2, "In DESTROY" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 1 | ▶ | $self->logout if ( $self->_client ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||