| 1 | package WWW::Hetzner::Robot; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Perl client for Hetzner Robot API (Dedicated Servers) | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 58 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 58 | ▶ | use URI::Escape (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 58 | ▶ | use WWW::Hetzner::Robot::API::Servers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 58 | ▶ | use WWW::Hetzner::Robot::API::Keys; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 58 | ▶ | use WWW::Hetzner::Robot::API::IPs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 58 | ▶ | use WWW::Hetzner::Robot::API::Reset; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 58 | ▶ | use WWW::Hetzner::Robot::API::Traffic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 58 | ▶ | use WWW::Hetzner::Robot::API::Boot; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 58 | ▶ | use WWW::Hetzner::Robot::API::RDNS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 58 | ▶ | use WWW::Hetzner::Robot::API::Failover; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 58 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 58 | our $VERSION = '0.102'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | has user => ( | ||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | default => sub { $ENV{HETZNER_ROBOT_USER} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 58 | ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has password => ( | ||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | default => sub { $ENV{HETZNER_ROBOT_PASSWORD} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | 58 | ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # For Role::HTTP compatibility | ||||||||||||||||||||||||||||
| 33 | 66 | ▶ | sub token { | ||||||||||||||||||||||||||
|
Subroutine
token: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 66 | my $self = shift; | |||||||||||||||||||||||||||
| 35 | 66 | ▶ | return $self->user && $self->password; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->user && $self->password
MC/DC0%0/2
$self->user && $self->password
$self->user
$self->password
Truth table33%1/3
A && B
A= $self->userB= $self->password
| |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 66 | ▶ | sub _check_auth { | ||||||||||||||||||||||||||
|
Subroutine
_check_auth: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 39 | 66 | my ($self) = @_; | |||||||||||||||||||||||||||
| 40 | 66 | ▶ | unless ($self->user && $self->password) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->user and $self->password
MC/DC0%0/2
$self->user and $self->password
$self->user
$self->password
Truth table33%1/3
A and B
A= $self->userB= $self->password
| |||||||||||||||||||||||||||||
| 41 | 0 | die "No Robot credentials configured.\n\n" . | |||||||||||||||||||||||||||
| 42 | "Set credentials via:\n" . | ||||||||||||||||||||||||||||
| 43 | " Environment: HETZNER_ROBOT_USER and HETZNER_ROBOT_PASSWORD\n" . | ||||||||||||||||||||||||||||
| 44 | " Options: --user and --password\n\n" . | ||||||||||||||||||||||||||||
| 45 | "Get credentials at: https://robot.hetzner.com/preferences/index\n"; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 58 | has base_url => ( | |||||||||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||||||||||
| 51 | default => 'https://robot-ws.your-server.de', | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 58 | with 'WWW::Hetzner::Role::HTTP'; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 66 | ▶ | around _request => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 66 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||
| 59 | 66 | $self->_check_auth; | |||||||||||||||||||||||||||
| 60 | 66 | return $self->$orig(@args); | |||||||||||||||||||||||||||
| 61 | 58 | }; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Override auth for Basic Auth | ||||||||||||||||||||||||||||
| 64 | 68 | ▶ | sub _set_auth { | ||||||||||||||||||||||||||
|
Subroutine
_set_auth: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 68 | my ($self, $headers) = @_; | |||||||||||||||||||||||||||
| 66 | 68 | require MIME::Base64; | |||||||||||||||||||||||||||
| 67 | 68 | $headers->{Authorization} = 'Basic ' . | |||||||||||||||||||||||||||
| 68 | MIME::Base64::encode_base64($self->user . ':' . $self->password, ''); | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 67 | ▶ | sub _content_type { 'application/x-www-form-urlencoded' } | ||||||||||||||||||||||||||
|
Subroutine
_content_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 50 | ▶ | sub _encode_body { | ||||||||||||||||||||||||||
|
Subroutine
_encode_body: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 75 | 50 | my ($self, $body) = @_; | |||||||||||||||||||||||||||
| 76 | 50 | my @pairs; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 50 | for my $key (keys %$body) { | |||||||||||||||||||||||||||
| 79 | 90 | my $value = $body->{$key}; | |||||||||||||||||||||||||||
| 80 | 90 | my $is_array = ref $value eq 'ARRAY'; | |||||||||||||||||||||||||||
| 81 | 90 | ▶ | my $form_key = $is_array && $key !~ /\[\]\z/ ? "$key\[\]" : $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_array and not $key =~ /\[\]\z/
MC/DC100%2/2
$is_array and not $key =~ /\[\]\z/
$is_array
not $key =~ /\[\]\z/
Truth table100%3/3
A and not(B)
A= $is_arrayB= not $key =~ /\[\]\z/
| |||||||||||||||||||||||||||||
| 82 | 90 | ▶ | my @values = $is_array ? @$value : ($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 90 | for my $value (@values) { | |||||||||||||||||||||||||||
| 85 | 96 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 96 | push @pairs, URI::Escape::uri_escape_utf8($form_key) . '=' . | |||||||||||||||||||||||||||
| 87 | URI::Escape::uri_escape_utf8($value); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 50 | return join '&', @pairs; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Resource accessors | ||||||||||||||||||||||||||||
| 96 | has servers => ( | ||||||||||||||||||||||||||||
| 97 | is => 'lazy', | ||||||||||||||||||||||||||||
| 98 | 4 | ▶ | builder => sub { WWW::Hetzner::Robot::API::Servers->new(client => shift) }, | ||||||||||||||||||||||||||
|
Subroutine
_build_servers: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 58 | ); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | has keys => ( | ||||||||||||||||||||||||||||
| 103 | is => 'lazy', | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | builder => sub { WWW::Hetzner::Robot::API::Keys->new(client => shift) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Subroutine
_build_keys: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 105 | 58 | ); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | has ips => ( | ||||||||||||||||||||||||||||
| 109 | is => 'lazy', | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | builder => sub { WWW::Hetzner::Robot::API::IPs->new(client => shift) }, | ||||||||||||||||||||||||||
|
Subroutine
_build_ips: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 111 | 58 | ); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | has reset => ( | ||||||||||||||||||||||||||||
| 115 | is => 'lazy', | ||||||||||||||||||||||||||||
| 116 | 3 | ▶ | builder => sub { WWW::Hetzner::Robot::API::Reset->new(client => shift) }, | ||||||||||||||||||||||||||
|
Subroutine
_build_reset: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 58 | ); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | has traffic => ( | ||||||||||||||||||||||||||||
| 121 | is => 'lazy', | ||||||||||||||||||||||||||||
| 122 | 2 | ▶ | builder => sub { WWW::Hetzner::Robot::API::Traffic->new(client => shift) }, | ||||||||||||||||||||||||||
|
Subroutine
_build_traffic: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | 58 | ); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | has boot => ( | ||||||||||||||||||||||||||||
| 127 | is => 'lazy', | ||||||||||||||||||||||||||||
| 128 | 5 | ▶ | builder => sub { WWW::Hetzner::Robot::API::Boot->new(client => shift) }, | ||||||||||||||||||||||||||
|
Subroutine
_build_boot: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | 58 | ); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | has rdns => ( | ||||||||||||||||||||||||||||
| 133 | is => 'lazy', | ||||||||||||||||||||||||||||
| 134 | 2 | ▶ | builder => sub { WWW::Hetzner::Robot::API::RDNS->new(client => shift) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 58 | ); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | has failover => ( | ||||||||||||||||||||||||||||
| 139 | is => 'lazy', | ||||||||||||||||||||||||||||
| 140 | 2 | ▶ | builder => sub { WWW::Hetzner::Robot::API::Failover->new(client => shift) }, | ||||||||||||||||||||||||||
|
Subroutine
_build_failover: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 141 | 58 | ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 58 | 1; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||