| 1 | package WWW::VastAI::Role::OperationMap; | ||||||||||||
| 2 | 10 | our $VERSION = '0.001'; | |||||||||||
| 3 | # ABSTRACT: Central Vast.ai API operation mapping used by WWW::VastAI | ||||||||||||
| 4 | |||||||||||||
| 5 | 10 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 10 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 10 | requires 'base_url'; | |||||||||||
| 9 | 10 | requires 'base_url_v1'; | |||||||||||
| 10 | 10 | requires 'run_url'; | |||||||||||
| 11 | 10 | requires 'get'; | |||||||||||
| 12 | 10 | requires 'post'; | |||||||||||
| 13 | 10 | requires 'put'; | |||||||||||
| 14 | 10 | requires 'delete'; | |||||||||||
| 15 | |||||||||||||
| 16 | 10 | has operation_map => ( | |||||||||||
| 17 | is => 'lazy', | ||||||||||||
| 18 | builder => '_build_operation_map', | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | |||||||||||||
| 21 | 6 | ▶ | sub _build_operation_map { | ||||||||||
|
Subroutine
_build_operation_map: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | return { | ||||||||||||
| 23 | 6 | searchOffers => { method => 'POST', base => 'v0', path => '/bundles/' }, | |||||||||||
| 24 | listInstances => { method => 'GET', base => 'v0', path => '/instances/' }, | ||||||||||||
| 25 | getInstance => { method => 'GET', base => 'v0', path => '/instances/{id}/' }, | ||||||||||||
| 26 | createInstance => { method => 'PUT', base => 'v0', path => '/asks/{id}/' }, | ||||||||||||
| 27 | manageInstance => { method => 'PUT', base => 'v0', path => '/instances/{id}/' }, | ||||||||||||
| 28 | deleteInstance => { method => 'DELETE', base => 'v0', path => '/instances/{id}/' }, | ||||||||||||
| 29 | requestInstanceLogs => { method => 'PUT', base => 'v0', path => '/instances/request_logs/{id}' }, | ||||||||||||
| 30 | listInstanceSSHKeys => { method => 'GET', base => 'v0', path => '/instances/{id}/ssh/' }, | ||||||||||||
| 31 | attachInstanceSSHKey => { method => 'POST', base => 'v0', path => '/instances/{id}/ssh/' }, | ||||||||||||
| 32 | detachInstanceSSHKey => { method => 'DELETE', base => 'v0', path => '/instances/{id}/ssh/{ssh_key_id}/' }, | ||||||||||||
| 33 | searchTemplates => { method => 'GET', base => 'v0', path => '/template/' }, | ||||||||||||
| 34 | createTemplate => { method => 'POST', base => 'v0', path => '/template/' }, | ||||||||||||
| 35 | updateTemplate => { method => 'PUT', base => 'v0', path => '/template/' }, | ||||||||||||
| 36 | deleteTemplate => { method => 'DELETE', base => 'v0', path => '/template/' }, | ||||||||||||
| 37 | listVolumes => { method => 'GET', base => 'v0', path => '/volumes/' }, | ||||||||||||
| 38 | createVolume => { method => 'PUT', base => 'v0', path => '/volumes/' }, | ||||||||||||
| 39 | deleteVolume => { method => 'DELETE', base => 'v0', path => '/volumes/' }, | ||||||||||||
| 40 | listSSHKeys => { method => 'GET', base => 'v0', path => '/ssh/' }, | ||||||||||||
| 41 | createSSHKey => { method => 'POST', base => 'v0', path => '/ssh/' }, | ||||||||||||
| 42 | updateSSHKey => { method => 'PUT', base => 'v0', path => '/ssh/{id}/' }, | ||||||||||||
| 43 | deleteSSHKey => { method => 'DELETE', base => 'v0', path => '/ssh/{id}/' }, | ||||||||||||
| 44 | listAPIKeys => { method => 'GET', base => 'v0', path => '/auth/apikeys/' }, | ||||||||||||
| 45 | createAPIKey => { method => 'POST', base => 'v0', path => '/auth/apikeys/' }, | ||||||||||||
| 46 | deleteAPIKey => { method => 'DELETE', base => 'v0', path => '/auth/apikeys/{id}/' }, | ||||||||||||
| 47 | getCurrentUser => { method => 'GET', base => 'v0', path => '/users/current/' }, | ||||||||||||
| 48 | listEnvVars => { method => 'GET', base => 'v0', path => '/secrets/' }, | ||||||||||||
| 49 | createEnvVar => { method => 'POST', base => 'v0', path => '/secrets/' }, | ||||||||||||
| 50 | updateEnvVar => { method => 'PUT', base => 'v0', path => '/secrets/' }, | ||||||||||||
| 51 | deleteEnvVar => { method => 'DELETE', base => 'v0', path => '/secrets/' }, | ||||||||||||
| 52 | listInvoices => { method => 'GET', base => 'v1', path => '/invoices/' }, | ||||||||||||
| 53 | listEndpoints => { method => 'GET', base => 'v0', path => '/endptjobs/' }, | ||||||||||||
| 54 | createEndpoint => { method => 'POST', base => 'v0', path => '/endptjobs/' }, | ||||||||||||
| 55 | deleteEndpoint => { method => 'DELETE', base => 'v0', path => '/endptjobs/{id}/' }, | ||||||||||||
| 56 | listWorkergroups => { method => 'GET', base => 'v0', path => '/workergroups/' }, | ||||||||||||
| 57 | createWorkergroup => { method => 'POST', base => 'v0', path => '/workergroups/' }, | ||||||||||||
| 58 | updateWorkergroup => { method => 'PUT', base => 'v0', path => '/workergroups/{id}/' }, | ||||||||||||
| 59 | deleteWorkergroup => { method => 'DELETE', base => 'v0', path => '/workergroups/{id}/' }, | ||||||||||||
| 60 | getEndpointLogs => { method => 'POST', base => 'run', path => '/get_endpoint_logs/' }, | ||||||||||||
| 61 | getEndpointWorkers => { method => 'POST', base => 'run', path => '/get_endpoint_workers/' }, | ||||||||||||
| 62 | getWorkergroupLogs => { method => 'POST', base => 'run', path => '/get_workergroup_logs/' }, | ||||||||||||
| 63 | getWorkergroupWorkers => { method => 'POST', base => 'run', path => '/get_workergroup_workers/' }, | ||||||||||||
| 64 | }; | ||||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 46 | ▶ | sub _base_for_operation { | ||||||||||
|
Subroutine
_base_for_operation: called CC=4 SCAR=14.1
| |||||||||||||
| 68 | 46 | my ($self, $base) = @_; | |||||||||||
| 69 | 46 | ▶ | return $self->base_url if $base eq 'v0'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 5 | ▶ | return $self->base_url_v1 if $base eq 'v1'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 4 | ▶ | return $self->run_url if $base eq 'run'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 0 | croak "Unknown Vast.ai API base '$base'"; | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 46 | ▶ | sub _expand_path { | ||||||||||
|
Subroutine
_expand_path: called CC=3 SCAR=11.0
| |||||||||||||
| 76 | 46 | my ($self, $path, $vars) = @_; | |||||||||||
| 77 | |||||||||||||
| 78 | 46 | ▶ | $vars ||= {}; | ||||||||||
|
Condition100%2/2
$vars ||= {}
MC/DC100%1/1
$vars ||= {}
$vars
Truth table100%2/2
A
A= $vars
| |||||||||||||
| 79 | 46 | $path =~ s/\{([^}]+)\}/ | |||||||||||
| 80 | exists $vars->{$1} | ||||||||||||
| 81 | 20 | ▶ | ? $vars->{$1} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | : croak "Missing path parameter '$1'" | ||||||||||||
| 83 | /ge; | ||||||||||||
| 84 | |||||||||||||
| 85 | 46 | return $path; | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 46 | ▶ | sub request_op { | ||||||||||
|
Subroutine
request_op: called CC=9 SCAR=22.4
Pod
documented
| |||||||||||||
| 89 | 46 | my ($self, $name, %args) = @_; | |||||||||||
| 90 | |||||||||||||
| 91 | 46 | ▶ | my $op = $self->operation_map->{$name} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | or croak "Unknown Vast.ai operation '$name'"; | ||||||||||||
| 93 | |||||||||||||
| 94 | 46 | my $path = $self->_expand_path($op->{path}, $args{path}); | |||||||||||
| 95 | my %http_opts = ( | ||||||||||||
| 96 | 46 | base_url => $self->_base_for_operation($op->{base}), | |||||||||||
| 97 | ); | ||||||||||||
| 98 | |||||||||||||
| 99 | 46 | ▶ | if ($op->{method} eq 'GET') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 15 | ▶ | return $self->get($path, %http_opts, ($args{query} ? (params => $args{query}) : ())); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | } | ||||||||||||
| 102 | 31 | ▶ | if ($op->{method} eq 'POST') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 12 | ▶ | return $self->post($path, ($args{body} || {}), %http_opts); | ||||||||||
|
Condition50%1/2
$args{'body'} || {}
MC/DC0%0/1
$args{'body'} || {}
$args{'body'}
Truth table50%1/2
A
A= $args{'body'}
| |||||||||||||
| 104 | } | ||||||||||||
| 105 | 19 | ▶ | if ($op->{method} eq 'PUT') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | 10 | ▶ | return $self->put($path, ($args{body} || {}), %http_opts); | ||||||||||
|
Condition50%1/2
$args{'body'} || {}
MC/DC0%0/1
$args{'body'} || {}
$args{'body'}
Truth table50%1/2
A
A= $args{'body'}
| |||||||||||||
| 107 | } | ||||||||||||
| 108 | 9 | ▶ | if ($op->{method} eq 'DELETE') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | 9 | return $self->delete($path, $args{body}, %http_opts); | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 0 | croak "Unsupported HTTP method '$op->{method}'"; | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | 10 | 1; | |||||||||||
| 116 | |||||||||||||
| 117 | __END__ | ||||||||||||