| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2019 cPanel, L.L.C. | ||||||||||||||||||||||||||||
| 3 | # All rights reserved. | ||||||||||||||||||||||||||||
| 4 | # http://cpanel.net/ | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Distributed under the terms of the MIT license. See the LICENSE file for | ||||||||||||||||||||||||||||
| 7 | # further details. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package OpenStack::Client::Auth::v2; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use OpenStack::Client (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub new ($$%) { | ||||||||||||||||||||||||||||
| 17 | 13 | ▶ | my ($class, $endpoint, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 13 | ▶ | die 'No OpenStack tenant name provided in "tenant"' unless defined $args{'tenant'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 12 | ▶ | die 'No OpenStack username provided in "username"' unless defined $args{'username'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 10 | ▶ | die 'No OpenStack password provided in "password"' unless defined $args{'password'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $client = OpenStack::Client->new($endpoint, | ||||||||||||||||||||||||||||
| 24 | 'package_ua' => $args{'package_ua'}, | ||||||||||||||||||||||||||||
| 25 | 'package_request' => $args{'package_request'}, | ||||||||||||||||||||||||||||
| 26 | 8 | 'package_response' => $args{'package_response'} | |||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | my $response = $client->call('POST' => '/tokens', { | ||||||||||||||||||||||||||||
| 30 | 'auth' => { | ||||||||||||||||||||||||||||
| 31 | 'tenantName' => $args{'tenant'}, | ||||||||||||||||||||||||||||
| 32 | 'passwordCredentials' => { | ||||||||||||||||||||||||||||
| 33 | 'username' => $args{'username'}, | ||||||||||||||||||||||||||||
| 34 | 8 | 'password' => $args{'password'} | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | }); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 8 | ▶ | unless (defined $response->{'access'}->{'token'}->{'id'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | die 'No token found in response'; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | return bless { | ||||||||||||||||||||||||||||
| 44 | 'package_ua' => $args{'package_ua'}, | ||||||||||||||||||||||||||||
| 45 | 'package_request' => $args{'package_request'}, | ||||||||||||||||||||||||||||
| 46 | 'package_response' => $args{'package_response'}, | ||||||||||||||||||||||||||||
| 47 | 'response' => $response, | ||||||||||||||||||||||||||||
| 48 | 'clients' => {}, | ||||||||||||||||||||||||||||
| 49 | 7 | 'services' => $response->{'access'}->{'serviceCatalog'} | |||||||||||||||||||||||||||
| 50 | }, $class; | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub response ($) { | ||||||||||||||||||||||||||||
| 54 | 1 | ▶ | shift->{'response'}; | ||||||||||||||||||||||||||
|
Subroutine
response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub access ($) { | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | shift->{'response'}->{'access'}; | ||||||||||||||||||||||||||
|
Subroutine
access: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub token ($) { | ||||||||||||||||||||||||||||
| 62 | 8 | ▶ | shift->{'response'}->{'access'}->{'token'}->{'id'}; | ||||||||||||||||||||||||||
|
Subroutine
token: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub services ($) { | ||||||||||||||||||||||||||||
| 66 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
services: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | my %types = map { | ||||||||||||||||||||||||||||
| 69 | 2 | $_->{'type'} => 1 | |||||||||||||||||||||||||||
| 70 | 1 | } @{$self->{'services'}}; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | return sort keys %types; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub service ($$%) { | ||||||||||||||||||||||||||||
| 76 | 11 | ▶ | my ($self, $type, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
service: called CC=15 SCAR=27.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 11 | ▶ | if (defined $self->{'clients'}->{$type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 2 | return $self->{'clients'}->{$type}; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 9 | ▶ | if (defined $opts{'uri'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | return $self->{'clients'}->{$type} = OpenStack::Client->new($opts{'uri'}, | ||||||||||||||||||||||||||||
| 84 | 'package_ua' => $self->{'package_ua'}, | ||||||||||||||||||||||||||||
| 85 | 'package_request' => $self->{'package_request'}, | ||||||||||||||||||||||||||||
| 86 | 1 | 'package_response' => $self->{'package_response'}, | |||||||||||||||||||||||||||
| 87 | 'token' => $self->token | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 8 | ▶ | $opts{'endpoint'} ||= 'public'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"endpoint"} ||= "public"
MC/DC100%1/1
$opts{"endpoint"} ||= "public"
$opts{"endpoint"}
Truth table100%2/2
A
A= $opts{"endpoint"}
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 8 | ▶ | if ($opts{'endpoint'} !~ /^(?:public|internal|admin)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | die 'Invalid endpoint type specified in "endpoint"'; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 7 | foreach my $service (@{$self->{'services'}}) { | |||||||||||||||||||||||||||
| 98 | 10 | ▶ | next unless $type eq $service->{'type'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 8 | my $uri; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 8 | foreach my $endpoint (@{$service->{'endpoints'}}) { | |||||||||||||||||||||||||||
| 103 | 8 | ▶ | next if defined $opts{'id'} && $endpoint->{'id'} ne $opts{'id'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opts{"id"} and $endpoint->{"id"} ne $opts{"id"}
MC/DC100%2/2
defined $opts{"id"} and $endpoint->{"id"} ne $opts{"id"}
defined $opts{"id"}
$endpoint->{"id"} ne $opts{"id"}
Truth table100%3/3
A and B
A= defined $opts{"id"}B= $endpoint->{"id"} ne $opts{"id"}
| |||||||||||||||||||||||||||||
| 104 | 7 | ▶ | next if defined $opts{'region'} && $endpoint->{'region'} ne $opts{'region'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opts{"region"} and $endpoint->{"region"} ne $opts{"region"}
MC/DC100%2/2
defined $opts{"region"} and $endpoint->{"region"} ne $opts{"region"}
defined $opts{"region"}
$endpoint->{"region"} ne $opts{"region"}
Truth table100%3/3
A and B
A= defined $opts{"region"}B= $endpoint->{"region"} ne $opts{"region"}
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 6 | ▶ | if ($opts{'endpoint'} eq 'public') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 107 | 4 | $uri = $endpoint->{'publicURL'}; | |||||||||||||||||||||||||||
| 108 | } elsif ($opts{'endpoint'} eq 'internal') { | ||||||||||||||||||||||||||||
| 109 | 1 | $uri = $endpoint->{'internalURL'}; | |||||||||||||||||||||||||||
| 110 | } elsif ($opts{'endpoint'} eq 'admin') { | ||||||||||||||||||||||||||||
| 111 | 1 | $uri = $endpoint->{'adminURL'}; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | return $self->{'clients'}->{$type} = OpenStack::Client->new($uri, | ||||||||||||||||||||||||||||
| 115 | 'package_ua' => $self->{'package_ua'}, | ||||||||||||||||||||||||||||
| 116 | 'package_request' => $self->{'package_request'}, | ||||||||||||||||||||||||||||
| 117 | 6 | 'package_response' => $self->{'package_response'}, | |||||||||||||||||||||||||||
| 118 | 'token' => $self->token | ||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | die "No service type '$type' found"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | 1; | |||||||||||||||||||||||||||