| 1 | package VM::HetznerCloud::Schema; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: OpenAPI schema for the cloud API | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Mojo::Base -strict, -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use JSON::Validator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use JSON::Validator::Formats; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use List::Util qw(uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Mojo::JSON qw(decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Mojo::Loader qw(data_section); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Mojo::Util qw(camelize); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use constant IV_SIZE => eval 'require Config;$Config::Config{ivsize}'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | our $VERSION = '0.0.3'; # VERSION | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 7 | ▶ | sub validate ( $class, $operation, $params = {} ) { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 7 | my ($spec, $validator) = _get_params($operation); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my %check_params = map { | ||||||||||||||||||||||||||||
| 27 | 7 | my $camelized = camelize $_; | |||||||||||||||||||||||||||
| 28 | 6 | $camelized =~ s{Id$}{ID}; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | $spec->{param_names}->{$camelized} ? | ||||||||||||||||||||||||||||
| 31 | ($camelized => $params->{$_}) : | ||||||||||||||||||||||||||||
| 32 | 6 | ▶ | ($_ => $params->{$_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } keys $params->%*; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 7 | my @errors = $validator->validate( | |||||||||||||||||||||||||||
| 36 | \%check_params, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 7 | ▶ | if ( @errors ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | return ( undef, @errors ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 7 | my %request_params; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 7 | for my $param_name ( sort keys $spec->{param_names}->%* ) { | |||||||||||||||||||||||||||
| 46 | 19 | my $key = $spec->{param_names}->{$param_name}; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 19 | ▶ | next if !$check_params{$param_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | $request_params{$key}->{$param_name} = $check_params{$param_name}; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 7 | return \%request_params; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub _get_subnmame ( $parts, $method ) { | ||||||||||||||||||||||||||
|
Subroutine
_get_subnmame: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $all = ( $parts->@* && $parts->[-1] =~ m{\{} ) ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$parts and $parts->[-1] =~ /\{/u
MC/DC0%0/2
@$parts and $parts->[-1] =~ /\{/u
@$parts
$parts->[-1] =~ /\{/u
Truth table0%0/3
A and B
A= @$partsB= $parts->[-1] =~ /\{/u
| |||||||||||||||||||||||||||||
| 58 | 0 | my @static_parts = grep { $_ !~ m/\{/ } $parts->@*; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $suffix = @static_parts ? ( join '_', '', @static_parts ) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ( $all ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return 'list' . $suffix if $method eq 'get'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return 'create' . $suffix if $method eq 'post'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | return $method . $suffix; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 7 | ▶ | sub _get_params ($operation) { | ||||||||||||||||||||||||||
|
Subroutine
_get_params: called CC=19 SCAR=44.2
| |||||||||||||||||||||||||||||
| 71 | 7 | state %operation_params; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 7 | my $op_data = $operation_params{$operation}; | |||||||||||||||||||||||||||
| 74 | 7 | ▶ | return $op_data->@* if $op_data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | my $api_spec = data_section(__PACKAGE__, 'openapi.json'); | |||||||||||||||||||||||||||
| 77 | 2 | $api_spec = encode_utf8( $api_spec ); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | my $data = decode_json( $api_spec ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | my ($path, $method) = split '#', $operation // '1#1'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$operation // "1#1"
MC/DC0%0/1
$operation // "1#1"
$operation
Truth table50%1/2
A
A= $operation
| |||||||||||||||||||||||||||||
| 82 | 2 | my $op = $data->{$path}->{$method}; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | return if !$op; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | my $params = $op->{parameters}; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | my %properties; | |||||||||||||||||||||||||||
| 89 | my @required; | ||||||||||||||||||||||||||||
| 90 | 2 | my %param_names; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | PARAM: | ||||||||||||||||||||||||||||
| 93 | 2 | for my $param ( $params->@* ) { | |||||||||||||||||||||||||||
| 94 | 5 | ▶ | next PARAM if $param->{name} eq 'Auth-API-Token'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 5 | my $name = $param->{name}; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 5 | ▶ | if ( $param->{required} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | push @required, $name; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 5 | $param_names{$name} = $param->{in}; | |||||||||||||||||||||||||||
| 103 | 5 | $properties{$name} = $param; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | my ($content_type, $body_required); | |||||||||||||||||||||||||||
| 107 | 2 | ▶ | if ( $op->{requestBody} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | my $body = $op->{requestBody}->{content}; | |||||||||||||||||||||||||||
| 109 | 0 | $body_required = $op->{requestBody}->{required}; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ($content_type) = sort keys $body->%*; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ( $content_type eq 'application/json' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 114 | 0 | my $schema = $body->{$content_type}->{schema}; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $prop = $schema->{properties} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$schema->{'properties'} || {}
MC/DC0%0/1
$schema->{'properties'} || {}
$schema->{'properties'}
Truth table0%0/2
A
A= $schema->{'properties'}
| |||||||||||||||||||||||||||||
| 116 | 0 | for my $property ( keys $prop->%* ) { | |||||||||||||||||||||||||||
| 117 | 0 | $properties{$property} = delete $prop->{$property}; | |||||||||||||||||||||||||||
| 118 | 0 | $param_names{$property} = 'body'; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | elsif ( $content_type eq 'text/plain' ) { | ||||||||||||||||||||||||||||
| 122 | 0 | $properties{text} = { type => "string" }; | |||||||||||||||||||||||||||
| 123 | 0 | $param_names{text} = 'body'; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | my $spec = { | |||||||||||||||||||||||||||
| 128 | type => 'object', | ||||||||||||||||||||||||||||
| 129 | required => \@required, | ||||||||||||||||||||||||||||
| 130 | body_required => $body_required, | ||||||||||||||||||||||||||||
| 131 | properties => \%properties, | ||||||||||||||||||||||||||||
| 132 | param_names => \%param_names, | ||||||||||||||||||||||||||||
| 133 | content_type => $content_type, | ||||||||||||||||||||||||||||
| 134 | }; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | my $validator = JSON::Validator->new->schema($spec); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | $validator->formats->{uint32} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 139 | 0 | my ($sub) = JSON::Validator::Formats->can('_match_number'); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | $sub->( unint32 => $_[0], 'L' ); | |||||||||||||||||||||||||||
| 142 | 2 | }; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $validator->formats->{uint64} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 145 | 0 | my ($sub) = JSON::Validator::Formats->can('_match_number'); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | $sub->( unint32 => $_[0], IV_SIZE >= 8 ? 'Q' : '' ); | |||||||||||||||||||||||||||
| 148 | 2 | }; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2 | ▶ | if ( $spec->{properties}->{'$ref'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | my @ref = $spec->{properties}->{'$ref'}; | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | while ( my $ref = shift @ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $ref =~ s{^#}{}; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | my $data = $validator->get( $ref ); | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ( $data->{properties} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | for my $property ( keys $data->{properties}->%* ) { | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | next if $data->{properties}->{$property}->{readOnly}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | $spec->{param_names}->{$property} = 'body'; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if ( $data->{allOf} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | push @ref, map{ $_->{'$ref'} } $data->{allOf}->@*; | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ( $data->{required} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | push $spec->{required}->@*, $data->{required}->@*; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 2 | $spec->{required}->@* = uniq $spec->{required}->@*; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | $operation_params{$operation} = [ $spec, $validator ]; | |||||||||||||||||||||||||||
| 176 | 2 | return $operation_params{$operation}->@*; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 4 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =pod | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 NAME | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | VM::HetznerCloud::Schema - OpenAPI schema for the cloud API | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 VERSION | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | version 0.0.3 | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 METHODS | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 validate | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Renee Baecker <reneeb@cpan.org> | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | This software is Copyright (c) 2018 by Renee Baecker. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | __DATA__ | ||||||||||||||||||||||||||||