Skip to content

Commit 2da60f1

Browse files
authored
[4.7] Add VPC getAll (#315)
1 parent 6988545 commit 2da60f1

File tree

7 files changed

+203
-0
lines changed

7 files changed

+203
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,16 @@ $volume->getActionById(123, '506f78a4-e098-11e5-ad9f-000f53306ae1');
752752
$volume->getActions('506f78a4-e098-11e5-ad9f-000f53306ae1');
753753
```
754754

755+
### VPC
756+
757+
```php
758+
// return the VPC api
759+
vpc = $client->vpc();
760+
761+
// returns the all VPCs
762+
vpcs = $vpc->getAll();
763+
```
764+
755765

756766
## Contributing
757767

src/Api/Vpc.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the DigitalOcean API library.
7+
*
8+
* (c) Antoine Kirk <[email protected]>
9+
* (c) Graham Campbell <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace DigitalOceanV2\Api;
16+
17+
use DigitalOceanV2\Entity\Vpc as VpcEntity;
18+
use DigitalOceanV2\Exception\ExceptionInterface;
19+
20+
/**
21+
* @author Manuel Christlieb <[email protected]>
22+
*/
23+
class Vpc extends AbstractApi
24+
{
25+
/**
26+
* @throws ExceptionInterface
27+
*
28+
* @return VpcEntity[]
29+
*/
30+
public function getAll(): array
31+
{
32+
$vpcs = $this->get('vpcs');
33+
34+
return \array_map(static fn ($vpc) => new VpcEntity($vpc), $vpcs->vpcs);
35+
}
36+
}

src/Client.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use DigitalOceanV2\Api\Snapshot;
3636
use DigitalOceanV2\Api\Tag;
3737
use DigitalOceanV2\Api\Volume;
38+
use DigitalOceanV2\Api\Vpc;
3839
use DigitalOceanV2\HttpClient\Builder;
3940
use DigitalOceanV2\HttpClient\Message\ResponseMediator;
4041
use DigitalOceanV2\HttpClient\Plugin\Authentication;
@@ -281,6 +282,14 @@ public function volume(): Volume
281282
return new Volume($this);
282283
}
283284

285+
/**
286+
* @return Vpc
287+
*/
288+
public function vpc(): Vpc
289+
{
290+
return new Vpc($this);
291+
}
292+
284293
/**
285294
* @param string $token
286295
*

src/Entity/Vpc.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the DigitalOcean API library.
7+
*
8+
* (c) Antoine Kirk <[email protected]>
9+
* (c) Graham Campbell <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace DigitalOceanV2\Entity;
16+
17+
/**
18+
* @author Manuel Christlieb <[email protected]>
19+
*/
20+
final class Vpc extends AbstractEntity
21+
{
22+
public string $name;
23+
24+
public string $description;
25+
26+
public string $region;
27+
28+
public string $ipRange;
29+
30+
public string $id;
31+
32+
public string $urn;
33+
34+
public bool $default;
35+
36+
public string $createdAt;
37+
}

tests/Api/VpcTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the DigitalOcean API library.
7+
*
8+
* (c) Antoine Kirk <[email protected]>
9+
* (c) Graham Campbell <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace DigitalOceanV2\Tests\Api;
16+
17+
use DigitalOceanV2\Api\Vpc;
18+
use DigitalOceanV2\Client;
19+
use DigitalOceanV2\Entity\Vpc as VpcEntity;
20+
use GuzzleHttp\Psr7\Response;
21+
use GuzzleHttp\Psr7\Utils;
22+
use Http\Client\Common\HttpMethodsClientInterface;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* @author Manuel Christlieb <[email protected]>
27+
*/
28+
class VpcTest extends TestCase
29+
{
30+
public function testItCreatesAnArrayOfVpcEntities(): void
31+
{
32+
$client = $this->createMock(Client::class);
33+
$client->expects(self::once())
34+
->method('getHttpClient')
35+
->willReturn($httpClient = $this->createMock(HttpMethodsClientInterface::class));
36+
$httpClient->expects(self::once())
37+
->method('get')
38+
->with('/v2/vpcs')
39+
->willReturn(new Response(
40+
200,
41+
['Content-Type' => ['application/json']],
42+
Utils::streamFor(\json_encode(['vpcs' => [
43+
[
44+
'name' => 'env.prod-vpc',
45+
'description' => 'VPC for production environment',
46+
'region' => 'nyc1',
47+
'ip_range' => '10.10.10.0/24',
48+
'id' => '5a4981aa-9653-4bd1-bef5-d6bff52042e4',
49+
'urn' => 'do:vpc:5a4981aa-9653-4bd1-bef5-d6bff52042e4',
50+
'default' => false,
51+
'created_at' => '2020-03-13T19:20:47.442049222Z',
52+
],
53+
]]))
54+
));
55+
$vpcApi = new Vpc($client);
56+
$vpcs = $vpcApi->getAll();
57+
58+
self::assertInstanceOf(VpcEntity::class, $vpcs[0]);
59+
}
60+
}

tests/ClientTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use DigitalOceanV2\Api\Snapshot;
3434
use DigitalOceanV2\Api\Tag;
3535
use DigitalOceanV2\Api\Volume;
36+
use DigitalOceanV2\Api\Vpc;
3637
use DigitalOceanV2\Client;
3738
use Http\Client\Common\HttpMethodsClientInterface;
3839
use PHPUnit\Framework\TestCase;
@@ -73,5 +74,6 @@ public function testCreateApis(): void
7374
self::assertInstanceOf(Snapshot::class, $client->snapshot());
7475
self::assertInstanceOf(Tag::class, $client->tag());
7576
self::assertInstanceOf(Volume::class, $client->volume());
77+
self::assertInstanceOf(Vpc::class, $client->vpc());
7678
}
7779
}

tests/Entity/VpcTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the DigitalOcean API library.
7+
*
8+
* (c) Antoine Kirk <[email protected]>
9+
* (c) Graham Campbell <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace DigitalOceanV2\Tests\Entity;
16+
17+
use DigitalOceanV2\Entity\Vpc;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* @author Manuel Christlieb <[email protected]>
22+
*/
23+
class VpcTest extends TestCase
24+
{
25+
public function testConstructor(): void
26+
{
27+
$values = [
28+
'name' => 'env.prod-vpc',
29+
'description' => 'VPC for production environment',
30+
'region' => 'nyc1',
31+
'ip_range' => '10.10.10.0/24',
32+
'id' => '5a4981aa-9653-4bd1-bef5-d6bff52042e4',
33+
'urn' => 'do:vpc:5a4981aa-9653-4bd1-bef5-d6bff52042e4',
34+
'default' => false,
35+
'created_at' => '2020-03-13T19:20:47.442049222Z',
36+
];
37+
38+
$entity = new Vpc($values);
39+
40+
self::assertEquals($values['name'], $entity->name);
41+
self::assertEquals($values['description'], $entity->description);
42+
self::assertEquals($values['region'], $entity->region);
43+
self::assertEquals($values['ip_range'], $entity->ipRange);
44+
self::assertEquals($values['id'], $entity->id);
45+
self::assertEquals($values['urn'], $entity->urn);
46+
self::assertEquals($values['default'], $entity->default);
47+
self::assertEquals($values['created_at'], $entity->createdAt);
48+
}
49+
}

0 commit comments

Comments
 (0)