Skip to content

Commit ab1a026

Browse files
committed
Merge 7a0b58a into e03e34e
2 parents e03e34e + 7a0b58a commit ab1a026

File tree

4 files changed

+146
-12
lines changed

4 files changed

+146
-12
lines changed

src/Api/DigitalOceanRebooter.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Tylercd100\Rebooter\Api;
4+
5+
use GuzzleHttp\Client;
6+
use Tylercd100\Rebooter\ApiRebooter;
7+
8+
class DigitalOceanRebooter extends ApiRebooter {
9+
10+
protected $token;
11+
protected $server_id;
12+
protected $host;
13+
protected $client;
14+
15+
/**
16+
* @param string $token API Token from DigitalOcean.com
17+
* @param number $server_id The ID of the droplet you want to control
18+
* @param string $host The api host
19+
* @param Client $client The guzzle client to use
20+
*/
21+
public function __construct($token, $server_id, $host = "api.digitalocean.com", Client $client = null){
22+
23+
if(!$client instanceof Client){
24+
$client = new Client();
25+
}
26+
27+
$this->client = $client;
28+
$this->token = $token;
29+
$this->server_id = $server_id;
30+
$this->host = $host;
31+
}
32+
33+
/**
34+
* Executes a Boot command
35+
* @return \Psr\Http\Message\ResponseInterface
36+
*/
37+
public function boot(){
38+
return $this->exec("power_on");
39+
}
40+
41+
/**
42+
* Executes a Reboot command
43+
* @return \Psr\Http\Message\ResponseInterface
44+
*/
45+
public function reboot(){
46+
return $this->exec("reboot");
47+
}
48+
49+
/**
50+
* Executes a Shutdown command
51+
* @return \Psr\Http\Message\ResponseInterface
52+
*/
53+
public function shutdown(){
54+
return $this->exec("power_off");
55+
}
56+
57+
/**
58+
* Builds the request URL for the API call
59+
* @param string $action The DigitalOcean API action
60+
* @return string
61+
*/
62+
protected function buildRequestUrl($action){
63+
return "https://{$this->host}/v2/droplets/{$this->server_id}/actions";
64+
}
65+
66+
/**
67+
* Builds the request data for the API call
68+
* @param string $action The DigitalOcean API action
69+
* @return array
70+
*/
71+
protected function buildRequestData($action){
72+
return ["type"=>$action];
73+
}
74+
75+
/**
76+
* Executes a command on the API
77+
* @param string $action
78+
* @return \Psr\Http\Message\ResponseInterface
79+
*/
80+
protected function exec($action){
81+
$url = $this->buildRequestUrl($action);
82+
$data = $this->buildRequestData($action);
83+
return $this->client->request('POST', $url, [
84+
'auth' => [$this->token,""],
85+
'form_params' => $data,
86+
]);
87+
}
88+
}

src/Api/LinodeRebooter.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@
88
class LinodeRebooter extends ApiRebooter {
99

1010
protected $token;
11-
protected $linodeId;
11+
protected $server_id;
1212
protected $host;
1313
protected $client;
1414

1515
/**
16-
* @param string $token API Token from Linode.com
17-
* @param number $linodeId The ID of the linode you want to control
18-
* @param string $host The api host
19-
* @param Client $client The guzzle client to use
16+
* @param string $token API Token from Linode.com
17+
* @param number $server_id The ID of the linode you want to control
18+
* @param string $host The api host
19+
* @param Client $client The guzzle client to use
2020
*/
21-
public function __construct($token, $linodeId, $host = "api.linode.com", Client $client = null){
21+
public function __construct($token, $server_id, $host = "api.linode.com", Client $client = null){
2222

2323
if(!$client instanceof Client){
2424
$client = new Client();
2525
}
2626

2727
$this->client = $client;
2828
$this->token = $token;
29-
$this->linodeId = $linodeId;
29+
$this->server_id = $server_id;
3030
$this->host = $host;
3131
}
3232

@@ -60,7 +60,7 @@ public function shutdown(){
6060
* @return string
6161
*/
6262
protected function buildRequestUrl($action){
63-
return "https://{$this->host}/?api_key={$this->token}&api_action={$action}&LinodeID={$this->linodeId}";
63+
return "https://{$this->host}/?api_key={$this->token}&api_action={$action}&LinodeID={$this->server_id}";
6464
}
6565

6666
/**

tests/DigitalOceanTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Tylercd100\Rebooter\Tests;
4+
5+
use Tylercd100\Rebooter\Api\DigitalOceanRebooter;
6+
use GuzzleHttp\Client;
7+
8+
class DigitalOceanTest extends TestCase
9+
{
10+
public function testItCreatesInstanceSuccessfully(){
11+
$obj = new DigitalOceanRebooter("token",123456789);
12+
$this->assertInstanceOf(DigitalOceanRebooter::class,$obj);
13+
}
14+
15+
public function testItBuildsCorrectUrl(){
16+
$obj = new DigitalOceanRebooter("token",123456789);
17+
$actions = ['reboot','boot','shutdown'];
18+
foreach($actions as $action){
19+
$result = $this->invokeMethod($obj, 'buildRequestUrl', [$action]);
20+
$expected = "https://api.linode.com/?api_key=token&api_action={$action}&LinodeID=123456789";
21+
$this->assertEquals($expected,$result);
22+
}
23+
}
24+
25+
public function testItCallServerControllerMethods(){
26+
$methods = ['reboot','boot','shutdown'];
27+
28+
foreach ($methods as $m) {
29+
$mock = $this->getMock(DigitalOceanRebooter::class, array('exec'), ['token',1234]);
30+
$mock->expects($this->once())
31+
->method('exec');
32+
33+
$mock->{$m}();
34+
}
35+
}
36+
37+
public function testItCallsClientRequest(){
38+
$mock = $this->getMock(Client::class, array('request'));
39+
40+
$mock->expects($this->once())
41+
->method('request');
42+
43+
$obj = new DigitalOceanRebooter('token',1234,'asdf.com',$mock);
44+
$obj->reboot();
45+
}
46+
}

tests/SshPasswordTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public function testItCallServerControllerMethods(){
2727
}
2828

2929
public function testSessionGetterAndSetter(){
30-
$config = new Configuration("host",22);
31-
$auth = new Password("username", "password");
32-
$session= new Session($config,$auth);
33-
$obj = new PasswordRebooter("host", "username", "password", 22);
30+
$config = new Configuration("host",22);
31+
$auth = new Password("username", "password");
32+
$session = new Session($config,$auth);
33+
$obj = new PasswordRebooter("host", "username", "password", 22);
3434
$obj->setSession($session);
3535
$sessionFromGetter = $obj->getSession();
3636
$this->assertEquals($session,$sessionFromGetter);

0 commit comments

Comments
 (0)