Confman/API.pm

filter: Statementstmt 17.6% 22 / 125 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 29.1% 7 / 24 Podpod 0.0% 0 / 12 totaltotal 13.6% 29 / 212 CC 23 SCAR 23.8
CC
23
Cov
13%
aws_metadata
30.0
load_config
20.0
new
14.1
CRAP 10.8
↑ Summary Confman.pm »
1 package Confman::API;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
12 0 my $class = shift;
13 0 ▶ my $self = bless({}, ref($class) || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
14  
15 0 ▶ $self->{config_dir} ||= "/etc/confman";
Condition0%0/2
$self->{"config_dir"} ||= "/etc/confman"
l!l
00
MC/DC0%0/1
$self->{"config_dir"} ||= "/etc/confman"
$self->{"config_dir"}
Truth table0%0/2
A
A= $self->{"config_dir"}
Aresult
11
00
16 0 $self->{json} = JSON->new->allow_nonref;
17 0 $self;
18 }
19  
20 0 ▶sub config_path {
Subroutine config_path: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my $self = shift;
22 0 return $self->{config_dir} . "/config.json"
23 }
24  
25 0 ▶sub api_key {
Subroutine api_key: not called CC=2 SCAR=17.9
Pod undocumented
26 0 my $self = shift;
27 0 ▶ $self->{api_key} = shift if scalar(@_) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
28 0 return $self->{api_key};
29 }
30  
31 0 ▶sub secret {
Subroutine secret: not called CC=2 SCAR=17.9
Pod undocumented
32 0 my $self = shift;
33 0 ▶ $self->{secret} = shift if scalar(@_) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
34 0 return $self->{secret};
35 }
36  
37 0 ▶sub endpoint_url {
Subroutine endpoint_url: not called CC=2 SCAR=17.9
Pod undocumented
38 0 my $self = shift;
39 0 ▶ $self->{endpoint_url} = shift if scalar(@_) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
40 0 return $self->{endpoint_url};
41 }
42  
43 0 ▶sub load_config {
Subroutine load_config: not called CC=4 SCAR=30.0
Pod undocumented
44 0 my $self = shift;
45 0 ▶ my $config_path = shift || $self->config_path;
Condition0%0/3
shift() || $self->config_path
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->config_path
shift() $self->config_path
Truth table0%0/3
A || B
A= shift()B= $self->config_path
ABresult
1X1
011
000
46 0 ▶ if(-e $config_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $config_path) 0 0 0
47 0 my $fh = IO::File->new($config_path);
48 0 local $/ = undef;
49 0 my $config = $self->json->decode(<$fh>);
50 0 $fh->close();
51  
52 0 ▶ while(my ($key, $value) = each(%$config)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$config) 0 0 0
53 0 $self->{$key} = $value;
54 }
55 }
56 0 return $self;
57 }
58  
59 0 ▶sub find_by_name {
Subroutine find_by_name: not called CC=2 SCAR=17.9
Pod undocumented
60 0 my $self = shift;
61 0 my $name = shift;
62  
63 0 my $results = $self->search(name => $name);
64 0 ▶ return undef if($results->{count} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $results->{"count"} == 0 0 0 0
65 0 my $conf_set_data = $results->{results}[0];
66 0 return Confman::ConfSet->new(
67 $self,
68 %$conf_set_data
69 );
70 }
71  
72 0 ▶sub search {
Subroutine search: not called CC=1 SCAR=6.9
Pod undocumented
73 0 my $self = shift;
74 0 my %query = @_;
75  
76 0 my ($results, $response) = $self->request('GET', "confman/sets/search", \%query);
77 0 return $results;
78 }
79  
80  
81 0 ▶sub json { my $self = shift; $self->{json}; }
Subroutine json: not called CC=1 SCAR=6.9
Pod undocumented
82  
83 0 ▶sub request {
Subroutine request: not called CC=3 SCAR=24.8
Pod undocumented
84 0 my $self = shift;
85 0 my $method = shift;
86 0 my $path = shift;
87 0 my $object = shift;
88  
89 0 my $client = REST::Client->new();
90 0 $client->getUseragent()->ssl_opts(verify_hostname => 0);
91  
92 0 my $timestamp = time();
93 0 my $secret_hash = md5_hex($self->secret.":".$timestamp);
94  
95 0 my $body;
96 0 ▶ if($method eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'GET') { } 0 0 0
97 0 $path = $path.$client->buildQuery($object);
98 }
99 else {
100 0 $body = $self->json->encode($object);
101 }
102 0 my $cloud_meta = encode_base64($self->json->encode($self->cloud_metadata));
103 0 $cloud_meta =~ s/\s+//ism;
104 0 my $headers = {
105 Accept => 'application/json',
106 'Content-type' => 'application/json',
107 Authorization => 'Basic ' . encode_base64($self->api_key . ':' . "$secret_hash:$timestamp"),
108 HTTP_CLOUD_META => $cloud_meta
109 };
110  
111 0 $client->setHost($self->endpoint_url);
112 0 $client->request($method, $path, $body, $headers);
113  
114 0 my $response;
115 0 ▶ if($client->responseCode eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->responseCode eq 200) 0 0 0
116 0 $response = $self->json->decode($client->responseContent());
117 }
118 0 return $response, $client;
119 }
120  
121 0 ▶sub cloud_metadata {
Subroutine cloud_metadata: not called CC=3 SCAR=24.8
Pod undocumented
122 0 my $self = shift;
123 0 ▶ return if $self->{no_cloud};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_cloud"} 0 0 0
124 0 ▶ $self->{cloud_metadata} ||= $self->aws_metadata;
Condition0%0/3
$self->{"cloud_metadata"} ||= $self->aws_metadata
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"cloud_metadata"} ||= $self->aws_metadata
$self->{"cloud_metadata"} $self->aws_metadata
Truth table0%0/3
A ||= B
A= $self->{"cloud_metadata"}B= $self->aws_metadata
ABresult
1X1
011
000
125 0 $self->{cloud_metadata};
126 }
127  
128 0 ▶sub aws_metadata {
Subroutine aws_metadata: not called CC=5 SCAR=34.0
Pod undocumented
129 0 my $self = shift;
130 0 ▶ return $self->{aws_metadata} if $self->{aws_metadata};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aws_metadata"} 0 0 0
131  
132 0 my @fields = qw(
133 instance-id ami-id availability-zone
134 instance-type
135 kernel-id local-hostname
136 mac public-hostname
137 );
138  
139 0 $self->{aws_metadata} = {};
140  
141 0 my $client = REST::Client->new();
142 0 $client->setHost("http://169.254.169.254/latest/meta-data");
143 0 $client->setTimeout(1);
144  
145 0 foreach my $field (@fields) {
146 0 $client->GET($field);
147 0 ▶ if($client->responseCode() =~ /2\d\d/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($client->responseCode =~ /2\d\d/) { } 0 0 0
elsif (scalar keys %{$$self{'aws_metadata'};} == 0) { } 0 0 0
148 0 $self->{aws_metadata}{$field} = $client->responseContent();
149 0 $self->{aws_metadata}{type} = 'aws';
150 }
151 0 elsif(scalar(keys %{$self->{aws_metadata}}) == 0) {
152 0 last;
153 }
154 }
155  
156 0 return $self->{aws_metadata};
157 }
158  
159  
160 package Confman::ConfSet;
161  
162 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=26.4
163 0 my $class = shift;
164 0 my $api = shift;
165 0 my %conf = @_;
166 0 ▶ my $self = bless(\%conf, ref($class) || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
167 0 $self->{api} = $api;
168  
169 {
170 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
171 0 foreach my $key (keys %{$self->pairs}) {
172 0 ▶ *{$class.'::'.$key} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 0 my $self = shift;
174 0 $self->{pairs}{$key};
175 0 ▶ } unless($self->can($key));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($key) 0 0 0
176 }
177 }
178  
179 0 $self;
180 }
181  
182 0 ▶sub update_pairs {
Subroutine update_pairs: not called CC=1 SCAR=6.9
183 0 my $self = shift;
184 0 my %pairs = @_;
185 0 my ($results, $response) = $self->api->request('PUT', "confman/sets/$self->{id}/update_pairs", {
186 conf_pairs => \%pairs
187 });
188 }
189  
190 0 ▶sub api {
Subroutine api: not called CC=1 SCAR=6.9
191 0 my $self = shift;
192 0 return $self->{api};
193 }
194  
195 0 ▶sub pairs {
Subroutine pairs: not called CC=3 SCAR=24.8
196 0 my $self = shift;
197 0 ▶ unless($self->{pairs}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"pairs"}) 0 0 0
198 0 $self->{pairs} = {};
199 0 foreach my $pair (@{$self->{conf_pairs}}) {
200 0 $self->{pairs}{$pair->{name}} = $pair->{value};
201 }
202 }
203 0 return $self->{pairs};
204 }
205  
206 1 1;
↑ Summary Confman.pm »