| 1 | package VM::CloudAtCost; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Data::Clean::FromJSON; | ||
| 9 | |||
| 10 | use Exporter qw(import); | ||
| 11 | |||
| 12 | our $VERSION = "0.1"; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ($class, %params) = @_; | ||
| 16 | |||
| 17 | my $url = $params{url}; | ||
| 18 | |||
| 19 | my $agent = LWP::UserAgent->new(); | ||
| 20 | |||
| 21 | $agent->timeout($params{timeout}) | ||
| 22 | if $params{timeout}; | ||
| 23 | |||
| 24 | $agent->ssl_opts(%{$params{ssl_opts}}) | ||
| 25 | if $params{ssl_opts} && ref $params{ssl_opts} eq 'HASH'; | ||
| 26 | |||
| 27 | my $self = { | ||
| 28 | url => $url, | ||
| 29 | agent => $agent | ||
| 30 | }; | ||
| 31 | bless $self, $class; | ||
| 32 | |||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | # syntax: $cloud->listServers(login => $UserID, key => $APIkey ); | ||
| 37 | sub listServers { | ||
| 38 | my ($self,%params) = @_; | ||
| 39 | die "missing login parameter" unless $params{login}; | ||
| 40 | die "missing key parameter" unless $params{key}; | ||
| 41 | |||
| 42 | my $result = $self->_get("/api/v1/listservers.php", %params); | ||
| 43 | return $result->{data}; | ||
| 44 | # return $result->{data} ? @{$result->{data}} : (); | ||
| 45 | } | ||
| 46 | |||
| 47 | # syntax: $cloud->listTemplates(login => $UserID, key => $APIkey ); | ||
| 48 | sub listTemplates { | ||
| 49 | my ($self,%params) = @_; | ||
| 50 | die "missing login parameter" unless $params{login}; | ||
| 51 | die "missing key parameter" unless $params{key}; | ||
| 52 | |||
| 53 | my $result = $self->_get("/api/v1/listtemplates.php", %params); | ||
| 54 | return $result->{data} ? @{$result->{data}} : (); | ||
| 55 | } | ||
| 56 | |||
| 57 | # syntax: $cloud->listTasks(login => $UserID, key => $APIkey ); | ||
| 58 | sub listTasks { | ||
| 59 | my ($self,%params) = @_; | ||
| 60 | die "missing login parameter" unless $params{login}; | ||
| 61 | die "missing key parameter" unless $params{key}; | ||
| 62 | |||
| 63 | my $result = $self->_get("/api/v1/listtasks.php", %params); | ||
| 64 | return $result->{data} ? @{$result->{data}} : (); | ||
| 65 | } | ||
| 66 | |||
| 67 | # syntax: $cloud->powerOperation(login => $UserID, key => $APIkey, sid => $ServerID, action => '[poweron, poweroff, reset]') | ||
| 68 | sub powerOperation { | ||
| 69 | my ($self,%params) = @_; | ||
| 70 | die "missing login parameter" unless $params{login}; | ||
| 71 | die "missing key parameter" unless $params{key}; | ||
| 72 | die "missing sid parameter" unless $params{sid}; | ||
| 73 | die "missing action parameter" unless $params{action}; | ||
| 74 | die "invalid action parameter" unless $params{action} eq 'poweron' or | ||
| 75 | $params{action} eq 'poweroff' or | ||
| 76 | $params{action} eq 'reset'; | ||
| 77 | |||
| 78 | my $result = $self->_post("/api/v1/powerop.php", %params); | ||
| 79 | return $result->{data} ? @{$result->{data}} : (); | ||
| 80 | } | ||
| 81 | |||
| 82 | # syntax: $cloud->renameServer(login => $UserID, key => $APIkey, sid => $ServerID, name => $NewServerName ) | ||
| 83 | sub renameServer { | ||
| 84 | my ($self,%params) = @_; | ||
| 85 | die "missing login parameter" unless $params{login}; | ||
| 86 | die "missing key parameter" unless $params{key}; | ||
| 87 | die "missing name parameter" unless $params{name}; | ||
| 88 | die "missing sid parameter" unless $params{sid}; | ||
| 89 | |||
| 90 | my $result = $self->_post("/api/v1/renameserver.php", %params); | ||
| 91 | return $result->{data} ? @{$result->{data}} : (); | ||
| 92 | } | ||
| 93 | |||
| 94 | # | ||
| 95 | # syntax: $cloud->rDNS(login => $UserID, key => $APIkey, sid => $ServerID, hostname => $HostName ) | ||
| 96 | sub rDNS { | ||
| 97 | my ($self,%params) = @_; | ||
| 98 | die "missing login parameter" unless $params{login}; | ||
| 99 | die "missing key parameter" unless $params{key}; | ||
| 100 | die "missing hostname parameter" unless $params{hostname}; | ||
| 101 | die "missing sid parameter" unless $params{sid}; | ||
| 102 | |||
| 103 | my $result = $self->_post("/api/v1/rdns.php", %params); | ||
| 104 | return $result->{data} ? @{$result->{data}} : (); | ||
| 105 | } | ||
| 106 | |||
| 107 | # syntax: $cloud->console(login => $UserID, key => $APIkey, sid => $ServerID) | ||
| 108 | sub console { | ||
| 109 | my ($self,%params) = @_; | ||
| 110 | die "missing login parameter" unless $params{login}; | ||
| 111 | die "missing key parameter" unless $params{key}; | ||
| 112 | die "missing sid parameter" unless $params{sid}; | ||
| 113 | |||
| 114 | my $result = $self->_post("/api/v1/console.php", %params); | ||
| 115 | return $result->{data} ? @{$result->{data}} : (); | ||
| 116 | } | ||
| 117 | |||
| 118 | # syntax: $cloud->runMode(login => $UserID, key => $APIkey, sid => $ServerID, mode => '[normal | safe]' ) | ||
| 119 | sub runMode { | ||
| 120 | my ($self,%params) = @_; | ||
| 121 | die "missing login parameter" unless $params{login}; | ||
| 122 | die "missing key parameter" unless $params{key}; | ||
| 123 | die "missing name parameter" unless $params{name}; | ||
| 124 | die "missing sid parameter" unless $params{sid}; | ||
| 125 | die "missing mode parameter" unless $params{mode}; | ||
| 126 | die "invalid mode parameter" unless $params{mode} eq 'normal' or | ||
| 127 | $params{mode} eq 'safe'; | ||
| 128 | |||
| 129 | my $result = $self->_post("/api/v1/runmode.php", %params); | ||
| 130 | return $result->{data} ? @{$result->{data}} : (); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _get { | ||
| 134 | my ($self, $path, %params) = @_; | ||
| 135 | |||
| 136 | my $url = URI->new($self->{url} . $path); | ||
| 137 | $url->query_form(%params); | ||
| 138 | |||
| 139 | my $response = $self->{agent}->get($url); | ||
| 140 | |||
| 141 | my $result = eval { from_json($response->content()) }; | ||
| 142 | |||
| 143 | if ($response->is_success()) { | ||
| 144 | my $JSONcleaner=Data::Clean::FromJSON->new(); | ||
| 145 | my $cleanJSON = $JSONcleaner->clean_in_place($result); | ||
| 146 | return $result; | ||
| 147 | } else { | ||
| 148 | if (($response->code eq "400") || | ||
| 149 | ($response->code eq "403") || | ||
| 150 | ($response->code eq "412") || | ||
| 151 | ($response->code eq "500") || | ||
| 152 | ($response->code eq "503") ) { | ||
| 153 | # certain status codes means certain things....... | ||
| 154 | return $response->code; | ||
| 155 | } elsif ($result) { die "server error: " . $result->{error}; | ||
| 156 | } else { die "communication error: " . $response->message() | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _post { | ||
| 162 | my ($self, $path, %params) = @_; | ||
| 163 | |||
| 164 | my $url = URI->new($self->{url} . $path); | ||
| 165 | |||
| 166 | # this is a hack | ||
| 167 | my @array; | ||
| 168 | for my $key ( keys %params) { push(@array,"$key=$params{$key}"); }; | ||
| 169 | my $content = join('&',@array); | ||
| 170 | |||
| 171 | my $response = $self->{agent}->post( | ||
| 172 | $self->{url} . $path, | ||
| 173 | 'Content-Type' => 'application/x-www-form-urlencoded', | ||
| 174 | 'Content' => $content | ||
| 175 | ); | ||
| 176 | |||
| 177 | my $result = eval { from_json($response->content()) }; | ||
| 178 | |||
| 179 | if ($response->is_success()) { | ||
| 180 | my $JSONcleaner=Data::Clean::FromJSON->new(); | ||
| 181 | my $cleanJSON = $JSONcleaner->clean_in_place($result); | ||
| 182 | return $result; | ||
| 183 | } else { | ||
| 184 | if (($response->code eq "400") || | ||
| 185 | ($response->code eq "403") || | ||
| 186 | ($response->code eq "412") || | ||
| 187 | ($response->code eq "500") || | ||
| 188 | ($response->code eq "503") ) { | ||
| 189 | # certain status codes means certain things....... | ||
| 190 | return $response->code; | ||
| 191 | } elsif ($result) { | ||
| 192 | croak "server error: " . $result->{error}; | ||
| 193 | } else { | ||
| 194 | croak "communication error: " . $response->message() | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | } | ||
| 199 | 1; | ||
| 200 | __END__ | ||