| 1 | package Net::Kubernetes::Role::APIAccess; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Role allowing direct access to the REST api | ||||||||||||||||||||||||||||||||
| 3 | 4 | $Net::Kubernetes::Role::APIAccess::VERSION = '1.03'; | |||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 4 | require LWP::UserAgent; | |||||||||||||||||||||||||||||||
| 6 | 4 | require HTTP::Request; | |||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 4 | require Cpanel::JSON::XS; | |||||||||||||||||||||||||||||||
| 9 | 4 | require URI; | |||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 4 | has url => ( | |||||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 15 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 16 | required => 1, | ||||||||||||||||||||||||||||||||
| 17 | default => 'http://localhost:8080', | ||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 4 | has api_version => ( | |||||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 22 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 23 | required => 1, | ||||||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 4 | has base_path => ( | |||||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 28 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 29 | required => 1, | ||||||||||||||||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 31 | builder => '_create_default_base_path' | ||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 4 | has password => ( | |||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 36 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 37 | required => 0, | ||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 4 | has username => ( | |||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 42 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 43 | required => 0, | ||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 4 | has ua => ( | |||||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 48 | isa => 'LWP::UserAgent', | ||||||||||||||||||||||||||||||||
| 49 | required => 1, | ||||||||||||||||||||||||||||||||
| 50 | builder => '_build_lwp_agent', | ||||||||||||||||||||||||||||||||
| 51 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 4 | has token => ( | |||||||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 56 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 57 | required => 0 | ||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 4 | has 'json' => ( | |||||||||||||||||||||||||||||||
| 61 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 62 | isa => JSON::MaybeXS::JSON, | ||||||||||||||||||||||||||||||||
| 63 | required => 1, | ||||||||||||||||||||||||||||||||
| 64 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 65 | builder => '_build_json', | ||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 4 | has 'ssl_cert_file' => ( | |||||||||||||||||||||||||||||||
| 69 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 70 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 71 | required => 0, | ||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 4 | has 'ssl_key_file' => ( | |||||||||||||||||||||||||||||||
| 75 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 76 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 77 | required => 0, | ||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 4 | has 'ssl_ca_file' => ( | |||||||||||||||||||||||||||||||
| 81 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 82 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 83 | required => 0, | ||||||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 4 | has 'ssl_verify' => ( | |||||||||||||||||||||||||||||||
| 87 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 88 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 89 | required => 0, | ||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 133 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.8
| |||||||||||||||||||||||||||||||||
| 93 | 133 | my $orig = shift; | |||||||||||||||||||||||||||||||
| 94 | 133 | my $class = shift; | |||||||||||||||||||||||||||||||
| 95 | 133 | my(%input) = @_; | |||||||||||||||||||||||||||||||
| 96 | 133 | ▶ | if(ref($input{token})){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | if($input{token}->can('getlines')){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 98 | 1 | $input{token} = join('', $input{token}->getlines); | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | elsif (ref($input{token}) eq 'GLOB') { | ||||||||||||||||||||||||||||||||
| 101 | 0 | my $fh = $input{token}; | |||||||||||||||||||||||||||||||
| 102 | 0 | $input{token} = do{ local $/; <$fh>}; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | ▶ | }elsif (exists $input{token} && -f $input{token}) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
exists $input{"token"} and -f $input{"token"}
MC/DC100%2/2
exists $input{"token"} and -f $input{"token"}
exists $input{"token"}
-f $input{"token"}
Truth table100%3/3
A and B
A= exists $input{"token"}B= -f $input{"token"}
| |||||||||||||||||||||||||||||||||
| 105 | 1 | open(my $fh, '<', $input{token}); | |||||||||||||||||||||||||||||||
| 106 | 1 | $input{token} = do{ local $/; <$fh>}; | |||||||||||||||||||||||||||||||
| 107 | 1 | close($fh); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 133 | ▶ | if(! exists $input{api_version}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 96 | ▶ | if(exists $input{base_path}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 43 | ▶ | if($input{base_path} =~ m{/api/(v[^/]+)}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 43 | $input{api_version} = $1; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||||||
| 116 | 53 | $input{api_version}='v1'; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | 133 | return $class->$orig(%input); | |||||||||||||||||||||||||||||||
| 120 | 4 | }; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 41 | ▶ | sub _create_default_base_path { | ||||||||||||||||||||||||||||||
|
Subroutine
_create_default_base_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 123 | 41 | my($self) = @_; | |||||||||||||||||||||||||||||||
| 124 | 41 | return '/api/'.$self->api_version; | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 95 | ▶ | sub path { | ||||||||||||||||||||||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 128 | 95 | my($self) = @_; | |||||||||||||||||||||||||||||||
| 129 | 95 | return $self->url.$self->base_path; | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 80 | ▶ | sub _build_lwp_agent { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_lwp_agent: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||
| 133 | 80 | my $self = shift; | |||||||||||||||||||||||||||||||
| 134 | 80 | my $ua = LWP::UserAgent->new(agent=>'net-kubernetes-perl/0.20'); | |||||||||||||||||||||||||||||||
| 135 | 80 | ▶ | if($self->ssl_cert_file){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 0 | $ua = LWP::UserAgent->new(ssl_opts => { | |||||||||||||||||||||||||||||||
| 137 | verify_hostname => $self->ssl_verify, | ||||||||||||||||||||||||||||||||
| 138 | SSL_cert_file => $self->ssl_cert_file, | ||||||||||||||||||||||||||||||||
| 139 | SSL_key_file => $self->ssl_key_file, | ||||||||||||||||||||||||||||||||
| 140 | SSL_ca_file => $self->ssl_ca_file, | ||||||||||||||||||||||||||||||||
| 141 | }); | ||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | 80 | return $ua; | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 67 | ▶ | sub _build_json { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 147 | 67 | return JSON::MaybeXS->new->allow_blessed(1)->convert_blessed(1); | |||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 102 | ▶ | sub create_request { | ||||||||||||||||||||||||||||||
|
Subroutine
create_request: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 151 | 102 | my($self, @options) = @_; | |||||||||||||||||||||||||||||||
| 152 | 102 | my $req = HTTP::Request->new(@options); | |||||||||||||||||||||||||||||||
| 153 | 102 | ▶ | if ($self->username && $self->password) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$self->username and $self->password
MC/DC50%1/2
$self->username and $self->password
$self->username
$self->password
Truth table66%2/3
A and B
A= $self->usernameB= $self->password
| |||||||||||||||||||||||||||||||||
| 154 | 1 | $req->header(Authorization=>"Basic ".encode_base64($self->username.':'.$self->password)); | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | elsif($self->token){ | ||||||||||||||||||||||||||||||||
| 157 | 3 | $req->header(Authorization=>"Bearer ".$self->token); | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | 102 | return $req; | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 4 | return 42; | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||||||