Skip to content

Commit c0d6b27

Browse files
authored
Merge pull request #108 from ptmcnally/iops-client
Iops client
2 parents 89b89a0 + 4f0d1b8 commit c0d6b27

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

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/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/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/eCloud/IopsTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Tests\eCloud;
4+
5+
use GuzzleHttp\Client as Guzzle;
6+
use GuzzleHttp\HandlerStack;
7+
use GuzzleHttp\Handler\MockHandler;
8+
use GuzzleHttp\Psr7\Response;
9+
use PHPUnit\Framework\TestCase;
10+
use UKFast\SDK\Account\Entities\Iops;
11+
12+
class IopsTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*/
17+
public function get_page_of_iops()
18+
{
19+
$mock = new MockHandler([
20+
new Response(200, [], json_encode([
21+
'data' => [
22+
[
23+
'id' => '22891f55-cd6b-11e9-89fb-005056a64a16',
24+
'name' => 'Gold',
25+
'limit' => 1500
26+
],
27+
[
28+
'id' => '3d12186c-cd6b-11e9-89fb-005056a64a16',
29+
'name' => 'Platinum',
30+
'limit' => 2000
31+
]
32+
],
33+
"meta" => [
34+
"pagination" => [
35+
"total" => 2,
36+
"count" => 2,
37+
"per_page" => 100,
38+
"current_page" => 1,
39+
"total_pages" => 1,
40+
"links" => []
41+
]
42+
]
43+
])),
44+
]);
45+
46+
$handler = HandlerStack::create($mock);
47+
$guzzle = new Guzzle(['handler' => $handler]);
48+
49+
$client = new \UKFast\SDK\eCloud\Client($guzzle);
50+
$page = $client->iops()->getPage();
51+
52+
$this->assertTrue($page instanceof \UKFast\SDK\Page);
53+
$request = $page->getItems()[0];
54+
55+
$this->assertTrue($request instanceof \UKFast\SDK\eCloud\Entities\Iops);
56+
$this->assertEquals('22891f55-cd6b-11e9-89fb-005056a64a16', $request->id);
57+
$this->assertEquals('Gold', $request->name);
58+
$this->assertEquals(1500, $request->limit);
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function get_iops_by_id()
65+
{
66+
$mock = new MockHandler([
67+
new Response(200, [], json_encode([
68+
'data' => [
69+
'id' => '22891f55-cd6b-11e9-89fb-005056a64a16',
70+
'name' => 'Gold',
71+
'limit' => 1500
72+
],
73+
"meta" => []
74+
])),
75+
]);
76+
77+
$handler = HandlerStack::create($mock);
78+
$guzzle = new Guzzle(['handler' => $handler]);
79+
80+
$id = '22891f55-cd6b-11e9-89fb-005056a64a16';
81+
82+
$client = new \UKFast\SDK\eCloud\Client($guzzle);
83+
$iopsRecord = $client->iops()->getById($id);
84+
85+
$this->assertTrue($iopsRecord instanceof \UKFast\SDK\eCloud\Entities\Iops);
86+
$this->assertEquals($id, $iopsRecord->id);
87+
$this->assertEquals('Gold', $iopsRecord->name);
88+
$this->assertEquals(1500, $iopsRecord->limit);
89+
}
90+
}

0 commit comments

Comments
 (0)