Skip to content

Commit 26856a9

Browse files
authored
[4.7] Add support for Reserved IPs (#316)
1 parent cee2351 commit 26856a9

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

src/Api/ReservedIp.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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\Action as ActionEntity;
18+
use DigitalOceanV2\Entity\ReservedIp as ReservedIpEntity;
19+
use DigitalOceanV2\Exception\ExceptionInterface;
20+
21+
/**
22+
* @author Graham Campbell <[email protected]>
23+
* @author Manuel Christlieb <[email protected]>
24+
*/
25+
class ReservedIp extends AbstractApi
26+
{
27+
/**
28+
* @throws ExceptionInterface
29+
*
30+
* @return ReservedIpEntity[]
31+
*/
32+
public function getAll(): array
33+
{
34+
$ips = $this->get('reserved_ips');
35+
36+
return \array_map(function ($ip) {
37+
return new ReservedIpEntity($ip);
38+
}, $ips->reserved_ips);
39+
}
40+
41+
/**
42+
* @throws ExceptionInterface
43+
*/
44+
public function getById(string $ipAddress): ReservedIpEntity
45+
{
46+
$ip = $this->get(\sprintf('reserved_ips/%s', $ipAddress));
47+
48+
return new ReservedIpEntity($ip->reserved_ip);
49+
}
50+
51+
/**
52+
* @throws ExceptionInterface
53+
*/
54+
public function createAssigned(int $dropletId): ReservedIpEntity
55+
{
56+
$ip = $this->post('reserved_ips', ['droplet_id' => $dropletId]);
57+
58+
return new ReservedIpEntity($ip->reserved_ip);
59+
}
60+
61+
/**
62+
* @throws ExceptionInterface
63+
*/
64+
public function createReserved(string $regionSlug): ReservedIpEntity
65+
{
66+
$ip = $this->post('reserved_ips', ['region' => $regionSlug]);
67+
68+
return new ReservedIpEntity($ip->reserved_ip);
69+
}
70+
71+
/**
72+
* @throws ExceptionInterface
73+
*/
74+
public function remove(string $ipAddress): void
75+
{
76+
$this->delete(\sprintf('reserved_ips/%s', $ipAddress));
77+
}
78+
79+
/**
80+
* @throws ExceptionInterface
81+
*/
82+
public function getActions(string $ipAddress): array
83+
{
84+
$actions = $this->get(\sprintf('reserved_ips/%s/actions', $ipAddress));
85+
86+
return \array_map(function ($action) {
87+
return new ActionEntity($action);
88+
}, $actions->actions);
89+
}
90+
91+
/**
92+
* @throws ExceptionInterface
93+
*/
94+
public function getActionById(string $ipAddress, int $actionId): ActionEntity
95+
{
96+
$action = $this->get(\sprintf('reserved_ips/%s/actions/%d', $ipAddress, $actionId));
97+
98+
return new ActionEntity($action->action);
99+
}
100+
101+
/**
102+
* @throws ExceptionInterface
103+
*/
104+
public function assign(string $ipAddress, int $dropletId): ActionEntity
105+
{
106+
return $this->executeAction($ipAddress, ['type' => 'assign', 'droplet_id' => $dropletId]);
107+
}
108+
109+
/**
110+
* @throws ExceptionInterface
111+
*/
112+
public function unassign(string $ipAddress): ActionEntity
113+
{
114+
return $this->executeAction($ipAddress, ['type' => 'unassign']);
115+
}
116+
117+
/**
118+
* @throws ExceptionInterface
119+
*/
120+
private function executeAction(string $ipAddress, array $options): ActionEntity
121+
{
122+
$action = $this->post(\sprintf('reserved_ips/%s/actions', $ipAddress), $options);
123+
124+
return new ActionEntity($action->action);
125+
}
126+
}

src/Client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use DigitalOceanV2\Api\Monitoring;
3232
use DigitalOceanV2\Api\ProjectResource;
3333
use DigitalOceanV2\Api\Region;
34+
use DigitalOceanV2\Api\ReservedIp;
3435
use DigitalOceanV2\Api\Size;
3536
use DigitalOceanV2\Api\Snapshot;
3637
use DigitalOceanV2\Api\Tag;
@@ -250,6 +251,11 @@ public function region(): Region
250251
return new Region($this);
251252
}
252253

254+
public function reservedIp(): ReservedIp
255+
{
256+
return new ReservedIp($this);
257+
}
258+
253259
/**
254260
* @return Size
255261
*/

src/Entity/ReservedIp.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 Graham Campbell <[email protected]>
19+
* @author Manuel Christlieb <[email protected]>
20+
*/
21+
final class ReservedIp extends AbstractEntity
22+
{
23+
public string $ip;
24+
25+
public ?Droplet $droplet = null;
26+
27+
public Region $region;
28+
public bool $locked;
29+
public string $projectId;
30+
31+
public function build(array $parameters): void
32+
{
33+
if (isset($parameters['droplet'])) {
34+
$this->droplet = new Droplet($parameters['droplet']);
35+
unset($parameters['droplet']);
36+
}
37+
$this->region = new Region($parameters['region']);
38+
unset($parameters['region']);
39+
parent::build($parameters);
40+
}
41+
}

tests/ClientTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use DigitalOceanV2\Api\LoadBalancer;
3030
use DigitalOceanV2\Api\ProjectResource;
3131
use DigitalOceanV2\Api\Region;
32+
use DigitalOceanV2\Api\ReservedIp;
3233
use DigitalOceanV2\Api\Size;
3334
use DigitalOceanV2\Api\Snapshot;
3435
use DigitalOceanV2\Api\Tag;
@@ -70,6 +71,7 @@ public function testCreateApis(): void
7071
self::assertInstanceOf(LoadBalancer::class, $client->loadBalancer());
7172
self::assertInstanceOf(ProjectResource::class, $client->projectResource());
7273
self::assertInstanceOf(Region::class, $client->region());
74+
self::assertInstanceOf(ReservedIp::class, $client->reservedIp());
7375
self::assertInstanceOf(Size::class, $client->size());
7476
self::assertInstanceOf(Snapshot::class, $client->snapshot());
7577
self::assertInstanceOf(Tag::class, $client->tag());

tests/Entity/ReservedIpTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\AbstractEntity;
18+
use DigitalOceanV2\Entity\Region as RegionEntity;
19+
use DigitalOceanV2\Entity\ReservedIp as ReservedIpEntity;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* @author Graham Campbell <[email protected]>
24+
* @author Manuel Christlieb <[email protected]>
25+
*/
26+
class ReservedIpTest extends TestCase
27+
{
28+
public function testConstructor(): void
29+
{
30+
$values = [
31+
'ip' => '45.55.96.47',
32+
'droplet' => null,
33+
'region' => [
34+
'name' => 'New York 3',
35+
'slug' => 'nyc3',
36+
'features' => [
37+
'private_networking',
38+
'backups',
39+
'image_transfer',
40+
],
41+
'available' => true,
42+
'sizes' => [
43+
's-1vcpu-1gb',
44+
's-1vcpu-2gb',
45+
's-32vcpu-192g',
46+
],
47+
],
48+
'locked' => false,
49+
'project_id' => '746c6152-2fa2-11ed-92d3-27aaa54e4988',
50+
];
51+
52+
$entity = new ReservedIpEntity($values);
53+
54+
self::assertInstanceOf(AbstractEntity::class, $entity);
55+
self::assertInstanceOf(ReservedIpEntity::class, $entity);
56+
self::assertInstanceOf(RegionEntity::class, $entity->region);
57+
self::assertSame($values['ip'], $entity->ip);
58+
self::assertNull($entity->droplet);
59+
60+
self::assertSame($values['locked'], $entity->locked);
61+
self::assertSame($values['project_id'], $entity->projectId);
62+
}
63+
}

0 commit comments

Comments
 (0)