Skip to content

Commit fd77a4d

Browse files
committed
2 parents 44969cc + c0d6b27 commit fd77a4d

File tree

11 files changed

+426
-35
lines changed

11 files changed

+426
-35
lines changed

src/Exception/ApiException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public function __construct($response)
3333
$this->message = $message;
3434
}
3535

36+
if (!empty($response->getHeader('Request-ID')[0])) {
37+
$this->requestId = $response->getHeader('Request-ID')[0];
38+
}
39+
3640
$this->response = $response;
3741
}
3842

@@ -60,6 +64,15 @@ public function getResponse()
6064
return $this->response;
6165
}
6266

67+
public function getRequestId()
68+
{
69+
if (!empty($this->response->getHeader('Request-ID'))) {
70+
return $this->response->getHeader('Request-ID')[0];
71+
}
72+
73+
return null;
74+
}
75+
6376
private function getErrorsFromBody($body)
6477
{
6578
$errors = [];

src/eCloud/Client.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ public function virtualMachines()
7272
{
7373
return (new VirtualMachineClient($this->httpClient))->auth($this->token);
7474
}
75+
76+
/**
77+
* @return BaseClient
78+
*/
79+
public function iops()
80+
{
81+
return (new IopsClient($this->httpClient))->auth($this->token);
82+
}
7583
}

src/eCloud/DatastoreClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getPage($page = 1, $perPage = 15, $filters = [])
3535
*/
3636
public function expand(Datastore $datastore)
3737
{
38-
$data = json_encode(['size_gb' => $datastore->capacity]);
38+
$data = json_encode(['capacity' => $datastore->capacity]);
3939

4040
$response = $this->post(
4141
'v1/datastores/'. $datastore->id .'/expand',

src/eCloud/Entities/Host.php

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,16 @@
22

33
namespace UKFast\SDK\eCloud\Entities;
44

5-
class Host
5+
use UKFast\SDK\Entity;
6+
7+
/**
8+
* @property int $id Host Identifier
9+
* @property string $name Host name
10+
* @property object $cpu CPU specification
11+
* @property object $ram RAM specification
12+
* @property int $solutionId Solution ID
13+
* @property int $podId Pod ID
14+
*/
15+
class Host extends Entity
616
{
7-
public $id;
8-
public $name;
9-
10-
public $cpu;
11-
public $ram;
12-
13-
public $solutionId;
14-
public $podId;
15-
16-
17-
/**
18-
* Host constructor.
19-
* @param null $item
20-
*/
21-
public function __construct($item = null)
22-
{
23-
if (empty($item)) {
24-
return;
25-
}
26-
27-
$this->id = $item->id;
28-
$this->name = $item->name;
29-
30-
$this->cpu = $item->cpu;
31-
$this->ram = $item->ram;
32-
33-
$this->solutionId = $item->solution_id;
34-
$this->podId = $item->pod_id;
35-
}
3617
}

src/eCloud/Entities/Iops.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace UKFast\SDK\eCloud\Entities;
4+
5+
use UKFast\SDK\Entities\Entity;
6+
7+
class Iops extends Entity
8+
{
9+
/**
10+
* @var string IOPS tier UUID
11+
*/
12+
public $id;
13+
14+
/**
15+
* @var string IOPS tier name
16+
*/
17+
public $name;
18+
19+
/**
20+
* @var int IOPS limit to apply to a datastore
21+
*/
22+
public $limit;
23+
}

src/eCloud/HostClient.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use UKFast\SDK\Page;
66

7+
use UKFast\SDK\Entities\ClientEntityInterface;
78
use UKFast\SDK\eCloud\Entities\Host;
89

9-
class HostClient extends Client
10+
class HostClient extends Client implements ClientEntityInterface
1011
{
1112
/**
1213
* Gets a paginated response of Hosts
@@ -21,7 +22,7 @@ public function getPage($page = 1, $perPage = 15, $filters = [])
2122
{
2223
$page = $this->paginatedRequest("v1/hosts", $page, $perPage, $filters);
2324
$page->serializeWith(function ($item) {
24-
return new Host($item);
25+
return $this->loadEntity($item);
2526
});
2627

2728
return $page;
@@ -38,6 +39,25 @@ public function getById($id)
3839
{
3940
$response = $this->get("v1/hosts/$id");
4041
$body = $this->decodeJson($response->getBody()->getContents());
41-
return new Host($body->data);
42+
return $this->loadEntity($body->data);
43+
}
44+
45+
/**
46+
* Load an instance of Host from API data
47+
* @param $data
48+
* @return Host
49+
*/
50+
public function loadEntity($data)
51+
{
52+
return new Host(
53+
[
54+
'id' => $data->id,
55+
'name' => $data->name,
56+
'cpu' => $data->cpu,
57+
'ram' => $data->ram,
58+
'solutionId' => $data->solution_id,
59+
'podId' => $data->pod_id
60+
]
61+
);
4262
}
4363
}

src/eCloud/IopsClient.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace UKFast\SDK\eCloud;
4+
5+
use UKFast\SDK\Entities\ClientEntityInterface;
6+
use UKFast\SDK\Page;
7+
8+
use UKFast\SDK\eCloud\Entities\Iops;
9+
10+
class IopsClient extends Client implements ClientEntityInterface
11+
{
12+
/**
13+
* Gets a paginated response of Iops
14+
* @param int $page Page number
15+
* @param int $perPage Number of items to return per page
16+
* @param array $filters Filter to apply
17+
* @return int|Page
18+
*/
19+
public function getPage($page = 1, $perPage = 15, $filters = [])
20+
{
21+
$page = $this->paginatedRequest("v1/iops", $page, $perPage, $filters);
22+
$page->serializeWith(function ($item) {
23+
return $this->loadEntity($item);
24+
});
25+
26+
return $page;
27+
}
28+
29+
/**
30+
* Gets an individual Iops entity
31+
*
32+
* @param string $id UUID for required IOPS record
33+
* @return Iops
34+
*/
35+
public function getById($id)
36+
{
37+
$response = $this->get("v1/iops/$id");
38+
$body = $this->decodeJson($response->getBody()->getContents());
39+
return $this->loadEntity($body->data);
40+
}
41+
42+
43+
/**
44+
* Load an instance of Iops from API data
45+
* @param $data API response data
46+
* @return Iops
47+
*/
48+
public function loadEntity($data)
49+
{
50+
return new Iops(
51+
[
52+
'id' => $data->id,
53+
'name' => $data->name,
54+
'limit' => $data->limit,
55+
]
56+
);
57+
}
58+
}

tests/ApiExceptionTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ class ApiExceptionTest extends TestCase
1414
*/
1515
public function constructs_from_standard_api_error()
1616
{
17-
$response = new Response(500, [], json_encode([
17+
$headers = [
18+
'Request-ID' => 'test-request-id',
19+
];
20+
$response = new Response(500, $headers, json_encode([
1821
'errors' => [
1922
[
2023
'detail' => 'Test'
@@ -26,6 +29,7 @@ public function constructs_from_standard_api_error()
2629

2730
$this->assertEquals(1, count($exception->getErrors()));
2831
$this->assertEquals('Test', $exception->getErrors()[0]->detail);
32+
$this->assertEquals('test-request-id', $exception->getRequestId());
2933
}
3034

3135
/**
@@ -54,4 +58,22 @@ public function throws_invalid_json_exception_when_given_bad_json()
5458
$this->expectException(InvalidJsonException::class);
5559
$exception = new ApiException($response);
5660
}
61+
62+
/**
63+
* @test
64+
*/
65+
public function request_id_is_set_to_null_if_none_is_returned()
66+
{
67+
$response = new Response(500, [], json_encode([
68+
'errors' => [
69+
[
70+
'detail' => 'Test'
71+
]
72+
]
73+
]));
74+
75+
$exception = new ApiException($response);
76+
77+
$this->assertNull($exception->getRequestId());
78+
}
5779
}

tests/eCloud/DatastoreExpandTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Tests\eCloud;
3+
use PHPUnit\Framework\TestCase;
4+
use GuzzleHttp\Client as Guzzle;
5+
use GuzzleHttp\HandlerStack;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\Psr7\Response;
8+
use \UKFast\SDK\eCloud\Entities\Datastore;
9+
10+
class DatastoreExpandTest extends TestCase
11+
{
12+
/**
13+
* @test
14+
*/
15+
public function expand_datastore()
16+
{
17+
$mock = new MockHandler([
18+
new Response(202),
19+
]);
20+
$handler = HandlerStack::create($mock);
21+
$guzzle = new Guzzle(['handler' => $handler]);
22+
$client = new \UKFast\SDK\eCloud\Client($guzzle);
23+
24+
$datastore = new Datastore(
25+
[
26+
'id' => 123,
27+
'capacity' => 500
28+
]
29+
);
30+
$response = $client->datastores()->expand($datastore);
31+
32+
$this->assertTrue($response);
33+
}
34+
}

0 commit comments

Comments
 (0)